Solved com.google.gson.JsonSyntaxException

Discussion in 'Spigot Plugin Development' started by DrOreo002, Jun 4, 2018.

  1. Error / Warn log :
    Code (Text):
    04.06 17:06:40 [Server] WARN com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 693 path $[8].text
    04.06 17:06:40 [Server] WARN at com.google.gson.internal.Streams.parse(Streams.java:60)
    04.06 17:06:40 [Server] WARN at com.google.gson.internal.bind.TreeTypeAdapter.read(TreeTypeAdapter.java:65)
    04.06 17:06:40 [Server] WARN at net.minecraft.server.v1_12_R1.ChatDeserializer.a(SourceFile:492)
    04.06 17:06:40 [Server] WARN at net.minecraft.server.v1_12_R1.ChatDeserializer.a(SourceFile:517)
    04.06 17:06:40 [Server] WARN at net.minecraft.server.v1_12_R1.IChatBaseComponent$ChatSerializer.a(SourceFile:212)
    04.06 17:06:40 [Server] WARN at me.droreo002.oreoclan.listener.ChatListenerBukkit.send(ChatListenerBukkit.java:435)
    04.06 17:06:40 [Server] WARN at me.droreo002.oreoclan.listener.ChatListenerBukkit.onChat(ChatListenerBukkit.java:394)
    04.06 17:06:40 [Server] WARN at sun.reflect.GeneratedMethodAccessor326.invoke(Unknown Source)
    04.06 17:06:40 [Server] WARN at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    04.06 17:06:40 [Server] WARN at java.lang.reflect.Method.invoke(Method.java:498)
    04.06 17:06:40 [Server] WARN at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302)
    04.06 17:06:40 [Server] WARN at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    04.06 17:06:40 [Server] WARN at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500)
    04.06 17:06:40 [Server] WARN at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:482)
    04.06 17:06:40 [Server] WARN at net.minecraft.server.v1_12_R1.PlayerConnection.chat(PlayerConnection.java:1319)
    04.06 17:06:40 [Server] WARN at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1257)
    04.06 17:06:40 [Server] WARN at net.aufdemrand.denizen.nms.impl.packets.handlers.AbstractListenerPlayIn_v1_12_R1.a(AbstractListenerPlayIn_v1_12_R1.java:81)
    04.06 17:06:40 [Server] WARN at net.minecraft.server.v1_12_R1.PacketPlayInChat$1.run(PacketPlayInChat.java:39)
    04.06 17:06:40 [Server] WARN at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    04.06 17:06:40 [Server] WARN at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    04.06 17:06:40 [Server] WARN at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    04.06 17:06:40 [Server] WARN at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    04.06 17:06:40 [Server] WARN at java.lang.Thread.run(Thread.java:748)
    04.06 17:06:40 [Server] WARN Caused by: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 693 path $[8].text
    04.06 17:06:40 [Server] WARN at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1559)
    04.06 17:06:40 [Server] WARN at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:491)
    04.06 17:06:40 [Server] WARN at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:414)
    04.06 17:06:40 [Server] WARN at com.google.gson.internal.bind.TypeAdapters$29.read(TypeAdapters.java:738)
    04.06 17:06:40 [Server] WARN at com.google.gson.internal.bind.TypeAdapters$29.read(TypeAdapters.java:731)
    04.06 17:06:40 [Server] WARN at com.google.gson.internal.bind.TypeAdapters$29.read(TypeAdapters.java:714)
    04.06 17:06:40 [Server] WARN at com.google.gson.internal.Streams.parse(Streams.java:48)
    04.06 17:06:40 [Server] WARN ... 22 more
    04.06 17:06:40 [Server] WARN com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 693 path $[8].text
    04.06 17:06:40 [Server] WARN at com.google.gson.internal.Streams.parse(Streams.java:60)
    04.06 17:06:40 [Server] WARN at com.google.gson.internal.bind.TreeTypeAdapter.read(TreeTypeAdapter.java:65)
    04.06 17:06:40 [Server] WARN at net.minecraft.server.v1_12_R1.ChatDeserializer.a(SourceFile:492)
    04.06 17:06:40 [Server] WARN at net.minecraft.server.v1_12_R1.ChatDeserializer.a(SourceFile:517)
    04.06 17:06:40 [Server] WARN at net.minecraft.server.v1_12_R1.IChatBaseComponent$ChatSerializer.a(SourceFile:212)
    04.06 17:06:40 [Server] WARN at me.droreo002.oreoclan.listener.ChatListenerBukkit.send(ChatListenerBukkit.java:435)
    04.06 17:06:40 [Server] WARN at me.droreo002.oreoclan.listener.ChatListenerBukkit.onChat(ChatListenerBukkit.java:394)
    04.06 17:06:40 [Server] WARN at sun.reflect.GeneratedMethodAccessor326.invoke(Unknown Source)
    04.06 17:06:40 [Server] WARN at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    04.06 17:06:40 [Server] WARN at java.lang.reflect.Method.invoke(Method.java:498)
    04.06 17:06:40 [Server] WARN at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302)
    04.06 17:06:40 [Server] WARN at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    04.06 17:06:40 [Server] WARN at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500)
    04.06 17:06:40 [Server] WARN at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:482)
    04.06 17:06:40 [Server] WARN at net.minecraft.server.v1_12_R1.PlayerConnection.chat(PlayerConnection.java:1319)
    04.06 17:06:40 [Server] WARN at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1257)
     

    Code :
    Code (Text):
    private void send(AsyncPlayerChatEvent even, Player player, Player target) {
            try {
                // Send text (Json test lol)
                String json = main.getConfigManager().getConfig().getString("ChatSetting.format");

                // Placeholder thing
                if (PlaceholderAPI.containsPlaceholders(json)) {
                    json = PlaceholderAPI.setPlaceholders(player, json);
                }
                // Setup the message
                Clan clan = main.getClanManager().getPlayerClan(player);
                if (clan != null) {
                    if (clan.isChatColorEnabled()) {
                        if (!clan.getChatColor().equals(ChatColor.WHITE.toString())) {
                            json = json.replaceAll("%message%", ChatColor.valueOf(clan.getChatColor()) + even.getMessage());
                        } else {
                            json = json.replaceAll("%message%", even.getMessage());
                        }
                    } else {
                        json = json.replaceAll("%message%",  even.getMessage());
                    }
                } else {
                    json = json.replaceAll("%message%",  even.getMessage());
                }
                // Send the message
                final IChatBaseComponent icbc = IChatBaseComponent.ChatSerializer.a(ChatColor.translateAlternateColorCodes('&', json));
                final PacketPlayOutChat chat = new PacketPlayOutChat(icbc, ChatMessageType.CHAT);
                ((CraftPlayer)target).getHandle().playerConnection.sendPacket(chat);
            }
            catch (Exception e) {
                // Ignore
                for (Player lol : even.getRecipients()) {
                    lol.sendMessage(lol.getName() + " : " + e.getMessage());
                }
                even.getPlayer().sendMessage("FATAL ERROR (THIS WILL CRASH THE SERVER). Bilang ke owner secepatnya! @AnomSuryadinata / @DrOreo002");
                e.getMessage();
                e.printStackTrace();
            }
        }
     
  2. What are line 435 and 394 of ChatListenerBukkit?
     
  3. Code (Text):

    final IChatBaseComponent icbc = IChatBaseComponent.ChatSerializer.a(ChatColor.translateAlternateColorCodes('&', json));
    final PacketPlayOutChat chat = new PacketPlayOutChat(icbc, ChatMessageType.CHAT);
    ((CraftPlayer)target).getHandle().playerConnection.sendPacket(chat);
     
    prob on one of those. Not sure, the line on the error log is different on my IDE
     
  4. Can you show us the json?
     
  5. Code (Text):
      format: '["",{"text":"%oreoclan_player_clan_label%"},{"text":"&7[ &r"},{"text":"%oreoclan_player_clan_prefix%","hoverEvent":{"action":"show_text","value":"%oreoclan_request_join_hover%"},"clickEvent":{"action":"suggest_command","value":"%oreoclan_request_join_click%"}},{"text":" &7] &7| "},{"text":"%luckperms_prefix%","hoverEvent":{"action":"show_text","value":"&fPlayer ini mempunyai rank : %luckperms_prefix%&f, mau?. &6/donate"}},{"text":"%essentials_nickname%","clickEvent":{"action":"suggest_command","value":"/msg %player_name% Hello World"},"hoverEvent":{"action":"show_text","value":"Klik untuk &cmessage &fplayer ini!"}},{"text":" ","hoverEvent":{"action":"show_text","value":"Klik untuk message"}},{"text":"&7➟ &r%message%"}]'
     
  6. BUMP. I need help here
     
  7. Simply throw it in a validator. This time I went ahead and did it for you. Try this
    Code (json (Unknown Language)):

    [
       {
          "text":"%oreoclan_player_clan_label%"
       },
       {
          "text":"&7[ &r"
       },
       {
          "text":"%oreoclan_player_clan_prefix%",
          "hoverEvent":{
             "action":"show_text",
             "value":"%oreoclan_request_join_hover%"
          },
          "clickEvent":{
             "action":"suggest_command",
             "value":"%oreoclan_request_join_click%"
          }
       },
       {
          "text":" &7] &7| "
       },
       {
          "text":"%luckperms_prefix%",
          "hoverEvent":{
             "action":"show_text",
             "value":"&fPlayer ini mempunyai rank : %luckperms_prefix%&f, mau?. &6/donate"
          }
       },
       {
          "text":"%essentials_nickname%",
          "clickEvent":{
             "action":"suggest_command",
             "value":"/msg %player_name% Hello World"
          },
          "hoverEvent":{
             "action":"show_text",
             "value":"Klik untuk &cmessage &fplayer ini!"
          }
       },
       {
          "text":" ",
          "hoverEvent":{
             "action":"show_text",
             "value":"Klik untuk message"
          }
       },
       {
          "text":"&7➟ &r%message%"
       }
    ]
     
     
  8. Hmmm. I'll try it, I'll reply here if its working. Ty for your help ;)
     
  9. So I need to remove the ' ' right?

    Edit NOPE : Its not. I got an error because it didn't contain ' '. Do I need to make it so the plugin will load it from .json file??
     
    #10 DrOreo002, Jun 18, 2018
    Last edited: Jun 18, 2018
  10. I dont really get the format prefix in the first place. But what I posted is valid JSON and should work fine. You might be parsing it wrongly.
     
  11. I changed the way to send the json message btw. I use this
    Code (Text):
    target.spigot().sendMessage(ComponentSerializer.parse(json));
     
  12. Okay, I think I know the problem. Its not because there's something wrong with the json formatting. Its because I'm replacing the %message% with the chat event's message. And then parse it to json.

    Soo, I need to know how to convert basecomponent to text component. And then I can use TextComponent#addExtra method.

    Edit. Nvm Its solved now, seems like I didn't get the error anymore. Ty for the help ;)
     
    #13 DrOreo002, Jun 19, 2018
    Last edited: Jun 19, 2018
    • Like Like x 1