Solved ASyncPlayerChatEvent and the % Sign

Discussion in 'Spigot Plugin Development' started by xSinclare, Mar 12, 2017.

Thread Status:
Not open for further replies.
  1. Hey, Spigot! So recently I have come across an issue with my plugin and chat events. Everything works just dandy with chat and color codes, however, when a player sends a percent sign in chat this comes up:
    Code (Text):
    public class ChatEvent implements Listener
    {

        @EventHandler
        public void onChat(AsyncPlayerChatEvent event)
        {
            Player player = event.getPlayer();
            String UUID = player.getUniqueId().toString();
            String Message = event.getMessage();
            event.setFormat(RankUtils.getRankPlayer(UUID).getTagWithBrackets(true) + " " + player.getName() + ChatColor.GRAY + ": " + ChatColor.WHITE + ChatColor.translateAlternateColorCodes('&', event.getMessage()));
        }
    }
    I looked into the issue online and the code below was their solution:
    Code (Text):
    e.setFormat(ChatColor.translateAlternateColorCodes('&', "&a%s&7: %s"));

    However, I don't think that setup will exactly work with mine. Our format is fairly custom, so using the fix above won't satisfy our needs. Can any of you guys give me some tips on what to do with this? It would be greatly appreciated! For now, I have advised players to not use percent codes that much. Oh and I mind as well attach the error code that popped in the console. Here is is:
    Code (Text):
    [21:32:03] [Async Chat Thread - #0/ERROR]: Could not pass event AsyncPlayerChatEvent to [PLUGIN NAME] v1.4
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.chat(PlayerConnection.java:1084) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1022) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat$1.run(PacketPlayInChat.java:39) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_121]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_121]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_121]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_121]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_121]
    Caused by: java.util.FormatFlagsConversionMismatchException: Conversion = e, Flags =
        at org.bukkit.event.player.AsyncPlayerChatEvent.setFormat(AsyncPlayerChatEvent.java:100) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at us.thenerdcave.Events.ChatEvent.onChat(ChatEvent.java:23) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_121]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_121]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_121]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        ... 11 more
    [21:32:03] [Async Chat Thread - #0/INFO]: <xSinclare> % test[m
     
  2. use something like this
    Code (Java):

    public String colorcode(String msg){
    return ChatColor.translateAlternateColorCodes('&', string)
    }
     
    then use it like so

    Code (Java):

    #sendMessage(colorcode("&6ColoredMessage"))
     
    also your problem is for the code try adding 2 %...

    so %%
     
  3. If you're having a problem with % signs a work around could be to just make a new string without them before you translate.

    Of course this would mean that players would no longer be able to use % signs, but... just a suggestion.

    Code (Text):
    String withoutPercent = withPercent.replace('%', ' ');
     
  4. Heres something I found, i havent messed around with ASyncPlayerChat before

    https://bukkit.org/threads/correct-usage-of-asyncplayerchatevent.107127/

    Try reading that and see if it helps
     
  5. I saw this, but it would look really weird even if players could use color codes. That's why I am skeptical about this option.

    I saw this option too... and I want players to be able to use percent signs still.

    I checked that thread too. Seems interesting. I may probably end up choosing the better of the three options, but I'm gonna see what others say.
     
  6. The problem is you are trying to set the message in the format, which is the wrong usage of that method.
    To fix everything, you simply need to replace certain parts of your format.
    Replace
    Code (Text):
    player.getName()
    with "%1$s" and replace
    Code (Text):
    event.getMessage()
    with "%2$s"
    Then to parse the color codes you can simply do
    Code (Text):
    event.setMessage(ChatColor.translateAlternateColorCodes('&', event.getMessage()));
    on another line.

    Remember, the setFormat method uses the Java Formatter, and in there, the % sign represents a variable, which is why it's breaking when a player uses it.
     
    • Winner Winner x 1
  7. Ahhh. I get what you mean and what all the other threads meant. I completely understand the difference between event.setFormat() and event.setMessage(). You just happened to explain it the best! The final code works perfectly fine and I'm able to use percent signs in chat as normal. Here it is below:
    Code (Text):
    public class ChatEvent implements Listener
    {

        @EventHandler
        public void onChat(AsyncPlayerChatEvent event)
        {
            Player player = event.getPlayer();
            String UUID = player.getUniqueId().toString();
            String Message = event.getMessage();
            event.setFormat(RankUtils.getRankPlayer(UUID).getTagWithBrackets(true) + " " + "%s" + ChatColor.GRAY + ": " + ChatColor.WHITE + "%s");
            event.setMessage(ChatColor.translateAlternateColorCodes('&', event.getMessage()));
        }
    }
     
Thread Status:
Not open for further replies.