local assert = assert local tableCopy, tableMerge, colorize = table.copy, table.merge, core.colorize local stringMatch = string.match local coreGetTranslator, coreTranslate = core.get_translator, core.translate local coreColorize = core.colorize local coreRegisterItem, coreRegisterTool, coreRegisterNode = core.register_craftitem, core.register_tool, core.register_node local smcConcatItemDescription = Smc.concatItemDescription local smcAddRegisteredItem = Smc.addRegisteredItem local Mod = Mod local ItemFactory = Smc.requireItemFactory() ---Colors the title according to the rarity of the item. --- ---Note: that if `true`, the `_title_color` will be ignored. local TITLE_RARITY_COLOR = true ---Shows rarity on a separate line. local SHOW_RARITY = false ---Determines whether common rarity will be shown in item descriptions or not. local SHOW_COMMON_RARITY = true ---A line that specifies the background color for item descriptions. local ITEM_DESC_BACKGROUND_ESCAPE_SEQ = core.get_background_escape_sequence('#110F13') ---Colors corresponding to the rarity color in the item description. ---@type table local RARITY_COLORS = { ['common'] = '#C0C0C0', ['uncommon'] = '#7cd2e2', ['distinguished'] = '#7ce2da', ['eminent'] = '#44d1a9', ['prime'] = '#9CF0B1', ['unique'] = '#f4c95a' } ---@class Item : Class ---@field itemDef Item.ItemDefinition ---@field luantiDef ItemDefinition ---@field getType fun(self:Item):string ---@field defaultDef Item.ItemDefinition local Item = Class:extend({ defaultDef = { settings = { name = '', _title = '', _description = '', _title_color = '#FFFFFF', _description_color = '#838383', _rarity = 'common', _add_mod_name = true, stack_max = 128, }, callbacks = {} } }) local DESC_PATTERNS = { [1] = '{title}\n {desc}', [2] = '{title}\n {desc}\n{rarity_title}' } ---Returns an item description pattern in accordance ---with global rarity display settings. ---@param rarity Item.Rarity ---@return string local function getDescPattern(rarity) if not SHOW_RARITY then return DESC_PATTERNS[1] end if not SHOW_COMMON_RARITY and rarity == 'common' then return DESC_PATTERNS[1] end return DESC_PATTERNS[2] end ---@param itemDef Item.ItemDefinition ---@return ItemDefinition local function itemDefToLuantiDef(itemDef) local modName = Mod:getInfo().shortName local S = coreGetTranslator(modName) local s = itemDef.settings local c = itemDef.callbacks local title, description = s._title, s._description if title == nil or title == '' then title = stringMatch(s.name, '%S:(%S+)')..'-title' end if description == nil or description == '' then description = stringMatch(s.name, '%S:(%S+)')..'-desc' end assert(s._rarity ~= nil, 'The rarity of an item cannot be empty.') local rarityColor = RARITY_COLORS[s._rarity] if s._title_color ~= nil and s._description_color ~= nil then if TITLE_RARITY_COLOR then title = colorize(rarityColor, S(title)) else title = colorize(s._title_color, S(title)) end description = colorize(s._description_color, S(description)) else error('Item description color parameters cannot be empty.') end local rarityTitle = coreColorize(rarityColor, coreTranslate('item', s._rarity)) s._title = title s._description = description s._rarity_title = rarityTitle local luantiDescription = smcConcatItemDescription(s, nil, getDescPattern(s._rarity)) s.description = luantiDescription..ITEM_DESC_BACKGROUND_ESCAPE_SEQ local luantiDef = tableMerge(s, c, true) --[[@as ItemDefinition]] return luantiDef end ---@param luantiDef ItemDefinition local function registerCraftitem(luantiDef) local name = luantiDef.name ---@diagnostic disable-next-line: inject-field luantiDef.name = nil coreRegisterItem(':'..name, luantiDef) end ---@param luantiDef ItemDefinition local function registerTool(luantiDef) local name = luantiDef.name ---@diagnostic disable-next-line: inject-field luantiDef.name = nil coreRegisterTool(':'..name, luantiDef) end ---@param luantiDef NodeDefinition local function registerNode(luantiDef) local name = luantiDef.name ---@diagnostic disable-next-line: inject-field luantiDef.name = nil coreRegisterNode(':'..name, luantiDef) end ---@param itemDef Item.ItemDefinition|Node.NodeDefinition ---@return Item function Item:new(itemDef) assert(itemDef.settings.description == nil, 'The item description was specified using `description`, not `_description`. '.. 'Please note that the `description` field should not be used when registering an item. '.. 'Use `_title` and `_description` instead.') itemDef = tableCopy(itemDef) -- Adding default parameterss itemDef = tableMerge(itemDef, self.defaultDef, true) --[[@as Item.ItemDefinition]] local s = itemDef.settings local shortModName = Mod:getInfo().shortName if s._add_mod_name then s.name = shortModName .. ':' .. s.name end ---@type Item local instance = self:getInstance({}) local luantiDef = itemDefToLuantiDef(itemDef) local type = itemDef.settings._type if type == nil then type = 'craftitem' end itemDef.settings._type = nil function instance:getType() return type end if type == 'node' then registerNode(luantiDef --[[@as NodeDefinition]]) elseif type == 'craftitem' then registerCraftitem(luantiDef) elseif type == 'tool' then registerTool(luantiDef) else error(('Unknown item type: `%s`.'):format(type)) end instance.itemDef = itemDef instance.luantiDef = luantiDef smcAddRegisteredItem(shortModName, instance) return instance end ---Allows you to create a child class of an item by specifying ---default parameters that will be added to the item's parameters ---when an instance is received. ---@param defaultDef Item.ItemDefinition ---@return Item function Item:getExtended(defaultDef) defaultDef = tableMerge(defaultDef, self.defaultDef, true) ---@type Item local ChildClass = self:extend({ defaultDef = defaultDef, }) return ChildClass end ---Returns an instance of the `ItemFactory` class ---that can be used to mass-register identical items. ---@param defaultDef? Item.ItemDefinition ---@return ItemFactory function Item:getFactory(defaultDef) if defaultDef == nil then defaultDef = self.defaultDef else defaultDef = tableMerge(self.defaultDef, defaultDef, true) end ---@type ItemFactory local instance = ItemFactory:new(self, defaultDef) return instance end return Item