diff --git a/mods/bg_api_event/init.lua b/mods/bg_api/event.lua similarity index 61% rename from mods/bg_api_event/init.lua rename to mods/bg_api/event.lua index 16cc166..1ed1019 100644 --- a/mods/bg_api_event/init.lua +++ b/mods/bg_api/event.lua @@ -1,4 +1,3 @@ ---[[ local modname = minetest.get_current_modname() blockgame.events = blockgame.events or {} @@ -13,28 +12,29 @@ function api.namespace (name) namespaces[name] = { listen = function (...) return api.listen(name, ...) end, broadcast = function (...) return api.broadcast(name, ...) end, + listeners = {}, } return namespaces[name] end function api.listen (namespace_name, event, callback) - local namespace = namespaces[namespace_name] - if not namespace then return false end + local listeners = api.namespace(namespace_name).listeners + listeners[event] = listeners[event] or {} - namespace[event] = namespace[event] or {} - table.insert(namespace[event], { + table.insert(listeners[event], { event = event, callback = callback, }) - return #namespace[event] + return #listeners[event] end function api.broadcast (namespace_name, event, data) - local namespace = namespaces[namespace_name] - if not namespace then return false end + if not namespaces[namespace_name] then return end - for _, listener in pairs(namespace[event]) do + local listeners = api.namespace(namespace_name).listeners + if not listeners[event] then return end + + for _, listener in pairs(listeners[event]) do listener.callback(data, event) end end -]]-- diff --git a/mods/bg_api/init.lua b/mods/bg_api/init.lua index 2c81af5..ed149f3 100644 --- a/mods/bg_api/init.lua +++ b/mods/bg_api/init.lua @@ -6,6 +6,8 @@ rawset(_G, "load_file", load_file) local blockgame = {} rawset(_G, "blockgame", blockgame) +load_file("event") + load_file("util_table") load_file("util_string") load_file("util_debug") diff --git a/mods/bg_api_event/mod.conf b/mods/bg_api_event/mod.conf deleted file mode 100644 index 2b29dcf..0000000 --- a/mods/bg_api_event/mod.conf +++ /dev/null @@ -1,3 +0,0 @@ -name = bg_api_event -description = events api for blockgame. -depends = bg_api