1 local S
= minetest
.get_translator("central_message")
9 cmsg
.settings
.max_messages
= 7
10 local setting
= minetest
.setting_get("central_message_max")
11 if type(tonumber(setting
)) == "number" then
12 if tonumber(setting
) == 0 then
14 cmsg
.settings
.max_messages
= nil
16 cmsg
.settings
.max_messages
= tonumber(setting
)
20 cmsg
.settings
.color
= 0xFFFFFF
21 setting
= minetest
.setting_get("central_message_color")
23 local r
, g
, b
= string.match(setting
, "%((%d+),(%d+),(%d+)%)")
27 if type(r
) == "number" and type(g
) == "number" and type(b
) == "number" and
28 r
>= 0 and r
<= 255 and g
>= 0 and g
<= 255 and b
>= 0 and b
<= 255 then
29 cmsg
.settings
.color
= r
* 0x10000 + g
* 0x100 + b
31 minetest
.log("warning", "[central_message] Invalid syntax of central_message_color setting!")
35 cmsg
.settings
.display_time
= 5
36 local setting
= minetest
.setting_get("central_message_time")
37 if type(tonumber(setting
)) == "number" then
38 if tonumber(setting
) >= 1 then
39 cmsg
.settings
.display_time
= tonumber(setting
)
41 minetest
.log("warning", "[central_message] Invalid value for central_message_time! Using default display time of 5 seconds.")
47 local function update_display(player
, pname
)
50 stop
= #cmsg
.messages
[pname
]
51 if cmsg
.settings
.max_messages
~= nil then
52 local max = math
.min(cmsg
.settings
.max_messages
, #cmsg
.messages
[pname
])
53 if #cmsg
.messages
[pname
] > cmsg
.settings
.max_messages
then
62 table.insert(messages
, cmsg
.messages
[pname
][i
].text
)
64 local concat
= table.concat(messages
, "\n")
65 player
:hud_change(cmsg
.hudids
[pname
], "text", concat
)
68 cmsg
.push_message_player
= function(player
, text
)
69 local function push(tbl
)
70 -- Horrible Workaround code starts here
71 if not (cmsg
.last_push
< cmsg
.steps
) then
72 minetest
.after(0, push
, tbl
)
76 local player
= tbl
.player
78 -- Horrible Workaround code ends here
83 local pname
= player
:get_player_name()
87 if cmsg
.hudids
[pname
] == nil then
88 cmsg
.hudids
[pname
] = player
:hud_add({
89 hud_elem_type
= "text",
91 number = cmsg
.settings
.color
,
92 position
= {x
=0.5, y
=0.5},
93 offset
= {x
=-0,y
=-256},
95 alignment
= {x
=0,y
=1},
96 scale
= {x
=800,y
=20*cmsg
.settings
.max_messages
},
99 cmsg
.messages
[pname
] = {}
100 cmsg
.next_msgids
[pname
] = 0
101 table.insert(cmsg
.messages
[pname
], {text
=text
, msgid
=cmsg
.next_msgids
[pname
]})
103 cmsg
.next_msgids
[pname
] = cmsg
.next_msgids
[pname
] + 1
104 table.insert(cmsg
.messages
[pname
], {text
=text
, msgid
=cmsg
.next_msgids
[pname
]})
105 update_display(player
, pname
)
108 minetest
.after(cmsg
.settings
.display_time
, function(param
)
109 if not param
.player
then
112 local pname
= param
.player
:get_player_name()
113 if (not pname
) or (not cmsg
.messages
[pname
]) then
116 for i
=1, #cmsg
.messages
[pname
] do
117 if param
.msgid
== cmsg
.messages
[pname
][i
].msgid
then
118 table.remove(cmsg
.messages
[pname
], i
)
122 update_display(player
, pname
)
123 end, {player
=player
, msgid
=cmsg
.next_msgids
[pname
]})
125 -- Update timer for Horrible Workaround
126 cmsg
.last_push
= cmsg
.steps
129 if cmsg
.last_push
< cmsg
.steps
then
130 push({player
=player
, text
=text
})
132 minetest
.after(0, push
, {player
=player
, text
=text
})
136 cmsg
.push_message_all
= function(text
)
137 local players
= minetest
.get_connected_players()
139 cmsg
.push_message_player(players
[i
], text
)
143 minetest
.register_on_leaveplayer(function(player
)
144 cmsg
.hudids
[player
:get_player_name()] = nil
147 minetest
.register_privilege("announce", {
148 description
= S("Can use /cmsg"),
149 give_to_singleplayer
= false,
151 minetest
.register_chatcommand("cmsg", {
152 description
= S("Show message in the center of the screen to player (“*” sends to all players)"),
153 privs
= {announce
= true},
154 params
= S("<player> <text>"),
155 func
= function(name
, params
)
156 local player
= minetest
.get_player_by_name(name
)
157 local targetname
, text
= string.match(params
, "^(%S+)%s(.+)$")
158 if not targetname
then
159 return false, S("Invalid usage, see “/help cmsg”.")
161 if targetname
== "*" then
162 cmsg
.push_message_all(text
)
163 return true, S("Message sent.")
165 local target
= minetest
.get_player_by_name(targetname
)
167 return false, S("The player @1 is not online.", targetname
)
169 cmsg
.push_message_player(target
, text
)
170 return true, S("Message sent.")
175 -- Horrible Workaround code starts here
178 minetest
.register_globalstep(function(dtime
)
179 cmsg
.steps
= cmsg
.steps
+ 1
181 -- Horrible Workaround code ends here