From 8ec2da920c722e27361125d52cab996c6d85e1f3 Mon Sep 17 00:00:00 2001 From: sys4-fr Date: Wed, 5 Sep 2018 00:56:02 +0200 Subject: [PATCH] =?UTF-8?q?Remplissage=20du=20d=C3=A9p=C3=B4t.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- init.lua | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100755 init.lua diff --git a/init.lua b/init.lua new file mode 100755 index 0000000..7268e3e --- /dev/null +++ b/init.lua @@ -0,0 +1,79 @@ +------------------------------- +-- Action timers +-- Mod handling action timers +-- + +action_timers = {} +action_timers.timers = {} +action_timers.limits = {} + +action_timers.api = {} + + +function action_timers.api.register_timer(name, limit) + if action_timers.timers[name] then + minetest.log("error", "[ACTimers] Cannot register timer " .. name .. " a second time") + return + elseif not limit then + minetest.log("error", "[ACTimers] Cannot register timer " .. name .. " without limit") + return + end + + action_timers.timers[name] = limit + action_timers.limits[name] = limit + minetest.log("action", "[ACTimers] Timer " .. name .. " registered with time limit " .. limit) +end + +function action_timers.api.do_action(name, func, params) + if not action_timers.timers[name] then + minetest.log("error", "[ACTimers] Timer " .. name .. " doesn't exist") + return + elseif not func then + minetest.log("error", "[ACTimers] Function passed to time checker for " .. name .. " is nil") + return + elseif action_timers.timers[name] > 0 then + minetest.log("error", "[ACTimers] Timer " .. name .. " is still up to 0") + return action_timers.timers[name] + end + + action_timers.timers[name] = action_timers.limits[name] + if not params then + return func() + else + return func(unpack(params)) + end +end + +function action_timers.api.get_timer(name) + return action_timers.timers[name] +end + +local function step() + for name, _ in pairs(action_timers.timers) do + if (action_timers.timers[name] > 0) then + action_timers.timers[name] = action_timers.timers[name] - 1 + end + end + minetest.after(1, step) +end +minetest.after(0, step) + +minetest.log("action", "[ACTimers] Loaded") + + +function action_timers.wrapper(pname, name, timername, value, func, params) + if not action_timers.api.get_timer(timername) then + action_timers.api.register_timer(timername, value) + return func(unpack(params)) + else + local res = action_timers.api.do_action(timername, func, params) + if tonumber(res) then + minetest.chat_send_player(pname, "Please retry later, you used " .. name .. " last time less than " .. value .. " seconds ago.") + minetest.chat_send_player(pname, "Retry in: " .. math.floor(res) .. " seconds.") + minetest.log("action", "[action_timers] Player " .. pname .. " tried to use'" .. name .. "' within forbidden interval.") + return false + elseif res == true then + return res + end + end +end