40 lines
989 B
Lua
40 lines
989 B
Lua
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,
|
|
listeners = {},
|
|
}
|
|
return namespaces[name]
|
|
end
|
|
|
|
function api.listen (namespace_name, event, callback)
|
|
local listeners = api.namespace(namespace_name).listeners
|
|
listeners[event] = listeners[event] or {}
|
|
|
|
table.insert(listeners[event], {
|
|
event = event,
|
|
callback = callback,
|
|
})
|
|
return #listeners[event]
|
|
end
|
|
|
|
function api.broadcast (namespace_name, event, data)
|
|
if not namespaces[namespace_name] then return end
|
|
|
|
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
|