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

4
mods/CORE/Tool/init.lua Normal file
View File

@@ -0,0 +1,4 @@
local modInfo = Mod.getInfo('smc__core__tool')
local require = modInfo.require
return require('Tool')

2
mods/CORE/Tool/mod.conf Normal file
View File

@@ -0,0 +1,2 @@
name = smc__core__tool
depends = smc__core__item

View File

@@ -0,0 +1,70 @@
local Item, ItemFactory = Smc.requireItem(), Smc.requireItemFactory()
---@class Tool : Item
---@field defaultDef Item.ItemDefinition
local Tool = Class.extend(Item, {})
---The alias `table.merge` for EmmyLua
---@type fun(...):(Item.ItemDefinition)
local defMerge = function(...)
return table.merge(...)
end
---@param ToolDef Item.ItemDefinition
---@return Tool
function Tool:new(ToolDef)
---Adding default parameters
ToolDef = defMerge(ToolDef, self.defaultDef, true)
ToolDef.settings._type = 'tool'
---@type Tool
local instance = Item:new(ToolDef)
return instance
end
---Allows you to create a child class of an tool by specifying
---default parameters that will be added to the tool's parameters
---when an instance is received.
---@param defaultDef Item.ItemDefinition
---@return Tool
function Tool:getExtended(defaultDef)
defaultDef = defMerge(defaultDef, self.defaultDef)
---@type Tool
local ChildClass = Class.extend(self, {
defaultDef = defaultDef,
})
return ChildClass
end
---Returns an instance of the `ItemFactory` class that can be used to mass-register identical items.
---@param defaultDef? Item.ItemDefinition
---@return ItemFactory
function Tool:getFactory(defaultDef)
if defaultDef == nil then
defaultDef = self.defaultDef
else
defaultDef = defMerge(self.defaultDef, defaultDef)
end
---@type ItemFactory
local instance = ItemFactory:new(self, defaultDef)
return instance
end
--[[
Tool:new({
settings = {
name = 'tool',
inventory_image = 'anoba.png',
tool_capabilities = {
groupcaps={
crumbly={maxlevel=2, uses=20, times={[1]=1.60, [2]=1.20, [3]=0.80}}
},
}
}
})--]]
return Tool