user3658271
user3658271

Reputation: 11

Adding color code translation for a string in a Bukkit config.yml

I am trying to add color support to my plugin and when I did, it works, but i get a weird symbol on the left side of it. (http://postimg.org/image/5tuimwlsb/). Here is my code:

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
    if(sender instanceof Player)
        {
            Player player = (Player)sender;

            if(player.hasPermission("di.use"))
                { 
                if(label.equalsIgnoreCase("di"))
                {
                if(args.length == 0)
                {
                    ///////////////////////////////////////////////////////
                    player.sendMessage(ChatColor.DARK_GRAY+""+ChatColor.BOLD + "+" + ChatColor.AQUA+""+ChatColor.BOLD + "---------------" + ChatColor.GREEN+""+ChatColor.BOLD + "[" + ChatColor.YELLOW+""+ChatColor.BOLD + "DonationInfo" + ChatColor.GREEN+""+ChatColor.BOLD + "]" + ChatColor.AQUA+""+ChatColor.BOLD + "--------------" + ChatColor.DARK_GRAY+""+ChatColor.BOLD + "+");
                    player.sendMessage(ChatColor.AQUA+""+ChatColor.BOLD + "/di" + ChatColor.DARK_AQUA+""+ChatColor.BOLD+ " Spinel " + ChatColor.BLUE+""+ChatColor.BOLD + " - " + ChatColor.AQUA+""+ChatColor.BOLD + "Spinel Donation Info");
                    player.sendMessage(ChatColor.AQUA+""+ChatColor.BOLD + "/di" + ChatColor.DARK_BLUE+""+ChatColor.BOLD+ " Sapphire " +  ChatColor.BLUE+""+ChatColor.BOLD + " - " + ChatColor.AQUA+""+ChatColor.BOLD + "Sapphire Donation Info");
                    player.sendMessage(ChatColor.AQUA+""+ChatColor.BOLD + "/di" + ChatColor.RED+""+ChatColor.BOLD+ " Ruby " +  ChatColor.BLUE+""+ChatColor.BOLD + " - " + ChatColor.AQUA+""+ChatColor.BOLD + "Ruby Donation Info");
                    player.sendMessage(ChatColor.AQUA+""+ChatColor.BOLD + "/di" + ChatColor.LIGHT_PURPLE+""+ChatColor.BOLD+ " Amethyst " +  ChatColor.BLUE+""+ChatColor.BOLD + " - " + ChatColor.AQUA+""+ChatColor.BOLD + "Amethyst Donation Info");
                    player.sendMessage(ChatColor.DARK_GRAY+""+ChatColor.BOLD + "+" + ChatColor.AQUA+""+ChatColor.BOLD + "-----------------------------------------" + ChatColor.DARK_GRAY+""+ChatColor.BOLD + "+");
                    return true;
                    //////////////////////////////////////////////////////
                }
            }
        }else{
            player.sendMessage(ChatColor.RED + "Your do not have permissions to use this command!");
        }
            if(args[0].equalsIgnoreCase("Spinel"))
            {String a = getConfig().getString("Spinel");
            String coloredText = ChatColor.translateAlternateColorCodes('§', a);
                ///////////////////////////////////////////////////////////
                player.sendMessage(coloredText);
                return true;
                ///////////////////////////////////////////////////////////
            }
            if(args[0].equalsIgnoreCase("Sapphire"))
            {String b = getConfig().getString("Sapphire");
            String coloredText = ChatColor.translateAlternateColorCodes('§', b);
                ///////////////////////////////////////////////////////////
                player.sendMessage(coloredText);
                return true; 
                ///////////////////////////////////////////////////////////
            }
            if(args[0].equalsIgnoreCase("Ruby"))
            {String c = getConfig().getString("Ruby");
            String coloredText = ChatColor.translateAlternateColorCodes('§', c);
                ///////////////////////////////////////////////////////////
                player.sendMessage(coloredText);
                return true;
                ///////////////////////////////////////////////////////////
            }
            if(args[0].equalsIgnoreCase("Amethyst"))
            {String d = getConfig().getString("Amethyst");
            String coloredText = ChatColor.translateAlternateColorCodes('§', d);
                ///////////////////////////////////////////////////////////
                player.sendMessage(coloredText);
                return true;
                ///////////////////////////////////////////////////////////
            }
            if(player.hasPermission("di.admin"))
            {
                if(args[0].equalsIgnoreCase("reload"))
                {
                                        this.reloadConfig();
                    //////////////////////////////////////////////////////////
                    player.sendMessage(ChatColor.AQUA+""+ChatColor.BOLD+"[Donation_Info]" + ChatColor.DARK_AQUA+""+ChatColor.BOLD + " || " + ChatColor.AQUA+""+ChatColor.BOLD + "Plugin reloaded!");
                    //////////////////////////////////////////////////////////
                }
            }
        }

Please help me. Thank you.

Upvotes: 0

Views: 5701

Answers (2)

user2982130
user2982130

Reputation:

If you already have a § then you don't need to do anything; I do believe that minecraft parses the color codes using special characters on its own.

You would only need to use translateAlternateColorCodes(...) when it is not the symbol §.

Otherwise, jojodmo explains it pretty well.

Upvotes: 0

Jojodmo
Jojodmo

Reputation: 23616

First off, just thought that I would point out that this is Java. Bukkit plugins are coded with Java. Java is to JavaScript as car is to carrot... Your code is not in JavaScript.

Anyways, the reason your code is not working properly is because you cannot use the § symbol for Minecraft, because MineCraft does not allow all special characters, So when you try to translate Color Codes with it, it DOES work, but the § is not removed, but instead replaced with Å.

If you want to translate alternate color codes, you can use ChatColor.translateAlternetColorCodes(Char, String). So, let's say that for example you wanted to make a message green, having the message Hi. You could use:

String toTranslate = "&4Donation 1";
String translated = ChatColor.translateAlternetColorCodes('&', toTranslate);

The string translated would then become ChatColor.DARK_RED + "Donation 1".

You could change the & in the arguments for the first line of code to any other character, for example you could use $. If you used $, then $4Donation 1 would also ake a dark red message that says "Hi". You cannot use the § symbol, though, and some other special characters for translating color codes.

Here is a complete list of color codes: List Here

If you really need to use the § symbol for some reason, you MAY be able to get away with it by doing this:

String toTranslate = "§4Donation 1";
String translated = ChatColor.translateAlternetColorCodes('§', toTranslate)
String final = translated.replaceAll("Å", "") //replace the special characters with nothing, but keep the color

The above might work, although I don't recommend it because it's a little bit messy, you have to UTF-8 (not that it's bad), and it's also a lot harder to create the § symbol than it is to create the & symbol.

Upvotes: 1

Related Questions