Problem with Chat Color Translate

Discussion in 'Spigot Plugin Development' started by Nothing00, Jan 18, 2018.

Thread Status:
Not open for further replies.
  1. hi, I'm developing a plugin in java, the only thing I can not do, however, is to give the possibility to insert colored personalized messages via config.
    I have tried several of the following method:

    ChatColor.translateAlternateColorCodes('&', String);


    String.replace('&', '§');

    but the result is always the same, in chat you will see the blank message containing the & with the respective code.
    any suggestions?
  2. Choco


    Remember that in Java, Strings are immutable. Therefore, the modified String is returned by the #translateAlternateColorCodes() method.
    Code (Java):
    String message = "&6Some string with a colour code";
    ChatColor.translateAlternateColorCodes("&", message);
    player.sendMessage(message); // This will not work
    Code (Java):
    String message = "&6Some string with a colour code";
    player.sendMessage(ChatColor.translateAlternateColorCodes("&", message)); // This WILL work

    // Alternatively, this works too because you're reassigning the value to the colourized String
    String message = "&6Some string with a colour code";
    message = ChatColor.translateAlternateColorCodes("&", message)
    You should always opt for #translateAlternateColorCodes() over using the section symbol in source. Some operating systems do not support that character encoding, therefore the symbol will print as a question mark in chat and not colourize your message
    • Agree Agree x 1

  3. I just said it does not work
  4. Choco


    I just said you're not doing it right ;) A method doesn't just "not work" unless you're not using it properly. The other 99.99% of people using that method find it works perfectly. It would be best if you show us exactly what you're doing. Give us the code you've tried to make it work and I will tell you why it is not.
    • Like Like x 2
    • Agree Agree x 2
  5. *edits java so a basic method doesn't work no matter what and then comes and complains about it*
  6. Not the answer you want but here's a tip for you:
    Use the [ CODE ] element if you want to show us your code.
    It's just so much better for us to read it.[/CODE]
  7. Code (Text):
    if(e.getPlayer().hasPermission("spaceprison.break")){ //controllo se l'utente ha il permesso
                    }else{ //controllo permessi
                        e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&' , getConfig().getString("Message.NoPerm")));
  8. Can you send a snippet of the config containing the message you're trying to translate?

    Also PLEASE FOLLOW JAVA CONVENTIONS! Don't use capital letters at the start of method names...
    Code (Java):
    Bypass() -> bypass()
  9. on the config.yml
    Code (Text):
      OnBreakDefault: '&9SpacePrison> &7La Pietra rimane qua!'
      Bypass: '&9SpacePrison> &7Hai rimosso uno SpacePrison Block!'
      NoPerm: '&9SpacePrison> &7Non hai il permesso!'
      Reload: '&9SpacePrison> &7Config reloaded!'
    Bypass() it works perfectly and is not in fact what we are talking about
  10. So it still says '&9SpacePrison> &7Non hai il permesso!'?
    Can you give us the full method so we can see if it's possibly caused by something else?

    Doesn't matter if it works, you should follow Java Naming Conventions, doesn't matter if it's off-topic - it makes code neat.
  11. Its not about its functionality, its just about that fact that we have a universal java language and it simply makes it easier for developers to read and understand :D

    Also to make your code a bit easier, instead of doing e.getPlayer() everytime, just do:

    Code (Text):
    if(sender instanceOf Player) {

    Player p = e.getPlayer()

    //code here

    • Agree Agree x 1
  12. the message string is called only in that condition
  13. Try doing

    Code (Text):
    and see if that outputs anything.
  14. Code (Text):
    String message = config.getString("Join Message").replaceAll("&", "§");
  15. replaceAll() do not exist
  16. Then what version of Java do you use? geez...
    • Agree Agree x 1
  17. sender is not used only when making a command? The event is a BlockBreak
  18. java 8
  19. It should....
Thread Status:
Not open for further replies.