blockgame/mods/bg_api_event/init.lua

41 lines
942 B
Lua
Raw Normal View History

--[[
local modname = minetest.get_current_modname()
blockgame.events = blockgame.events or {}
local api = blockgame.events
local namespaces = {}
function api.namespace (name)
if namespaces[name] then return namespaces[name] end
namespaces[name] = {
listen = function (...) return api.listen(name, ...) end,
broadcast = function (...) return api.broadcast(name, ...) end,
}
return namespaces[name]
end
function api.listen (namespace_name, event, callback)
local namespace = namespaces[namespace_name]
if not namespace then return false end
namespace[event] = namespace[event] or {}
table.insert(namespace[event], {
event = event,
callback = callback,
})
return #namespace[event]
end
function api.broadcast (namespace_name, event, data)
local namespace = namespaces[namespace_name]
if not namespace then return false end
for _, listener in pairs(namespace[event]) do
listener.callback(data, event)
end
end
]]--