proper metatables usage

This commit is contained in:
Alexander Weber 2018-09-09 22:19:26 +02:00
parent da2e1cd480
commit d2328eb1b8
3 changed files with 11 additions and 14 deletions

View File

@ -9,7 +9,7 @@ crecipes.crecipes = {} --list of all recipes
-- crecipe: Class
-----------------------------------------------------
local crecipe_class = {}
crecipe_class.__index = crecipe_class
local crecipe_class_mt = {__index = crecipe_class}
crecipes.crecipe_class = crecipe_class
-----------------------------------------------------
@ -285,14 +285,12 @@ end
-- Recipe object Constructor
-----------------------------------------------------
function crecipes.new(recipe)
local def = {}
def.out_item = nil
def._recipe = recipe
def.recipe_type = recipe.type
def._items = {}
setmetatable(def, crecipe_class)
def.__index = crecipe_class
return def
local self = setmetatable({}, crecipe_class_mt)
-- self.out_item = nil
self._recipe = recipe
self.recipe_type = recipe.type
self._items = {}
return self
end
-----------------------------------------------------

View File

@ -4,7 +4,7 @@ local txt = smart_inventory.txt
-- Filter class
--------------------------------------------------------------
local filter_class = {}
filter_class.__index = filter_class
local filter_class_mt = {__index = filter_class}
function filter_class:check_item_by_name(itemname)
if minetest.registered_items[itemname] then
@ -52,8 +52,7 @@ function filter.register_filter(def)
assert(def.name, "filter needs a name")
assert(def.check_item_by_def, "filter function check_item_by_def required")
assert(not filter.registered_filter[def.name], "filter already exists")
setmetatable(def, filter_class)
def.__index = filter_class
setmetatable(def, filter_class_mt)
filter.registered_filter[def.name] = def
end

View File

@ -1,6 +1,6 @@
-- Enhanced main inventory methods
local maininvClass = {}
maininvClass.__index = maininvClass
maininvClass_mt = {__index = maininvClass}
-- Clear the inventory
function maininvClass:remove_all()
@ -144,7 +144,7 @@ end
-- player inventory class
local maininv = {}
function maininv.get(playername)
local self = setmetatable({}, maininvClass)
local self = setmetatable({}, maininvClass_mt)
self.playername = playername
self.inventory = minetest.get_player_by_name(playername):get_inventory()
self.inventory:set_width("craft", 3)