Fix crash when player leaves b4 msg disappears
[minetest_central_message.git] / init.lua
blob1379dbb712d2f059625bf5065b5f35775dd681c8
1 cmsg = {}
2 cmsg.hudids = {}
3 cmsg.messages = {}
4 cmsg.settings = {}
5 cmsg.next_msgids = {}
7 cmsg.settings.max_messages = 7
8 local setting = minetest.setting_get("central_message_max")
9 if type(tonumber(setting)) == "number" then
10 if tonumber(setting) == 0 then
11 -- Infinite messages
12 cmsg.settings.max_messages = nil
13 else
14 cmsg.settings.max_messages = tonumber(setting)
15 end
16 end
18 cmsg.settings.color = 0xFFFFFF
19 setting = minetest.setting_get("central_message_color")
20 if setting then
21 local r, g, b = string.match(setting, "%((%d+),(%d+),(%d+)%)")
22 r = tonumber(r)
23 g = tonumber(g)
24 b = tonumber(b)
25 if type(r) == "number" and type(g) == "number" and type(b) == "number" and
26 r >= 0 and r <= 255 and g >= 0 and g <= 255 and b >= 0 and b <= 255 then
27 cmsg.settings.color = r * 0x10000 + g * 0x100 + b
28 else
29 minetest.log("warning", "[central_message] Invalid syntax of central_message_color setting!")
30 end
31 end
33 cmsg.settings.display_time = 5
34 local setting = minetest.setting_get("central_message_time")
35 if type(tonumber(setting)) == "number" then
36 if tonumber(setting) >= 1 then
37 cmsg.settings.display_time = tonumber(setting)
38 else
39 minetest.log("warning", "[central_message] Invalid value for central_message_time! Using default display time of 5 seconds.")
40 end
41 end
45 local function update_display(player, pname)
46 local messages = {}
47 local start, stop
48 stop = #cmsg.messages[pname]
49 if cmsg.settings.max_messages ~= nil then
50 local max = math.min(cmsg.settings.max_messages, #cmsg.messages[pname])
51 if #cmsg.messages[pname] > cmsg.settings.max_messages then
52 start = stop - max
53 else
54 start = 1
55 end
56 else
57 start = 1
58 end
59 for i=start, stop do
60 table.insert(messages, cmsg.messages[pname][i].text)
61 end
62 local concat = table.concat(messages, "\n")
63 player:hud_change(cmsg.hudids[pname], "text", concat)
64 end
66 cmsg.push_message_player = function(player, text)
67 local function push(tbl)
68 -- Horrible Workaround code starts here
69 if not (cmsg.last_push < cmsg.steps) then
70 minetest.after(0, push, tbl)
71 return
72 end
74 local player = tbl.player
75 local text = tbl.text
76 -- Horrible Workaround code ends here
78 if not player then
79 return
80 end
81 local pname = player:get_player_name()
82 if (not pname) then
83 return
84 end
85 if cmsg.hudids[pname] == nil then
86 cmsg.hudids[pname] = player:hud_add({
87 hud_elem_type = "text",
88 text = text,
89 number = cmsg.settings.color,
90 position = {x=0.5, y=0.5},
91 offset = {x=-0,y=-256},
92 direction = 3,
93 alignment = {x=0,y=1},
94 scale = {x=800,y=20*cmsg.settings.max_messages},
96 cmsg.messages[pname] = {}
97 cmsg.next_msgids[pname] = 0
98 table.insert(cmsg.messages[pname], {text=text, msgid=cmsg.next_msgids[pname]})
99 else
100 cmsg.next_msgids[pname] = cmsg.next_msgids[pname] + 1
101 table.insert(cmsg.messages[pname], {text=text, msgid=cmsg.next_msgids[pname]})
102 update_display(player, pname)
105 minetest.after(cmsg.settings.display_time, function(param)
106 if not param.player then
107 return
109 local pname = param.player:get_player_name()
110 if (not pname) or (not cmsg.messages[pname]) then
111 return
113 for i=1, #cmsg.messages[pname] do
114 if param.msgid == cmsg.messages[pname][i].msgid then
115 table.remove(cmsg.messages[pname], i)
116 break
119 update_display(player, pname)
120 end, {player=player, msgid=cmsg.next_msgids[pname]})
122 -- Update timer for Horrible Workaround
123 cmsg.last_push = cmsg.steps
126 if cmsg.last_push < cmsg.steps then
127 push({player=player, text=text})
128 else
129 minetest.after(0, push, {player=player, text=text})
133 cmsg.push_message_all = function(text)
134 local players = minetest.get_connected_players()
135 for i=1,#players do
136 cmsg.push_message_player(players[i], text)
140 minetest.register_on_leaveplayer(function(player)
141 cmsg.hudids[player:get_player_name()] = nil
142 end)
144 -- Horrible Workaround code starts here
145 cmsg.steps = 0
146 cmsg.last_push = -1
147 minetest.register_globalstep(function(dtime)
148 cmsg.steps = cmsg.steps + 1
149 end)
150 -- Horrible Workaround code ends here