diff --git a/mods/CORE/CraftSystem/src/CraftSystem/BaseCraft.lua b/mods/CORE/CraftSystem/src/CraftSystem/BaseCraft.lua index a2ec1c1..6064ea1 100644 --- a/mods/CORE/CraftSystem/src/CraftSystem/BaseCraft.lua +++ b/mods/CORE/CraftSystem/src/CraftSystem/BaseCraft.lua @@ -1,13 +1,15 @@ ---Abstract class for craft. ---@class CraftSystem.BaseCraft ----@field name string ----@field group CraftSystem.CraftGroup +---@field name string +---@field output ItemStackString +---@field group CraftSystem.CraftGroup local BaseCraft = {} ---Definition table for the `CraftSystem.BaseCraft`. --- ---**Only for EmmyLua.** ---@class CraftSystem.BaseCraftDef ----@field group CraftSystem.CraftGroup? +---@field output ItemStackString +---@field group CraftSystem.CraftGroup? return BaseCraft diff --git a/mods/CORE/CraftSystem/src/CraftSystem/BaseCraftHandler.lua b/mods/CORE/CraftSystem/src/CraftSystem/BaseCraftHandler.lua index baa8434..fcecc31 100644 --- a/mods/CORE/CraftSystem/src/CraftSystem/BaseCraftHandler.lua +++ b/mods/CORE/CraftSystem/src/CraftSystem/BaseCraftHandler.lua @@ -1,3 +1,6 @@ +local tableInsert = table.insert +local stringMatch = string.match + local PLAYERS_KNOW_ALL_RECIPES = true ---Abstract class for handling crafting. @@ -6,13 +9,15 @@ local PLAYERS_KNOW_ALL_RECIPES = true ---@field craftsByGroup table ---@field craftsList CraftSystem.BaseCraft[] ---@field groupedCrafts (CraftSystem.BaseCraft[]|CraftSystem.BaseCraft)[] ----@field learnedCrafts table +---@field learnedCrafts table +---@field learnedGroupedCrafts table local BaseCraftHandler = { craftsByName = {}, craftsByGroup = {}, craftsList = {}, groupedCrafts = {}, learnedCrafts = {}, + learnedGroupedCrafts = {}, } ---@param craftName string @@ -44,6 +49,46 @@ function BaseCraftHandler:getLearnedCrafts(playerName) return self.groupedCrafts 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] if crafts == nil then @@ -54,4 +99,6 @@ function BaseCraftHandler:getLearnedCrafts(playerName) return crafts end +--TODO: сохранять выученные крафты игроков в сторадже + return BaseCraftHandler diff --git a/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraft.lua b/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraft.lua index eb9f47b..a517fe2 100644 --- a/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraft.lua +++ b/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraft.lua @@ -19,7 +19,6 @@ local stageTypes = { ---@class CraftSystem.ImmersiveCraft : CraftSystem.BaseCraft ---@field startStageName string ---@field triggerItem ItemStackString ----@field output ItemStackString ---@field stagesByName table local ImmersiveCraft = extend(BaseCraft, {}) @@ -29,7 +28,6 @@ local ImmersiveCraft = extend(BaseCraft, {}) ---@class CraftSystem.ImmersiveCraftDef : CraftSystem.BaseCraftDef ---@field startStageName string ---@field triggerItem ItemStackString ----@field output ItemStackString ---@field stages CraftSystem.ImmersiveCraftStageDef[] ---@param name string diff --git a/mods/CORE/CraftSystem/src/CraftSystem/PrimitiveCraft.lua b/mods/CORE/CraftSystem/src/CraftSystem/PrimitiveCraft.lua index 75b81c7..ca8e543 100644 --- a/mods/CORE/CraftSystem/src/CraftSystem/PrimitiveCraft.lua +++ b/mods/CORE/CraftSystem/src/CraftSystem/PrimitiveCraft.lua @@ -7,7 +7,6 @@ local require = modInfo.require local BaseCraft = require('CraftSystem.BaseCraft') ---@class CraftSystem.PrimitiveCraft : CraftSystem.BaseCraft ----@field output ItemStackString ---@field recipe CraftSystem.CraftRecipe ---@field returnItems ItemStackString[] local PrimitiveCraft = extend(BaseCraft, {}) @@ -16,7 +15,6 @@ local PrimitiveCraft = extend(BaseCraft, {}) --- ---**Only for EmmyLua.** ---@class CraftSystem.PrimitiveCraftDef : CraftSystem.BaseCraftDef ----@field output ItemStackString ---@field recipe CraftSystem.CraftRecipe ---@field returnItems ItemStackString[]?