Item: add param addModName

This commit is contained in:
Van
2026-01-05 15:44:53 +03:00
parent 6236a12ca9
commit a08934c6e4
6 changed files with 26 additions and 27 deletions

View File

@@ -15,7 +15,8 @@ local Item = {
title = '',
description = '',
titleColor = '#FFFFFF',
descriptionColor = '#C0C0C0'
descriptionColor = '#C0C0C0',
addModName = true,
},
callbacks = {}
}
@@ -28,21 +29,16 @@ local defMerge = function(...)
end
---@param itemDef Item.ItemDefinition
---@param addModName boolean? Whether to add the mod name to the settings.name. Default: `true`
---@return NodeDefinition|ItemDefinition
local function itemDefToLuantiDef(itemDef, addModName)
local function itemDefToLuantiDef(itemDef)
local modName = Mod.getInfo().shortName
local S = core.get_translator(modName)
if addModName == nil then
addModName = true
end
itemDef = table.copy(itemDef)
local s = itemDef.settings
local c = itemDef.callbacks
if addModName then
if s.addModName then
s.name = modName .. ':' .. s.name
end
@@ -57,8 +53,12 @@ local function itemDefToLuantiDef(itemDef, addModName)
description = string.match(s.name, '%S:(%S+)')..'-desc'
end
---@diagnostic disable-next-line: param-type-not-match
local description = core.colorize(s.titleColor ,S(title))..'\n'..core.colorize(s.descriptionColor, S(description))
if s.titleColor ~= nil and s.descriptionColor ~= nil then
description = core.colorize(s.titleColor ,S(title))..'\n'..
core.colorize(s.descriptionColor, S(description))
else
error('Item description color parameters cannot be empty.')
end
s.title, s.description = nil, description
@@ -90,16 +90,15 @@ local defMerge = function(...)
end
---@param itemDef Item.ItemDefinition|Node.NodeDefinition
---@param addModName boolean? Whether to add the mod name to the settings.name. Default: `true`
---@return Item
function Item:new(itemDef, addModName)
function Item:new(itemDef)
---Adding default parameters
itemDef = defMerge(itemDef, self.defaultDef, true)
---@type Item
local instance = setmetatable({}, {__index = self})
local luantiDef = itemDefToLuantiDef(itemDef, addModName)
local luantiDef = itemDefToLuantiDef(itemDef)
local visual = itemDef.settings.visual
if visual == nil then

View File

@@ -3,7 +3,11 @@ local Settings = {}
---Technical name of the item. Must be unique and have the format `mod_name:item_name`
---@type string
Settings.name = ""
Settings.name = ''
---Whether to add the mod name to the settings.name. Default: `true`
---@type boolean?
Settings.addModName = nil
---Item description. 1st line of item description.
---@type string?

View File

@@ -23,13 +23,12 @@ local defMerge = function(...)
end
---@param itemDefs Item.ItemDefinition[]|Node.NodeDefinition[] List of nodes with parameters unique to them.
---@param addModName boolean? Whether to add the mod name to the settings.name. Default: `true`
function ItemFactory:registerItems(itemDefs, addModName)
function ItemFactory:registerItems(itemDefs)
---@param nodeDef Item.ItemDefinition | Node.NodeDefinition
for _, itemDef in ipairs(itemDefs) do
itemDef = defMerge(itemDef, self.defaultDef, true)
self.itemClass:new(itemDef, addModName)
self.itemClass:new(itemDef)
end
end
@@ -41,8 +40,7 @@ local defMerge = function(...)
end
---@param shortNodeDefs table Table of the form `{ {name, title, description, tiles}, {name, title, description, tiles}, ...}`
---@param addModName boolean? Whether to add the mod name to the settings.name. Default: `true`
function ItemFactory:registerNodesByShortDef(shortNodeDefs, addModName)
function ItemFactory:registerNodesByShortDef(shortNodeDefs)
for _, shortNodeDef in ipairs(shortNodeDefs) do
---@type Node.NodeDefinition
local nodeDef = {
@@ -56,8 +54,7 @@ function ItemFactory:registerNodesByShortDef(shortNodeDefs, addModName)
nodeDef = defMerge(nodeDef, self.defaultDef, true)
self.itemClass:new(nodeDef, addModName)
self.itemClass:new(nodeDef)
end
end

View File

@@ -17,17 +17,15 @@ local defMerge = function(...)
end
---@param nodeDef Node.NodeDefinition
---@param addModName boolean? Whether to add the mod name to the settings.name. Default: `true`
---@return Node
function Node:new(nodeDef, addModName)
function Node:new(nodeDef)
---Adding default parameters
nodeDef = defMerge(nodeDef, self.defaultDef, true)
nodeDef.settings.visual = 'item_3d'
---@type Node
local instance = Item:new(nodeDef, addModName)
local instance = Item:new(nodeDef)
return instance
end