From b314f7adf889fdfa9b263dafced2cee879bde041 Mon Sep 17 00:00:00 2001 From: Vanic Date: Tue, 28 Apr 2026 19:30:32 +0300 Subject: [PATCH] `Item` & `PlayerInventoryUI`: Bags now have the ability to filter items by groups --- mods/CORE/Item/src/Item/Settings.lua | 13 +++ mods/GAME/ITEMS/equipment/init.lua | 8 +- .../src/PlayerInventoryUI.lua | 87 ++++++++++++++++++- 3 files changed, 104 insertions(+), 4 deletions(-) diff --git a/mods/CORE/Item/src/Item/Settings.lua b/mods/CORE/Item/src/Item/Settings.lua index a9bfd62..3432e54 100644 --- a/mods/CORE/Item/src/Item/Settings.lua +++ b/mods/CORE/Item/src/Item/Settings.lua @@ -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. diff --git a/mods/GAME/ITEMS/equipment/init.lua b/mods/GAME/ITEMS/equipment/init.lua index 03f9132..235cc7e 100644 --- a/mods/GAME/ITEMS/equipment/init.lua +++ b/mods/GAME/ITEMS/equipment/init.lua @@ -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'} } }) diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua index 27ee59a..01e773b 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua @@ -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