PlayerInventoryUI: autolistring equipments slots

This commit is contained in:
Van
2026-04-21 21:06:19 +03:00
parent ad521ce2b7
commit 7c7a318d22
4 changed files with 118 additions and 1 deletions

View File

@@ -449,10 +449,78 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('.+::craft_item', function(play
end
end)
---Automatically places equipment items into their corresponding slots.
---@param player Player
---@param inventory InvRef
---@param fromList string
---@param fromIndex integer
local function equipListring(player, inventory, fromList, fromIndex)
local itemStack = inventory:get_stack(fromList, fromIndex)
assert(itemStack ~= nil)
local playerName = player:get_player_name()
-- We check whether the item has a group that matches the equipment slot.
-- If so, we move the item to the slot.
--
-- We also check if the target slot is empty.
-- If it's already occupied, we prevent items from being moved.
--
-- TODO: дописать автолистринг для одежды
if core.get_item_group(itemStack:get_name(), 'equip_head') == 2 then
if not inventory:get_stack('equip_head', 2):is_empty() then
return 0
end
inventory:set_stack('equip_head', 2, itemStack)
inventory:set_stack(fromList, fromIndex, nil)
PlayerInventoryUI:showForm(playerName)
return
elseif core.get_item_group(itemStack:get_name(), 'equip_torso') == 2 then
if not inventory:get_stack('equip_torso', 2):is_empty() then
return 0
end
inventory:set_stack('equip_torso', 2, itemStack)
inventory:set_stack(fromList, fromIndex, nil)
PlayerInventoryUI:showForm(playerName)
return
elseif core.get_item_group(itemStack:get_name(), 'equip_legs') == 2 then
if not inventory:get_stack('equip_legs', 2):is_empty() then
return 0
end
inventory:set_stack('equip_legs', 2, itemStack)
inventory:set_stack(fromList, fromIndex, nil)
PlayerInventoryUI:showForm(playerName)
return
elseif core.get_item_group(itemStack:get_name(), 'equip_feet') == 2 then
if not inventory:get_stack('equip_feet', 2):is_empty() then
return 0
end
inventory:set_stack('equip_feet', 2, itemStack)
inventory:set_stack(fromList, fromIndex, nil)
PlayerInventoryUI:showForm(playerName)
return
else
-- If an item does not have any equipment groups,
-- then we prohibit its movement (the item remains in its original location)
return 0
end
end
-- We only allow items to be placed in equipment slot
-- that have groups that match the slot name and index.
core.register_allow_player_inventory_action(function(player, action, inventory, inventoryInfo)
if action == 'move' then
-- If the move occurs in a special slot, then we add the item to the desired slot.
-- This is necessary to bypass the standard listring[] limitation.
-- Equipment items will automatically place themselves in their slots
-- when you hold down the shift key.
if inventoryInfo.to_list == 'listring_equip' then
return equipListring(player, inventory, inventoryInfo.from_list, inventoryInfo.from_index)
end
-- Checking if a slot is an equipment slot.
local equipGroup = string.match(inventoryInfo.to_list, 'equip_%S+')
if equipGroup == nil then
@@ -490,7 +558,7 @@ end)
-- Update the interface if the player has moved something from the equipment slots
-- (this enables and disables the slot overlay).
core.register_on_player_inventory_action(function(player, action, inventory, inventoryInfo)
local playerName = player:get_player_name()
local playerName = player:get_player_name()
if action == 'move' then
local equipGroup = string.match(inventoryInfo.to_list, 'equip_%S+') or string.match(inventoryInfo.from_list, 'equip_%S+')
if equipGroup ~= nil then

View File

@@ -21,6 +21,22 @@ local function getEquipTab(self, playerName, tabType)
:list('current_player', 'equip_torso', equipListsX, 2.25, 2, 1)
:list('current_player', 'equip_legs', equipListsX, 3.5, 2, 1)
:list('current_player', 'equip_feet', equipListsX, 4.75, 2, 1)
-- Hidden equipment slot for correct listing
-- (distribute equipment across its slots while holding shift):
:list('current_player', 'listring_equip', 0, 0, 1, 1)
:listring({
{'current_player', 'main'},
{'current_player', 'listring_equip'},
{'current_player', 'main'},
{'current_player', 'equip_head'},
{'current_player', 'main'},
{'current_player', 'equip_torso'},
{'current_player', 'main'},
{'current_player', 'equip_legs'},
{'current_player', 'main'},
{'current_player', 'equip_feet'},
{'current_player', 'main'},
})
local player = core.get_player_by_name(playerName)
assert(player)
@@ -28,6 +44,7 @@ local function getEquipTab(self, playerName, tabType)
local inv = player:get_inventory()
-- If the equipment slots are empty, then we apply an overlay.
-- TODO: оверлей одежды
if inv:get_stack('equip_head', 2):is_empty() then
EquipTab:image(equipListsX+1.25, 1, 1, 1, slotsOverlayTileSheet:t(0,0))
end