From 09a929ff578e3bc7d0497b60feda025e57f57585 Mon Sep 17 00:00:00 2001 From: varttist Date: Thu, 19 Nov 2026 21:20:16 +0300 Subject: [PATCH] `Item`: add concat description support --- mods/CORE/Item/src/Item.lua | 109 ++++++++++++++------------- mods/CORE/Item/src/Item/Settings.lua | 27 ++++++- 2 files changed, 82 insertions(+), 54 deletions(-) diff --git a/mods/CORE/Item/src/Item.lua b/mods/CORE/Item/src/Item.lua index 4bfc28c..a432998 100644 --- a/mods/CORE/Item/src/Item.lua +++ b/mods/CORE/Item/src/Item.lua @@ -1,5 +1,16 @@ +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. @@ -13,6 +24,9 @@ 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 = { @@ -24,19 +38,6 @@ local RARITY_COLORS = { ['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 : Class ---@field itemDef Item.ItemDefinition ---@field luantiDef ItemDefinition @@ -58,36 +59,32 @@ local Item = Class:extend({ } }) ----Adds an item's rarity tag to the description. +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 ----@param desc string ---@return string -local function addRarityToDesc(rarity, desc) +local function getDescPattern(rarity) if not SHOW_RARITY then - return desc + return DESC_PATTERNS[1] end if not SHOW_COMMON_RARITY and rarity == 'common' then - return desc + return DESC_PATTERNS[1] 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) + return DESC_PATTERNS[2] end ---@param itemDef Item.ItemDefinition ---@return ItemDefinition local function itemDefToLuantiDef(itemDef) local modName = Mod:getInfo().shortName - local S = core.get_translator(modName) + local S = coreGetTranslator(modName) local s = itemDef.settings local c = itemDef.callbacks @@ -95,18 +92,20 @@ local function itemDefToLuantiDef(itemDef) local title, description = s._title, s._description if title == nil or title == '' then - title = string.match(s.name, '%S:(%S+)')..'-title' + title = stringMatch(s.name, '%S:(%S+)')..'-title' end if description == nil or description == '' then - description = string.match(s.name, '%S:(%S+)')..'-desc' + 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(RARITY_COLORS[s._rarity], S(title)) + title = colorize(rarityColor, S(title)) else title = colorize(s._title_color, S(title)) end @@ -115,33 +114,27 @@ local function itemDefToLuantiDef(itemDef) error('Item description color parameters cannot be empty.') end - local luantiDescription = addRarityToDesc(s._rarity, title..'\n '..description) + local rarityTitle = coreColorize(rarityColor, coreTranslate('item', s._rarity)) - s._title = title - s._description = description - s.description = luantiDescription..core.get_background_escape_sequence('#110F13') + 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 NodeDefinition -local function registerNode(luantiDef) - local name = luantiDef.name - ---@diagnostic disable-next-line: inject-field - luantiDef.name = nil - - core.register_node(':'..name, luantiDef) -end - ---@param luantiDef ItemDefinition local function registerCraftitem(luantiDef) local name = luantiDef.name ---@diagnostic disable-next-line: inject-field luantiDef.name = nil - core.register_craftitem(':'..name, luantiDef) + coreRegisterItem(':'..name, luantiDef) end ---@param luantiDef ItemDefinition @@ -150,7 +143,16 @@ local function registerTool(luantiDef) ---@diagnostic disable-next-line: inject-field luantiDef.name = nil - core.register_tool(':'..name, luantiDef) + 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 @@ -185,6 +187,10 @@ function Item:new(itemDef) itemDef.settings._type = nil + function instance:getType() + return type + end + if type == 'node' then registerNode(luantiDef --[[@as NodeDefinition]]) elseif type == 'craftitem' then @@ -195,14 +201,10 @@ function Item:new(itemDef) error(('Unknown item type: `%s`.'):format(type)) end - function instance:getType() - return type - end - instance.itemDef = itemDef instance.luantiDef = luantiDef - Smc.addRegisteredItem(shortModName, instance) + smcAddRegisteredItem(shortModName, instance) return instance end @@ -222,7 +224,8 @@ function Item:getExtended(defaultDef) return ChildClass end ----Returns an instance of the `ItemFactory` class that can be used to mass-register identical items. +---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) diff --git a/mods/CORE/Item/src/Item/Settings.lua b/mods/CORE/Item/src/Item/Settings.lua index 37f93c8..29af650 100644 --- a/mods/CORE/Item/src/Item/Settings.lua +++ b/mods/CORE/Item/src/Item/Settings.lua @@ -19,6 +19,20 @@ Settings._title = nil ---@type string? Settings._description = nil +---Allows you to define exactly how the item description +---displayed to the player will be put together. +--- +---A default item description will look like this: `'{title}\n{desc}'` +--- +---Keys: +--- * `title` - item name, defined `settings._title` +--- * `desc` - item description, defined `settings._description` +--- * `rarity_title` - human-readable item rarity +--- * `node_groups_desc` - human-readable node groups +--- * `tool_groups_desc` - human-readable tool groups +---@type string? +Settings._description_pattern = nil + ---Item title color in description. --- ---Default: `#FFFFFF` @@ -35,10 +49,21 @@ Settings._description_color = nil ---@type Item.Type? Settings._type = nil ----Formal (lore) rarity of an item. Default `common`. +---The formal (lore-based) rarity of an item. +--- +---This serves as a technical key for registering items and performing comparison checks. +--- +---Defaults to `common`. ---@type Item.Rarity? Settings._rarity = nil +---Formal (lore-based) item rarity. +--- +---This line is generated automatically (you do not need to fill it in) +---and contains a human-readable name for the rarity (including translation and color). +---@type string? +Settings._rarity_title = nil + ---The number of inventory slots that can be stored in an ItemStack. ---@type integer? Settings._itemstack_inventory_size = nil