aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Rudin <thomas@rudin.io>2018-12-07 10:08:59 +0100
committerorwell96 <mono96.mml@gmail.com>2018-12-10 17:16:23 +0100
commite7488abf583faee0cd458ef2b3e6a89a101a8acf (patch)
tree8cf1c80b1a9125bdb44879283087765b0ba43986
parentedeb3406ef164f37829d231803460d154a73dee9 (diff)
downloadposhud-e7488abf583faee0cd458ef2b3e6a89a101a8acf.zip
poshud-e7488abf583faee0cd458ef2b3e6a89a101a8acf.tar.gz
poshud-e7488abf583faee0cd458ef2b3e6a89a101a8acf.tar.bz2
allow toggling of hud with /poshud [pn|off]
disables display of hud by default
-rw-r--r--init.lua56
1 files changed, 52 insertions, 4 deletions
diff --git a/init.lua b/init.lua
index 4b2dddc..6d64c7c 100644
--- a/init.lua
+++ b/init.lua
@@ -23,10 +23,21 @@ enable_star = true
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
+
+-- hud id map (playername -> hud-id)
local player_hud = {}
+-- hud enabled map (playername -> bool)
+local player_hud_enabled = {}
+
local function generatehud(player)
local name = player:get_player_name()
+
+ if player_hud[name] then
+ -- already set up
+ return
+ end
+
local hud = {}
hud.id = player:hud_add({
hud_elem_type = "text",
@@ -40,8 +51,15 @@ local function generatehud(player)
})
player_hud[name] = hud
end
+
local function updatehud(player, text)
local name = player:get_player_name()
+
+ if not player_hud_enabled[name] then
+ -- check if the player enabled the hud
+ return
+ end
+
if not player_hud[name] then
generatehud(player)
end
@@ -51,19 +69,49 @@ local function updatehud(player, text)
hud.text = text
end
end
+
local function removehud(player)
local name = player:get_player_name()
if player_hud[name] then
player:hud_remove(player_hud[name].id)
+ player_hud[name] = nil
end
end
-minetest.register_on_joinplayer(function(player)
- minetest.after(0,generatehud,player)
-end)
+
minetest.register_on_leaveplayer(function(player)
- minetest.after(1,removehud,player)
+ minetest.after(1,removehud,player)
end)
+
+--------------------------------------------------------------------------------
+--------------------------------------------------------------------------------
+-- hud enabled/disable
+
+
+minetest.register_chatcommand("poshud", {
+ params = "on|off",
+ description = "Turn poshud on or off",
+ func = function(name, param)
+ local player = minetest.get_player_by_name(name)
+
+ if param == "on" then
+ player_hud_enabled[name] = true
+ generatehud(player)
+
+ elseif param == "off" then
+ player_hud_enabled[name] = false
+ removehud(player)
+
+ else
+ return true, "Usage: poshud [on|off]"
+
+ end
+ end
+})
+
+
+--------------------------------------------------------------------------------
+--------------------------------------------------------------------------------
-- time
-- from https://gitlab.com/Rochambeau/mthudclock/blob/master/init.lua