16 Commits

Author SHA1 Message Date
066fba7a3c Version 0.3.0 2016-11-12 19:18:58 +01:00
47f62069be Fix typo in README 2016-11-12 19:17:12 +01:00
4e1570dd26 Fix crash when player leaves b4 msg disappears 2016-11-12 19:16:33 +01:00
374f494c02 Fix README.md syntax error 2016-11-09 04:49:00 +01:00
6bda349b58 “for a few seconds” 2016-11-04 04:09:40 +01:00
0149e3bb01 Allow to config message duration 2016-11-04 04:06:22 +01:00
b0304e3493 Fix README typos 2016-11-04 03:55:09 +01:00
6cef0b65a6 Add mod.conf 2016-11-04 02:08:57 +01:00
2d62ac5b4e Add support for settingtypes.txt 2016-11-04 02:08:30 +01:00
1a175c31ec Version 0.2.0 2015-07-15 06:38:35 +02:00
8fb08978f8 Regresssion cleanup: Text color as global setting 2015-07-15 06:37:32 +02:00
f31e92572e Version 0.1.1 2015-07-03 23:55:25 +02:00
ae9b1db692 Fix some alignment issues 2015-07-03 22:46:57 +02:00
4d6193d6cc Messages are now displayed in a single HUD element 2015-07-03 21:54:59 +02:00
d557b5d4e4 Limit # of messages shown at once (default 7) 2015-07-01 23:59:49 +02:00
8b773dbd36 Horrible Workaround for messages sometimes failing to be moved with hud_changed 2015-07-01 23:13:31 +02:00
4 changed files with 159 additions and 44 deletions

View File

