From bddcb4e36cfe5b654f6c28bf3344c91ea40680ef Mon Sep 17 00:00:00 2001 From: Vanic Date: Sat, 4 Apr 2026 18:00:24 +0300 Subject: [PATCH] `PlayerInventory` & `PlayerInventoryUI`: You can now specify groups as materials for primitive crafting in your inventory. #34 --- mods/CORE/Player/src/Player/inventory.lua | 35 +++++++++++++++++-- mods/GAME/ITEMS/storages/init.lua | 8 +++++ mods/GAME/ITEMS/wood/init.lua | 1 + .../src/PlayerInventoryUI/getCraftPane.lua | 10 ++++-- 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/mods/CORE/Player/src/Player/inventory.lua b/mods/CORE/Player/src/Player/inventory.lua index 201df04..13da9d0 100644 --- a/mods/CORE/Player/src/Player/inventory.lua +++ b/mods/CORE/Player/src/Player/inventory.lua @@ -42,10 +42,17 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('.+::craft_item', function(play OutputStack:set_count(OutputStack:get_count() * fieldValue) local recipeStacks = {} + local recipeGroups = {} for _, itemName in ipairs(craft.recipe) do - local item = ItemStack(itemName) - item:set_count(item:get_count() * fieldValue) - table.insert(recipeStacks, item) + + local group, count = itemName:match('group:(%S+)'), itemName:match('group:%S+ (%S+)') or 1 + if group == nil then + local item = ItemStack(itemName) + item:set_count(item:get_count() * fieldValue) + table.insert(recipeStacks, item) + else + recipeGroups[group] = tonumber(count) + end end for _, item in ipairs(recipeStacks) do @@ -55,6 +62,28 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('.+::craft_item', function(play end end + local mainList = inv:get_list('main') + for group, count in pairs(recipeGroups) do + for _, item in ipairs(mainList) do + if core.get_item_group(item:get_name(), group) > 0 then + local itemCount = item:get_count() + if count > itemCount then + count = count - itemCount + table.insert(recipeStacks, item) + else + item:set_count(count) + table.insert(recipeStacks, item) + count = 0 + end + end + end + + if count > 0 then + core.chat_send_player(playerName, 'Not enough materials for crafting!') + return + end + end + if inv:room_for_item('main', OutputStack) then for _, item in ipairs(recipeStacks) do inv:remove_item('main', item) diff --git a/mods/GAME/ITEMS/storages/init.lua b/mods/GAME/ITEMS/storages/init.lua index 0dbb28c..2a36041 100644 --- a/mods/GAME/ITEMS/storages/init.lua +++ b/mods/GAME/ITEMS/storages/init.lua @@ -97,3 +97,11 @@ storageFactory:registerItems({ }, }) +local PrimitiveCraftHandler = Smc.requireCraftSystemPrimitiveCraftHandler() + +PrimitiveCraftHandler:registerCraft('storages:case_closed', { + recipe = { + 'group:log 4' + }, + output = 'storages:case_closed', +}) \ No newline at end of file diff --git a/mods/GAME/ITEMS/wood/init.lua b/mods/GAME/ITEMS/wood/init.lua index a7d3893..4c5db5b 100644 --- a/mods/GAME/ITEMS/wood/init.lua +++ b/mods/GAME/ITEMS/wood/init.lua @@ -88,6 +88,7 @@ local function registerWood(name, x, def) tiles = getLogTiles(x, 2), paramtype2 = 'facedir', _rarity = def.rarity, + groups = {log = 1} }, callbacks = { on_place = core.rotate_node diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getCraftPane.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getCraftPane.lua index 1566593..cca6899 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getCraftPane.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getCraftPane.lua @@ -105,8 +105,14 @@ local function getCraftPane(self, playerName, paneType) local recipe = '' for _, itemName in ipairs(craft.recipe) do - local recipeItem = ItemStack(itemName) - recipe = recipe..'- '..core.strip_colors(recipeItem:get_definition()._title)..' x'..recipeItem:get_count()..'\n' + local group, count = itemName:match('group:(%S+)'), itemName:match('group:%S+ (%S+)') or 1 + + if group == nil then + local recipeItem = ItemStack(itemName) + recipe = recipe..'- '..core.strip_colors(recipeItem:get_definition()._title)..' x'..recipeItem:get_count()..'\n' + else + recipe = recipe..'- any item from the group "'..group..'" x'..count..'\n' + end end CraftPane: