From a0dce51af62e41ddda312b7e877f6a350667a17d Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Sat, 7 Dec 2013 22:03:07 -0500 Subject: Move script_run_callbacks to Lua --- builtin/misc_register.lua | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'builtin/misc_register.lua') diff --git a/builtin/misc_register.lua b/builtin/misc_register.lua index 54022a361..249d272eb 100644 --- a/builtin/misc_register.lua +++ b/builtin/misc_register.lua @@ -314,6 +314,45 @@ minetest.register_item(":", { groups = {not_in_creative_inventory=1}, }) + +function minetest.run_callbacks(callbacks, mode, ...) + assert(type(callbacks) == "table") + local cb_len = #callbacks + if cb_len == 0 then + if mode == 2 or mode == 3 then + return true + elseif mode == 4 or mode == 5 then + return false + end + end + local ret = nil + for i = 1, cb_len do + local cb_ret = callbacks[i](...) + + if mode == 0 and i == 1 then + ret = cb_ret + elseif mode == 1 and i == cb_len then + ret = cb_ret + elseif mode == 2 then + if not cb_ret or i == 1 then + ret = cb_ret + end + elseif mode == 3 then + if cb_ret then + return cb_ret + end + ret = cb_ret + elseif mode == 4 then + if (cb_ret and not ret) or i == 1 then + ret = cb_ret + end + elseif mode == 5 and cb_ret then + return cb_ret + end + end + return ret +end + -- -- Callback registration -- -- cgit v1.2.3