diff --git a/mods/CORE/Player/src/Player/inventory.lua b/mods/CORE/Player/src/Player/inventory.lua index 280bcf3..b4c756f 100644 --- a/mods/CORE/Player/src/Player/inventory.lua +++ b/mods/CORE/Player/src/Player/inventory.lua @@ -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) diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua index 9a0db07..9d4275d 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua @@ -2,6 +2,8 @@ local stringMatch = string.match local BaseUI, Formspec = Smc.requireBaseUI(), Smc.requireFormspec() +local PrimitiveCraftHandler = Smc.requireCraftSystemPrimitiveCraftHandler() + local require = Mod.getInfo('smc__ui__player_inventory').require ---@alias PlayerInventoryUI.TabType 'left'|'right' @@ -69,7 +71,6 @@ local function getCompactForm(self, playerName) -- TODO: убрать, если будет не нужно local leftTabIsBlock = self:getContex(playerName, 'left_tab_is_block') - -- Create an instance of the inventory form. local InventoryForm = Formspec:new({ size = {x=14, y=12.5}, position = {x=0.5, y=0.55}, @@ -77,6 +78,8 @@ local function getCompactForm(self, playerName) }) -- We get a table for textures for tab selection buttons according to the active tab. + -- The active button should be lighter than the others. + -- This is achieved by using a different texture on the button. local leftTabButtonTextures = tabButtonTextures[leftTabName] InventoryForm @@ -122,7 +125,6 @@ local function getFullForm(self, playerName) local leftTabIsBlock = self:getContex(playerName, 'left_tab_is_block') local rightTabIsBlock = self:getContex(playerName, 'right_tab_is_block') - -- Create an instance of the inventory form. local InventoryForm = Formspec:new({ size = {x=22, y=12.5}, position = {x=0.5, y=0.55}, @@ -130,6 +132,8 @@ local function getFullForm(self, playerName) }) -- We get texture tables for the tab selection buttons depending on the active tabs. + -- The active button should be lighter than the others. + -- This is achieved by using a different texture on the button. local leftTabButtonTextures = tabButtonTextures[leftTabName] local rightTabButtonTextures = tabButtonTextures[rightTabName] @@ -174,18 +178,14 @@ local function getFormSpecString(self, playerName) ---@type boolean local compactMode = self:getContex(playerName, 'compact_mode') - -- If the mode is not selected... + -- By default, the player will be shown a compact form. if compactMode == nil then - -- ...by default it will be `true` (compact mode). compactMode = true end - -- If compact mode is selected... if compactMode then - -- ...return the compact inventory definition. return getCompactForm(self, playerName) else - -- ...else we return the full version of the inventory. return getFullForm(self, playerName) end end @@ -220,7 +220,6 @@ local PlayerInventoryUI = BaseUI:new('', getFormSpecString, false) local showForm = PlayerInventoryUI.showForm function PlayerInventoryUI:showForm(playerName) - -- We get a player by his name. local player = core.get_player_by_name(playerName) assert(player ~= nil) @@ -237,6 +236,8 @@ PlayerInventoryUI:registerOnFormFieldAction('change_compact_mode', function(play local playerName = player:get_player_name() local compactMode = PlayerInventoryUI:getContex(playerName, 'compact_mode') + + -- By default, the player will be shown a compact form. if compactMode == nil then compactMode = true end @@ -311,6 +312,141 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::close_group', function(pl PlayerInventoryUI:showForm(playerName) end) --- TODO: перенести сюда обработку вызовов из Player +-- 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) + +-- Handling the click to craft an item. +-- If the player has crafting materials, they are removed and the crafted item is added to the inventory. +-- +-- Note: Materials are only searched in the player's main inventory. +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') + + -- We get the recipe selected in the crafting menu. + local craftName = PlayerInventoryUI:getContex(playerName, tabType..'_tab_craft_selected_craft') + local craft = PrimitiveCraftHandler:getCraftByName(craftName) + + -- We create an ItemStack of the crafted item by multiplying + -- its count by a multiplier - the number of items the player wants to craft at once. + -- The multiplier value is determined by the crafting button. + local OutputStack = ItemStack(craft.output) + OutputStack:set_count(OutputStack:get_count() * fieldValue) + + ---A table containing the items required for crafting. + local materialStacks = {} + + ---A table containing the item groups required for crafting. + local materialGroups = {} + + -- We fill in the tables of required materials based on the crafting recipe. + for _, itemName in ipairs(craft.recipe) do + local group, count = itemName:match('group:(%S+)'), itemName:match('group:%S+ (%S+)') or 1 + + -- If the specified material is not a group of items (does not meet the 'group:(%S+)' format)... + if group == nil then + -- ...then it is the name of a specific item. + -- And we enter it into the list of materials as ItemStack, + -- also multiplying the count by the multiplier. + local item = ItemStack(itemName) + item:set_count(item:get_count() * fieldValue) + + table.insert(materialStacks, item) + else + -- If the specified item is a group, + -- then enter this into the materials table as a key, + -- and the value is the number of items needed. + materialGroups[group] = (tonumber(count) or 1) * fieldValue + end + end + + -- We check if the player has the required items. + -- If they don't have at least one or if they don't have enough, we notify them. + for _, item in ipairs(materialStacks) do + if not inv:contains_item('main', item) then + core.chat_send_player(playerName, 'Not enough materials for crafting!') + return + end + end + + + -- We check whether the player has the required number + -- of items with the required group for crafting. + -- If they don't have at least one or if they don't have enough, we notify them. + local mainList = inv:get_list('main') + for group, count in pairs(materialGroups) do + for _, item in ipairs(mainList) do + -- For each item in the inventory, we check whether it has the required group. + if core.get_item_group(item:get_name(), group) > 0 then + local itemCount = item:get_count() + -- If the number of required items is greater than the number of items in the stack... + if count > itemCount then + -- ...then we add the entire stack to the list of materials, + -- and subtract the materials already found from the required count. + -- + -- This is necessary to create an item even + -- if the player does not have a single stack in the required count, + -- but has several items that together give the required count. + count = count - itemCount + table.insert(materialStacks, item) + else + -- else (if one stack of items is enough) + -- we add the amount we need to the list of materials. + item:set_count(count) + table.insert(materialStacks, item) + count = 0 + end + end + end + + -- If the required quantity is still positive, + -- it means we haven't found enough materials. + -- There will be no crafting. + if count > 0 then + core.chat_send_player(playerName, 'Not enough materials for crafting!') + return + end + end + + -- Finally, we check if there is free space in the inventory + -- and if so, we take the materials from the player's inventory + -- and return the crafted item to him. + -- + --TODO: добавить сброс предмета на землю, если нет места. + if inv:room_for_item('main', OutputStack) then + for _, item in ipairs(materialStacks) 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) return PlayerInventoryUI