local getInstance, extend = Class.getInstance, Class.extend local tableCopy, tableMerge, colorize = table.copy, table.merge, core.colorize 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 ---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 getType fun():string ---@field defaultDef Item.ItemDefinition local Item = { defaultDef = { settings = { name = '', _title = '', _description = '', _title_color = '#FFFFFF', _description_color = '#838383', _rarity = 'common', _add_mod_name = true, stack_max = 128, }, 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_RARITY then return desc end 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) local s = itemDef.settings local c = itemDef.callbacks local title, description = s._title, 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 assert(s._rarity ~= nil, 'The rarity of an item cannot be empty.') if s._title_color ~= nil and s._description_color ~= nil then if TITLE_RARITY_COLOR then title = colorize(RARITY_COLORS[s._rarity], 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 luantiDescription = addRarityToDesc(s._rarity, title..'\n '..description) s._title = title s._description = description s.description = luantiDescription..core.get_background_escape_sequence('#110F13') local luantiDef = defMerge(s, c, true) return luantiDef end ---@param luantiDef NodeDefinition local function registerNode(luantiDef) local name = luantiDef.name luantiDef.name = nil core.register_node(':'..name, luantiDef) end ---@param luantiDef ItemDefinition local function registerCraftitem(luantiDef) local name = luantiDef.name luantiDef.name = nil core.register_craftitem(':'..name, luantiDef) end ---@param luantiDef ItemDefinition local function registerTool(luantiDef) local name = luantiDef.name luantiDef.name = nil core.register_tool(':'..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 ---@type Item.ItemDefinition itemDef = tableMerge(itemDef, self.defaultDef, true) 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 = getInstance(self, {}) local luantiDef = itemDefToLuantiDef(itemDef) local type = itemDef.settings._type if type == nil then type = 'craftitem' end itemDef.settings._type = nil if type == 'node' then registerNode(luantiDef) elseif type == 'craftitem' then registerCraftitem(luantiDef) elseif type == 'tool' then registerTool(luantiDef) else error(('Unknown item type: `%s`.'):format(type)) end function instance:getType() return type end instance.itemDef = itemDef instance.luantiDef = luantiDef Smc.addRegisteredItem(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 = 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 = tableMerge(self.defaultDef, defaultDef, true) end ---@type ItemFactory local instance = ItemFactory:new(self, defaultDef) return instance end return Item