Tool: create tool class
This commit is contained in:
@@ -34,7 +34,7 @@ local RARITY_INDEX = {
|
|||||||
---@class Item
|
---@class Item
|
||||||
---@field itemDef Item.ItemDefinition
|
---@field itemDef Item.ItemDefinition
|
||||||
---@field luantiDef ItemDefinition
|
---@field luantiDef ItemDefinition
|
||||||
---@field getVisual fun(): string
|
---@field getType fun():string
|
||||||
---@field defaultDef Item.ItemDefinition
|
---@field defaultDef Item.ItemDefinition
|
||||||
local Item = {
|
local Item = {
|
||||||
defaultDef = {
|
defaultDef = {
|
||||||
@@ -121,7 +121,7 @@ local function itemDefToLuantiDef(itemDef)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---@param luantiDef NodeDefinition
|
---@param luantiDef NodeDefinition
|
||||||
local function register3dItem(luantiDef)
|
local function registerNode(luantiDef)
|
||||||
local name = luantiDef.name
|
local name = luantiDef.name
|
||||||
luantiDef.name = nil
|
luantiDef.name = nil
|
||||||
|
|
||||||
@@ -129,13 +129,21 @@ local function register3dItem(luantiDef)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---@param luantiDef ItemDefinition
|
---@param luantiDef ItemDefinition
|
||||||
local function registerFlatItem(luantiDef)
|
local function registerCraftitem(luantiDef)
|
||||||
local name = luantiDef.name
|
local name = luantiDef.name
|
||||||
luantiDef.name = nil
|
luantiDef.name = nil
|
||||||
|
|
||||||
core.register_craftitem(':'..name, luantiDef)
|
core.register_craftitem(':'..name, luantiDef)
|
||||||
end
|
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
|
---The alias `table.merge` for EmmyLua
|
||||||
---@type fun(...):(Item.ItemDefinition)
|
---@type fun(...):(Item.ItemDefinition)
|
||||||
local defMerge = function(...)
|
local defMerge = function(...)
|
||||||
@@ -162,23 +170,26 @@ function Item:new(itemDef)
|
|||||||
local instance = getInstance(self, {})
|
local instance = getInstance(self, {})
|
||||||
|
|
||||||
local luantiDef = itemDefToLuantiDef(itemDef)
|
local luantiDef = itemDefToLuantiDef(itemDef)
|
||||||
local visual = itemDef.settings._visual
|
local type = itemDef.settings._type
|
||||||
|
|
||||||
if visual == nil then
|
if type == nil then
|
||||||
visual = 'item_2d'
|
type = 'craftitem'
|
||||||
end
|
end
|
||||||
|
|
||||||
itemDef.settings._visual = nil
|
itemDef.settings._type = nil
|
||||||
|
|
||||||
if visual == 'item_3d' then
|
if type == 'node' then
|
||||||
register3dItem(luantiDef)
|
registerNode(luantiDef)
|
||||||
|
elseif type == 'craftitem' then
|
||||||
|
registerCraftitem(luantiDef)
|
||||||
|
elseif type == 'tool' then
|
||||||
|
registerTool(luantiDef)
|
||||||
else
|
else
|
||||||
visual = 'item_2d'
|
error(('Unknown item type: `%s`.'):format(type))
|
||||||
registerFlatItem(luantiDef)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function instance:getVisual()
|
function instance:getType()
|
||||||
return visual
|
return type
|
||||||
end
|
end
|
||||||
|
|
||||||
instance.itemDef = itemDef
|
instance.itemDef = itemDef
|
||||||
|
|||||||
@@ -33,8 +33,8 @@ Settings._title_color = nil
|
|||||||
Settings._description_color = nil
|
Settings._description_color = nil
|
||||||
|
|
||||||
---Visual node type.
|
---Visual node type.
|
||||||
---@type Item.Visual?
|
---@type Item.Type?
|
||||||
Settings._visual = nil
|
Settings._type = nil
|
||||||
|
|
||||||
---Formal (lore) rarity of an item. Default `common`.
|
---Formal (lore) rarity of an item. Default `common`.
|
||||||
---@type Item.Rarity?
|
---@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
|
---Adding default parameters
|
||||||
nodeDef = defMerge(nodeDef, self.defaultDef, true)
|
nodeDef = defMerge(nodeDef, self.defaultDef, true)
|
||||||
|
|
||||||
nodeDef.settings._visual = 'item_3d'
|
nodeDef.settings._type = 'node'
|
||||||
|
|
||||||
---@type Node
|
---@type Node
|
||||||
local instance = Item:new(nodeDef)
|
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
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
core.log(tostring(stack))
|
---@type ItemStack
|
||||||
|
stack = ItemStack(stack)
|
||||||
|
|
||||||
|
core.log(dump(stack:get_definition()))
|
||||||
|
|
||||||
---@type InvRef
|
---@type InvRef
|
||||||
local inv = player:get_inventory()
|
local inv = player:get_inventory()
|
||||||
|
|||||||
@@ -16,6 +16,14 @@ function Smc.requireNode()
|
|||||||
return class
|
return class
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@return Tool
|
||||||
|
function Smc.requireTool()
|
||||||
|
---@type Tool
|
||||||
|
local class = Mod.getInfo('smc__core__tool').require('Tool')
|
||||||
|
|
||||||
|
return class
|
||||||
|
end
|
||||||
|
|
||||||
---@return Plant
|
---@return Plant
|
||||||
function Smc.requirePlant()
|
function Smc.requirePlant()
|
||||||
---@type Plant
|
---@type Plant
|
||||||
|
|||||||
Reference in New Issue
Block a user