Item: add concat description support
This commit is contained in:
@@ -1,5 +1,16 @@
|
|||||||
|
local assert = assert
|
||||||
local tableCopy, tableMerge, colorize = table.copy, table.merge, core.colorize
|
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()
|
local ItemFactory = Smc.requireItemFactory()
|
||||||
|
|
||||||
---Colors the title according to the rarity of the item.
|
---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.
|
---Determines whether common rarity will be shown in item descriptions or not.
|
||||||
local SHOW_COMMON_RARITY = true
|
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.
|
---Colors corresponding to the rarity color in the item description.
|
||||||
---@type table<Item.Rarity, ColorString>
|
---@type table<Item.Rarity, ColorString>
|
||||||
local RARITY_COLORS = {
|
local RARITY_COLORS = {
|
||||||
@@ -24,19 +38,6 @@ local RARITY_COLORS = {
|
|||||||
['unique'] = '#f4c95a'
|
['unique'] = '#f4c95a'
|
||||||
}
|
}
|
||||||
|
|
||||||
---Whether to show the numeric index in the rarity description.
|
|
||||||
local SHOW_RARITY_INDEX = true
|
|
||||||
|
|
||||||
---@type table<Item.Rarity, integer>
|
|
||||||
local RARITY_INDEX = {
|
|
||||||
['common'] = 1,
|
|
||||||
['uncommon'] = 2,
|
|
||||||
['distinguished'] = 3,
|
|
||||||
['eminent'] = 4,
|
|
||||||
['prime'] = 5,
|
|
||||||
['unique'] = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
---@class Item : Class
|
---@class Item : Class
|
||||||
---@field itemDef Item.ItemDefinition
|
---@field itemDef Item.ItemDefinition
|
||||||
---@field luantiDef 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 rarity Item.Rarity
|
||||||
---@param desc string
|
|
||||||
---@return string
|
---@return string
|
||||||
local function addRarityToDesc(rarity, desc)
|
local function getDescPattern(rarity)
|
||||||
if not SHOW_RARITY then
|
if not SHOW_RARITY then
|
||||||
return desc
|
return DESC_PATTERNS[1]
|
||||||
end
|
end
|
||||||
|
|
||||||
if not SHOW_COMMON_RARITY and rarity == 'common' then
|
if not SHOW_COMMON_RARITY and rarity == 'common' then
|
||||||
return desc
|
return DESC_PATTERNS[1]
|
||||||
end
|
end
|
||||||
|
|
||||||
local S = core.get_translator('item')
|
return DESC_PATTERNS[2]
|
||||||
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
|
end
|
||||||
|
|
||||||
---@param itemDef Item.ItemDefinition
|
---@param itemDef Item.ItemDefinition
|
||||||
---@return ItemDefinition
|
---@return ItemDefinition
|
||||||
local function itemDefToLuantiDef(itemDef)
|
local function itemDefToLuantiDef(itemDef)
|
||||||
local modName = Mod:getInfo().shortName
|
local modName = Mod:getInfo().shortName
|
||||||
local S = core.get_translator(modName)
|
local S = coreGetTranslator(modName)
|
||||||
|
|
||||||
local s = itemDef.settings
|
local s = itemDef.settings
|
||||||
local c = itemDef.callbacks
|
local c = itemDef.callbacks
|
||||||
@@ -95,18 +92,20 @@ local function itemDefToLuantiDef(itemDef)
|
|||||||
local title, description = s._title, s._description
|
local title, description = s._title, s._description
|
||||||
|
|
||||||
if title == nil or title == '' then
|
if title == nil or title == '' then
|
||||||
title = string.match(s.name, '%S:(%S+)')..'-title'
|
title = stringMatch(s.name, '%S:(%S+)')..'-title'
|
||||||
end
|
end
|
||||||
|
|
||||||
if description == nil or description == '' then
|
if description == nil or description == '' then
|
||||||
description = string.match(s.name, '%S:(%S+)')..'-desc'
|
description = stringMatch(s.name, '%S:(%S+)')..'-desc'
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(s._rarity ~= nil, 'The rarity of an item cannot be empty.')
|
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 s._title_color ~= nil and s._description_color ~= nil then
|
||||||
if TITLE_RARITY_COLOR then
|
if TITLE_RARITY_COLOR then
|
||||||
title = colorize(RARITY_COLORS[s._rarity], S(title))
|
title = colorize(rarityColor, S(title))
|
||||||
else
|
else
|
||||||
title = colorize(s._title_color, S(title))
|
title = colorize(s._title_color, S(title))
|
||||||
end
|
end
|
||||||
@@ -115,33 +114,27 @@ local function itemDefToLuantiDef(itemDef)
|
|||||||
error('Item description color parameters cannot be empty.')
|
error('Item description color parameters cannot be empty.')
|
||||||
end
|
end
|
||||||
|
|
||||||
local luantiDescription = addRarityToDesc(s._rarity, title..'\n '..description)
|
local rarityTitle = coreColorize(rarityColor, coreTranslate('item', s._rarity))
|
||||||
|
|
||||||
s._title = title
|
s._title = title
|
||||||
s._description = description
|
s._description = description
|
||||||
s.description = luantiDescription..core.get_background_escape_sequence('#110F13')
|
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]]
|
local luantiDef = tableMerge(s, c, true) --[[@as ItemDefinition]]
|
||||||
|
|
||||||
return luantiDef
|
return luantiDef
|
||||||
end
|
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
|
---@param luantiDef ItemDefinition
|
||||||
local function registerCraftitem(luantiDef)
|
local function registerCraftitem(luantiDef)
|
||||||
local name = luantiDef.name
|
local name = luantiDef.name
|
||||||
---@diagnostic disable-next-line: inject-field
|
---@diagnostic disable-next-line: inject-field
|
||||||
luantiDef.name = nil
|
luantiDef.name = nil
|
||||||
|
|
||||||
core.register_craftitem(':'..name, luantiDef)
|
coreRegisterItem(':'..name, luantiDef)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param luantiDef ItemDefinition
|
---@param luantiDef ItemDefinition
|
||||||
@@ -150,7 +143,16 @@ local function registerTool(luantiDef)
|
|||||||
---@diagnostic disable-next-line: inject-field
|
---@diagnostic disable-next-line: inject-field
|
||||||
luantiDef.name = nil
|
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
|
end
|
||||||
|
|
||||||
---@param itemDef Item.ItemDefinition|Node.NodeDefinition
|
---@param itemDef Item.ItemDefinition|Node.NodeDefinition
|
||||||
@@ -185,6 +187,10 @@ function Item:new(itemDef)
|
|||||||
|
|
||||||
itemDef.settings._type = nil
|
itemDef.settings._type = nil
|
||||||
|
|
||||||
|
function instance:getType()
|
||||||
|
return type
|
||||||
|
end
|
||||||
|
|
||||||
if type == 'node' then
|
if type == 'node' then
|
||||||
registerNode(luantiDef --[[@as NodeDefinition]])
|
registerNode(luantiDef --[[@as NodeDefinition]])
|
||||||
elseif type == 'craftitem' then
|
elseif type == 'craftitem' then
|
||||||
@@ -195,14 +201,10 @@ function Item:new(itemDef)
|
|||||||
error(('Unknown item type: `%s`.'):format(type))
|
error(('Unknown item type: `%s`.'):format(type))
|
||||||
end
|
end
|
||||||
|
|
||||||
function instance:getType()
|
|
||||||
return type
|
|
||||||
end
|
|
||||||
|
|
||||||
instance.itemDef = itemDef
|
instance.itemDef = itemDef
|
||||||
instance.luantiDef = luantiDef
|
instance.luantiDef = luantiDef
|
||||||
|
|
||||||
Smc.addRegisteredItem(shortModName, instance)
|
smcAddRegisteredItem(shortModName, instance)
|
||||||
return instance
|
return instance
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -222,7 +224,8 @@ function Item:getExtended(defaultDef)
|
|||||||
return ChildClass
|
return ChildClass
|
||||||
end
|
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
|
---@param defaultDef? Item.ItemDefinition
|
||||||
---@return ItemFactory
|
---@return ItemFactory
|
||||||
function Item:getFactory(defaultDef)
|
function Item:getFactory(defaultDef)
|
||||||
|
|||||||
@@ -19,6 +19,20 @@ Settings._title = nil
|
|||||||
---@type string?
|
---@type string?
|
||||||
Settings._description = nil
|
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.
|
---Item title color in description.
|
||||||
---
|
---
|
||||||
---Default: `#FFFFFF`
|
---Default: `#FFFFFF`
|
||||||
@@ -35,10 +49,21 @@ Settings._description_color = nil
|
|||||||
---@type Item.Type?
|
---@type Item.Type?
|
||||||
Settings._type = nil
|
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?
|
---@type Item.Rarity?
|
||||||
Settings._rarity = nil
|
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.
|
---The number of inventory slots that can be stored in an ItemStack.
|
||||||
---@type integer?
|
---@type integer?
|
||||||
Settings._itemstack_inventory_size = nil
|
Settings._itemstack_inventory_size = nil
|
||||||
|
|||||||
Reference in New Issue
Block a user