From 67a8908d9f41894abb30b57a837a0d22a71a6ff0 Mon Sep 17 00:00:00 2001 From: Och Noe Date: Sun, 10 May 2020 10:08:15 +0200 Subject: first useable version - colour names possible, even for fading - mi/mw use server commands --- init.lua | 282 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 282 insertions(+) create mode 100644 init.lua (limited to 'init.lua') diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..e55419d --- /dev/null +++ b/init.lua @@ -0,0 +1,282 @@ +local modstorage = core.get_mod_storage() + +local register_on_message = core.register_on_sending_chat_message +if core.register_on_sending_chat_messages then + register_on_message = core.register_on_sending_chat_messages +end + +local function rgb_to_hex(rgb) + local hexadecimal = '#' + + for key, value in pairs(rgb) do + local hex = '' + + while(value > 0)do + local index = math.fmod(value, 16) + 1 + value = math.floor(value / 16) + hex = string.sub('0123456789ABCDEF', index, index) .. hex + end + + if(string.len(hex) == 0)then + hex = '00' + + elseif(string.len(hex) == 1)then + hex = '0' .. hex + end + + hexadecimal = hexadecimal .. hex + end + + return hexadecimal +end + +local function color_from_hue(hue) + local h = hue / 60 + local c = 255 + local x = (1 - math.abs(h%2 - 1)) * 255 + + local i = math.floor(h); + if (i == 0) then + return rgb_to_hex({c, x, 0}) + elseif (i == 1) then + return rgb_to_hex({x, c, 0}) + elseif (i == 2) then + return rgb_to_hex({0, c, x}) + elseif (i == 3) then + return rgb_to_hex({0, x, c}); + elseif (i == 4) then + return rgb_to_hex({x, 0, c}); + else + return rgb_to_hex({c, 0, x}); + end +end + +local function pastel_from_hue(hue) + local h = hue / 60 + local c = 255 + local d = 192 + local x = (1 - math.abs(h%2 - 1)) * 255 + + local i = math.floor(h); + if (i == 0) then + return rgb_to_hex({c, x, d}) + elseif (i == 1) then + return rgb_to_hex({x, c, d}) + elseif (i == 2) then + return rgb_to_hex({d, c, x}) + elseif (i == 3) then + return rgb_to_hex({d, x, c}); + elseif (i == 4) then + return rgb_to_hex({x, d, c}); + else + return rgb_to_hex({c, d, x}); + end +end + +function hex2rgb(hex) -- added - found on github + hex = hex:gsub("#","") + return {tonumber("0x"..hex:sub(1,2)), tonumber("0x"..hex:sub(3,4)), tonumber("0x"..hex:sub(5,6))} +end + +local function color_step(pos, col1, col2) -- added - gets a color between colors + local rgb1 = hex2rgb(col1) + local rgb2 = hex2rgb(col2) + local rgb = {} + for i, v in pairs(rgb1) do + rgb[i] = rgb1[i]+(rgb2[i]-rgb1[i])*(pos/255) + end + return rgb_to_hex(rgb) +end + +local function canTalk() + if core.get_privilege_list then + return core.get_privilege_list().shout + else + return true + end +end + +local function say(message) + if not canTalk() then + minetest.display_chat_message("You need 'shout' in order to talk") + return + end + minetest.send_chat_message(message) + if minetest.get_server_info().protocol_version < 29 then + local name = minetest.localplayer:get_name() + minetest.display_chat_message("<"..name.."> " .. message) + end +end + +function fade(parameter) + local col1 = string.sub(parameter, 1, 7) + local col2 = string.sub(parameter, 9, 15) + local message = string.sub(parameter, 17) + if not canTalk() then + return false, "You need 'shout' in order to use this command" + end + if col1 == col2 then + say(core.get_color_escape_sequence(col1) .. message) + return true + end + local step = 255 / message:len() + local pos = 0 + local output = "" + for i = 1, message:len() do + local char = message:sub(i,i) + if char:match("%s") then + output = output .. char + else + output = output .. core.get_color_escape_sequence(color_step(pos, col1, col2)) .. char + end + pos = pos + step + end + say(output) + return true +end + +register_on_message(function(message) + if message:sub(1,1) == "/" or modstorage:get_string("colour") == "" or modstorage:get_string("colour") == "white" then + return false + end + + fade(modstorage:get_string("colour") .. " " .. message) + return true +end) + +core.register_chatcommand("set_colour", { + description = core.gettext("Change chat colour"), + func = function(colour) + if colour:len() == 7 then + modstorage:set_string("colour", colour.." "..colour) + else + modstorage:set_string("colour", colour) + end + return true, "Chat colour changed." + end, +}) + +core.register_chatcommand("get_colour", { + description = core.gettext("Display chat colour"), + func = function() + local cc = modstorage:get_string("colour") + return true, "Current chat colour: " .. cc + end, + }) + + +core.register_chatcommand("rainbow", { + description = core.gettext("rainbow text"), + func = function(param) + if not canTalk() then + return false, "You need 'shout' in order to use this command" + end + local step = 360 / param:len() + local hue = 0 + -- iterate the whole 360 degrees + local output = "" + for i = 1, param:len() do + local char = param:sub(i,i) + if char:match("%s") then + output = output .. char + else + output = output .. core.get_color_escape_sequence(colour_from_hue(hue)) .. char + end + hue = hue + step + end + say(output) + return true +end, +}) + +core.register_chatcommand("fade", { -- added - fades between any two colors + description = core.gettext("fade message between two colors"), + func = fade, +}) + +core.register_chatcommand("pastel", { + description = core.gettext("pastel rainbow text"), + func = function(param) + if not canTalk() then + return false, "You need 'shout' in order to use this command" + end + local step = 360 / param:len() + local hue = 0 + -- iterate the whole 360 degrees + local output = "" + for i = 1, param:len() do + local char = param:sub(i,i) + if char:match("%s") then + output = output .. char + else + output = output .. core.get_color_escape_sequence(pastel_from_hue(hue)) .. char + end + hue = hue + step + end + say(output) + return true +end, +}) + +core.register_chatcommand("mw", { + description = core.gettext("moderator warning"), + func = function(param) + if not canTalk() then + return false, "You need 'shout' in order to use this comand" + end + local output = "" + output = core.get_color_escape_sequence("#ffdf00") .. "MODERATOR WARNING: " + output = output .. core.get_color_escape_sequence("#ff7fff") ..param + say(output) + return true +end, +}) + + +core.register_chatcommand("mi", { + description = core.gettext("moderator info"), + func = function(param) + if not canTalk() then + return false, "You need 'shout' in order to use this command" + end + local output = "" + output = core.get_color_escape_sequence("#d0ff00") .. "MODERATOR INFO: " + output = output .. core.get_color_escape_sequence("#dfdfff") .. param + say(output) + return true +end, +}) + + + +core.register_chatcommand("rainbow", { + description = core.gettext("rainbow text"), + func = function(param) + if not canTalk() then + return false, "You need 'shout' in order to use this command" + end + local step = 360 / param:len() + local hue = 0 + -- iterate the whole 360 degrees + local output = "" + for i = 1, param:len() do + local char = param:sub(i,i) + if char:match("%s") then + output = output .. char + else + output = output .. core.get_color_escape_sequence(color_from_hue(hue)) .. char + end + hue = hue + step + end + say(output) + return true +end, +}) + +core.register_chatcommand("say", { + description = core.gettext("Send text without applying colour to it"), + func = function(text) + say(text) + return true + end, +}) -- cgit v1.2.3