Tool: create tool class
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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?
|
||||
|
||||
1
mods/CORE/Item/src/Item/Type.lua
Normal file
1
mods/CORE/Item/src/Item/Type.lua
Normal file
@@ -0,0 +1 @@
|
||||
---@alias Item.Type 'craftitem'|'node'|'tool'
|
||||
@@ -1 +0,0 @@
|
||||
---@alias Item.Visual 'item_2d'|'item_3d'
|
||||
@@ -16,7 +16,7 @@ function Node:new(nodeDef)
|
||||
---Adding default parameters
|
||||
nodeDef = defMerge(nodeDef, self.defaultDef, true)
|
||||
|
||||
nodeDef.settings._visual = 'item_3d'
|
||||
nodeDef.settings._type = 'node'
|
||||
|
||||
---@type Node
|
||||
local instance = Item:new(nodeDef)
|
||||
|
||||
4
mods/CORE/Tool/init.lua
Normal file
4
mods/CORE/Tool/init.lua
Normal 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
2
mods/CORE/Tool/mod.conf
Normal file
@@ -0,0 +1,2 @@
|
||||
name = smc__core__tool
|
||||
depends = smc__core__item
|
||||
70
mods/CORE/Tool/src/Tool.lua
Normal file
70
mods/CORE/Tool/src/Tool.lua
Normal 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
|
||||
@@ -64,7 +64,10 @@ core.register_on_player_receive_fields(function(player, formname, fields)
|
||||
return
|
||||
end
|
||||
|
||||
core.log(tostring(stack))
|
||||
---@type ItemStack
|
||||
stack = ItemStack(stack)
|
||||
|
||||
core.log(dump(stack:get_definition()))
|
||||
|
||||
---@type InvRef
|
||||
local inv = player:get_inventory()
|
||||
|
||||
Reference in New Issue
Block a user