How do I add chat colors?
I want to add color formats to my plugin (for example, essentials have colors in chat). For example, it &6test
will become the "test" in gold.
I almost added these colors, but I have a problem. It deletes the entire message and leaves a green test message. How do I add colors?
I am currently using this:
public void onPlayerChat(AsyncPlayerChatEvent chatevent){
for (String word : chatevent.getMessage().split(" ")){
word = word.replaceAll("&2", "Β§2test");
chatevent.setMessage(word);
if(SysMng.getConfig().getStringList("badwords").contains(word)){
if (!chatevent.getPlayer().hasPermission("bypassbadwords")){
chatevent.setCancelled(true);
chatevent.getPlayer().sendMessage(ChatColor.RED + "Dont use dirty or swear words!");
}
}
}
}
But as I said, it deletes the whole message and leaves a green test message. This is caused by:
word = word.replaceAll("&2", "Β§2test");
chatevent.setMessage(word);
Ignore the "badwords" part, it just stops people from using bad words. How do I fix this so players can use color codes in chat?
source to share
If you want to translate color codes (for example from a symbol &
) you can useChatColor.translateAlternateColorCodes
ChatColor.translateAlternateColorCodes('&', str);
This will automatically convert all color codes &
to color codes with a symbol Β§
and then to the appropriate one ChatColor
.
So, for example, if you used
String original = "&atest";
String formatted = ChatColor.translateAlternateColorCodes('&', original);
formatted
will be equal "Β§atest"
to equal ChatColor.GREEN + "test"
.
If you want to allow players to communicate using color codes &
, you can first listen AsyncPlayerChatEvent
(inside a class that implements Listener
) and then set the message to the correct colored message with.translateAlternateColorCodes
@EventHandler
public void playerChat(AsyncPlayerChatEvent e){
//get the chat message
String original = e.getMessage();
//format the chat message with &colorCodes
String formatted = ChatColor.translateAlternateColorCodes('&', original);
//set the message to the formatted message
e.setMessage(formatted);
}
Using this, if a player typed something like "& 6Hello, & aWorld!" It will be translated to ChatColor.GOLD + "Hello, " + ChatColor.GREEN + "World!"
.
The reason the whole message is replaced with "Β§2test" is because you are splitting the line up and not rebuilding it.
Instead, you should split the line after using translateAlternateColorCodes
, and then strip the colors with the line using ChatColor.stripColor(String)
, before checking if it's a scourge (this can prevent players from bypassing censorship by putting color codes in front of the cursed words)
source to share
Use ChatColor.{COLORNAME}
.
Example:
word = word.replaceAll("&2", ChatColor.GREEN + "test");
A complete list of all available color codes is here: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/ChatColor.html
You may need to import org.bukkit.ChatColor
like this:
import org.bukkit.ChatColor;
EDIT: For the reason that it doesn't replace the correct part, it has nothing to do with colors. You are using .replaceAll(String regex, String replacement)
that accepts a regex, since you don't need regex, just use a function .replace(CharSequence target, CharSequence replacement)
. It will replace all occurrences of the string anyway.
EDIT2: You are also calling chatevent.setMessage(word);
for each word, you only want to call this after you have processed all the words. Use a StringBuilder and add each word to it and then set the message at the end (from the for loop).
source to share
Your deconstruction is a string with a few words in it. What you need to do is create an extra line and recompile the message in your loop. Then make yours: chatevent.setMessage (new_contructed_message_from_your_loop);
But I would imagine what you are using, you might have to split them into several different objects to create the message from the loop.
source to share