PlayerInventoryUI: autolistring equipments slots

This commit is contained in:
Van
2026-04-20 21:15:11 +03:00
parent ad521ce2b7
commit bbe7a14401
4 changed files with 118 additions and 1 deletions

View File

@@ -14,6 +14,7 @@ core.register_on_joinplayer(function(player)
inv:set_size('equip_torso', 2) inv:set_size('equip_torso', 2)
inv:set_size('equip_legs', 2) inv:set_size('equip_legs', 2)
inv:set_size('equip_feet', 2) inv:set_size('equip_feet', 2)
inv:set_size('listring_equip', 1) -- Slot for correct listring.
end) end)
core.register_chatcommand('show_inv', core.register_chatcommand('show_inv',

View File

@@ -31,3 +31,34 @@ Item:new({
} }
} }
}) })
Item:new({
settings = {
name = 'bronze_knee_pads',
inventory_image = EquipTileSheet:t(2, 2),
groups = {
equip_legs = 2
}
}
})
Item:new({
settings = {
name = 'steel_boots',
inventory_image = EquipTileSheet:t(3, 3),
groups = {
equip_feet = 2
}
}
})
Item:new({
settings = {
name = 'steel_helmet',
inventory_image = EquipTileSheet:t(3, 0),
groups = {
equip_head = 2
}
}
})

View File

@@ -449,10 +449,78 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('.+::craft_item', function(play
end end
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 -- We only allow items to be placed in equipment slot
-- that have groups that match the slot name and index. -- that have groups that match the slot name and index.
core.register_allow_player_inventory_action(function(player, action, inventory, inventoryInfo) core.register_allow_player_inventory_action(function(player, action, inventory, inventoryInfo)
if action == 'move' then 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. -- Checking if a slot is an equipment slot.
local equipGroup = string.match(inventoryInfo.to_list, 'equip_%S+') local equipGroup = string.match(inventoryInfo.to_list, 'equip_%S+')
if equipGroup == nil then 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_torso', equipListsX, 2.25, 2, 1)
:list('current_player', 'equip_legs', equipListsX, 3.5, 2, 1) :list('current_player', 'equip_legs', equipListsX, 3.5, 2, 1)
:list('current_player', 'equip_feet', equipListsX, 4.75, 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) local player = core.get_player_by_name(playerName)
assert(player) assert(player)
@@ -28,6 +44,7 @@ local function getEquipTab(self, playerName, tabType)
local inv = player:get_inventory() local inv = player:get_inventory()
-- If the equipment slots are empty, then we apply an overlay. -- If the equipment slots are empty, then we apply an overlay.
-- TODO: оверлей одежды
if inv:get_stack('equip_head', 2):is_empty() then if inv:get_stack('equip_head', 2):is_empty() then
EquipTab:image(equipListsX+1.25, 1, 1, 1, slotsOverlayTileSheet:t(0,0)) EquipTab:image(equipListsX+1.25, 1, 1, 1, slotsOverlayTileSheet:t(0,0))
end end