diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua index c6a9b93..35e14b4 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua @@ -230,6 +230,14 @@ local function getFormSpecString(self, playerName) end end +---@class PlayerInventoryUI.CraftTabData +---@field search_field_value string? +---@field selected_craft string? +---@field selected_recipe_index integer? +---@field selected_group CraftSystem.CraftGroup? +---@field page_number integer? +---@field handler CraftSystem.BaseCraftHandler + ---Context keys for the `PlayerInventoryUI` class. ---@alias PlayerInventoryUI.ContexKey --- 'compact_mode'| @@ -239,16 +247,11 @@ end --- 'left_tab_name'| --- 'right_tab_name'| --- ---- 'left_tab_craft_search_field_value'| ---- 'right_tab_craft_search_field_value'| ---- 'left_tab_craft_selected_craft'| ---- 'right_tab_craft_selected_craft'| ---- 'left_tab_craft_selected_recipe_index'| ---- 'right_tab_craft_selected_recipe_index'| ---- 'left_tab_craft_selected_group'| ---- 'right_tab_craft_selected_group'| ---- 'left_tab_craft_page_number'| ---- 'right_tab_craft_page_number' +--- 'left_craft_tab_craft_data'| +--- 'right_craft_tab_craft_data'| +--- 'left_node_tab_craft_data'| +--- 'right_node_tab_craft_data' + ---@class PlayerInventoryUI : BaseUI ---@field getFormSpecString fun(self:PlayerInventoryUI,playerName:string):string @@ -257,6 +260,27 @@ end ---@field setContex fun(self:PlayerInventoryUI,player:string|Player,key:PlayerInventoryUI.ContexKey,value:any) local PlayerInventoryUI = BaseUI:new('', getFormSpecString, false) +---@param playerName string +---@param tabType PlayerInventoryUI.TabType +---@param tabName 'craft_tab'|'node_tab' +---@return PlayerInventoryUI.CraftTabData +function PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName) + ---@diagnostic disable-next-line: param-type-not-match + ---@type PlayerInventoryUI.CraftTabData + local craftTabData = self:getContex(playerName, tabType..'_'..tabName..'_craft_data') + + return craftTabData +end + +---@param playerName string +---@param tabType PlayerInventoryUI.TabType +---@param tabName 'craft_tab'|'node_tab' +---@param data PlayerInventoryUI.CraftTabData +function PlayerInventoryUI:setTabCraftData(playerName, tabType, tabName, data) + ---@diagnostic disable-next-line: param-type-not-match + self:setContex(playerName, tabType..'_'..tabName..'_craft_data', data) +end + ---@type table PlayerInventoryUI.ActiveNodeForms = {} @@ -334,6 +358,8 @@ local function closeNodeTab(player) PlayerInventoryUI:setContex(playerName, 'node_tab_pos', nil) PlayerInventoryUI:setContex(playerName, 'node_tab', nil) + PlayerInventoryUI:setContex(playerName, 'left_node_tab_craft_data', nil) + PlayerInventoryUI:setContex(playerName, 'right_node_tab_craft_data', nil) ---@type PlayerInventoryUI.TabName local leftTabName = PlayerInventoryUI:getContex(playerName, 'left_tab_name') or 'craft_tab' @@ -407,245 +433,183 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('select_right_%S+', function(pl end) -- Hadling input in the crafting search bar. -PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::craft_search_field', function(player, fieldName, fieldValue) - local tabType = stringMatch(fieldName, '(%S+)::craft_search_field') +PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::craft_search_field', function(player, fieldName, fieldValue) + local tabType, tabName = stringMatch(fieldName, '(%S+)::(%S+)::craft_search_field') assert(tabType == 'left' or tabType == 'right') + assert(tabName == 'craft_tab' or tabName == 'node_tab') local playerName = player:get_player_name() - PlayerInventoryUI:setContex(playerName, tabType..'_tab_craft_search_field_value', fieldValue) + local craftTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName) + if craftTabData.search_field_value == fieldValue then + return + end + + craftTabData.search_field_value = fieldValue PlayerInventoryUI:showForm(playerName) end) -- Handling the click of the button to turning back through the crafting menu pages. -PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::page_back', function(player, fieldName, fieldValue) - local tabType = stringMatch(fieldName, '(%S+)::page_back') +PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::page_back', function(player, fieldName, fieldValue) + local tabType, tabName = stringMatch(fieldName, '(%S+)::(%S+)::page_back') assert(tabType == 'left' or tabType == 'right') + assert(tabName == 'craft_tab' or tabName == 'node_tab') local playerName = player:get_player_name() - local pageNumber = PlayerInventoryUI:getContex(playerName, tabType..'_tab_craft_page_number') or 1 + local craftTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName) + local pageNumber = craftTabData.page_number or 1 pageNumber = pageNumber - 1 if pageNumber <= 0 then pageNumber = 1 end - PlayerInventoryUI:setContex(playerName, tabType..'_tab_craft_page_number', pageNumber) + craftTabData.page_number = pageNumber PlayerInventoryUI:showForm(playerName) end) -- Handling the click of the button to turning forward through the crafting menu pages. -PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::page_forward', function(player, fieldName, fieldValue) - local tabType = stringMatch(fieldName, '(%S+)::page_forward') +PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::page_forward', function(player, fieldName, fieldValue) + local tabType, tabName = stringMatch(fieldName, '(%S+)::(%S+)::page_forward') assert(tabType == 'left' or tabType == 'right') + assert(tabName == 'craft_tab' or tabName == 'node_tab') local playerName = player:get_player_name() - local pageNumber = PlayerInventoryUI:getContex(playerName, tabType..'_tab_craft_page_number') or 1 + local craftTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName) + local pageNumber = craftTabData.page_number or 1 pageNumber = pageNumber + 1 - PlayerInventoryUI:setContex(playerName, tabType..'_tab_craft_page_number', pageNumber) + craftTabData.page_number = pageNumber PlayerInventoryUI:showForm(playerName) end) -- Handlings the click of the button to move back in the list of alternative recipes. -PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::alt_recipe_back', function(player, fieldName, fieldValue) - local tabType = stringMatch(fieldName, '(%S+)::alt_recipe_back') +PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::alt_recipe_back', function(player, fieldName, fieldValue) + local tabType, tabName = stringMatch(fieldName, '(%S+)::(%S+)::alt_recipe_back') assert(tabType == 'left' or tabType == 'right') + assert(tabName == 'craft_tab' or tabName == 'node_tab') local playerName = player:get_player_name() - local recipeIndex = PlayerInventoryUI:getContex(playerName, tabType..'_tab_craft_selected_recipe_index') or 0 + local craftTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName) + local recipeIndex = craftTabData.selected_recipe_index or 0 recipeIndex = recipeIndex - 1 if recipeIndex < 0 then recipeIndex = 0 end - PlayerInventoryUI:setContex(playerName, tabType..'_tab_craft_selected_recipe_index', recipeIndex) + craftTabData.selected_recipe_index = recipeIndex PlayerInventoryUI:showForm(playerName) end) -- Handlings the click of the button to move forward in the list of alternative recipes. -PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::alt_recipe_forward', function(player, fieldName, fieldValue) - local tabType = stringMatch(fieldName, '(%S+)::alt_recipe_forward') +PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::alt_recipe_forward', function(player, fieldName, fieldValue) + local tabType, tabName = stringMatch(fieldName, '(%S+)::(%S+)::alt_recipe_forward') assert(tabType == 'left' or tabType == 'right') + assert(tabName == 'craft_tab' or tabName == 'node_tab') local playerName = player:get_player_name() - local recipeIndex = PlayerInventoryUI:getContex(playerName, tabType..'_tab_craft_selected_recipe_index') or 0 + local craftTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName) + local recipeIndex = craftTabData.selected_recipe_index or 0 recipeIndex = recipeIndex + 1 - PlayerInventoryUI:setContex(playerName, tabType..'_tab_craft_selected_recipe_index', recipeIndex) + craftTabData.selected_recipe_index = recipeIndex PlayerInventoryUI:showForm(playerName) end) -- Processing closing of the selected item group in the crafting menu (returning to the main list of crafts). -PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::close_group', function(player, fieldName, fieldValue) - local tabType = stringMatch(fieldName, '(%S+)::close_group') +PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::close_group', function(player, fieldName, fieldValue) + local tabType, tabName = stringMatch(fieldName, '(%S+)::(%S+)::close_group') assert(tabType == 'left' or tabType == 'right') + assert(tabName == 'craft_tab' or tabName == 'node_tab') local playerName = player:get_player_name() - PlayerInventoryUI:setContex(playerName, tabType..'_tab_craft_selected_group', nil) + local craftTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName) + craftTabData.selected_group = nil PlayerInventoryUI:showForm(playerName) end) -- 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') +PlayerInventoryUI:registerOnFormFieldActionMatch('.+::%S+::%S+::item_button',function(player, fieldName, fieldValue) + local craftName, tabType, tabName = string.match(fieldName, '(.+)::(%S+)::(%S+)::item_button') assert(craftName ~= nil) assert(tabType == 'left' or tabType == 'right') + assert(tabName == 'craft_tab' or tabName == 'node_tab') local playerName = player:get_player_name() - PlayerInventoryUI:setContex(playerName, tabType..'_tab_craft_selected_recipe_index', 0) - PlayerInventoryUI:setContex(playerName, tabType..'_tab_craft_selected_craft', craftName) + local craftTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName) + craftTabData.selected_recipe_index = 0 + craftTabData.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') +PlayerInventoryUI:registerOnFormFieldActionMatch('.+::%S+::%S+::group_button',function(player, fieldName, fieldValue) + local groupName, tabType, tabName = string.match(fieldName, '(.+)::(%S+)::(%S+)::group_button') assert(groupName ~= nil) assert(tabType == 'left' or tabType == 'right') + assert(tabName == 'craft_tab' or tabName == 'node_tab') local playerName = player:get_player_name() - PlayerInventoryUI:setContex(playerName, tabType..'_tab_craft_selected_group', groupName) + local craftTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName) + craftTabData.selected_group = groupName PlayerInventoryUI:showForm(playerName) end) ----Returns the crafting recipe corresponding to the index. ----@param craft CraftSystem.PrimitiveCraft ----@param index integer Recipe number. If `0` is specified, ---- the primary craft (`craft.recipe`) will be used; ---- if `1` or greater, the alternative craft ---- corresponding to the index will be used (`craft.altRecipes`). ----@return CraftSystem.CraftRecipe -local function getCraftRecipe(craft, index) - local recipe - if index == 0 or craft.altRecipes == nil then - recipe = craft.recipe - else - if index > #craft.altRecipes then - recipe = craft.altRecipes[#craft.altRecipes] - else - recipe = craft.altRecipes[index] - end - end - assert(recipe ~= nil) - - return recipe -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() - +PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::craft_item', function(player, fieldName, fieldValue) local playerName = player:get_player_name() - local tabType = string.match(fieldName, '(.+)::craft_item') + local tabType, tabName = string.match(fieldName, '(%S+)::(%S+)::craft_item') assert(tabType == 'left' or tabType == 'right') + assert(tabName == 'craft_tab' or tabName == 'node_tab') -- We get the recipe selected in the crafting menu. - local craftName = PlayerInventoryUI:getContex(playerName, tabType..'_tab_craft_selected_craft') - local recipeIndex = PlayerInventoryUI:getContex(playerName, tabType..'_tab_craft_selected_recipe_index') or 0 - local craft = InventoryCraftHandler:getCraftByName(craftName) + local craftTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName) + local handler = craftTabData.handler + local craftName = craftTabData.selected_craft + assert(craftName ~= nil) + local recipeIndex = craftTabData.selected_recipe_index + assert(recipeIndex ~= nil) + local craft = handler: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) + local allowCrafting, materialStacks = CraftSystem:checkPossiblePlayerInventoryCrafting(player, craft, recipeIndex, fieldValue) + if allowCrafting and handler.baseType == 'primitive' then + ---@type CraftSystem.PrimitiveCraft + craft = craft + local inventory = player:get_inventory() - ---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(getCraftRecipe(craft, recipeIndex)) 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 + --TODO: выделить это в функцию Smc.playerTakeOrDrop + --TODO: добавить сброс предмета на землю, если нет места. + if inventory:room_for_item('main', craft.output) then + for _, item in ipairs(materialStacks) do + inventory:remove_item('main', item) 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!') + inventory:add_item('main', craft.output) + else + core.chat_send_player(player:get_player_name(), 'There is no space in the inventory!') + end + elseif tabName == 'node_tab' and allowCrafting and handler.baseType == 'immersive' then + ---@type CraftSystem.ImmersiveCraft + craft = craft + local inventory = player:get_inventory() + local nodePos = PlayerInventoryUI:getContex(playerName, 'node_tab_pos') + + local isCraftStart = craft:startCraft(nodePos, player, fieldValue) + if not isCraftStart then 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) + inventory:remove_item('main', item) end - inv:add_item('main', OutputStack) - else - core.chat_send_player(playerName, 'There is no space in the inventory!') + PlayerInventoryUI:closeForm(playerName) end end) @@ -664,8 +628,6 @@ local function equipListring(player, inventory, fromList, fromIndex) -- -- We also check if the target slot is empty. -- If it's already occupied, we prevent items from being moved. - -- - -- TODO: дописать автолистринг для одежды if core.get_item_group(itemStack:get_name(), 'equip_head') == 1 then if not inventory:get_stack('equip_head', 1):is_empty() then return 0 diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/baseGetCraftTab.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/baseGetCraftTab.lua new file mode 100644 index 0000000..c06deee --- /dev/null +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/baseGetCraftTab.lua @@ -0,0 +1,147 @@ +local mathFloor = math.floor + +---Number of crafting recipes on one menu page. +local PAGE_NUMBER_ITEMS = 20 + +local Formspec = Smc.requireFormspec() + +---@alias PlayerInventoryUI.CraftTabPrefix 'inside_player_inventory'|'node' + +---@param self PlayerInventoryUI +---@param playerName string +---@param tabType PlayerInventoryUI.TabType +---@param tabName 'craft_tab'|'node_tab' +---@param handler CraftSystem.BaseCraftHandler +---@return Formspec +local function getCraftTabTemplate(self, playerName, tabType, tabName, handler) + if tabType ~= 'left' and tabType ~= 'right' then + error('Incorrect argument `tabType`: '..tabType) + end + + if tabName ~= 'craft_tab' and tabName ~= 'node_tab' then + error('Incorrect argument `tabName`: '..tabName) + end + + local CraftTab = Formspec:new() + + local craftTabData = self:getTabCraftData(playerName, tabType, tabName) + if craftTabData == nil then + craftTabData = {handler = handler} + self:setTabCraftData(playerName, tabType, tabName, craftTabData) + end + + local selectedCraft = craftTabData.selected_craft or '' + local selectedGroup = craftTabData.selected_group + local searchFieldValue = craftTabData.search_field_value + local pageNumber = craftTabData.page_number or 1 + + local crafts + if searchFieldValue ~= '' and searchFieldValue ~= nil then + ---@type CraftSystem.PrimitiveCraft[] + crafts = handler:getLearnedCraftsMatch(playerName, searchFieldValue) + elseif selectedGroup == nil then + ---@type (CraftSystem.PrimitiveCraft[]|CraftSystem.PrimitiveCraft)[] + crafts = handler:getLearnedCrafts(playerName) + else + ---@type CraftSystem.PrimitiveCraft[] + crafts = handler:getCraftsByGroup(selectedGroup) + end + + local craftsListForm = Formspec:new() + + ---Maximum number of crafting recipe pages + ---@type integer + local pagesMax = math.ceil(#crafts/PAGE_NUMBER_ITEMS) + + if pagesMax == 0 then + pagesMax = 1 + end + + if pageNumber > pagesMax then + pageNumber = pagesMax + craftTabData.page_number = pageNumber + end + + local w, h = 0.0, 0.0 + + if selectedGroup ~= nil and (searchFieldValue == nil or searchFieldValue == '') then + craftsListForm + :imageButton(0, 0, 1.125, 1.125, 'player_inventory_craft_tab_close_group_button_background.png', tabType..'::'..tabName..'::close_group') + w = 1.125 + end + + for i = PAGE_NUMBER_ITEMS*pageNumber-PAGE_NUMBER_ITEMS+1, PAGE_NUMBER_ITEMS*pageNumber do + local craft = crafts[i] + if craft == nil then + break + end + + ---@type ItemStackString? + local output = craft.output + + if output ~= nil then + if craft.name == selectedCraft then + craftsListForm + :imageButton(w, h, 1.125, 1.125, 'player_inventory_craft_tab_button_pressed_background.png', craft.name..'::'..tabType..'::'..tabName..'::item_button') + else + craftsListForm + :imageButton(w, h, 1.125, 1.125, 'player_inventory_craft_tab_button_background.png', craft.name..'::'..tabType..'::'..tabName..'::item_button') + end + + craftsListForm + :itemImage(w+0.0625, h+0.0625, 1, 1, output) + else + local craft = craft[1] + assert(craft ~= nil) + + craftsListForm + :imageButton(w, h, 1.125, 1.125, 'player_inventory_craft_tab_button_background.png', craft.group..'::'..tabType..'::'..tabName..'::group_button') + :itemImage(w+0.0625, h+0.0625, 1, 1, craft.output) + :image(w, h, 1.125, 1.125, 'player_inventory_craft_tab_group_button_overlay.png') + end + w = w + 1.125 + + if w >= 4 then + w = 0 + h = h + 1.125 + end + end + + CraftTab + :background(0, 0, 11, 9, 'player_inventory_craft_tab_background.png') + :field(0.25, 0.5, 5, 0.75, tabType..'::'..tabName..'::craft_search_field', nil, searchFieldValue) + :fieldCloseOnEnter(tabType..'::'..tabName..'::craft_search_field', false) + :container(craftsListForm, 0.5, 1.5) + :button(1.75, 7.75, 1, 0.5, tabType..'::'..tabName..'::page_back', '<') + :button(2.75, 7.75, 1, 0.5, tabType..'::'..tabName..'::page_forward', '>') + :label(2.5,8.5, ('%s/%s'):format(pageNumber, pagesMax)) + + if selectedCraft ~= '' then + local craft = handler:getCraftByName(selectedCraft) + local item = ItemStack(craft.output) + local itemDef = item:get_definition() + + local recipeIndex = craftTabData.selected_recipe_index or 0 + if recipeIndex > #craft.altRecipes then + recipeIndex = #craft.altRecipes + craftTabData.selected_recipe_index = #craft.altRecipes + end + + local player = core.get_player_by_name(playerName) + assert(player ~= nil) + local mainList = player:get_inventory():get_list('main') + + CraftTab + :itemImage(5.5, 0.5, 2, 2, craft.output) + :label(10, 0.5, ('%s/%s'):format(recipeIndex+1, #craft.altRecipes+1)) + :textArea(5.5, 3, 5, 4, nil, nil, craft:toString(recipeIndex, mainList)) + :button(5.5, 7.5, 0.5, 1, tabType..'::'..tabName..'::alt_recipe_back', '<') + :button(6, 7.5, 2, 1, tabType..'::'..tabName..'::craft_item', '1') + :button(8, 7.5,2, 1, tabType..'::'..tabName..'::craft_item', ''..tostring(itemDef.stack_max)) + :button(10, 7.5, 0.5, 1, tabType..'::'..tabName..'::alt_recipe_forward', '>') + end + + return CraftTab +end + +return getCraftTabTemplate diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getCraftTab.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getCraftTab.lua index 32e00ca..ad8dbb1 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getCraftTab.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/getCraftTab.lua @@ -1,204 +1,15 @@ -local mathFloor = math.floor +local require = Mod.getInfo('smc__ui__player_inventory').require +local getCraftTabTemplate = require('PlayerInventoryUI.baseGetCraftTab') ----Number of crafting recipes on one menu page. -local PAGE_NUMBER_ITEMS = 20 - -local Formspec = Smc.requireFormspec() local CraftSystem = Smc.requireCraftSystem() - local InventoryCraftHandler = CraftSystem:getPrimitiveCraftHandler('inside_player_inventory') ----A text indicator of the presence or absence ----of a material in the player's inventory. -local CHECK = {YES = 'x', NO = ' '} - ----@param inventory InvRef ----@param material ItemStack ----@return string -local function getCheckMaterialItem(inventory, material) - if inventory:contains_item('main', material) then - return CHECK.YES - else - return CHECK.NO - end -end - ----@param inventory InvRef ----@param material string ----@return string -local function getCheckMaterialGroup(inventory, material) - for _, item in ipairs(inventory:get_list('main')) do - if core.get_item_group(item:get_name(), material) > 0 then - return CHECK.YES - end - end - - return CHECK.NO -end - ----Returns a human-readable list of materials required for crafting (recipe). ----@param inventory InvRef ----@param craft CraftSystem.PrimitiveCraft ----@param index integer Recipe number. If `0` is specified, ---- the primary craft (`craft.recipe`) will be used; ---- if `1` or greater, the alternative craft ---- corresponding to the index will be used (`craft.altRecipes`). ----@return string -local function getRecipeDesc(inventory, craft, index) - local recipe - if index == 0 or craft.altRecipes == nil then - recipe = craft.recipe - else - if index > #craft.altRecipes then - recipe = craft.altRecipes[#craft.altRecipes] - else - recipe = craft.altRecipes[index] - end - end - assert(recipe ~= nil) - - local recipeDesc = '' - for _, itemName in ipairs(recipe) do - local group, count = itemName:match('group:(%S+)'), itemName:match('group:%S+ (%S+)') or 1 - - if group == nil then - local recipeItem = ItemStack(itemName) - recipeDesc = recipeDesc..('[%s] %s x%s\n'):format(getCheckMaterialItem(inventory, recipeItem), core.strip_colors(recipeItem:get_definition()._title), recipeItem:get_count()) - else - recipeDesc = recipeDesc..('[%s] any item from the group %s x%s\n'):format(getCheckMaterialGroup(inventory, group), group, count) - end - end - - return recipeDesc -end - ---@param self PlayerInventoryUI ---@param playerName string ---@param tabType PlayerInventoryUI.TabType ---@return Formspec local function getCraftTab(self, playerName, tabType) - if tabType ~= 'left' and tabType ~= 'right' then - error('Incorrect argument `tabType`: '..tabType) - end - - local CraftTab = Formspec:new() - - ---@type string - local selectedCraft = self:getContex(playerName, tabType..'_tab_craft_selected_craft') or '' - ---@type CraftSystem.CraftGroup? - local selectedGroup = self:getContex(playerName, tabType..'_tab_craft_selected_group') - local searchFieldValue = self:getContex(playerName, tabType..'_tab_craft_search_field_value') - local pageNumber = self:getContex(playerName, tabType..'_tab_craft_page_number') or 1 - - local crafts - if searchFieldValue ~= '' and searchFieldValue ~= nil then - ---@type CraftSystem.PrimitiveCraft[] - crafts = InventoryCraftHandler:getLearnedCraftsMatch(playerName, searchFieldValue) - elseif selectedGroup == nil then - ---@type (CraftSystem.PrimitiveCraft[]|CraftSystem.PrimitiveCraft)[] - crafts = InventoryCraftHandler:getLearnedCrafts(playerName) - else - ---@type CraftSystem.PrimitiveCraft[] - crafts = InventoryCraftHandler:getCraftsByGroup(selectedGroup) - end - - local craftsListForm = Formspec:new() - - ---Maximum number of crafting recipe pages - ---@type integer - local pagesMax = math.ceil(#crafts/PAGE_NUMBER_ITEMS) - - if pagesMax == 0 then - pagesMax = 1 - end - - if pageNumber > pagesMax then - pageNumber = pagesMax - self:setContex(playerName, tabType..'_tab_craft_page_number', pageNumber) - end - - local w, h = 0.0, 0.0 - - if selectedGroup ~= nil and (searchFieldValue == nil or searchFieldValue == '') then - craftsListForm - :imageButton(0, 0, 1.125, 1.125, 'player_inventory_craft_tab_close_group_button_background.png', tabType..'::close_group') - w = 1.125 - end - - for i = PAGE_NUMBER_ITEMS*pageNumber-PAGE_NUMBER_ITEMS+1, PAGE_NUMBER_ITEMS*pageNumber do - local craft = crafts[i] - if craft == nil then - break - end - - ---@type ItemStackString? - local output = craft.output - - if output ~= nil then - if craft.name == selectedCraft then - craftsListForm - :imageButton(w, h, 1.125, 1.125, 'player_inventory_craft_tab_button_pressed_background.png', craft.name..'::'..tabType..'::item_button') - else - craftsListForm - :imageButton(w, h, 1.125, 1.125, 'player_inventory_craft_tab_button_background.png', craft.name..'::'..tabType..'::item_button') - end - - craftsListForm - :itemImage(w+0.0625, h+0.0625, 1, 1, output) - else - local craft = craft[1] - assert(craft ~= nil) - - craftsListForm - :imageButton(w, h, 1.125, 1.125, 'player_inventory_craft_tab_button_background.png', craft.group..'::'..tabType..'::group_button') - :itemImage(w+0.0625, h+0.0625, 1, 1, craft.output) - :image(w, h, 1.125, 1.125, 'player_inventory_craft_tab_group_button_overlay.png') - end - w = w + 1.125 - - if w >= 4 then - w = 0 - h = h + 1.125 - end - end - - CraftTab - :background(0, 0, 11, 9, 'player_inventory_craft_tab_background.png') - :field(0.25, 0.5, 5, 0.75, tabType..'::craft_search_field', nil, searchFieldValue) - :fieldCloseOnEnter(tabType..'::craft_search_field', false) - :container(craftsListForm, 0.5, 1.5) - :button(1.75, 7.75, 1, 0.5, tabType..'::page_back', '<') - :button(2.75, 7.75, 1, 0.5, tabType..'::page_forward', '>') - :label(2.5,8.5, ('%s/%s'):format(pageNumber, pagesMax)) - - if selectedCraft ~= '' then - local craft = InventoryCraftHandler:getCraftByName(selectedCraft) - local item = ItemStack(craft.output) - local itemDef = item:get_definition() - - local recipeIndex = self:getContex(playerName, tabType..'_tab_craft_selected_recipe_index') or 0 - if recipeIndex > #craft.altRecipes then - recipeIndex = #craft.altRecipes - self:setContex(playerName, tabType..'_tab_craft_selected_recipe_index', #craft.altRecipes) - end - - local player = core.get_player_by_name(playerName) - assert(player ~= nil) - local recipeDesc = getRecipeDesc(player:get_inventory(), craft, recipeIndex) - - CraftTab - :itemImage(5.5, 0.5, 2, 2, craft.output) - :label(10, 0.5, ('%s/%s'):format(recipeIndex+1, #craft.altRecipes+1)) - :textArea(5.5, 3, 5, 4, nil, nil, - itemDef._title..'\n\n'.. - 'Needed:\n'..recipeDesc) - :button(5.5, 7.5, 0.5, 1, tabType..'::alt_recipe_back', '<') - :button(6, 7.5, 2, 1, tabType..'::craft_item', '1') - :button(8, 7.5,2, 1, tabType..'::craft_item', ''..tostring(itemDef.stack_max)) - :button(10, 7.5, 0.5, 1, tabType..'::alt_recipe_forward', '>') - end - - return CraftTab + return getCraftTabTemplate(self, playerName, tabType, 'craft_tab', InventoryCraftHandler) end -return getCraftTab +return getCraftTab \ No newline at end of file