Techy5's new colored chat CSM
Posted: Fri Dec 01, 2017 06:16
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:
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!
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)
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!