Item & PlayerInventoryUI: Bags now have the ability to filter items by groups

This commit is contained in:
Van
2026-04-28 19:30:32 +03:00
parent 0b372111cf
commit b314f7adf8
3 changed files with 104 additions and 4 deletions

View File

@@ -57,6 +57,19 @@ Settings._itemstack_inventory_size = nil
---@type string?
Settings._itemstack_inventory_background = nil
---Defines a list of group names that items
---with which can be placed in an item's inventory.
---
---If it is `nil`, then any items will be allowed except those disallowed in `_itemstack_inventory_disallowed_groups`.
---@type string[]?
Settings._itemstack_inventory_allowed_groups = nil
---Defines a list of group names that items
---with which cannot be placed in an item's inventory.
---@type string[]?
Settings._itemstack_inventory_disallowed_groups = nil
---Texture shown in the inventory GUI.
---Defaults to a 3D rendering of the node if left empty.

View File

@@ -67,11 +67,13 @@ Item:new({
name = 'bag',
inventory_image = EquipTileSheet:t(4, 1),
groups = {
bag = 1,
equip_back = 1,
},
_itemstack_inventory_size = 24,
stack_max = 1,
_itemstack_inventory_background = 'equipment_bag_background_24_slots.png'
_itemstack_inventory_background = 'equipment_bag_background_24_slots.png',
_itemstack_inventory_disallowed_groups = {'bag'}
}
})
@@ -80,12 +82,14 @@ Item:new({
name = 'bag_thigh',
inventory_image = EquipTileSheet:t(5, 1),
groups = {
bag = 1,
equip_left_thigh = 1,
equip_right_thigh = 1
},
_itemstack_inventory_size = 16,
stack_max = 1,
_itemstack_inventory_background = 'equipment_bag_background_16_slots.png'
_itemstack_inventory_background = 'equipment_bag_background_16_slots.png',
_itemstack_inventory_disallowed_groups = {'bag'}
}
})

View File

@@ -544,8 +544,7 @@ core.register_allow_player_inventory_action(function(player, action, inventory,
end
-- We receive the item that the player wants to place in the equipment slot.
local itemStack = inventory:get_stack(inventoryInfo.listname, inventoryInfo.index)
assert(itemStack ~= nil)
local itemStack = inventoryInfo.stack
if core.get_item_group(itemStack:get_name(), equipGroup) == inventoryInfo.index then
return
@@ -555,6 +554,90 @@ core.register_allow_player_inventory_action(function(player, action, inventory,
end
end)
---Checks if the inventory list is an inventory list of bags.
---@param listName string
---@return boolean
local function isBagInventoryList(listName)
if listName == 'inventory_back' or listName == 'inventory_left_thigh' or listName == 'inventory_right_thigh' then
return true
else
return false
end
end
---Returns the copy ItemStack of the bag whose inventory is expanded in the specified inventory list.
---@param inventory InvRef
---@param listName string
---@return ItemStack
local function getBagSlotListName(inventory, listName)
local bagSlotName = string.match(listName, 'inventory_(%S+)')
assert(bagSlotName ~= nil)
local bagItem = inventory:get_stack('equip_'..bagSlotName, 1)
assert(not bagItem:is_empty())
return bagItem
end
-- We check whether an item contains allow or disallow item groups
-- to ensure that only allow items are placed in bags.
core.register_allow_player_inventory_action(function(player, action, inventory, inventoryInfo)
if action == 'move' and isBagInventoryList(inventoryInfo.to_list) then
local bagItem = getBagSlotListName(inventory, inventoryInfo.to_list)
local movableItemName = inventory:get_stack(inventoryInfo.from_list, inventoryInfo.from_index):get_name()
local bagSetting = Smc.getItemDefinition(bagItem).settings
local allowedGroups, disallowedGroups = bagSetting._itemstack_inventory_allowed_groups, bagSetting._itemstack_inventory_disallowed_groups
if allowedGroups ~= nil then
for _, group in ipairs(allowedGroups) do
if core.get_item_group(movableItemName, group) > 0 then
return
end
end
return 0
end
if disallowedGroups ~= nil then
for _, group in ipairs(disallowedGroups) do
if core.get_item_group(movableItemName, group) > 0 then
return 0
end
end
return
end
elseif action == 'put' then
local bagItem = getBagSlotListName(inventory, inventoryInfo.listname)
local movableItemName = inventoryInfo.stack
local bagSetting = Smc.getItemDefinition(bagItem).settings
local allowedGroups, disallowedGroups = bagSetting._itemstack_inventory_allowed_groups, bagSetting._itemstack_inventory_disallowed_groups
if allowedGroups ~= nil then
for _, group in ipairs(allowedGroups) do
if core.get_item_group(movableItemName, group) > 0 then
return
end
end
return 0
end
if disallowedGroups ~= nil then
for _, group in ipairs(disallowedGroups) do
if core.get_item_group(movableItemName, group) > 0 then
return 0
end
end
return
end
end
end)
---Returns the name of the inventory list that will contain the items in the bag.
---@param equipList string
---@return string