adminCommands: update /list command

This commit is contained in:
Van
2026-02-27 21:55:34 +03:00
parent c7cfe26335
commit 8b55ccff72
9 changed files with 166 additions and 113 deletions

View File

@@ -0,0 +1,72 @@
local BaseUI, Formspec = Smc.requireBaseUI(), Smc.requireFormspec()
---@param self BaseUI
---@param playerName string
---@param isSelect boolean
---@return string
local function getFormSpecString(self, playerName, isSelect)
local items = {}
for name, def in pairs(core.registered_items) do
if def.groups and not def.groups.not_in_creative_inventory then
table.insert(items, name)
end
end
table.sort(items)
local listFormItems = Formspec:new()
local w, h = 0, 0
---@param name string
for _, name in ipairs(items) do
listFormItems:itemImageButton(w, h, 1, 1, name, name..'::item_button')
w = w + 1
if w >= 9 then
w = 0
h = h + 1
end
end
local listForm = Formspec:new({
size = {x=21, y=12},
})
listForm:
list('current_player', 'main', 10.5, 6.5, 8, 4):
scrollContainer(listFormItems, 0.5, 0.5, 9, 11, 'admin_commands:list_items', 'vertical'):
scrollbar(10, 0.5, 0.25, 10, 'vertical', 'admin_commands:list_items', self:getContex(playerName, 'scroll'))
if isSelect then
---@type ItemStackString
local item = self:getContex(playerName, 'select_item')
local itemDef = core.registered_items[item]
listForm:
itemImage(11, 0.5, 2, 2, item):
areaLabel(11, 4.5, 4, 1, 'Add to inventory:'):
button(11, 5, 3, 1, 'admin_commands:list_add_item', '1'):
button(14, 5,3, 1, 'admin_commands:list_add_item', ''..tostring(itemDef.stack_max)):
areaLabel(14.5, 0.5, 6, 4,
item..'\n\n'..
itemDef._title..'\n'..
itemDef._description)
end
return listForm:toString()
end
---@class AdminListUI : BaseUI
---@field getFormSpecString fun(self:BaseUI,playerName:string,isSelect:boolean):string
---@field showForm fun(self:BaseUI,playerName:string,isSelect:boolean):string
local AdminListUI = BaseUI:new('admin_commands:list_form', getFormSpecString)
AdminListUI:registerOnFormFieldAction('admin_commands:list_items', function(player, fieldName, fieldValue)
local scrollValue = string.match(fieldValue, 'CHG:(%S+)')
if scrollValue then
AdminListUI:setContex(player, 'scroll', scrollValue)
end
end)
return AdminListUI