PlayerInventory & PlayerInventoryUI: Transferring inventory action handlers from Player to PlayerInventoryUI. #34

This commit is contained in:
Van
2026-04-16 20:30:10 +03:00
parent 7661cacbfe
commit 0a97ef3a83
2 changed files with 145 additions and 101 deletions

View File

@@ -1,98 +1,6 @@
local PrimitiveCraftHandler = Smc.requireCraftSystemPrimitiveCraftHandler()
---@type PlayerInventoryUI
local PlayerInventoryUI = Mod.getInfo('smc__ui__player_inventory').require('PlayerInventoryUI')
-- TODO: перенести обработки в PlayerUI
-- Handling the button click for selecting an item for crafting.
-- Opens the item description and allows you to craft the selected item.
PlayerInventoryUI:registerOnFormFieldActionMatch('.+::%S+::item_button',function(player, fieldName, fieldValue)
local craftName, tabType = string.match(fieldName, '(.+)::(%S+)::item_button')
assert(craftName ~= nil)
assert(tabType == 'left' or tabType == 'right')
local playerName = player:get_player_name()
PlayerInventoryUI:setContex(playerName, tabType..'_tab_craft_selected_craft', craftName)
PlayerInventoryUI:showForm(playerName)
end)
-- Handling the click of the crafting group selection button.
-- Causes grouped items to be expanded into a separate list.
PlayerInventoryUI:registerOnFormFieldActionMatch('.+::%S+::group_button',function(player, fieldName, fieldValue)
local groupName, tabType = string.match(fieldName, '(.+)::(%S+)::group_button')
assert(groupName ~= nil)
assert(tabType == 'left' or tabType == 'right')
local playerName = player:get_player_name()
PlayerInventoryUI:setContex(playerName, tabType..'_tab_craft_selected_group', groupName)
PlayerInventoryUI:showForm(playerName)
end)
PlayerInventoryUI:registerOnFormFieldActionMatch('.+::craft_item', function(player, fieldName, fieldValue)
---@type InvRef
local inv = player:get_inventory()
local playerName = player:get_player_name()
local tabType = string.match(fieldName, '(.+)::craft_item')
assert(tabType == 'left' or tabType == 'right')
local craftName = PlayerInventoryUI:getContex(playerName, tabType..'_tab_craft_selected_craft')
local craft = PrimitiveCraftHandler:getCraftByName(craftName)
local OutputStack = ItemStack(craft.output)
OutputStack:set_count(OutputStack:get_count() * fieldValue)
local recipeStacks = {}
local recipeGroups = {}
for _, itemName in ipairs(craft.recipe) do
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
if not inv:contains_item('main', item) then
core.chat_send_player(playerName, 'Not enough materials for crafting!')
return
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)
end
inv:add_item('main', OutputStack)
else
core.chat_send_player(playerName, 'There is no space in the inventory!')
end
end)