Reaper123
Reaper123

Reputation: 109

How do I add chat colors?

I want to add color formats to my plugin (like essentials has colors in chat). For example, &6test would become "test" in gold.

I almost added those colors, but I have a problem. It deletes the whole message and leaves the green test message. How do I add colors?

Right now I'm 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, like I said, it deletes the whole message and leaves the green test message. This is caused by:

word = word.replaceAll("&2", "§2test");
chatevent.setMessage(word);

Ignore the "badwords" part, it is just to prevent people from using swear words. How do I fix this so that players could use colors codes in chat?

Upvotes: 3

Views: 3258

Answers (3)

Jojodmo
Jojodmo

Reputation: 23616

If you would like to translate color codes (from an & symbol, for example), you can use ChatColor.translateAlternateColorCodes

ChatColor.translateAlternateColorCodes('&', str);

This will automatically convert all & color codes to color codes with the § symbol, and therefor to its respective ChatColor.

So, for example, if you used

String original = "&atest";
String formatted = ChatColor.translateAlternateColorCodes('&', original);

formatted would be equal to "§atest", which is equal to ChatColor.GREEN + "test".

If you wanted to allow players to chat using the & color codes, you could first listen for AsyncPlayerChatEvent (inside of a class that implements Listener), and then set the message to the correctly colored message using .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 would be translated to ChatColor.GOLD + "Hello, " + ChatColor.GREEN + "World!".

The reason the entire message is being replaced by "§2test" is because you're splitting the string up, and not reconstructing it.

Instead, you should split the string up after you use translateAlternateColorCodes, and then strip the colors from the string using ChatColor.stripColor(String) before checking if it's a curse word (this would prevent players from bypassing the censor by putting color codes in front of curse words)

Upvotes: 1

Syrrus
Syrrus

Reputation: 79

Your deconstructing the String with multiple words in it. What you need to do is create an additional string and re-compile the message within your loop. Then do your: chatevent.setMessage(newly_contructed_message_from_your_loop);

But I would imagine whatever you are using you might have to separate them into multiple different objects to create the message from within the loop.

Upvotes: 1

nulldev
nulldev

Reputation: 615

Use ChatColor.{COLORNAME}.

Example: word = word.replaceAll("&2", ChatColor.GREEN + "test");

A complete list of all available color codes are here: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/ChatColor.html

You might have to import org.bukkit.ChatColor like this: import org.bukkit.ChatColor;

EDIT: As for the reason why it's not replacing the correct part, it has nothing to do with colors. You are using the .replaceAll(String regex, String replacement) which takes a regex, since you don't care about regexs, just use the .replace(CharSequence target, CharSequence replacement) function. It will still replace all occurrences of the string.

EDIT2: You are also calling chatevent.setMessage(word); for every word, you want to call this only once you have processed all the words. Use a StringBuilder and append each word to it and then set the message at the end (out of the for loop).

Upvotes: 1

Related Questions