Send title to player - Packets

May 12, 2018
Send title to player - Packets
  • You might be wondering: 'How can I send titles to players and make my plugin work across all versions that support titles?', and the answer is: 'Using NMS.'

    ----
    An alternative is to use md_5's ChatComponent API bundled with the SpigotAPI since many previous versions.
    Code (Java):
    Player player = Bukkit.getPlayer("ExpDev");
    player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new ComponentBuilder("Title to send").color(ChatColor.AQUA).create());
    ----

    Create a class named Reflection that contains these two methods:
    Code (Text):
    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 ex) {
            }
        }

        public  Class<?> getNMSClass(String name) {
            try {
                return Class.forName("net.minecraft.server"
                        + Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3] + "." + name);
            } catch (ClassNotFoundException ex) {
            }
            return null;
        }
    Then, create a class named Title that extends the Reflection class and contains this method:
    Code (Text):
    public void send(Player player, int fadeInTime, int showTime, int fadeOutTime) {
            try {
                Object chatTitle = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", String.class)
                        .invoke(null, "{\"text\": \"" + title + "\"}");
                Constructor<?> titleConstructor = getNMSClass("PacketPlayOutTitle").getConstructor(
                        getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0], getNMSClass("IChatBaseComponent"),
                        int.class, int.class, int.class);
                Object packet = titleConstructor.newInstance(
                        getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0].getField("TITLE").get(null), chatTitle,
                        fadeInTime, showTime, fadeOutTime);
     
                Object chatsTitle = getNMSClass("IChatBaseComponent").getDeclaredClasses()[0].getMethod("a", String.class)
                        .invoke(null, "{\"text\": \"" + subtitle + "\"}");
                Constructor<?> stitleConstructor = getNMSClass("PacketPlayOutTitle").getConstructor(
                        getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0], getNMSClass("IChatBaseComponent"),
                        int.class, int.class, int.class);
                Object spacket = stitleConstructor.newInstance(
                        getNMSClass("PacketPlayOutTitle").getDeclaredClasses()[0].getField("SUBTITLE").get(null), chatsTitle,
                        fadeInTime, showTime, fadeOutTime);

                sendPacket(player, packet);
                sendPacket(player, spacket);
            } catch (Exception ex) {
            }
        }
    This has almost been copied from here:
  • Loading...
  • Loading...