PlayerInventoryUI: Support and display of alternative recipes in the crafting menu. #34
This commit is contained in:
@@ -289,6 +289,10 @@ local function registerWood(name, x, def)
|
|||||||
recipe = {
|
recipe = {
|
||||||
'wood:'..name..'_bar'
|
'wood:'..name..'_bar'
|
||||||
},
|
},
|
||||||
|
altRecipes = {
|
||||||
|
{ 'wood:'..name..'_plank 2' },
|
||||||
|
{ 'wood:'..name..'_log'}
|
||||||
|
},
|
||||||
output = 'wood:'..name..'_stick 4',
|
output = 'wood:'..name..'_stick 4',
|
||||||
group = 'wood:sticks',
|
group = 'wood:sticks',
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -203,6 +203,8 @@ end
|
|||||||
--- 'right_tab_craft_search_field_value'|
|
--- 'right_tab_craft_search_field_value'|
|
||||||
--- 'left_tab_craft_selected_craft'|
|
--- 'left_tab_craft_selected_craft'|
|
||||||
--- 'right_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'|
|
--- 'left_tab_craft_selected_group'|
|
||||||
--- 'right_tab_craft_selected_group'|
|
--- 'right_tab_craft_selected_group'|
|
||||||
--- 'left_tab_craft_page_number'|
|
--- '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
|
local pageNumber = PlayerInventoryUI:getContex(playerName, tabType..'_tab_craft_page_number') or 1
|
||||||
pageNumber = pageNumber + 1
|
pageNumber = pageNumber + 1
|
||||||
|
|
||||||
if pageNumber <= 0 then
|
PlayerInventoryUI:setContex(playerName, tabType..'_tab_craft_page_number', pageNumber)
|
||||||
pageNumber = 1
|
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
|
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)
|
PlayerInventoryUI:showForm(playerName)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@@ -330,6 +358,7 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('.+::%S+::item_button',function
|
|||||||
assert(tabType == 'left' or tabType == 'right')
|
assert(tabType == 'left' or tabType == 'right')
|
||||||
|
|
||||||
local playerName = player:get_player_name()
|
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:setContex(playerName, tabType..'_tab_craft_selected_craft', craftName)
|
||||||
PlayerInventoryUI:showForm(playerName)
|
PlayerInventoryUI:showForm(playerName)
|
||||||
end)
|
end)
|
||||||
@@ -346,6 +375,29 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('.+::%S+::group_button',functio
|
|||||||
PlayerInventoryUI:showForm(playerName)
|
PlayerInventoryUI:showForm(playerName)
|
||||||
end)
|
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.
|
-- 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.
|
-- 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')
|
assert(tabType == 'left' or tabType == 'right')
|
||||||
|
|
||||||
-- We get the recipe selected in the crafting menu.
|
-- 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)
|
local craft = PrimitiveCraftHandler:getCraftByName(craftName)
|
||||||
|
|
||||||
-- We create an ItemStack of the crafted item by multiplying
|
-- We create an ItemStack of the crafted item by multiplying
|
||||||
@@ -375,7 +428,7 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('.+::craft_item', function(play
|
|||||||
local materialGroups = {}
|
local materialGroups = {}
|
||||||
|
|
||||||
-- We fill in the tables of required materials based on the crafting recipe.
|
-- 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
|
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 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()
|
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 self PlayerInventoryUI
|
||||||
---@param playerName string
|
---@param playerName string
|
||||||
---@param tabType PlayerInventoryUI.TabType
|
---@param tabType PlayerInventoryUI.TabType
|
||||||
@@ -111,27 +146,24 @@ local function getCraftTab(self, playerName, tabType)
|
|||||||
local item = ItemStack(craft.output)
|
local item = ItemStack(craft.output)
|
||||||
local itemDef = item:get_definition()
|
local itemDef = item:get_definition()
|
||||||
|
|
||||||
local recipe = ''
|
local recipeIndex = self:getContex(playerName, tabType..'_tab_craft_selected_recipe_index') or 0
|
||||||
for _, itemName in ipairs(craft.recipe) do
|
if recipeIndex > #craft.altRecipes then
|
||||||
local group, count = itemName:match('group:(%S+)'), itemName:match('group:%S+ (%S+)') or 1
|
recipeIndex = #craft.altRecipes
|
||||||
|
self:setContex(playerName, tabType..'_tab_craft_selected_recipe_index', #craft.altRecipes)
|
||||||
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
|
end
|
||||||
|
|
||||||
|
local recipeDesc = getRecipeDesc(craft, recipeIndex)
|
||||||
|
|
||||||
CraftTab
|
CraftTab
|
||||||
:itemImage(5.5, 0.5, 2, 2, craft.output)
|
:itemImage(5.5, 0.5, 2, 2, craft.output)
|
||||||
:areaLabel(5.5, 3, 4, 4,
|
:label(10, 0.5, ('%s/%s'):format(recipeIndex+1, #craft.altRecipes+1))
|
||||||
itemDef._title..'\n'..
|
:textArea(5.5, 3, 5, 4, nil, nil,
|
||||||
itemDef._description..'\n\n'..
|
itemDef._title..'\n\n'..
|
||||||
'Needed:\n'..recipe)
|
'Needed:\n'..recipeDesc)
|
||||||
:areaLabel(5.5, 7, 4, 1, 'Craft:')
|
:button(5.5, 7.5, 0.5, 1, tabType..'::alt_recipe_back', '<')
|
||||||
:button(5.5, 7.5, 2, 1, tabType..'::craft_item', '1')
|
:button(6, 7.5, 2, 1, tabType..'::craft_item', '1')
|
||||||
:button(7.5, 7.5,2, 1, tabType..'::craft_item', ''..tostring(itemDef.stack_max))
|
: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
|
end
|
||||||
|
|
||||||
return CraftTab
|
return CraftTab
|
||||||
|
|||||||
Reference in New Issue
Block a user