Solved Using Titles(The Big Text On The Screen)

Discussion in 'Plugin Development' started by DominicGamesHD, Jun 5, 2016.

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

    DominicGamesHD

    How would I make big text on the screen
     
  2. Offline

    A5H73Y

  3. Offline

    DominicGamesHD

    @A5H73Y Thanks but I am really just looking for an example code :p
     
  4. @DominicGamesHD
    There is not need of that plugin.

    Here's the code.
    Code:
     
        public void sendPacket(Player player, Object packet) {
            try {
                    Object handle = player.getClass().getMethod("getHandle").invoke(player);
                    Object playerConnection = handle.getClass().getField("playerConnection").get(handle);
                    playerConnection.getClass().getMethod("sendPacket", getNMSClass("Packet")).invoke(playerConnection, packet);
            }
           
            catch (Exception e) {
                    e.printStackTrace();
            }
    }
    
    public Class<?> getNMSClass(String name) {
            // org.bukkit.craftbukkit.v1_8_R3...
            String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
            try {
                    return Class.forName("net.minecraft.server." + version + "." + name);
            }
           
            catch (ClassNotFoundException e) {
                    e.printStackTrace();
                    return null;
            }
    }
        public void sendTitle(Player player, String title, String subtitle, int fadeIn, int stay, int fadeOut) {
            try {
                Object enumTitle = getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0].getField("TITLE").get(null);
                Object enumSubTitle = getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0].getField("SUBTITLE").get(null);
                Object chat = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", String.class).invoke(null, "{\"text\":\""+title+"\"}");
                Object subchat = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", String.class).invoke(null, "{\"text\":\""+subtitle+"\"}");
                Constructor<?> titleConstructor = getNMSClass("PacketPlayOutTitle").getConstructor(getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0], getNMSClass("IChatBaseComponent"), int.class, int.class, int.class);
                Object packet = titleConstructor.newInstance(enumTitle, chat, fadeIn, stay, fadeOut);
                Object packet2 = titleConstructor.newInstance(enumSubTitle, chat, fadeIn, stay, fadeOut);
                sendPacket(player, packet);
                sendPacket(player, packet2);
        }
    
    Example of usage
    Code:
    sendTitle(player, "Title", "subtitle,leave blank for nothing", 10, 20, 10);
    If you don't know what this fadeIn, stay and fadeOut mean, maybe this can help you.
    http://minecraft.gamepedia.com/Commands#title


    EDIT: I have updated the code
     
    Last edited: Jun 5, 2016
  5. Offline

    A5H73Y

    I must remind you, your plugin will break as soon as you try it on a different version.
     
  6. Offline

    Xerox262

    @A5H73Y He could use reflection to bypass that, assuming they don't change constructors
     
  7. Offline

    DominicGamesHD

    @Xerox262 Where can I find out about this reflection thing?
     
  8. Offline

    Xerox262

    @Minecraft1o1 It'll work unless they change the constructors, the method constructors, or the method names

    @DominicGamesHD Reflection is what @Minecraft1o1 posted above, you might want to find it else where though, there are tutorials in the resource sections that actually explain what does what.
     
  9. Offline

    Mr. Sandwich


    I dont know why when I change the fade in/fade out/time the text stays, it doesnt work, it just keeps them the default way(with the default fade in, default fade out and the default time the text says
    [/code]
    Code:
    sendTitle(player, "Title", "subtitle,leave blank for nothing", 10, 20, 10);
     
  10. I have put the reflection to it. Maybe it isn't working.
    This is the code you put in your plugin, but it won't work on all versions. You will need to update your imports.
    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;
            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);
        }
     
  11. Offline

    Xerox262

    @Mr. Sandwich What did you try changing it to, last time I messed with reflection the fade in/out worked, you know it's in ticks, so 20 mean 1 second
     
  12. Offline

    DominicGamesHD

    I am going to mark this as solved now because I have it working :)
     
  13. Offline

    Mr. Sandwich

    I know
    and @Minecraft1o1 this method doesnt work, here's the error log:
    Code:
    [10:37:14 ERROR]: Could not pass event PlayerMoveEvent to Speedsters v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.PlayerConnection.a(PlayerConnection.java:607) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.PacketPlayInFlying.a(SourceFile:126) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.PacketPlayInFlying$PacketPlayInPosition.a(SourceFile:57) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_91]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
            at net.minecraft.server.v1_9_R2.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:726) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [spigot.jar:git-Spigot-4af49dc-23da8b0]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: 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-4af49dc-23da8b0]
            at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:54) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.ChatDeserializer.a(SourceFile:501) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.ChatDeserializer.a(SourceFile:512) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.IChatBaseComponent$ChatSerializer.a(SourceFile:205) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at me.xXMrSandwichXx.Speedsters.Main.sendTitle(Main.java:2294) ~[?:?]
            at me.xXMrSandwichXx.Speedsters.Main.onPlayerMove(Main.java:1075) ~[?:?]
            at sun.reflect.GeneratedMethodAccessor47.invoke(Unknown Source) ~[?:?]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_91]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            ... 15 more
    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-4af49dc-23da8b0]
            at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1386) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:488) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:403) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:666) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:642) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at com.google.gson.internal.Streams.parse(Streams.java:44) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:54) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.ChatDeserializer.a(SourceFile:501) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.ChatDeserializer.a(SourceFile:512) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at net.minecraft.server.v1_9_R2.IChatBaseComponent$ChatSerializer.a(SourceFile:205) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            at me.xXMrSandwichXx.Speedsters.Main.sendTitle(Main.java:2294) ~[?:?]
            at me.xXMrSandwichXx.Speedsters.Main.onPlayerMove(Main.java:1075) ~[?:?]
            at sun.reflect.GeneratedMethodAccessor47.invoke(Unknown Source) ~[?:?]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_91]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_91]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-4af49dc-23da8b0]
            ... 15 more
    
    
     
  14. Offline

    Mr. Sandwich

  15. Offline

    DominicGamesHD

    @Mr. Sandwich Please make another thread as now this is marked solved :p
     
Thread Status:
Not open for further replies.

Share This Page