CraftSystem: reworking the code, adding something useful

This commit is contained in:
Van
2026-05-20 21:20:55 +03:00
parent 83aac87ab9
commit 0051dea067
11 changed files with 363 additions and 201 deletions

View File

@@ -1,3 +1,5 @@
local tableJoin = table.join
local modInfo = Mod.getInfo('smc__core__craft_system')
local require = modInfo.require
@@ -25,8 +27,9 @@ local CraftSystem = {
--- Craft Types ---
---@alias CraftSystem.BaseCraftType 'primitive'|'immersive'|'processing'
---@alias CraftSystem.PrimitiveCraftType 'inside_player_inventory'
---@alias CraftSystem.ImmersiveCraftType 'workbench'
---@alias CraftSystem.ImmersiveCraftType 'workbench' -- DEBUG
---@alias CraftSystem.ProcessingCraftType 'test' -- DEBUG
---@alias CraftSystem.CraftType CraftSystem.PrimitiveCraftType|CraftSystem.ImmersiveCraftType|CraftSystem.ProcessingCraftType
@@ -35,7 +38,7 @@ local CraftSystem = {
local function registerPrimitiveCraftType(type, def)
assert(CraftSystem.PrimitiveCraftHandlers[type] == nil, ('A primitive craft type `%s` already exists.'):format(type))
local handler = PrimitiveCraftHandler:new(type, def)
local handler = PrimitiveCraftHandler:new('primitive', type, def)
CraftSystem.PrimitiveCraftHandlers[type] = handler
end
@@ -44,7 +47,7 @@ end
local function registerImmersiveCraftType(type, def)
assert(CraftSystem.ImmersiveCraftHandlers[type] == nil, ('A immersive craft type `%s` already exists.'):format(type))
local handler = ImmersiveCraftHandler:new(type, def)
local handler = ImmersiveCraftHandler:new('immersive', type, def)
CraftSystem.ImmersiveCraftHandlers[type] = handler
end
@@ -53,7 +56,7 @@ end
local function registerProcessingCraftType(type, def)
assert(CraftSystem.ProcessingCraftHandlers[type] == nil, ('A processing craft type `%s` already exists.'):format(type))
local handler = ProcessingCraftHandler:new(type, def)
local handler = ProcessingCraftHandler:new('processing', type, def)
CraftSystem.ProcessingCraftHandlers[type] = handler
end
@@ -120,4 +123,112 @@ function CraftSystem:registerProcessingCraft(type, craftName, def)
handler:registerCraft(craftName, def)
end
--- Methods for performing crafting ---
---@param craft CraftSystem.BaseCraft
---@param recipeIndex integer
---@param count integer
---@return ItemStack[] materialStacks A table containing the items required for crafting.
---@return table<string, integer> materialGroups A table containing the item groups required for crafting.
local function getMaterials(craft, recipeIndex, count)
local materialStacks = {}
local materialGroups = {}
for _, itemName in ipairs(craft:getRecipe(recipeIndex)) do
local group = itemName:match('group:(%S+)')
local materialCount = itemName:match('group:%S+ (%S+)') or 1
if group == nil then
local item = ItemStack(itemName)
item:set_count(item:get_count() * count)
table.insert(materialStacks, item)
else
materialGroups[group] = (tonumber(materialCount) or 1) * count
end
end
return materialStacks, materialGroups
end
---Check if the player has the required items.
---@param materialStacks ItemStack[]
---@param inventory InvRef
---@return boolean
local function checkMaterialStacks(materialStacks, inventory)
for _, item in ipairs(materialStacks) do
if not inventory:contains_item('main', item) then
return false
end
end
return true
end
---Check whether the player has the required number of items with the required group for crafting.
---@param materialGroups table<string, integer>
---@param inventory InvRef
---@return boolean
---@return ItemStack[] materialStacks A list of item stacks that match the required material groups.
local function checkMaterialGroups(materialGroups, inventory)
local materialStacks = {}
local mainList = inventory:get_list('main')
for group, count in pairs(materialGroups) do
for _, item in ipairs(mainList) do
if core.get_item_group(item:get_name(), group) > 0 then
local itemCount = item:get_count()
if count > itemCount then
count = count - itemCount
table.insert(materialStacks, item)
else
item:set_count(count)
table.insert(materialStacks, item)
count = 0
end
end
end
if count > 0 then
return false, {}
end
end
return true, materialStacks
end
---Checks whether the player can perform crafting (whether he has the required materials in his inventory).
---@param player Player
---@param craft CraftSystem.BaseCraft
---@param recipeIndex integer
---@param count integer
---@return boolean check true if craft can be produced
---@return ItemStack[] materialStacks list of itemStacks for crafting
function CraftSystem:checkPossiblePlayerInventoryCrafting(player, craft, recipeIndex, count, flags)
local inventory = player:get_inventory()
local OutputStack = ItemStack(craft.output)
OutputStack:set_count(OutputStack:get_count() * count)
local materialStacks, materialGroups = getMaterials(craft, recipeIndex, count)
local isHaveStacks = checkMaterialStacks(materialStacks, inventory)
if not isHaveStacks then
core.chat_send_player(player:get_player_name(), 'Not enough materials for crafting!')
return false, {}
end
local isHaveGroups, materialGroupStacks = checkMaterialGroups(materialGroups, inventory)
if not isHaveGroups then
core.chat_send_player(player:get_player_name(), 'Not enough materials for crafting!')
return false, {}
end
materialStacks = tableJoin(materialStacks, materialGroupStacks)
return true, materialStacks
end
return CraftSystem