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() local modname = minetest.get_current_modname()
blockgame.events = blockgame.events or {} blockgame.events = blockgame.events or {}
@ -13,28 +12,29 @@ function api.namespace (name)
namespaces[name] = { namespaces[name] = {
listen = function (...) return api.listen(name, ...) end, listen = function (...) return api.listen(name, ...) end,
broadcast = function (...) return api.broadcast(name, ...) end, broadcast = function (...) return api.broadcast(name, ...) end,
listeners = {},
} }
return namespaces[name] return namespaces[name]
end end
function api.listen (namespace_name, event, callback) function api.listen (namespace_name, event, callback)
local namespace = namespaces[namespace_name] local listeners = api.namespace(namespace_name).listeners
if not namespace then return false end listeners[event] = listeners[event] or {}
namespace[event] = namespace[event] or {} table.insert(listeners[event], {
table.insert(namespace[event], {
event = event, event = event,
callback = callback, callback = callback,
}) })
return #namespace[event] return #listeners[event]
end end
function api.broadcast (namespace_name, event, data) function api.broadcast (namespace_name, event, data)
local namespace = namespaces[namespace_name] if not namespaces[namespace_name] then return end
if not namespace then return false 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) listener.callback(data, event)
end end
end end
]]--

View file

@ -6,6 +6,8 @@ rawset(_G, "load_file", load_file)
local blockgame = {} local blockgame = {}
rawset(_G, "blockgame", blockgame) rawset(_G, "blockgame", blockgame)
load_file("event")
load_file("util_table") load_file("util_table")
load_file("util_string") load_file("util_string")
load_file("util_debug") load_file("util_debug")

View file

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