HelperLib: add concatItemDescription function

This commit is contained in:
Van
2026-11-19 21:19:27 +03:00
parent c3defaaa15
commit c90e012ea6
3 changed files with 45 additions and 1 deletions

View File

@@ -0,0 +1,39 @@
local assert = assert
local ItemStack = ItemStack
---A pattern for describing an item, used when the
---item does not have a clearly defined pattern.
local DESC_PATTERN = '{title}\n {desc}'
---Returns the description of the item,
---concatenated according to the pattern specified in `settings._description_pattern`.
---
---If the `meta` parameter is passed, it can retrieve the necessary params from there.
---@param settings Item.Settings|Node.Settings
---@param meta ItemStackMetaRef?
---@param pattern string?
---@return string
function Smc.concatItemDescription(settings, meta, pattern)
-- If the meta is not provided,
-- we create an empty item stack in order to get dummy metadata.
meta = meta or ItemStack():get_meta()
local description = pattern or settings._description_pattern or DESC_PATTERN
local title = meta:get_string('_title')
if title == '' or title == nil then
title = settings._title or settings.description or ''
end
description = description:gsub('{title}', title)
local desc = meta:get_string('_desc')
if desc == '' or desc == nil then
desc = settings._description or settings.description or ''
end
description = description:gsub('{desc}', desc)
local rarityTitle = settings._rarity_title
assert(rarityTitle ~= nil)
description = description:gsub('{rarity_title}', rarityTitle)
return description
end