2 Commits

6 changed files with 134 additions and 32 deletions

View File

@@ -25,7 +25,7 @@ local BaseCraftHandler = {
function BaseCraftHandler:getCraftByName(craftName) function BaseCraftHandler:getCraftByName(craftName)
local craft = self.craftsByName[craftName] local craft = self.craftsByName[craftName]
assert(craft ~= nil, ('There is no primitive craft named `%s`.'):format( assert(craft ~= nil, ('A craft `%s` not exists.'):format(
craftName)) craftName))
return craft return craft
@@ -36,7 +36,7 @@ end
function BaseCraftHandler:getCraftsByGroup(craftGroup) function BaseCraftHandler:getCraftsByGroup(craftGroup)
local crafts = self.craftsByGroup[craftGroup] local crafts = self.craftsByGroup[craftGroup]
assert(crafts ~= nil, ('There are no primitive crafts with `%s` group'):format( assert(crafts ~= nil, ('A craft with group `%s` not exists.'):format(
craftGroup)) craftGroup))
return crafts return crafts

View File

@@ -8,7 +8,8 @@ local BaseCraft = require('CraftSystem.BaseCraft')
---@class CraftSystem.PrimitiveCraft : CraftSystem.BaseCraft ---@class CraftSystem.PrimitiveCraft : CraftSystem.BaseCraft
---@field recipe CraftSystem.CraftRecipe ---@field recipe CraftSystem.CraftRecipe
---@field returnItems ItemStackString[] ---@field altRecipes CraftSystem.CraftRecipe[]
---@field returnItems ItemStackString[]?
local PrimitiveCraft = extend(BaseCraft, {}) local PrimitiveCraft = extend(BaseCraft, {})
---Definition table for the `CraftSystem.PrimitiveCraft`. ---Definition table for the `CraftSystem.PrimitiveCraft`.
@@ -16,6 +17,7 @@ local PrimitiveCraft = extend(BaseCraft, {})
---**Only for EmmyLua.** ---**Only for EmmyLua.**
---@class CraftSystem.PrimitiveCraftDef : CraftSystem.BaseCraftDef ---@class CraftSystem.PrimitiveCraftDef : CraftSystem.BaseCraftDef
---@field recipe CraftSystem.CraftRecipe ---@field recipe CraftSystem.CraftRecipe
---@field altRecipes CraftSystem.CraftRecipe[]?
---@field returnItems ItemStackString[]? ---@field returnItems ItemStackString[]?
---@param name string ---@param name string
@@ -23,13 +25,13 @@ local PrimitiveCraft = extend(BaseCraft, {})
---@return CraftSystem.PrimitiveCraft ---@return CraftSystem.PrimitiveCraft
function PrimitiveCraft:new(name, def) function PrimitiveCraft:new(name, def)
---@type CraftSystem.PrimitiveCraft ---@type CraftSystem.PrimitiveCraft
local instance = getInstance(self, { local instance = getInstance(self, {})
name = name, instance.name = name
group = def.group, instance.group = def.group
output = def.output, instance.output = def.output
recipe = def.recipe, instance.recipe = def.recipe
returnItems = def.returnItems or {} instance.altRecipes = def.altRecipes or {}
}) instance.returnItems = def.returnItems
return instance return instance
end end

View File

@@ -42,6 +42,17 @@ function PrimitiveCraftHandler:registerCraft(craftName, def)
tableInsert(self.craftsList, craft) tableInsert(self.craftsList, craft)
end end
---Adds alternative recipes to the craft with the specified name.
---
---If a craft with the specified name does not exist, an error will call.
---@param craftName string
---@param recipes CraftSystem.CraftRecipe[]
function PrimitiveCraftHandler:registerAlternativeRecipes(craftName, recipes)
local craft = PrimitiveCraftHandler:getCraftByName(craftName)
craft.altRecipes = tableJoin(craft.altRecipes or {}, recipes)
end
core.register_on_mods_loaded(function() core.register_on_mods_loaded(function()
for groupName, crafts in pairs(PrimitiveCraftHandler.craftsByGroup) do for groupName, crafts in pairs(PrimitiveCraftHandler.craftsByGroup) do
tableSort(crafts, function(a, b) tableSort(crafts, function(a, b)

View File

@@ -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',
}) })

View File

@@ -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)...

View File

@@ -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