From 70ac89e095dc0d08bf00d5238c8f48b0fd155159 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Tue, 16 Jun 2015 18:40:54 +0200 Subject: [PATCH] Initial commit --- README.md | 47 +++++++++++++++++++++++++++++++++++++++++++ depends.txt | 0 description.txt | 1 + init.lua | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 README.md create mode 100644 depends.txt create mode 100644 description.txt create mode 100644 init.lua diff --git a/README.md b/README.md new file mode 100644 index 0000000..312e882 --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +# Central Message +## Overview +* Description: Simple API to display short messages at the center of the screen +* Author: Wuzzy +* License of everything: WTFPL +* Shortname: `central_message` +* Version: 0.1.0 (using Semantic Versioning 2.0.0, see [http://semver.org/]) + +## Longer description +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. +When multiple messages are pushed quickly in succession, the messages will be “stacked” +on the screen. + +This mod can be useful to inform about all sorts of events and is an alternative to use the chat log +to display special events. + +Some usage examples: +* Messages about game events, like victory, defeat, next round starting, etc. +* Error message directed to a single player +* Informational messages +* Administational messages to warn players about a coming server shutdown + + +## API +### `cmsg.push_message_player(player, message, color)` +Display a new message to one player only. + +#### Parameters +* `player`: An `ObjectRef` to the player to which to send the message +* `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 +Always `nil`. + + +### `cmsg.push_message_all(message, color)` +Display a new message to all connected players. + +#### Parameters +* `player`: An `ObjectRef` to the player to which to send the message +* `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 +Always `nil`. diff --git a/depends.txt b/depends.txt new file mode 100644 index 0000000..e69de29 diff --git a/description.txt b/description.txt new file mode 100644 index 0000000..4a6c5db --- /dev/null +++ b/description.txt @@ -0,0 +1 @@ +Simple API to show messages to the center of the screen to players. diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..32d0318 --- /dev/null +++ b/init.lua @@ -0,0 +1,53 @@ +cmsg = {} +cmsg.hudids = {} +cmsg.active_messages = {} + +cmsg.default_color = 0xFFFFFF + +cmsg.push_message_player = function(player, text, color) + local pname = player:get_player_name() + if color == nil then color = cmsg.default_color end + if cmsg.hudids[pname] == nil then + cmsg.hudids[pname] = {} + cmsg.active_messages[pname] = 0 + else + -- move older HUD IDs up + 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 + 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 + +cmsg.push_message_all = function(text, color) + local players = minetest.get_connected_players() + for i=1,#players do + cmsg.push_message_player(players[i], text, color) + end +end + +minetest.register_on_leaveplayer(function(player) + cmsg.hudids[player:get_player_name()] = nil +end)