Horrible Workaround for messages sometimes failing to be moved with hud_changed
[minetest_central_message.git] / init.lua
blob5c39567c2b5921b46b59b602e4dfd609afed9b5e
1 cmsg = {}
2 cmsg.hudids = {}
3 cmsg.active_messages = {}
5 cmsg.default_color = 0xFFFFFF
7 cmsg.push_message_player = function(player, text, color)
8 local function push(tbl)
9 -- Horrible Workaround code starts here
10 if not (cmsg.last_push < cmsg.steps) then
11 minetest.after(0, push, tbl)
12 return
13 end
15 local player = tbl.player
16 local text = tbl.text
17 local color = tbl.color
18 -- Horrible Workaround code ends here
20 local pname = player:get_player_name()
21 if color == nil then color = cmsg.default_color end
22 if cmsg.hudids[pname] == nil then
23 cmsg.hudids[pname] = {}
24 cmsg.active_messages[pname] = 0
25 else
26 -- move older HUD IDs up
27 for hudid,tbl in pairs(cmsg.hudids[pname]) do
28 tbl.stackpos = tbl.stackpos + 1
29 player:hud_change(hudid, "offset", {x=0,y=-128-(18*tbl.stackpos)})
30 end
31 end
32 local hudid = player:hud_add({
33 hud_elem_type = "text",
34 text = text,
35 number = color,
36 position = {x=0.5, y=0.5},
37 offset = {x=0,y=-128},
38 direction = 0,
39 alignment = {x=0,y=0},
40 scale = {x=300,y=18},
43 cmsg.hudids[pname][hudid] = {stackpos=0}
44 cmsg.active_messages[pname] = cmsg.active_messages[pname] + 1
46 minetest.after(5, function(param)
47 local pname = param.player:get_player_name()
48 param.player:hud_remove(param.hudid)
49 cmsg.hudids[pname][param.hudid] = nil
50 cmsg.active_messages[pname] = cmsg.active_messages[pname] - 1
51 end, {player=player, hudid = hudid})
53 -- Update timer for Horrible Workaround
54 cmsg.last_push = cmsg.steps
55 end
57 if cmsg.last_push < cmsg.steps then
58 push({player=player, text=text, color=color})
59 else
60 minetest.after(0, push, {player=player, text=text, color=color})
61 end
62 end
64 cmsg.push_message_all = function(text, color)
65 local players = minetest.get_connected_players()
66 for i=1,#players do
67 cmsg.push_message_player(players[i], text, color)
68 end
69 end
71 minetest.register_on_leaveplayer(function(player)
72 cmsg.hudids[player:get_player_name()] = nil
73 end)
75 -- Horrible Workaround code starts here
76 cmsg.steps = 0
77 cmsg.last_push = -1
78 minetest.register_globalstep(function(dtime)
79 cmsg.steps = cmsg.steps + 1
80 end)
81 -- Horrible Workaround code ends here