CraftSystem: Added the ability to select crafts by pattern

This commit is contained in:
Van
2026-04-29 21:35:28 +03:00
parent bbaf18f352
commit e8210924bf
4 changed files with 53 additions and 8 deletions

View File

@@ -1,13 +1,15 @@
---Abstract class for craft. ---Abstract class for craft.
---@class CraftSystem.BaseCraft ---@class CraftSystem.BaseCraft
---@field name string ---@field name string
---@field group CraftSystem.CraftGroup ---@field output ItemStackString
---@field group CraftSystem.CraftGroup
local BaseCraft = {} local BaseCraft = {}
---Definition table for the `CraftSystem.BaseCraft`. ---Definition table for the `CraftSystem.BaseCraft`.
--- ---
---**Only for EmmyLua.** ---**Only for EmmyLua.**
---@class CraftSystem.BaseCraftDef ---@class CraftSystem.BaseCraftDef
---@field group CraftSystem.CraftGroup? ---@field output ItemStackString
---@field group CraftSystem.CraftGroup?
return BaseCraft return BaseCraft

View File

@@ -1,3 +1,6 @@
local tableInsert = table.insert
local stringMatch = string.match
local PLAYERS_KNOW_ALL_RECIPES = true local PLAYERS_KNOW_ALL_RECIPES = true
---Abstract class for handling crafting. ---Abstract class for handling crafting.
@@ -6,13 +9,15 @@ local PLAYERS_KNOW_ALL_RECIPES = true
---@field craftsByGroup table<CraftSystem.CraftGroup, CraftSystem.BaseCraft[]> ---@field craftsByGroup table<CraftSystem.CraftGroup, CraftSystem.BaseCraft[]>
---@field craftsList CraftSystem.BaseCraft[] ---@field craftsList CraftSystem.BaseCraft[]
---@field groupedCrafts (CraftSystem.BaseCraft[]|CraftSystem.BaseCraft)[] ---@field groupedCrafts (CraftSystem.BaseCraft[]|CraftSystem.BaseCraft)[]
---@field learnedCrafts table<string, (CraftSystem.BaseCraft[]|CraftSystem.BaseCraft)[]> ---@field learnedCrafts table<string, CraftSystem.BaseCraft[]>
---@field learnedGroupedCrafts table<string, (CraftSystem.BaseCraft[]|CraftSystem.BaseCraft)[]>
local BaseCraftHandler = { local BaseCraftHandler = {
craftsByName = {}, craftsByName = {},
craftsByGroup = {}, craftsByGroup = {},
craftsList = {}, craftsList = {},
groupedCrafts = {}, groupedCrafts = {},
learnedCrafts = {}, learnedCrafts = {},
learnedGroupedCrafts = {},
} }
---@param craftName string ---@param craftName string
@@ -44,6 +49,46 @@ function BaseCraftHandler:getLearnedCrafts(playerName)
return self.groupedCrafts return self.groupedCrafts
end end
local crafts = self.learnedGroupedCrafts[playerName]
if crafts == nil then
crafts = {}
self.learnedGroupedCrafts[playerName] = crafts
end
return crafts
end
---@param crafts CraftSystem.BaseCraft[]
---@param pattern string
---@return CraftSystem.BaseCraft[]
local function getMathedCrafts(crafts, pattern)
local matchedCrafts = {}
for _, craft in ipairs(crafts) do
local outputItem = ItemStack(craft.output)
local itemSettings = Smc.getItemDefinition(outputItem).settings
local ruName = core.get_translated_string('ru', itemSettings._title)
local enName = core.get_translated_string('en', itemSettings._title)
if stringMatch(outputItem:get_name(), pattern) or
stringMatch(ruName, pattern) or
stringMatch(enName, pattern) then
tableInsert(matchedCrafts, craft)
end
end
return matchedCrafts
end
---@param playerName string
---@param pattern string
---@return CraftSystem.BaseCraft[]
function BaseCraftHandler:getLearnedCraftsMatch(playerName, pattern)
if PLAYERS_KNOW_ALL_RECIPES then
return getMathedCrafts(self.craftsList, pattern)
end
local crafts = self.learnedCrafts[playerName] local crafts = self.learnedCrafts[playerName]
if crafts == nil then if crafts == nil then
@@ -54,4 +99,6 @@ function BaseCraftHandler:getLearnedCrafts(playerName)
return crafts return crafts
end end
--TODO: сохранять выученные крафты игроков в сторадже
return BaseCraftHandler return BaseCraftHandler

View File

@@ -19,7 +19,6 @@ local stageTypes = {
---@class CraftSystem.ImmersiveCraft : CraftSystem.BaseCraft ---@class CraftSystem.ImmersiveCraft : CraftSystem.BaseCraft
---@field startStageName string ---@field startStageName string
---@field triggerItem ItemStackString ---@field triggerItem ItemStackString
---@field output ItemStackString
---@field stagesByName table<string, CraftSystem.ImmersiveCraftStage> ---@field stagesByName table<string, CraftSystem.ImmersiveCraftStage>
local ImmersiveCraft = extend(BaseCraft, {}) local ImmersiveCraft = extend(BaseCraft, {})
@@ -29,7 +28,6 @@ local ImmersiveCraft = extend(BaseCraft, {})
---@class CraftSystem.ImmersiveCraftDef : CraftSystem.BaseCraftDef ---@class CraftSystem.ImmersiveCraftDef : CraftSystem.BaseCraftDef
---@field startStageName string ---@field startStageName string
---@field triggerItem ItemStackString ---@field triggerItem ItemStackString
---@field output ItemStackString
---@field stages CraftSystem.ImmersiveCraftStageDef[] ---@field stages CraftSystem.ImmersiveCraftStageDef[]
---@param name string ---@param name string

View File

@@ -7,7 +7,6 @@ local require = modInfo.require
local BaseCraft = require('CraftSystem.BaseCraft') local BaseCraft = require('CraftSystem.BaseCraft')
---@class CraftSystem.PrimitiveCraft : CraftSystem.BaseCraft ---@class CraftSystem.PrimitiveCraft : CraftSystem.BaseCraft
---@field output ItemStackString
---@field recipe CraftSystem.CraftRecipe ---@field recipe CraftSystem.CraftRecipe
---@field returnItems ItemStackString[] ---@field returnItems ItemStackString[]
local PrimitiveCraft = extend(BaseCraft, {}) local PrimitiveCraft = extend(BaseCraft, {})
@@ -16,7 +15,6 @@ local PrimitiveCraft = extend(BaseCraft, {})
--- ---
---**Only for EmmyLua.** ---**Only for EmmyLua.**
---@class CraftSystem.PrimitiveCraftDef : CraftSystem.BaseCraftDef ---@class CraftSystem.PrimitiveCraftDef : CraftSystem.BaseCraftDef
---@field output ItemStackString
---@field recipe CraftSystem.CraftRecipe ---@field recipe CraftSystem.CraftRecipe
---@field returnItems ItemStackString[]? ---@field returnItems ItemStackString[]?