integrate `bg_api_event` into `bg_api`.

This commit is contained in:
trans_soup 2023-10-17 20:38:43 +02:00
parent 75e66075d0
commit 09bcf2260b
3 changed files with 12 additions and 13 deletions

View File

@ -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
]]--

View File

@ -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")

View File

@ -1,3 +0,0 @@
name = bg_api_event
description = events api for blockgame.
depends = bg_api