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
12 cmsg
.settings
.max_messages
= nil
14 cmsg
.settings
.max_messages
= tonumber(setting
)
18 cmsg
.settings
.color
= 0xFFFFFF
19 setting
= minetest
.setting_get("central_message_color")
21 local r
, g
, b
= string.match(setting
, "%((%d+),(%d+),(%d+)%)")
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
29 minetest
.log("warning", "[central_message] Invalid syntax of central_message_color setting!")
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
)
39 minetest
.log("warning", "[central_message] Invalid value for central_message_time! Using default display time of 5 seconds.")
45 local function update_display(player
, pname
)
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
60 table.insert(messages
, cmsg
.messages
[pname
][i
].text
)
62 local concat
= table.concat(messages
, "\n")
63 player
:hud_change(cmsg
.hudids
[pname
], "text", concat
)
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
)
74 local player
= tbl
.player
76 -- Horrible Workaround code ends here
81 local pname
= player
:get_player_name()
85 if cmsg
.hudids
[pname
] == nil then
86 cmsg
.hudids
[pname
] = player
:hud_add({
87 hud_elem_type
= "text",
89 number = cmsg
.settings
.color
,
90 position
= {x
=0.5, y
=0.5},
91 offset
= {x
=-0,y
=-256},
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
]})
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
109 local pname
= param
.player
:get_player_name()
110 if (not pname
) or (not cmsg
.messages
[pname
]) then
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
)
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
})
129 minetest
.after(0, push
, {player
=player
, text
=text
})
133 cmsg
.push_message_all
= function(text
)
134 local players
= minetest
.get_connected_players()
136 cmsg
.push_message_player(players
[i
], text
)
140 minetest
.register_on_leaveplayer(function(player
)
141 cmsg
.hudids
[player
:get_player_name()] = nil
144 -- Horrible Workaround code starts here
147 minetest
.register_globalstep(function(dtime
)
148 cmsg
.steps
= cmsg
.steps
+ 1
150 -- Horrible Workaround code ends here