[PT-BR] Send title to player - Packets

May 12, 2018
[PT-BR] Send title to player - Packets
  • Você deve está se perguntando: 'Como eu posso enviar títulos a jogadores, sendo que meu plugin funcione em todas as versões?', e a resposta é: 'Usando NMS.'

    Crie uma classe chamada Reflection que tenha os seguintes métodos:
    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;
        }
    Depois, crie uma classe chamada Title que estenda a classe Reflection e que tenha os seguintes métodos:
    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) {
            }
        }
  • Loading...
  • Loading...