Solved Problems with sending titles

Discussion in 'Plugin Development' started by Mr. Sandwich, Jun 17, 2016.

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

    Mr. Sandwich

    Code:
    sendTitle(player, "Test", "Test", 1, 20, 1);
    
    
        public static void sendTitle(Player player, String title, String subtitle, int fadeIn, int stay, int fadeOut) {
            CraftPlayer craftplayer = (CraftPlayer) player;
            PlayerConnection connection = craftplayer.getHandle().playerConnection;
            IChatBaseComponent titleJSON = ChatSerializer.a("{'text': '" + title + "'}");
            IChatBaseComponent subtitleJSON = ChatSerializer.a("{'text': '" + subtitle + "'}");
            PacketPlayOutTitle titlePacket = new PacketPlayOutTitle(EnumTitleAction.TITLE, titleJSON, fadeIn, stay, fadeOut);
            PacketPlayOutTitle subtitlePacket = new PacketPlayOutTitle(EnumTitleAction.SUBTITLE, subtitleJSON);
            connection.sendPacket(titlePacket);
            connection.sendPacket(subtitlePacket);
        }
    
    This code isn't working ^^^ Error Log:
    Code:
    [14:09:21 WARN]: [Speedsters] Task #16 for Speedsters v1.0 generated an exception
    com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 3
            at com.google.gson.internal.Streams.parse(Streams.java:56) ~[spigot.jar:git-Spigot-6016ac7-10c10b3]
            at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:54) ~[spigot.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.ChatDeserializer.a(SourceFile:501) ~[spigot.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.ChatDeserializer.a(SourceFile:512) ~[spigot.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.IChatBaseComponent$ChatSerializer.a(SourceFile:205) ~[spigot.jar:git-Spigot-6016ac7-10c10b3]
            at me.xXMrSandwichXx.Speedsters.Main.sendTitle(Main.java:2482) ~[?:?]
            at me.xXMrSandwichXx.Speedsters.Main$1.run(Main.java:338) ~[?:?]
            at org.bukkit.craftbukkit.v1_10_R1.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot.jar:git-Spigot-6016ac7-10c10b3]
            at org.bukkit.craftbukkit.v1_10_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [spigot.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:741) [spigot.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672) [spigot.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571) [spigot.jar:git-Spigot-6016ac7-10c10b3]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 3
            at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1505) ~[spigot.jar:git-Spigot-6016ac7-10c10b3]
            at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1386) ~[spigot.jar:git-Spigot-6016ac7-10c10b3]
            at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:488) ~[spigot.jar:git-Spigot-6016ac7-10c10b3]
            at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:403) ~[spigot.jar:git-Spigot-6016ac7-10c10b3]
            at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:666) ~[spigot.jar:git-Spigot-6016ac7-10c10b3]
            at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:642) ~[spigot.jar:git-Spigot-6016ac7-10c10b3]
            at com.google.gson.internal.Streams.parse(Streams.java:44) ~[spigot.jar:git-Spigot-6016ac7-10c10b3]
            ... 13 more
     
    Last edited by a moderator: Jun 17, 2016
  2. Offline

    Lordloss

    Something is wrong with your JSON syntax. The easyest way to solve this is a title generator, where you put in the text you want and it spits the formatted code out. There are online generators which can be found easily on google. A very lazy but working way would be Bukkit.dispatchCommand() the /title command.
     
  3. Offline

    Mr. Sandwich

    I feel like running a command from console is kind of cheap and cheating so I prefer not use to it..
    Anyone else?
     
  4. Offline

    I Al Istannen

    @Mr. Sandwich
    If you could say which line "Main.java:2482" (and, awful lot of lines in one class. Consider splitting in in multiple ones) corresponds to, you could see which JSON is wrong. Then you use an online generator (mincraft.science might have one) and copy-paste the resulting JSON. Then make the message configurable again. But since you already use NMS, the "ChatComponentText" (class) might automatically convert your message to valid JSON. Worked for books, don't know if it is applicable here.
     
  5. Offline

    Mr. Sandwich

    This is the line
    Code:
    IChatBaseComponent titleJSON = ChatSerializer.a("{'text': '" + title + "'}");
    
    Also I tried copying the resulting JSON and it didnt work
     
  6. Offline

    WolfMage1

    tried using \" instead of '
     
  7. Offline

    Mr. Sandwich

    same error message
     
  8. Offline

    WolfMage1

    Are you running the same bukkit version on your server as the version you imported?
     
  9. Offline

    Mr. Sandwich

    yes
     
  10. Offline

    Mr. Sandwich

  11. Offline

    I Al Istannen

    @Mr. Sandwich
    Have you tried using this as the JSON?
    "new ChatComponentText(<your text, can have colors and formats (e.g. §2hey§3§lLol")>)"
     
  12. Offline

    Mr. Sandwich

    I am new to this part of coding, can you show an example of what you mean? I tried
    Code:
        public static void sendTitle(Player player, String title, String subtitle, int fadeIn, int stay, int fadeOut) {
            CraftPlayer craftplayer = (CraftPlayer) player;
            PlayerConnection connection = craftplayer.getHandle().playerConnection;
            ChatComponentText titleJSON = new ChatComponentText("{/text/: /" + title + "/}");
            ChatComponentText subtitleJSON = new ChatComponentText("{/text/: /" + subtitle + "/}");
            PacketPlayOutTitle titlePacket = new PacketPlayOutTitle(EnumTitleAction.TITLE, titleJSON, fadeIn, stay, fadeOut);
            PacketPlayOutTitle subtitlePacket = new PacketPlayOutTitle(EnumTitleAction.SUBTITLE, subtitleJSON);
            connection.sendPacket(titlePacket);
            connection.sendPacket(subtitlePacket);
        }
    
    and it's not working
     
  13. Offline

    I Al Istannen

    @Mr. Sandwich
    new PacketPlayOutTitle(EnumTitleAction.TITLE, new ChatComponentText(ChatColor.GOLD + "Cool one"), fade in out and this stuff
     
  14. Offline

    Mr. Sandwich

    1. I was just exporting it wrong in my previous comment it does work
    2. but still the fadeIn, stay and fadeOut are on default mode so it still instead of displaying for like a second it displays it for a couple of seconds
     
    Last edited: Jun 21, 2016
  15. Offline

    I Al Istannen

    @Mr. Sandwich
    You need to send a TIMES packet first. This will set the time the title will stay. To do this, use this constructor:
    new PacketPlayOutTitle(int fadeInTime, int displayTime, int fadeOutTime)

    EDIT: I will explain it a bit further:
    There are two main types of this Packet. The TIMES one and the TITLE/SUBTITLE. The TIMES will just tell the client how long to display and the other two will tell WHAT to display. You can leave the fade in out and stay from the TITLE and SUBTITLE constructor.
     
  16. Offline

    Mr. Sandwich

    But isn't it exactly what I did?
     
  17. Offline

    I Al Istannen

  18. Offline

    Mr. Sandwich

    Oh I read your reply wrong sorry, yeah it works now thanks. Solved.
     
  19. Offline

    I Al Istannen

    @Mr. Sandwich
    Glad it works and I could help :)

    Good luck with it!
     
Thread Status:
Not open for further replies.

Share This Page