Whats up with "%"?

Discussion in 'Plugin Development' started by Meatiex, Aug 8, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Meatiex

    The code below works great, except when I type % in chat, the event doesn't run at all...
    Code:
        @EventHandler
        public void chat(AsyncPlayerChatEvent event) {
            event.setFormat(event.getPlayer().getDisplayName() + format("&f: " + event.getMessage()));
        }
    Code:
        public static String format(String input) {
            return ChatColor.translateAlternateColorCodes('&', input.replace("(heart)", "❤"));
        }
    Could anyone tell me why this happens...? and maybe how to fix it? Thanks :)
     
  2. Offline

    Eos

    I'm not really experienced with #translateAlternateColorCodes. The format(String input) needs to be called.
     
  3. @Meatiex Translate, then replace after.
     
    mine-care likes this.
  4. Offline

    Meatiex

    @Eos
    UnknownFormatConversionException
    Code:
        @EventHandler
        public void chat(AsyncPlayerChatEvent event) {
            event.setFormat(event.getPlayer().getDisplayName() + ": " + event.getMessage());
        }
    It doesn't have to do with the other thing, the code above returns the same error...
     
  5. Offline

    Konato_K

    @Meatiex Set the format like it's intended, don't put the player's name or message in there, just a format.
     
  6. Offline

    567legodude

    @Meatiex The reason it doesn't work is because you are using a character that doesn't work inside the Formatter. Bukkit is taking that input string and using it in a Formatter.
    In a Formatter, the % sign means that a formatting instruction follows.
    There are 2 variables in the default message format. One is "%1$s" which means the player's name. The other is "%2$s" which means the message they typed.
    The default message format is "<%1$s> %2$s".
    Just remember you can't use % because the Formatter uses that as a variable to format the string.

    To solve the problem, don't use event.getMessage() in the format string, wherever you want their message to appear, use the string "%2$s"

    EDIT: To format their message using your format method, you can use this line.
    Code:
    event.setMessage(format(event.getMessage()));
     
    Meatiex likes this.
  7. Offline

    Meatiex

    Thank you! :D
     
Thread Status:
Not open for further replies.

Share This Page