Error constructor to send title

Discussion in 'Spigot Plugin Help' started by ferith, Mar 4, 2020.

  1. Hello, i have this error in my server consol :
    Code (Text):
    [12:15:12 WARN]: java.lang.ArrayIndexOutOfBoundsException: 0
    [12:15:12 WARN]: at TierCraft.Minigame.SkyGiant.plugin.utils.Titles.sendTitle(Titles.java:88)
    [12:15:12 WARN]: at TierCraft.Minigame.SkyGiant.plugin.utils.Titles.sendSubtitle(Titles.java:25)
    [12:15:12 WARN]: at TierCraft.Minigame.SkyGiant.plugin.runnables.Lobby.run(Lobby.java:67)
    [12:15:12 WARN]: at org.bukkit.craftbukkit.v1_8_R1.scheduler.CraftTask.run(CraftTask.java:71)
    [12:15:12 WARN]: at org.bukkit.craftbukkit.v1_8_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350)
    [12:15:12 WARN]: at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:709)
    [12:15:12 WARN]: at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316)
    [12:15:12 WARN]: at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634)
    [12:15:12 WARN]: at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537)
    [12:15:12 WARN]: at java.lang.Thread.run(Thread.java:748)
    [12:15:13 WARN]: java.lang.ArrayIndexOutOfBoundsException: 0
    and my code is :
    Code (Java):
    public class Titles
    {
     
        public static void sendJustTitle( Player player,  Integer n,  Integer n2,  Integer n3,  String s) {
            sendTitle(player, n, n2, n3, s, null);
        }
       
        public static void sendSubtitle( Player player,  Integer n,  Integer n2,  Integer n3,  String s) {
            sendTitle(player, n, n2, n3, null, s);
        }
       
        public static void sendFullTitle( Player player,  Integer n,  Integer n2,  Integer n3,  String s,  String s2) {
            sendTitle(player, n, n2, n3, s, s2);
        }
       
       
        public static Integer getPlayerProtocol( Player player) {
            return 47;
        }
       
        public static void sendPacket(Player player,  Object o) {
            try {
                Object handle = player.getClass().getMethod("getHandle").invoke(player);
                Object value = handle.getClass().getField("playerConnection").get(handle);
                value.getClass().getMethod("sendPacket", getNMSClass("Packet")).invoke(value, o);
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
        }
       
        public static Class<?> getNMSClass( String s) {
            String s2 = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
            try {
                return Class.forName("net.minecraft.server." + s2 + "." + s);
            }
            catch (ClassNotFoundException ex) {
                ex.printStackTrace();
                return null;
            }
        }
       
     


        public static void sendTitle(Player player, int fadeIn, int showTime, int fadeOut, String title, String subtitle) {
            try {
                Class<?> PacketPlayClass = getNMSClass("PacketPlayOutTitle");
               
                Class<?> IChatClass = getNMSClass("IChatBaseComponent");
                Constructor<?> titleConstructor= PacketPlayClass.getConstructor(PacketPlayClass.getDeclaredClasses()[0], IChatClass, int.class, int.class, int.class);
                if(title!=null) {
                   
                    Object enumTitle = PacketPlayClass.getDeclaredClasses()[0].getField("TITLE").get(null);
                    Object titlechat = IChatClass.getDeclaredClasses()[0].getMethod("a", String.class).invoke(null, "{\"text\":\"" + title + "\"}" );
                   
                    Object titlePacket = titleConstructor.newInstance(enumTitle, titlechat,fadeIn,showTime,fadeOut);
                    sendPacket(player, titlePacket);
                }
                if(subtitle != null) {
                    Object enumSubtitle = PacketPlayClass.getDeclaredClasses()[0].getField("SUBTITLE").get(null);
                    Object subtitlechat = IChatClass.getDeclaredClasses()[0].getMethod("a", String.class).invoke(null, "{\"text\":\"" + subtitle + "\"}" );
                   
                    Object subtitlePacket = titleConstructor.newInstance(enumSubtitle, subtitlechat,fadeIn,showTime,fadeOut);
                    sendPacket(player, subtitlePacket);
                }
               
               
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    the line 88 is : Constructor<?> titleConstructor= PacketPlayClass.getConstructor(PacketPlayClass.getDeclaredClasses()[0], IChatClass, int.class, int.class, int.class);

    Can you help me please !!
     
  2. i dev on a plugin in 1.8 and i want try update with reflection
     
  3. On send title it seems like the issue is that a list of whatever you are trying to send is empty causing the error to fire in the try and catch statement.