filter: use metatables for filter definition interface

This commit is contained in:
Alexander Weber 2017-04-15 16:18:55 +02:00
parent 586dbf52dd
commit 4b062deb08

View File

@ -1,29 +1,22 @@
local txt = smart_inventory.txt local txt = smart_inventory.txt
local txt_usage = minetest.setting_get("smart_inventory_friendly_group_names") or false local txt_usage = minetest.setting_get("smart_inventory_friendly_group_names") or false
local filter = {} --------------------------------------------------------------
filter.registered_filter = {} -- Filter class
--------------------------------------------------------------
function filter.get(name) local filter_class = {}
return filter.registered_filter[name] filter_class.__index = filter_class
function filter_class:check_item_by_name(itemname)
if minetest.registered_items[itemname] then
return self.filter_func(minetest.registered_items[itemname])
end
end end
function filter.register_filter(def) function filter_class:check_item_by_def(def)
assert(def.name, "filter needs a name")
assert(def.filter_func, "filter function required")
assert(not filter.registered_filter[def.name], "filter already exists")
local self = def
function self:check_item_by_name(itemname)
if minetest.registered_items[itemname] then
return self.filteget_group_descriptionr_func(minetest.registered_items[itemname])
end
end
function self:check_item_by_def(def)
return self.filter_func(def) return self.filter_func(def)
end end
function self:get_description(group)
function filter_class:get_description(group)
local ret_desc local ret_desc
if self.shortdesc_func then if self.shortdesc_func then
ret_desc = self:shortdesc_func(group) ret_desc = self:shortdesc_func(group)
@ -39,9 +32,23 @@ function filter.register_filter(def)
else else
return group.name return group.name
end end
end end
filter.registered_filter[self.name] = self
local filter = {}
filter.registered_filter = {}
function filter.get(name)
return filter.registered_filter[name]
end
function filter.register_filter(def)
assert(def.name, "filter needs a name")
assert(def.filter_func, "filter function required")
assert(not filter.registered_filter[def.name], "filter already exists")
setmetatable(def, filter_class)
def.__index = filter_class
filter.registered_filter[def.name] = def
end end
filter.register_filter({ filter.register_filter({