commit 8ba3f0db78516988fe58d2273cb41081f8008af6 Author: Zenon Seth Date: Wed Nov 1 02:51:19 2023 +0000 Initial commit with working implementation diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8f0d5ff --- /dev/null +++ b/LICENSE @@ -0,0 +1,49 @@ +License of source code +---------------------- + +GNU Lesser General Public License, version 2.1 +Copyright (C) 2023-2023 Zenon Seth + +This program is free software; you can redistribute it and/or modify it under the terms +of the GNU Lesser General Public License as published by the Free Software Foundation; +either version 2.1 of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; +without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +See the GNU Lesser General Public License for more details: +https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html + + +Licenses of media (textures) +---------------------------- + +Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) +Copyright (C) 2023-2023 Zenon Seth + +You are free to: +Share — copy and redistribute the material in any medium or format. +Adapt — remix, transform, and build upon the material for any purpose, even commercially. +The licensor cannot revoke these freedoms as long as you follow the license terms. + +Under the following terms: + +Attribution — You must give appropriate credit, provide a link to the license, and +indicate if changes were made. You may do so in any reasonable manner, but not in any way +that suggests the licensor endorses you or your use. + +ShareAlike — If you remix, transform, or build upon the material, you must distribute +your contributions under the same license as the original. + +No additional restrictions — You may not apply legal terms or technological measures that +legally restrict others from doing anything the license permits. + +Notices: + +You do not have to comply with the license for elements of the material in the public +domain or where your use is permitted by an applicable exception or limitation. +No warranties are given. The license may not give you all of the permissions necessary +for your intended use. For example, other rights such as publicity, privacy, or moral +rights may limit how you use the material. + +For more details: +http://creativecommons.org/licenses/by-sa/3.0/ diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..31ea8a3 --- /dev/null +++ b/init.lua @@ -0,0 +1,76 @@ +local shown = {} + +local settings = minetest.settings +local MSG_BUBBLE_LIFETIME = tonumber(settings:get("message_bubbles_lifetime")) or 10 +local MAX_MSG_BUBBLE_LENGTH = tonumber(settings:get("message_bubbles_char_limit")) or 40 +local MSG_BUBBLE_PREFIX = settings:get("message_bubbles_prefix") or "Says: " + +local cumulativeTime = 0 + +local function trim(msg) + if not msg or not type(msg) == "string" then return "" end + if string.len(msg) > MAX_MSG_BUBBLE_LENGTH then + return string.sub(msg, 1, MAX_MSG_BUBBLE_LENGTH) + else + return msg + end +end + +minetest.register_on_chat_message(function(name, origMessage) + if minetest.get_player_privs(name).shout then + local player = minetest.get_player_by_name(name) + if not player then return end + local msg = "\n"..MSG_BUBBLE_PREFIX..trim(origMessage) + local nametag = player:get_nametag_attributes() + local nametagText = nametag.text + local hadToAddName = false + if shown[name] then + local currText = shown[name].text + if shown[name].wasEmpty then + shown[name].text = name..msg + shown[name].addedMsg = msg + else + shown[name].text = string.gsub(nametagText, currText, msg) + shown[name].addedMsg = msg + end + shown[name].time = minetest.get_gametime() + else + local addedMsg = msg + if not nametagText or nametagText == "" then + msg = name..msg + hadToAddName = true + end + shown[name] = { + text = msg, + addedMsg = addedMsg, + time = minetest.get_gametime(), + wasEmpty = hadToAddName, + } + end + nametag.text = shown[name].text + player:set_nametag_attributes(nametag) + end +end) + +minetest.register_globalstep(function(dtime) + cumulativeTime = cumulativeTime + dtime + if cumulativeTime > 1 then + cumulativeTime = 0 + local currTime = minetest.get_gametime() + for playerName, info in pairs(shown) do + if currTime - info.time > MSG_BUBBLE_LIFETIME then + local player = minetest.get_player_by_name(playerName) + if player then + local nametag = player:get_nametag_attributes() + if info.wasEmpty then + nametag.text = "" + else + nametag.text = string.gsub(nametag.text, info.addedMsg, "") + end + player:set_nametag_attributes(nametag) + end + shown[playerName] = nil + end + end + end +end) \ No newline at end of file diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..2f50c28 --- /dev/null +++ b/mod.conf @@ -0,0 +1,5 @@ +name = message_bubbles +min_minetest_version = 5.0 +author = ZenonSeth +description = Shows chat messages above player heads +title = Message Bubbles diff --git a/settingtypes.txt b/settingtypes.txt new file mode 100644 index 0000000..ffb5363 --- /dev/null +++ b/settingtypes.txt @@ -0,0 +1,4 @@ + +message_bubbles_lifetime (Lifetime for message bubbles, in seconds) int 10 1 600 +message_bubbles_char_limit (Max number of characters to display per message bubble) int 40 5 500 +message_bubbles_prefix (Prefix to show before user message bubble) string Says: