Tool: create tool class

This commit is contained in:
Van
2026-02-27 20:57:54 +03:00
parent 51e6763457
commit c7cfe26335
10 changed files with 116 additions and 18 deletions

View File

@@ -34,7 +34,7 @@ local RARITY_INDEX = {
---@class Item
---@field itemDef Item.ItemDefinition
---@field luantiDef ItemDefinition
---@field getVisual fun(): string
---@field getType fun():string
---@field defaultDef Item.ItemDefinition
local Item = {
defaultDef = {
@@ -121,7 +121,7 @@ local function itemDefToLuantiDef(itemDef)
end
---@param luantiDef NodeDefinition
local function register3dItem(luantiDef)
local function registerNode(luantiDef)
local name = luantiDef.name
luantiDef.name = nil
@@ -129,13 +129,21 @@ local function register3dItem(luantiDef)
end
---@param luantiDef ItemDefinition
local function registerFlatItem(luantiDef)
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
---The alias `table.merge` for EmmyLua
---@type fun(...):(Item.ItemDefinition)
local defMerge = function(...)
@@ -162,23 +170,26 @@ function Item:new(itemDef)
local instance = getInstance(self, {})
local luantiDef = itemDefToLuantiDef(itemDef)
local visual = itemDef.settings._visual
local type = itemDef.settings._type
if visual == nil then
visual = 'item_2d'
if type == nil then
type = 'craftitem'
end
itemDef.settings._visual = nil
itemDef.settings._type = nil
if visual == 'item_3d' then
register3dItem(luantiDef)
if type == 'node' then
registerNode(luantiDef)
elseif type == 'craftitem' then
registerCraftitem(luantiDef)
elseif type == 'tool' then
registerTool(luantiDef)
else
visual = 'item_2d'
registerFlatItem(luantiDef)
error(('Unknown item type: `%s`.'):format(type))
end
function instance:getVisual()
return visual
function instance:getType()
return type
end
instance.itemDef = itemDef

View File

@@ -33,8 +33,8 @@ Settings._title_color = nil
Settings._description_color = nil
---Visual node type.
---@type Item.Visual?
Settings._visual = nil
---@type Item.Type?
Settings._type = nil
---Formal (lore) rarity of an item. Default `common`.
---@type Item.Rarity?

View File

@@ -0,0 +1 @@
---@alias Item.Type 'craftitem'|'node'|'tool'

View File

@@ -1 +0,0 @@
---@alias Item.Visual 'item_2d'|'item_3d'