filter: use metatables for filter definition interface
This commit is contained in:
parent
586dbf52dd
commit
4b062deb08
@ -1,29 +1,22 @@
|
||||
local txt = smart_inventory.txt
|
||||
local txt_usage = minetest.setting_get("smart_inventory_friendly_group_names") or false
|
||||
|
||||
local filter = {}
|
||||
filter.registered_filter = {}
|
||||
|
||||
function filter.get(name)
|
||||
return filter.registered_filter[name]
|
||||
--------------------------------------------------------------
|
||||
-- Filter class
|
||||
--------------------------------------------------------------
|
||||
local filter_class = {}
|
||||
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
|
||||
|
||||
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")
|
||||
|
||||
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)
|
||||
function filter_class:check_item_by_def(def)
|
||||
return self.filter_func(def)
|
||||
end
|
||||
function self:get_description(group)
|
||||
end
|
||||
|
||||
function filter_class:get_description(group)
|
||||
local ret_desc
|
||||
if self.shortdesc_func then
|
||||
ret_desc = self:shortdesc_func(group)
|
||||
@ -39,9 +32,23 @@ function filter.register_filter(def)
|
||||
else
|
||||
return group.name
|
||||
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
|
||||
|
||||
filter.register_filter({
|
||||
|
Loading…
x
Reference in New Issue
Block a user