@ -4,11 +4,11 @@
* Author: Wuzzy * Author: Wuzzy
* License of everything: WTFPL * License of everything: WTFPL
* Shortname: `central_message` * Shortname: `central_message`
* Version: 0.1.0 (using Semantic Versioning 2.0.0, see [SemVer](http://semver.org/)) * Version: 0.3.0 (using Semantic Versioning 2.0.0, see [SemVer](http://semver.org/))
## Longer description ## Longer description
This Minetest mod allows other mods to display a short message at the center of the screen. This Minetest mod allows other mods to display a short message at the center of the screen.
Each message is displayed for 5 seconds, then it is removed. Each message is displayed for a few seconds, then it is removed.
When multiple messages are pushed quickly in succession, the messages will be “stacked” When multiple messages are pushed quickly in succession, the messages will be “stacked”
on the screen. on the screen.
@ -16,32 +16,40 @@ This mod can be useful to inform about all sorts of events and is an alternative
to display special events. to display special events.
Some usage examples: Some usage examples:
* Messages about game events, like victory, defeat, next round starting, etc. * Messages about game events, like victory, defeat, next round starting, etc.
* Error message directed to a single player * Error message directed to a single player
* Informational messages * Informational messages
* Administational messages to warn players about a coming server shutdown * Administational messages to warn players about a coming server shutdown
## Settings
This mod can be configured via `minetest.conf`.
Currently, these settings are recognized:
* `central_message_max`: Limit the number of messages displayed at once, by providing a number. Use `0` for no limit. Default: `7`
* `central_message_time`: How long (in seconds) a message is shown. Default: `5`
* `central_message_color`: Set the message color of all messages. Value must be of format `(R,G,B)`. Default: `(255,255,255)` (white).
## API ## API
### `cmsg.push_message_player(player, message, color)` ### `cmsg.push_message_player(player, message)`
Display a new message to one player only. Display a new message to one player only.
#### Parameters #### Parameters
* `player`: An `ObjectRef` to the player to which to send the message * `player`: An `ObjectRef` to the player to which to send the message
* `message`: A `string` containing the message to be displayed to the player * `message`: A `string` containing the message to be displayed to the player
* `color`: Optional. A `ColorString` for the color of the text. Default: `0xFFFFFF` (white)
#### Return value #### Return value
Always `nil`. Always `nil`.
### `cmsg.push_message_all(message, color)` ### `cmsg.push_message_all(message)`
Display a new message to all connected players. Display a new message to all connected players.
#### Parameters #### Parameters
* `player`: An `ObjectRef` to the player to which to send the message * `player`: An `ObjectRef` to the player to which to send the message
* `message`: A `string` containing the message to be displayed to all players * `message`: A `string` containing the message to be displayed to all players
* `color`: Optional. A `ColorString` for the color of the text. Default: `0xFFFFFF` (white)
#### Return value #### Return value
Always `nil`. Always `nil`.

173
init.lua
View File

@ -1,53 +1,150 @@
cmsg = {} cmsg = {}
cmsg.hudids = {} cmsg.hudids = {}
cmsg.active_messages = {} cmsg.messages = {}
cmsg.settings = {}
cmsg.next_msgids = {}
cmsg.default_color = 0xFFFFFF cmsg.settings.max_messages = 7
local setting = minetest.setting_get("central_message_max")
cmsg.push_message_player = function(player, text, color) if type(tonumber(setting)) == "number" then
local pname = player:get_player_name() if tonumber(setting) == 0 then
if color == nil then color = cmsg.default_color end -- Infinite messages
if cmsg.hudids[pname] == nil then cmsg.settings.max_messages = nil
cmsg.hudids[pname] = {}
cmsg.active_messages[pname] = 0
else else
-- move older HUD IDs up cmsg.settings.max_messages = tonumber(setting)
for hudid,tbl in pairs(cmsg.hudids[pname]) do
minetest.after(0, function()
tbl.stackpos = tbl.stackpos + 1
player:hud_change(hudid, "offset", {x=0,y=-128-(18*tbl.stackpos)})
end)
end
end end
local hudid = player:hud_add({
hud_elem_type = "text",
text = text,
number = color,
position = {x=0.5, y=0.5},
offset = {x=0,y=-128},
direction = 0,
alignment = {x=0,y=0},
scale = {x=300,y=18},
})
cmsg.hudids[pname][hudid] = {stackpos=0}
cmsg.active_messages[pname] = cmsg.active_messages[pname] + 1
minetest.after(5, function(param)
local pname = param.player:get_player_name()
param.player:hud_remove(param.hudid)
cmsg.hudids[pname][param.hudid] = nil
cmsg.active_messages[pname] = cmsg.active_messages[pname] - 1
end, {player=player, hudid = hudid})
end end
cmsg.push_message_all = function(text, color) cmsg.settings.color = 0xFFFFFF
setting = minetest.setting_get("central_message_color")
if setting then
local r, g, b = string.match(setting, "%((%d+),(%d+),(%d+)%)")
r = tonumber(r)
g = tonumber(g)
b = tonumber(b)
if type(r) == "number" and type(g) == "number" and type(b) == "number" and
r >= 0 and r <= 255 and g >= 0 and g <= 255 and b >= 0 and b <= 255 then
cmsg.settings.color = r * 0x10000 + g * 0x100 + b
else
minetest.log("warning", "[central_message] Invalid syntax of central_message_color setting!")
end
end
cmsg.settings.display_time = 5
local setting = minetest.setting_get("central_message_time")
if type(tonumber(setting)) == "number" then
if tonumber(setting) >= 1 then
cmsg.settings.display_time = tonumber(setting)
else
minetest.log("warning", "[central_message] Invalid value for central_message_time! Using default display time of 5 seconds.")
end
end
local function update_display(player, pname)
local messages = {}
local start, stop
stop = #cmsg.messages[pname]
if cmsg.settings.max_messages ~= nil then
local max = math.min(cmsg.settings.max_messages, #cmsg.messages[pname])
if #cmsg.messages[pname] > cmsg.settings.max_messages then
start = stop - max
else
start = 1
end
else
start = 1
end
for i=start, stop do
table.insert(messages, cmsg.messages[pname][i].text)
end
local concat = table.concat(messages, "\n")
player:hud_change(cmsg.hudids[pname], "text", concat)
end
cmsg.push_message_player = function(player, text)
local function push(tbl)
-- Horrible Workaround code starts here
if not (cmsg.last_push < cmsg.steps) then
minetest.after(0, push, tbl)
return
end
local player = tbl.player
local text = tbl.text
-- Horrible Workaround code ends here
if not player then
return
end
local pname = player:get_player_name()
if (not pname) then
return
end
if cmsg.hudids[pname] == nil then
cmsg.hudids[pname] = player:hud_add({
hud_elem_type = "text",
text = text,
number = cmsg.settings.color,
position = {x=0.5, y=0.5},
offset = {x=-0,y=-256},
direction = 3,
alignment = {x=0,y=1},
scale = {x=800,y=20*cmsg.settings.max_messages},
})
cmsg.messages[pname] = {}
cmsg.next_msgids[pname] = 0
table.insert(cmsg.messages[pname], {text=text, msgid=cmsg.next_msgids[pname]})
else
cmsg.next_msgids[pname] = cmsg.next_msgids[pname] + 1
table.insert(cmsg.messages[pname], {text=text, msgid=cmsg.next_msgids[pname]})
update_display(player, pname)
end
minetest.after(cmsg.settings.display_time, function(param)
if not param.player then
return
end
local pname = param.player:get_player_name()
if (not pname) or (not cmsg.messages[pname]) then
return
end
for i=1, #cmsg.messages[pname] do
if param.msgid == cmsg.messages[pname][i].msgid then
table.remove(cmsg.messages[pname], i)
break
end
end
update_display(player, pname)
end, {player=player, msgid=cmsg.next_msgids[pname]})
-- Update timer for Horrible Workaround
cmsg.last_push = cmsg.steps
end
if cmsg.last_push < cmsg.steps then
push({player=player, text=text})
else
minetest.after(0, push, {player=player, text=text})
end
end
cmsg.push_message_all = function(text)
local players = minetest.get_connected_players() local players = minetest.get_connected_players()
for i=1,#players do for i=1,#players do
cmsg.push_message_player(players[i], text, color) cmsg.push_message_player(players[i], text)
end end
end end
minetest.register_on_leaveplayer(function(player) minetest.register_on_leaveplayer(function(player)
cmsg.hudids[player:get_player_name()] = nil cmsg.hudids[player:get_player_name()] = nil
end) end)
-- Horrible Workaround code starts here
cmsg.steps = 0
cmsg.last_push = -1
minetest.register_globalstep(function(dtime)
cmsg.steps = cmsg.steps + 1
end)
-- Horrible Workaround code ends here

1
mod.conf Normal file
View File

@ -0,0 +1 @@
name = central_message

9
settingtypes.txt Normal file
View File

@ -0,0 +1,9 @@
#Maximum number of messages in the center displayed at once.
#Use 0 for no limit.
central_message_max (Maximum number of central messages) int 7 0
#How long (in seconds) a message in the center is shown.
central_message_time (Central message duration) int 5 1
#Message color of all messages shown in the center (R,G,B).
central_message_color (Central message color) string (255,255,255)