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

@@ -1,103 +0,0 @@
local modInfo = Mod.getInfo('smc__core__formspec')
local require = modInfo.require
---@type Formspec
local Formspec = require('Formspec')
local function getForm()
local listForm = Formspec:new({
size = {x=21, y=12},
})
listForm:list('current_player', 'main', 10.5, 6.5, 8, 4)
local listFormItems = Formspec:new()
local items = table.get_keys(core.registered_items)
table.sort(items)
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
--listFormItems:list('current_player', 'main', 0, 0, 1, 32)
listForm:
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')
return listForm
end
core.register_chatcommand('list', {
privs = {server = true},
func = function(name, param)
local player = core.get_player_by_name(name)
core.show_formspec(name, 'admin_commands:list_form', getForm():toString())
return true, ''
end
})
core.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= 'admin_commands:list_form' then
return
end
for key, value in pairs(fields) do
if string.ends_with(key, '::item_button') then
---@type ItemStackString?
local stack = string.match(key, '(%S+)::item_button')
if stack == nil then
return
end
---@type ItemStack
stack = ItemStack(stack)
core.log(dump(stack:get_definition()))
---@type InvRef
local inv = player:get_inventory()
if inv:room_for_item("main", stack) then
inv:add_item("main", stack)
end
end
end
end)
core.register_on_joinplayer(function(player, last_login)
player:set_sky({
sky_color = {
day_sky = '#FAE6E0',
day_horizon = '#E7A985',
},
type = "regular",
})
player:set_lighting({
shadows = { intensity = 0.33 },
volumetric_light = { strength = 0.45 },
exposure = {
luminance_min = -3.5,
luminance_max = -2.5,
exposure_correction = 0.35,
speed_dark_bright = 1500,
speed_bright_dark = 700,
},
saturation = 1.0,
})
end)

View File

@@ -1,2 +0,0 @@
name = smc__core__admin_commands
depends = smc__system__system_loader, smc__core__formspec

View File

@@ -0,0 +1,68 @@
local modInfo = Mod.getInfo('smc__core__formspec')
local require = modInfo.require
---@type AdminListUI
local AdminListUI = Mod.getInfo('smc__ui__admin_list').require('AdminListUI')
core.register_chatcommand('list', {
privs = {server = true},
func = function(name, param)
local player = core.get_player_by_name(name)
AdminListUI:showForm(name, false)
return true, ''
end
})
AdminListUI:registerOnFormFieldActionMatch('%S+::item_button',function(player, fieldName, fieldValue)
---@type ItemStackString?
local stack = string.match(fieldName, '(%S+)::item_button')
if stack == nil then
return
end
local playerName = player:get_player_name()
AdminListUI:setContex(playerName, 'select_item', stack)
AdminListUI:showForm(playerName, true)
end)
AdminListUI:registerOnFormFieldAction('admin_commands:list_add_item', function(player, fieldName, fieldValue)
---@type InvRef
local inv = player:get_inventory()
local playerName = player:get_player_name()
local stack = AdminListUI:getContex(playerName, 'select_item')
stack = ItemStack(stack)
stack:set_count(fieldValue)
if inv:room_for_item('main', stack) then
inv:add_item('main', stack)
end
end)
core.register_on_joinplayer(function(player, last_login)
player:set_sky({
sky_color = {
day_sky = '#FAE6E0',
day_horizon = '#E7A985',
},
type = 'regular',
})
player:set_lighting({
shadows = { intensity = 0.33 },
volumetric_light = { strength = 0.45 },
exposure = {
luminance_min = -3.5,
luminance_max = -2.5,
exposure_correction = 0.35,
speed_dark_bright = 1500,
speed_bright_dark = 700,
},
saturation = 1.0,
})
end)

View File

@@ -0,0 +1,2 @@
name = smc__game__admin_commands
depends = smc__system__system_loader, smc__ui__admin_list

View File

@@ -47,3 +47,20 @@ function Smc.requireTileSheet()
return class
end
---@return BaseUI
function Smc.requireBaseUI()
---@type BaseUI
local class = Mod.getInfo('smc__core__base_ui').require('BaseUI')
return class
end
---@return Formspec
function Smc.requireFormspec()
---@type Formspec
local class = Mod.getInfo('smc__core__formspec').require('Formspec')
return class
end

View File

@@ -0,0 +1,4 @@
local modInfo = Mod.getInfo('smc__ui__admin_list')
local require = modInfo.require
return require('AdminListUI')

View File

@@ -0,0 +1,2 @@
name = smc__ui__admin_list
depends = smc__core__formspec

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

View File

@@ -1,11 +1,4 @@
local modInfo = Mod.getInfo('smc__core__formspec')
local require = modInfo.require
---@type Formspec
local Formspec = require('Formspec')
---@type BaseUI
local BaseUI = Mod.getInfo('smc__core__base_ui').require('BaseUI')
local BaseUI, Formspec = Smc.requireBaseUI(), Smc.requireFormspec()
---@param self BaseUI
---@param playerName string