Solved Title

Discussion in 'Spigot Plugin Development' started by Vrsd12, May 11, 2017.

  1. I found this code, how can I use it?
    I know it starts with send()
    Code (Text):
    package me.gabri;

    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;

    public class Title
    {
      private Class<?> packetTitle;
      private Class<?> packetActions;
      private Class<?> nmsChatSerializer;
      private Class<?> chatBaseComponent;
      private String title = "";
      private ChatColor titleColor = ChatColor.WHITE;
      private String subtitle = "";
      private ChatColor subtitleColor = ChatColor.WHITE;
      private int fadeInTime = -1;
      private int stayTime = -1;
      private int fadeOutTime = -1;
      private boolean ticks = false;
      private static final Map<Class<?>, Class<?>> CORRESPONDING_TYPES = new HashMap();
     
      public Title(String paramString)
      {
        this.title = paramString;
        loadClasses();
      }
     
      public Title(String paramString1, String paramString2)
      {
        this.title = paramString1;
        this.subtitle = paramString2;
        loadClasses();
      }
     
      public Title(Title paramTitle)
      {
        this.title = paramTitle.title;
        this.subtitle = paramTitle.subtitle;
        this.titleColor = paramTitle.titleColor;
        this.subtitleColor = paramTitle.subtitleColor;
        this.fadeInTime = paramTitle.fadeInTime;
        this.fadeOutTime = paramTitle.fadeOutTime;
        this.stayTime = paramTitle.stayTime;
        this.ticks = paramTitle.ticks;
        loadClasses();
      }
     
      public Title(String paramString1, String paramString2, int paramInt1, int paramInt2, int paramInt3)
      {
        this.title = paramString1;
        this.subtitle = paramString2;
        this.fadeInTime = paramInt1;
        this.stayTime = paramInt2;
        this.fadeOutTime = paramInt3;
        loadClasses();
      }
     
      private void loadClasses()
      {
        this.packetTitle = getNMSClass("PacketPlayOutTitle");
        this.packetActions = getNMSClass("PacketPlayOutTitle$EnumTitleAction");
        this.chatBaseComponent = getNMSClass("IChatBaseComponent");
        this.nmsChatSerializer = getNMSClass("IChatBaseComponent$ChatSerializer");
      }
     
      public void setTitle(String paramString)
      {
        this.title = paramString;
      }
     
      public String getTitle()
      {
        return this.title;
      }
     
      public void setSubtitle(String paramString)
      {
        this.subtitle = paramString;
      }
     
      public String getSubtitle()
      {
        return this.subtitle;
      }
     
      public void setTitleColor(ChatColor paramChatColor)
      {
        this.titleColor = paramChatColor;
      }
     
      public void setSubtitleColor(ChatColor paramChatColor)
      {
        this.subtitleColor = paramChatColor;
      }
     
      public void setFadeInTime(int paramInt)
      {
        this.fadeInTime = paramInt;
      }
     
      public void setFadeOutTime(int paramInt)
      {
        this.fadeOutTime = paramInt;
      }
     
      public void setStayTime(int paramInt)
      {
        this.stayTime = paramInt;
      }
     
      public void setTimingsToTicks()
      {
        this.ticks = true;
      }
     
      public void setTimingsToSeconds()
      {
        this.ticks = false;
      }
     
      public void send(Player paramPlayer)
      {
        if (this.packetTitle != null)
        {
          resetTitle(paramPlayer);
          try
          {
            Object localObject1 = getHandle(paramPlayer);
            Object localObject2 = getField(localObject1.getClass(),
              "playerConnection").get(localObject1);
            Object[] arrayOfObject = this.packetActions.getEnumConstants();
            Method localMethod = getMethod(localObject2.getClass(),
              "sendPacket", new Class[0]);
            Object localObject3 = this.packetTitle.getConstructor(new Class[] { this.packetActions,
              this.chatBaseComponent, Integer.TYPE, Integer.TYPE,
              Integer.TYPE }).newInstance(new Object[] {arrayOfObject[2], null,
              Integer.valueOf(this.fadeInTime * (this.ticks ? 1 : 20)),
              Integer.valueOf(this.stayTime * (this.ticks ? 1 : 20)),
              Integer.valueOf(this.fadeOutTime * (this.ticks ? 1 : 20)) });
            if ((this.fadeInTime != -1) && (this.fadeOutTime != -1) && (this.stayTime != -1)) {
              localMethod.invoke(localObject2, new Object[] { localObject3 });
            }
            Object localObject4 = getMethod(this.nmsChatSerializer, "a", new Class[] {
              String.class }).invoke(
              null, new Object[] {
              "{text:\"" +
              ChatColor.translateAlternateColorCodes('&',
              this.title) + "\",color:" +
              this.titleColor.name().toLowerCase() + "}" });
            localObject3 = this.packetTitle.getConstructor(new Class[] { this.packetActions,
              this.chatBaseComponent }).newInstance(new Object[] {arrayOfObject[0], localObject4 });
            localMethod.invoke(localObject2, new Object[] { localObject3 });
            if (this.subtitle != "")
            {
              localObject4 =
                getMethod(this.nmsChatSerializer, "a", new Class[] { String.class }).invoke(null, new Object[] {
                "{text:\"" +
               
                ChatColor.translateAlternateColorCodes(
                '&', this.subtitle) +
                "\",color:" +
                this.subtitleColor.name()
                .toLowerCase() + "}" });
              localObject3 = this.packetTitle.getConstructor(new Class[] { this.packetActions,
                this.chatBaseComponent }).newInstance(new Object[] {arrayOfObject[1],
                localObject4 });
              localMethod.invoke(localObject2, new Object[] { localObject3 });
            }
          }
          catch (Exception localException)
          {
            localException.printStackTrace();
          }
        }
      }
     
        public void broadcast() {
            final Iterator<Player> iterator = (Iterator<Player>) Bukkit.getOnlinePlayers().iterator();
            while (iterator.hasNext()) {
                this.send(iterator.next());
            }
        }
     
      public void clearTitle(Player paramPlayer)
      {
        try
        {
          Object localObject1 = getHandle(paramPlayer);
          Object localObject2 = getField(localObject1.getClass(), "playerConnection")
            .get(localObject1);
          Object[] arrayOfObject = this.packetActions.getEnumConstants();
          Method localMethod = getMethod(localObject2.getClass(), "sendPacket", new Class[0]);
          Object localObject3 = this.packetTitle.getConstructor(new Class[] { this.packetActions,
            this.chatBaseComponent }).newInstance(new Object[] {arrayOfObject[3], null });
          localMethod.invoke(localObject2, new Object[] { localObject3 });
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
        }
      }
     
      public void resetTitle(Player paramPlayer)
      {
        try
        {
          Object localObject1 = getHandle(paramPlayer);
          Object localObject2 = getField(localObject1.getClass(), "playerConnection")
            .get(localObject1);
          Object[] arrayOfObject = this.packetActions.getEnumConstants();
          Method localMethod = getMethod(localObject2.getClass(), "sendPacket", new Class[0]);
          Object localObject3 = this.packetTitle.getConstructor(new Class[] { this.packetActions,
            this.chatBaseComponent }).newInstance(new Object[] {arrayOfObject[4], null });
          localMethod.invoke(localObject2, new Object[] { localObject3 });
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
        }
      }
     
      private Class<?> getPrimitiveType(Class<?> paramClass)
      {
        return CORRESPONDING_TYPES.containsKey(paramClass) ?
          (Class)CORRESPONDING_TYPES.get(paramClass) : paramClass;
      }
     
      private Class<?>[] toPrimitiveTypeArray(Class<?>[] paramArrayOfClass)
      {
        int i = paramArrayOfClass != null ? paramArrayOfClass.length : 0;
        Class[] arrayOfClass = new Class[i];
        for (int j = 0; j < i; j++) {
          arrayOfClass[j] = getPrimitiveType(paramArrayOfClass[j]);
        }
        return arrayOfClass;
      }
     
      private static boolean equalsTypeArray(Class<?>[] paramArrayOfClass1, Class<?>[] paramArrayOfClass2)
      {
        if (paramArrayOfClass1.length != paramArrayOfClass2.length) {
          return false;
        }
        for (int i = 0; i < paramArrayOfClass1.length; i++) {
          if ((!paramArrayOfClass1[i].equals(paramArrayOfClass2[i])) && (!paramArrayOfClass1[i].isAssignableFrom(paramArrayOfClass2[i]))) {
            return false;
          }
        }
        return true;
      }
     
      private Object getHandle(Object paramObject)
      {
        try
        {
          return getMethod("getHandle", paramObject.getClass(), new Class[0]).invoke(paramObject, new Object[0]);
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
        }
        return null;
      }
     
      private Method getMethod(String paramString, Class<?> paramClass, Class<?>... paramVarArgs)
      {
        Class[] arrayOfClass1 = toPrimitiveTypeArray(paramVarArgs);
        Method[] arrayOfMethod;
        int j = (arrayOfMethod = paramClass.getMethods()).length;
        for (int i = 0; i < j; i++)
        {
          Method localMethod = arrayOfMethod[i];
          Class[] arrayOfClass2 = toPrimitiveTypeArray(localMethod.getParameterTypes());
          if ((localMethod.getName().equals(paramString)) && (equalsTypeArray(arrayOfClass2, arrayOfClass1))) {
            return localMethod;
          }
        }
        return null;
      }
     
      private String getVersion()
      {
        String str1 = Bukkit.getServer().getClass().getPackage().getName();
        String str2 = str1.substring(str1.lastIndexOf('.') + 1) + ".";
        return str2;
      }
     
      private Class<?> getNMSClass(String paramString)
      {
        String str = "net.minecraft.server." + getVersion() + paramString;
        Class localClass = null;
        try
        {
          localClass = Class.forName(str);
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
        }
        return localClass;
      }
     
      private Field getField(Class<?> paramClass, String paramString)
      {
        try
        {
          Field localField = paramClass.getDeclaredField(paramString);
          localField.setAccessible(true);
          return localField;
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
        }
        return null;
      }
     
      private Method getMethod(Class<?> paramClass, String paramString, Class<?>... paramVarArgs)
      {
        Method[] arrayOfMethod;
        int j = (arrayOfMethod = paramClass.getMethods()).length;
        for (int i = 0; i < j; i++)
        {
          Method localMethod = arrayOfMethod[i];
          if ((localMethod.getName().equals(paramString)) && (
            (paramVarArgs.length == 0) ||
            (ClassListEqual(paramVarArgs, localMethod.getParameterTypes()))))
          {
            localMethod.setAccessible(true);
            return localMethod;
          }
        }
        return null;
      }
     
      private boolean ClassListEqual(Class<?>[] paramArrayOfClass1, Class<?>[] paramArrayOfClass2)
      {
        boolean bool = true;
        if (paramArrayOfClass1.length != paramArrayOfClass2.length) {
          return false;
        }
        for (int i = 0; i < paramArrayOfClass1.length; i++) {
          if (paramArrayOfClass1[i] != paramArrayOfClass2[i])
          {
            bool = false;
            break;
          }
        }
        return bool;
      }
    }
     
     
  2. you must create the object with the specified paramters and execute the send method. ex:
    Code (Java):
    new Title(params).send(param);
    suggest looking at some java tutorials, before grabbing code which makes no sense. ;)
     
    • Agree Agree x 1
  3. Is it your code?
     
  4. new Title("asd").send(player);
    Thanks bro
    ChefJava