Official Forum for the HOMETOWN Minetest game server.

Techy5's new colored chat CSM

I am writing about Techy5's new colored chat CSM for him. He made it for my request, so least i could do. It allows you to set custom colors for certain players chat. You can set a custom color with .setcolor username color (hex or standard color names like red, pink, purple, etc.) You can remove someone's custom color with .delcolor playername. You can see a list of all the people you have added colors for and their colors with .listcolors. He did an awesome job so far, though he says it is still WIP, and may have bugs. here is his code so far for the CSM:

Code: Select all

local data = minetest.get_mod_storage()
local dColor = "#00FF00"
 
local chatSource = function(msg) -- Find out who was talking to us
    if string.sub(msg, 1, 1) == "<" then -- Normal messages
        local parts = string.split(msg, ">") -- Split it at the closing >
        return string.sub(parts[1], 2, string.len(parts[1])) -- Return the first part minus the first character
    elseif string.sub(msg, 1, 2) == "* " then -- /me messages
        local parts = string.split(msg, " ") -- Split the message before and after the name
        return parts[2] -- Return the name (this one is easy)
    end
    return false -- If nothing else returned, return false
end
 
minetest.register_chatcommand("setcolor", { -- Assign a colour to chat messages from a specific person
    params = "<name> <color>",
    description = "Colourize a specified player's chat messages",
    func = function(param)
        local args = string.split(param, " ") -- Split up the arguments
        local setName = "player_" .. args[1] -- The setting name to set
        local color = args[2]
        if not color then -- If a color was not specified, use the default color.
            color = dColor
        end
        data:set_string(setName, color)
        minetest.display_chat_message("Player color set sucessfully!")
    end
})
 
minetest.register_chatcommand("delcolor", {
    params = "<name>",
    description = "Set a specified player's chat messages to the default colour",
    func = function(param)
        local setName = "player_" .. param
        data:set_string(setName, nil) -- Set the color as blank
        minetest.display_chat_message("Player color set sucessfully!")
    end
})
 
minetest.register_chatcommand("listcolors", {
    params = "",
    description = "List player/colour pairs",
    func = function(param)
        local list = data:to_table().fields
        for key,value in pairs(list) do
            if string.sub(key, 1, 7) == "player_" then
                key = string.sub(key, 8, string.len(key))
            end
            minetest.display_chat_message(key .. ", " .. minetest.colorize(value, value))
        end
    end
})
 
minetest.register_on_connect(function()
    minetest.register_on_receiving_chat_messages(function(message)
        local msgPlain = minetest.strip_colors(message)
        local source = chatSource(msgPlain)
       
        if not source then -- If the chat was not from a player, return
            return
        end
       
        local playerSet = "player_" .. source -- The setting name
        local color = data:get_string(playerSet) -- Get the desired color
        if color == "" then -- If no color, return
            return
        end
       
        message = minetest.colorize(color, msgPlain)
        minetest.display_chat_message(message)
        return true
    end)
end)
He is definitely open to help and suggestions, and would greatly appreciate the bug finders and fixers who might want to help out.

To install a CSM, you should navigate to you minetest directory, create a folder called "clientmods", create a second folder within that is called "colored_chat", and also in the clientmods directory, create a mods.conf file, and make line 1 say "load_mod_colored_chat = true", and line 2 say "load_mod_preview = false". Save this file as mods.conf. Then go into your colored_chat directory within the clientmods directory, and make a file called "init.lua". Copy and paste the code into it, and save it. Then go to settings, search "client", you will see "Client modding", set that to Enabled. After that you are all set to have custom colored chat!
Updated version (Added colored status messages, colored join/leave messages and code cleanup):

Code: Select all

local data = minetest.get_mod_storage()
local dColor = "#00FF00"

local chatSource = function(msg) -- Find out who was talking to us
	if string.sub(msg, 1, 1) == "<" then -- Normal messages
		local parts = string.split(msg, ">") -- Split it at the closing >
		return string.sub(parts[1], 2, string.len(parts[1])) -- Return the first part minus the first character
	elseif string.sub(msg, 1, 2) == "* " or string.sub(msg, 1, 4) == "*** " then -- /me messages or join/leave messages
		local parts = string.split(msg, " ") -- Split the message before and after the name
		return parts[2]
	end
	return false -- If nothing else returned, return false
end

minetest.register_chatcommand("setcolor", { -- Assign a colour to chat messages from a specific person
	params = "<name> <color>",
	description = "Colourize a specified player's chat messages",
	func = function(param)
		local args = string.split(param, " ") -- Split up the arguments
		local key = "player_" .. args[1] -- The setting name to set
		local color = args[2]
		if not color then color = dcolor end-- If a colour was not specified, use the default colour.
		data:set_string(key, color)
		minetest.display_chat_message("Player color set sucessfully!")
	end
})

minetest.register_chatcommand("delcolor", {
	params = "<name>",
	description = "Set a specified player's chat messages to the default colour",
	func = function(param)
		local key = "player_" .. param
		data:set_string(key, nil) -- Delete the key
		minetest.display_chat_message("Player color set sucessfully!")
	end
})

minetest.register_chatcommand("listcolors", {
	params = "",
	description = "List player/colour pairs",
	func = function(param)
		local list = data:to_table().fields
		for key,value in pairs(list) do -- Get key and value for all pairs
			if string.sub(key, 1, 7) == "player_" then -- Might prevent future problems
				key = string.sub(key, 8, string.len(key)) -- Isolate the player name
			end
			minetest.display_chat_message(key .. ", " .. minetest.colorize(value, value))
		end
	end
})

minetest.register_on_connect(function()
	minetest.register_on_receiving_chat_messages(function(message)
		local msgPlain = minetest.strip_colors(message)
		local sender = chatSource(msgPlain)
		
		if string.sub(msgPlain, 1, 2) == "# " then -- /status message
			local list = data:to_table().fields
			for key,value in pairs(list) do -- Get key and value for all pairs
				if string.sub(key, 1, 7) == "player_" then -- Might prevent future problems
					key = string.sub(key, 8, string.len(key)) -- Isolate the player name
					msgPlain = string.gsub(msgPlain, key, minetest.colorize(value, key)) -- Replace plain name with coloured version
				end
			end
			minetest.display_chat_message(msgPlain)
			return true
		elseif sender then -- Normal player messages
			local setKey = "player_" .. sender -- The setting name
			local color = data:get_string(setKey) -- Get the desired colour
			if color == "" then return end -- If no colour, set to default
			message = minetest.colorize(color, msgPlain)
			minetest.display_chat_message(message)
			return true
		end
	end)
end)
Where is the colored status thingy that CS talked about? :P
BBmine wrote:
Wed Dec 27, 2017 4:06 am
Where is the colored status thingy that CS talked about? :P
Type /status, the names will be colored
ChimneySwift wrote:
Wed Dec 27, 2017 6:27 am
BBmine wrote:
Wed Dec 27, 2017 4:06 am
Where is the colored status thingy that CS talked about? :P
Type /status, the names will be colored
Oh.

Is there a way to color PMs and any mentions?
BBmine wrote:
Wed Dec 27, 2017 1:14 pm
Is there a way to color PMs and any mentions?
Not through this CSM as of now.

Who is online

In total there are 3 users online :: 0 registered, 0 hidden and 3 guests (based on users active over the past 5 minutes)
Most users ever online was 23 on Wed Mar 14, 2018 2:43 am

Users browsing this forum: No registered users and 3 guests