PlayerInventoryUI: Support and display of alternative recipes in the crafting menu. #34
This commit is contained in:
@@ -203,6 +203,8 @@ end
|
||||
--- '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'|
|
||||
@@ -304,11 +306,37 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::page_forward', function(p
|
||||
local pageNumber = PlayerInventoryUI:getContex(playerName, tabType..'_tab_craft_page_number') or 1
|
||||
pageNumber = pageNumber + 1
|
||||
|
||||
if pageNumber <= 0 then
|
||||
pageNumber = 1
|
||||
PlayerInventoryUI:setContex(playerName, tabType..'_tab_craft_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')
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
local recipeIndex = PlayerInventoryUI:getContex(playerName, tabType..'_tab_craft_selected_recipe_index') or 0
|
||||
recipeIndex = recipeIndex - 1
|
||||
|
||||
if recipeIndex < 0 then
|
||||
recipeIndex = 0
|
||||
end
|
||||
|
||||
PlayerInventoryUI:setContex(playerName, tabType..'_tab_craft_page_number', pageNumber)
|
||||
PlayerInventoryUI:setContex(playerName, tabType..'_tab_craft_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')
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
|
||||
local playerName = player:get_player_name()
|
||||
local recipeIndex = PlayerInventoryUI:getContex(playerName, tabType..'_tab_craft_selected_recipe_index') or 0
|
||||
recipeIndex = recipeIndex + 1
|
||||
|
||||
PlayerInventoryUI:setContex(playerName, tabType..'_tab_craft_selected_recipe_index', recipeIndex)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
|
||||
@@ -330,6 +358,7 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('.+::%S+::item_button',function
|
||||
assert(tabType == 'left' or tabType == 'right')
|
||||
|
||||
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)
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end)
|
||||
@@ -346,6 +375,29 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('.+::%S+::group_button',functio
|
||||
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.
|
||||
--
|
||||
@@ -359,7 +411,8 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('.+::craft_item', function(play
|
||||
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 craftName = PlayerInventoryUI:getContex(playerName, tabType..'_tab_craft_selected_craft')
|
||||
local recipeIndex = PlayerInventoryUI:getContex(playerName, tabType..'_tab_craft_selected_recipe_index') or 0
|
||||
local craft = PrimitiveCraftHandler:getCraftByName(craftName)
|
||||
|
||||
-- We create an ItemStack of the crafted item by multiplying
|
||||
@@ -375,7 +428,7 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('.+::craft_item', function(play
|
||||
local materialGroups = {}
|
||||
|
||||
-- We fill in the tables of required materials based on the crafting recipe.
|
||||
for _, itemName in ipairs(craft.recipe) do
|
||||
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)...
|
||||
|
||||
@@ -7,6 +7,41 @@ local PrimitiveCraftHandler = Smc.requireCraftSystemPrimitiveCraftHandler()
|
||||
|
||||
local Formspec = Smc.requireFormspec()
|
||||
|
||||
---Returns a human-readable list of materials required for crafting (recipe).
|
||||
---@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(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..'- '..core.strip_colors(recipeItem:get_definition()._title)..' x'..recipeItem:get_count()..'\n'
|
||||
else
|
||||
recipeDesc = recipeDesc..'- any item from the group "'..group..'" x'..count..'\n'
|
||||
end
|
||||
end
|
||||
|
||||
return recipeDesc
|
||||
end
|
||||
|
||||
---@param self PlayerInventoryUI
|
||||
---@param playerName string
|
||||
---@param tabType PlayerInventoryUI.TabType
|
||||
@@ -111,27 +146,24 @@ local function getCraftTab(self, playerName, tabType)
|
||||
local item = ItemStack(craft.output)
|
||||
local itemDef = item:get_definition()
|
||||
|
||||
local recipe = ''
|
||||
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 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
|
||||
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 recipeDesc = getRecipeDesc(craft, recipeIndex)
|
||||
|
||||
CraftTab
|
||||
:itemImage(5.5, 0.5, 2, 2, craft.output)
|
||||
:areaLabel(5.5, 3, 4, 4,
|
||||
itemDef._title..'\n'..
|
||||
itemDef._description..'\n\n'..
|
||||
'Needed:\n'..recipe)
|
||||
:areaLabel(5.5, 7, 4, 1, 'Craft:')
|
||||
:button(5.5, 7.5, 2, 1, tabType..'::craft_item', '1')
|
||||
:button(7.5, 7.5,2, 1, tabType..'::craft_item', ''..tostring(itemDef.stack_max))
|
||||
: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
|
||||
|
||||
Reference in New Issue
Block a user