replaceAll("&","§");

Discussion in 'Plugin Development' started by TryKimko, Nov 18, 2016.

Thread Status:
Not open for further replies.
  1. Hey Guys... i've got a problem with a replace from a userinput... Here is my complete code:
    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
    
            if(sender instanceof Player)
            {
                Player player = (Player)sender;
                if(args.length == 0)
                {
                    player.sendMessage(ingameName + "§cDu musst eine Nachricht eingeben!");
                }
                else
                {
                    String message = "";
                    for(String part : args)
                    {
                        message += part;
                        message += " ";
                    }
                    message.replaceAll("&","§");
                    Bukkit.getServer().broadcastMessage(ingameName + message);
                }
            }
            else
            {
                System.out.print(pluginName + "Dieser Befehl muss von einem Spieler eingegeben werden!");
            }
            return true;
        }
    The Variables are all fine.. no error.. but it won't work :/
     
    Last edited: Nov 18, 2016
  2. Online

    timtower Administrator Administrator Moderator

    @TryKimko replaceAll has a return value, use it.
    And use ChatColor.translateAlternateColorCode instead
     
  3. Offline

    Zombie_Striker

    @TryKimko
    To add onto what @timtower said, the replaceAll method creates a new string. "Message" does not get affected by that method. Either way, use ChatColor.translate..... to add chat colors.
     
  4. @Zombie_Striker @timtower
    I'm sorry but could you please explain it a bit more?:)
    I'm atleast a newbie at Java and know what you mean but don't exactly who to use it...
     
  5. Offline

    jlin13

    replaceAll() returns a new String so you have to either store the return value in a String variable to use it, or use the String directly somehow. By just calling the method, all the code is executed in the method, however, upon returning the value you don't do anything with it

    ChatColor.translateAlternateColor(char altChar, String text) takes an alternate color character that you define in the first parameter altChar, translates the text you define in the second parameter, and returns the text with the right color character ready for you to sendMessage() or broadcastMessage()

    i.e.
    Code:
    broadcastMessage(ChatColor.translateAlternateColor('&', text));
     
Thread Status:
Not open for further replies.

Share This Page