CraftSystem: remove alt recipes
This commit is contained in:
@@ -21,9 +21,7 @@
|
|||||||
"diagnostics": {
|
"diagnostics": {
|
||||||
"libraryFiles": "Enable",
|
"libraryFiles": "Enable",
|
||||||
"workspaceDelay": 1,
|
"workspaceDelay": 1,
|
||||||
"disable": [
|
"disable": [],
|
||||||
"inject-field"
|
|
||||||
],
|
|
||||||
"globals": [
|
"globals": [
|
||||||
// Smc globals:
|
// Smc globals:
|
||||||
"Smc",
|
"Smc",
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ local function getMaterials(craft, recipeIndex, count)
|
|||||||
local materialStacks = {}
|
local materialStacks = {}
|
||||||
local materialGroups = {}
|
local materialGroups = {}
|
||||||
|
|
||||||
for _, itemName in ipairs(craft:getRecipe(recipeIndex)) do
|
for _, itemName in ipairs(craft.recipe) do
|
||||||
local group = itemName:match('group:(%S+)')
|
local group = itemName:match('group:(%S+)')
|
||||||
local materialCount = itemName:match('group:%S+ (%S+)') or 1
|
local materialCount = itemName:match('group:%S+ (%S+)') or 1
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ local CHECK = {YES = '[x]', NO = '[ ]', EMPTY = '-'}
|
|||||||
---@class CraftSystem.BaseCraft : Class
|
---@class CraftSystem.BaseCraft : Class
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field recipe ItemStackString[]
|
---@field recipe ItemStackString[]
|
||||||
---@field altRecipes CraftSystem.CraftRecipe[]
|
|
||||||
---@field output ItemStackString
|
---@field output ItemStackString
|
||||||
---@field group CraftSystem.CraftGroup
|
---@field group CraftSystem.CraftGroup
|
||||||
local BaseCraft = Class:extend({})
|
local BaseCraft = Class:extend({})
|
||||||
@@ -13,7 +12,6 @@ local BaseCraft = Class:extend({})
|
|||||||
---Definition table for the `CraftSystem.BaseCraft`.
|
---Definition table for the `CraftSystem.BaseCraft`.
|
||||||
---@class CraftSystem.BaseCraftDef
|
---@class CraftSystem.BaseCraftDef
|
||||||
---@field recipe ItemStackString[]
|
---@field recipe ItemStackString[]
|
||||||
---@field altRecipes CraftSystem.CraftRecipe[]?
|
|
||||||
---@field output ItemStackString
|
---@field output ItemStackString
|
||||||
---@field group CraftSystem.CraftGroup?
|
---@field group CraftSystem.CraftGroup?
|
||||||
|
|
||||||
@@ -25,7 +23,6 @@ function BaseCraft:new(name, def)
|
|||||||
local instance = self:getInstance({})
|
local instance = self:getInstance({})
|
||||||
instance.name = name
|
instance.name = name
|
||||||
instance.recipe = def.recipe
|
instance.recipe = def.recipe
|
||||||
instance.altRecipes = def.altRecipes or {}
|
|
||||||
instance.output = def.output
|
instance.output = def.output
|
||||||
instance.group = def.group
|
instance.group = def.group
|
||||||
|
|
||||||
@@ -80,46 +77,11 @@ local function getCheckMaterialGroup(material, count, list)
|
|||||||
return CHECK.NO
|
return CHECK.NO
|
||||||
end
|
end
|
||||||
|
|
||||||
---Returns the crafting recipe corresponding to the index.
|
---@param mainList ItemStack[]?
|
||||||
---@param index integer Recipe number. If `0` is specified,
|
---@return string
|
||||||
--- the primary craft (`craft.recipe`) will be used;
|
function BaseCraft:recipeToString(mainList)
|
||||||
--- if `1` or greater, the alternative craft
|
|
||||||
--- corresponding to the index will be used (`craft.altRecipes`).
|
|
||||||
---@return CraftSystem.CraftRecipe
|
|
||||||
function BaseCraft:getRecipe(index)
|
|
||||||
local recipe
|
|
||||||
if index == 0 or self.altRecipes == nil then
|
|
||||||
recipe = self.recipe
|
|
||||||
else
|
|
||||||
if index > #self.altRecipes then
|
|
||||||
recipe = self.altRecipes[#self.altRecipes]
|
|
||||||
else
|
|
||||||
recipe = self.altRecipes[index]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
assert(recipe ~= nil)
|
|
||||||
|
|
||||||
return recipe
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param recipeIndex integer?
|
|
||||||
---@param mainList ItemStack[]?
|
|
||||||
---@return string
|
|
||||||
function BaseCraft:recipeToString(recipeIndex, mainList)
|
|
||||||
local recipe
|
|
||||||
if recipeIndex == nil or recipeIndex == 0 or self.altRecipes == nil then
|
|
||||||
recipe = self.recipe
|
|
||||||
else
|
|
||||||
if recipeIndex > #self.altRecipes then
|
|
||||||
recipe = self.altRecipes[#self.altRecipes]
|
|
||||||
else
|
|
||||||
recipe = self.altRecipes[recipeIndex]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
assert(recipe ~= nil)
|
|
||||||
|
|
||||||
local recipeDesc = ''
|
local recipeDesc = ''
|
||||||
for _, itemName in ipairs(recipe) do
|
for _, itemName in ipairs(self.recipe) 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 group == nil then
|
if group == nil then
|
||||||
@@ -139,16 +101,15 @@ function BaseCraft:recipeToString(recipeIndex, mainList)
|
|||||||
return recipeDesc
|
return recipeDesc
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param recipeIndex integer?
|
---@param mainList ItemStack[]?
|
||||||
---@param mainList ItemStack[]?
|
---@return string
|
||||||
---@return string
|
function BaseCraft:toString(mainList)
|
||||||
function BaseCraft:toString(recipeIndex, mainList)
|
|
||||||
local outputItem = ItemStack(self.output)
|
local outputItem = ItemStack(self.output)
|
||||||
local outputItemSettings = Smc.getItemDefinition(outputItem).settings
|
local outputItemSettings = Smc.getItemDefinition(outputItem).settings
|
||||||
|
|
||||||
local craftDesc = ('%s\n\nNeeded:\n%s'):format(
|
local craftDesc = ('%s\n\nNeeded:\n%s'):format(
|
||||||
outputItemSettings._title,
|
outputItemSettings._title,
|
||||||
self:recipeToString(recipeIndex, mainList))
|
self:recipeToString(mainList))
|
||||||
|
|
||||||
return craftDesc
|
return craftDesc
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -198,7 +198,7 @@ function ImmersiveCraft:toString(recipeIndex, mainList)
|
|||||||
|
|
||||||
local craftDesc = ('%s\n\nRecipe:\n%s\nActions:\n%s'):format(
|
local craftDesc = ('%s\n\nRecipe:\n%s\nActions:\n%s'):format(
|
||||||
outputItemSettings._title,
|
outputItemSettings._title,
|
||||||
BaseCraft.recipeToString(self, recipeIndex, mainList),
|
BaseCraft.recipeToString(self, mainList),
|
||||||
self:recipeToString(recipeIndex, mainList))
|
self:recipeToString(recipeIndex, mainList))
|
||||||
|
|
||||||
return craftDesc
|
return craftDesc
|
||||||
|
|||||||
@@ -51,17 +51,6 @@ 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
|
|
||||||
|
|
||||||
-- Sort the list of crafts in alphabetical order by product name and combine crafts into groups.
|
-- Sort the list of crafts in alphabetical order by product name and combine crafts into groups.
|
||||||
-- Provides a convenient list of crafting items in the player's inventory.
|
-- Provides a convenient list of crafting items in the player's inventory.
|
||||||
core.register_on_mods_loaded(function()
|
core.register_on_mods_loaded(function()
|
||||||
|
|||||||
@@ -291,10 +291,6 @@ 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',
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -121,20 +121,14 @@ local function getCraftTabTemplate(self, playerName, tabType, tabName, handler)
|
|||||||
local item = ItemStack(craft.output)
|
local item = ItemStack(craft.output)
|
||||||
local itemDef = item:get_definition()
|
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)
|
local player = core.get_player_by_name(playerName)
|
||||||
assert(player ~= nil)
|
assert(player ~= nil)
|
||||||
local mainList = player:get_inventory():get_list('main')
|
local mainList = player:get_inventory():get_list('main')
|
||||||
|
|
||||||
CraftTab
|
CraftTab
|
||||||
:itemImage(5.5, 0.5, 2, 2, craft.output)
|
:itemImage(5.5, 0.5, 2, 2, craft.output)
|
||||||
:label(10, 0.5, ('%s/%s'):format(recipeIndex+1, #craft.altRecipes+1))
|
:label(10, 0.5, ('%s/%s'):format(0+1, 'TODO')) -- TODO
|
||||||
:textArea(5.5, 3, 5, 4, nil, nil, craft:toString(recipeIndex, mainList))
|
:textArea(5.5, 3, 5, 4, nil, nil, craft:toString(mainList))
|
||||||
:button(5.5, 7.5, 0.5, 1, tabType..'::'..tabName..'::alt_recipe_back', '<')
|
: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(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(8, 7.5,2, 1, tabType..'::'..tabName..'::craft_item', ''..tostring(itemDef.stack_max))
|
||||||
|
|||||||
Reference in New Issue
Block a user