local getInstance, extend = Class.getInstance, Class.extend local tableCopy, tableMerge, colorize = table.copy, table.merge, core.colorize local modInfo = Mod.getInfo('smc__core__item') local require = modInfo.require local ItemFactory = Mod.getInfo('smc__core__item_factory').require('ItemFactory') ---Determines whether common rarity will be shown in item descriptions or not. local SHOW_COMMON_RARITY = true ---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' } ---Whether to show the numeric index in the rarity description. local SHOW_RARITY_INDEX = true ---@type table local RARITY_INDEX = { ['common'] = 1, ['uncommon'] = 2, ['distinguished'] = 3, ['eminent'] = 4, ['prime'] = 5, ['unique'] = 0 } ---@class Item ---@field itemDef Item.ItemDefinition ---@field luantiDef ItemDefinition ---@field getVisual fun(): string ---@field defaultDef Item.ItemDefinition local Item = { defaultDef = { settings = { name = '', _title = '', _description = '', _title_color = '#FFFFFF', _description_color = '#C0C0C0', _rarity = 'common', _add_mod_name = true, }, callbacks = {} } } ---Adds an item's rarity tag to the description. ---@param rarity Item.Rarity ---@param desc string ---@return string local function addRarityToDesc(rarity, desc) if not SHOW_COMMON_RARITY and rarity == 'common' then return desc end local S = core.get_translator('item') local color = RARITY_COLORS[rarity] local rarityString = S('Rarity')..': '..S(rarity) if SHOW_RARITY_INDEX then rarityString = rarityString..' ['..RARITY_INDEX[rarity]..']' end return desc..'\n'..colorize(color, rarityString) end ---The alias `table.merge` for EmmyLua ---@type fun(...):(NodeDefinition|ItemDefinition) local defMerge = function(...) return tableMerge(...) end ---@param itemDef Item.ItemDefinition ---@return NodeDefinition|ItemDefinition local function itemDefToLuantiDef(itemDef) local modName = Mod.getInfo().shortName local S = core.get_translator(modName) itemDef = tableCopy(itemDef) local s = itemDef.settings local c = itemDef.callbacks local title = s._title local description = s._description if title == nil or title == '' then title = string.match(s.name, '%S:(%S+)')..'-title' end if description == nil or description == '' then description = string.match(s.name, '%S:(%S+)')..'-desc' end if s._title_color ~= nil and s._description_color ~= nil then description = colorize(s._title_color ,S(title))..'\n'.. colorize(s._description_color, S(description)) else error('Item description color parameters cannot be empty.') end assert(s._rarity ~= nil, 'The rarity of an item cannot be empty.') description = addRarityToDesc(s._rarity, description) s.description = description..core.get_background_escape_sequence('#110F13') local luantiDef = defMerge(s, c) return luantiDef end ---@param luantiDef NodeDefinition local function register3dItem(luantiDef) local name = luantiDef.name luantiDef.name = nil core.register_node(':'..name, luantiDef) end ---@param luantiDef ItemDefinition local function registerFlatItem(luantiDef) local name = luantiDef.name luantiDef.name = nil core.register_craftitem(':'..name, luantiDef) end ---The alias `table.merge` for EmmyLua ---@type fun(...):(Item.ItemDefinition) local defMerge = function(...) return tableMerge(...) 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.') -- Adding default parameters itemDef = defMerge(itemDef, self.defaultDef, true) local s = itemDef.settings if s._add_mod_name then s.name = Mod.getInfo().shortName .. ':' .. s.name end ---@type Item local instance = getInstance(self, {}) local luantiDef = itemDefToLuantiDef(itemDef) local visual = itemDef.settings._visual if visual == nil then visual = 'item_2d' end itemDef.settings._visual = nil if visual == 'item_3d' then register3dItem(luantiDef) else visual = 'item_2d' registerFlatItem(luantiDef) end function instance:getVisual() return visual end instance.itemDef = itemDef instance.luantiDef = luantiDef 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 = defMerge(self.defaultDef, defaultDef) ---@type Item local ChildClass = extend(self, { 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 = defMerge(self.defaultDef, defaultDef) end ---@type ItemFactory local instance = ItemFactory:new(self, defaultDef) return instance end return Item