PacketPlayOutChat + TextComponent?

Discussion in 'Spigot Plugin Development' started by GermanElectronix, Jun 20, 2015.

  1. Hey,

    currently I'm trying to implement tooltips in the chat. To do that, I'm first cancelling the AsyncPlayerChatEvent to send a PacketPlayOutChat to every player:

    Code (Text):
    public static final void broadcastChatMessage(BaseComponent message){
            final PacketPlayOutChat packet = new PacketPlayOutChat(IChatBaseComponent.ChatSerializer.a(message.toString()));
            chatPlayers.parallelStream().forEach(p -> p.getHandle().playerConnection.sendPacket(packet));
        }
    With that code, I am getting this error:
    Code (Text):
    [Async Chat Thread - #0/ERROR]: Could not pass event AsyncPlayerChatEvent to ChatPlus v0.1.1
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at net.minecraft.server.v1_8_R2.PlayerConnection.chat(PlayerConnection.java:1055) [spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:993) [spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat$1.run(PacketPlayInChat.java:39) [spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_45]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_45]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_45]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 15
        at com.google.gson.Gson.assertFullConsumption(Gson.java:779) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at com.google.gson.Gson.fromJson(Gson.java:769) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at com.google.gson.Gson.fromJson(Gson.java:717) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at com.google.gson.Gson.fromJson(Gson.java:689) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at net.minecraft.server.v1_8_R2.IChatBaseComponent$ChatSerializer.a(SourceFile:199) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at de.lenic.chatplus.ChatPlus.broadcastChatMessage(ChatPlus.java:59) ~[?:?]
        at de.lenic.chatplus.listeners.ChatListener.onChat(ChatListener.java:44) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_45]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        ... 11 more
    Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 15
        at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1505) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1386) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:531) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at com.google.gson.stream.JsonReader.peek(JsonReader.java:414) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at com.google.gson.Gson.assertFullConsumption(Gson.java:775) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at com.google.gson.Gson.fromJson(Gson.java:769) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at com.google.gson.Gson.fromJson(Gson.java:717) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at com.google.gson.Gson.fromJson(Gson.java:689) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at net.minecraft.server.v1_8_R2.IChatBaseComponent$ChatSerializer.a(SourceFile:199) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        at de.lenic.chatplus.ChatPlus.broadcastChatMessage(ChatPlus.java:59) ~[?:?]
        at de.lenic.chatplus.listeners.ChatListener.onChat(ChatListener.java:44) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_45]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-5cb9b70]
        ... 11 more
    [16:35:20]

    The exeception is thrown during the packet declaration. Does anyone know how to fix that?

    Thanks in advance :)
    GermanElectronix
     
  2. clip

    Benefactor

    You need to convert your BaseComponent to a JSON String:

    ComponentSerializer.toString(BaseComponent);

    Why not just use Player.Spigot().sendMessage(component) though?
     
    • Like Like x 1
  3. Thanks, that's working!
    I thought, the usage of packets is better in performance
     
  4. clip

    Benefactor

    I don't think there is a difference in performance from sending the packet yourself or using the player.spigot() method. Either way, if it works, it works.