diff --git a/README.md b/README.md new file mode 100644 index 0000000..2963505 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# Colored Names + +## Description + +![Screenshot](https://raw.githubusercontent.com/SmallJoker/colored_names/master/screenshot.png) +This client mod colorizes the nicknames in-game, so communicating gets easier, even when many people are chatting with each other. + +License: MIT + +## Installation +1. Close all Minetest processes +2. Join the Minetest directory which contains the directories `mods`, `games`, ... +3. (Create and) open the directory `clientmods` in this location +4. `git clone https://github.com/SmallJoker/colored_names.git` +5. (Create and) open the file `mods.conf` in `clientmods` +6. Add or update the line `load_mod_colored_names = true` +7. Start Minetest diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..6e9c477 --- /dev/null +++ b/init.lua @@ -0,0 +1,52 @@ +if not core.register_on_receiving_chat_message then + core.register_on_receiving_chat_message = core.register_on_receiving_chat_messages +end + +core.register_on_receiving_chat_message(function(line) + local myname_l = "~[CAPSĀ£" + if minetest.localplayer then + myname_l = minetest.localplayer:get_name():lower() + end + + local prefix + local chat_line = false + local name, message = line:match("^%<(%S+)%> (.*)") + if message then + -- To keep the notation + chat_line = true + else + prefix, name, message = line:match("^(%*+ )(%S+) (.*)") + end + if not message then + name, message = "", (line:match("^%s*(.*)") or line) + end + prefix = prefix or "" + + if name ~= "" then + local nick_color = 0 + for i = 1, #name do + local c = name:sub(i, i):byte() + nick_color = nick_color + c * 2^(i - 1) + end + local B = nick_color % 0x10 + local G = math.floor((nick_color % 0x100 ) / 0x10) + local R = math.floor((nick_color % 0x1000) / 0x100) + if R + G + B < 24 then + R = 15 - R + G = 15 - G + B = 15 - B + end + + if chat_line then + name = "<" .. name .. ">" + end + name = minetest.colorize(string.format("#%X%X%X", R, G, B), name) + end + + if (chat_line or prefix == "* ") + and message:lower():find(myname_l) then + prefix = minetest.colorize("#F33", "[!] " .. prefix) + end + + return minetest.display_chat_message(prefix .. name .. " " .. message) +end) \ No newline at end of file diff --git a/screenshot.png b/screenshot.png new file mode 100644 index 0000000..71d4ade Binary files /dev/null and b/screenshot.png differ