From d392414cb77a7509e2536d349265c30478e8f272 Mon Sep 17 00:00:00 2001 From: Vanic Date: Tue, 20 Jan 2026 21:42:16 +0300 Subject: [PATCH] Changes in class constructors. close #18 --- mods/CORE/Formspec/src/Formspec.lua | 8 +++++--- mods/CORE/Item/src/Item.lua | 4 +++- mods/CORE/ItemFactory/src/ItemFactory.lua | 6 ++++-- mods/CORE/TileSheet/src/TileSheet.lua | 6 ++++-- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/mods/CORE/Formspec/src/Formspec.lua b/mods/CORE/Formspec/src/Formspec.lua index a785ad4..3bb8750 100644 --- a/mods/CORE/Formspec/src/Formspec.lua +++ b/mods/CORE/Formspec/src/Formspec.lua @@ -1,3 +1,5 @@ +local getInstance = Class.getInstance + ---@class Formspec ---@field params string ---@field elements string @@ -45,7 +47,7 @@ local Formspec = { ---@return Formspec function Formspec:new(def) if def == nil then - local instance = setmetatable({}, {__index = self}) + local instance = getInstance(self, {}) return instance end @@ -98,9 +100,9 @@ function Formspec:new(def) local allowClose = ('allow_close[%s]'):format(def.allowClose or true) ---@type Formspec - local instance = setmetatable({ + local instance = getInstance(self, { params = version..size..position..anchor..padding..noPrepend..realCoordinates..allowClose, - }, {__index = self}) + }) return instance end diff --git a/mods/CORE/Item/src/Item.lua b/mods/CORE/Item/src/Item.lua index e8e5c2c..8058537 100644 --- a/mods/CORE/Item/src/Item.lua +++ b/mods/CORE/Item/src/Item.lua @@ -1,3 +1,5 @@ +local getInstance = Class.getInstance + local modInfo = Mod.getInfo('smc__core__item') local require = modInfo.require @@ -96,7 +98,7 @@ function Item:new(itemDef) itemDef = defMerge(itemDef, self.defaultDef, true) ---@type Item - local instance = setmetatable({}, {__index = self}) + local instance = getInstance(self, {}) local luantiDef = itemDefToLuantiDef(itemDef) local visual = itemDef.settings.visual diff --git a/mods/CORE/ItemFactory/src/ItemFactory.lua b/mods/CORE/ItemFactory/src/ItemFactory.lua index 17c4a3b..57f082a 100644 --- a/mods/CORE/ItemFactory/src/ItemFactory.lua +++ b/mods/CORE/ItemFactory/src/ItemFactory.lua @@ -1,3 +1,5 @@ +local getInstance = Class.getInstance + ---@class ItemFactory ---@field defaultDef Item.ItemDefinition|Node.NodeDefinition ---@field itemClass Item|Node @@ -8,10 +10,10 @@ local ItemFactory = {} ---@return ItemFactory function ItemFactory:new(itemClass, defaultDef) ---@type ItemFactory - local instance = setmetatable({ + local instance =getInstance(self, { defaultDef = defaultDef, itemClass = itemClass, - }, {__index = self}) + }) return instance end diff --git a/mods/CORE/TileSheet/src/TileSheet.lua b/mods/CORE/TileSheet/src/TileSheet.lua index 775378a..56b3b35 100644 --- a/mods/CORE/TileSheet/src/TileSheet.lua +++ b/mods/CORE/TileSheet/src/TileSheet.lua @@ -1,5 +1,7 @@ local mathRound = math.round +local getInstance = Class.getInstance + ---@class TileSheet ---@field image string Name of texture ---@field width number Number of tiles in the table horizontally @@ -70,11 +72,11 @@ function TileSheet:new(image, tileWidth, tileHeight) end ---@type TileSheet - local instance = setmetatable({ + local instance = getInstance(self, { image = image, width = mathRound(width / tileWidth), height = mathRound(height / tileHeight), - }, {__index = self}) + }) return instance end