From bcedf3eac231e5a3a45afa8f032ed7d8659c4a95 Mon Sep 17 00:00:00 2001 From: "A. Demant" Date: Wed, 9 Jan 2019 17:24:43 +0100 Subject: [PATCH] countdown timer using xpfw mod --- README.txt | 18 ++++++------------ chat_commands.lua | 11 ++++++++++- config.lua | 2 +- init.lua | 1 + register.lua | 20 ++++++++++++++++++-- 5 files changed, 36 insertions(+), 16 deletions(-) diff --git a/README.txt b/README.txt index 6bf080a..00e0867 100644 --- a/README.txt +++ b/README.txt @@ -1,19 +1,13 @@ -Minetest Game mod: XPFW +Minetest Game mod: timer ========================== See license.txt for license information. -XPFW provide an API for storing values relevant for experience mechanism into player metadata. It also stores several statistic values during playtime: -- Walked distances calculated out of velocity (important: teleporting does not influence this value) -- accumulated distance out of comparing with last known position (important: teleporting is included in this value) -- whole playtime on the server -- amount of dug nodes -- amount of build nodes -- amount of crafted items -- amount of occured deahts -- amount of text messages -- amount of logins +This mod provides a simple countdown timer for each player. It uses xpfw for storing value, so it is also visible in the xpfw hud. -For several amounts also a kind of speed is calculated, like walking speed or crafting speed. +chat command: + +/cd set countdown with seconds. if not given, start with default value +/cd r/rest give remainign countdown time Authors of source code ---------------------- diff --git a/chat_commands.lua b/chat_commands.lua index d95c6b9..25d634d 100644 --- a/chat_commands.lua +++ b/chat_commands.lua @@ -8,9 +8,18 @@ minetest.register_chatcommand("cd", { local xp= string.split(param, " ") if #xp == 1 then countdown=tonumber(xp[1]) + print(countdown,xp[1]) + if countdown == nil then + if (xp[1] == "rest") or (xp[1] == "r") then + minetest.chat_send_player(name,"countdown remaining: "..math.floor(100*xpfw.player_get_attribute(minetest.get_player_by_name(name),timer.prefix))/100) + end + end elseif #xp >1 then countdown=tonumber(xp[2]) end - timer.player[name]=countdown + if countdown ~=nil then + xpfw.player_set_attribute(minetest.get_player_by_name(name),timer.prefix,countdown) + timer.player[name]=countdown + end end }) diff --git a/config.lua b/config.lua index adc3ff3..02a2bf7 100644 --- a/config.lua +++ b/config.lua @@ -5,4 +5,4 @@ timer.prefix="timer:countdown" timer.player={} timer.dtime=0 ---xpfw.register_attribute(timer.prefix,{min=0,max=math.huge,default=timer.countdown_default,hud=1}) +xpfw.register_attribute(timer.prefix,{min=0,max=math.huge,default=0,hud=1}) diff --git a/init.lua b/init.lua index 2f2bdfa..4701002 100644 --- a/init.lua +++ b/init.lua @@ -12,5 +12,6 @@ basic_functions.import_settingtype(timer.path .. "/settingtypes.txt") dofile(timer.path .. "/config.lua") -- API dofile(timer.path .. "/chat_commands.lua") +dofile(timer.path .. "/register.lua") minetest.log("action", "[MOD]"..minetest.get_current_modname().." -- loaded ") diff --git a/register.lua b/register.lua index 612030a..38188ec 100644 --- a/register.lua +++ b/register.lua @@ -1,7 +1,7 @@ local M=timer minetest.register_on_joinplayer(function(player) - xpfw.player_set_attribute(player,timer.prefix,0) +-- xpfw.player_set_attribute(player,timer.prefix,0) end ) @@ -15,8 +15,24 @@ end) minetest.register_globalstep(function(dtime) timer.dtime=timer.dtime+dtime if timer.dtime>timer.precision then + local ttime=timer.dtime timer.dtime=0 + local players = minetest.get_connected_players() + if #players ~= nil then + if #players > 0 then + for i=1, #players do + local player=players[i] + local name = player:get_player_name() + if xpfw.player_get_attribute(player,timer.prefix)>0 then + xpfw.player_sub_attribute(player,timer.prefix,ttime) + if xpfw.player_get_attribute(player,timer.prefix)<=0 then + minetest.chat_send_player(name,"Countdown finished") + timer.player[name]=nil + end + end + end + end + end end --- print("xpfw_abm: "..1000*(os.clock()-starttime)) end)