Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.02 KB | None | 0 0
  1.  
  2. import org.bukkit.Location;
  3. import org.bukkit.entity.Player;
  4.  
  5. import java.lang.reflect.Constructor;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. /**
  10.  * Created by Jakeeeee
  11.  * https://bukkit.org/members/jakeeeee.91110457/
  12.  */
  13. public class Hologram {
  14.  
  15.     private ArrayList<Object> entites = new ArrayList();
  16.     private List<String> lines = new ArrayList();
  17.     private Location location;
  18.  
  19.     public Hologram(ArrayList<String> lines, Location location) {
  20.         this.lines = lines;
  21.         this.location = location;
  22.     }
  23.  
  24.     public void sendHologram(Player... players) {
  25.  
  26.         try {
  27.  
  28.             Constructor packetConstructor = Reflection.getNMSClass("PacketPlayOutSpawnEntityLiving")
  29.                     .getConstructor(Reflection.getNMSClass("EntityLiving"));
  30.  
  31.  
  32.             Object[] packets = new Object[entites.size()];
  33.             int counter = 0;
  34.             for (Object entity : entites) {
  35.                 packets[counter] = packetConstructor.newInstance(entity);
  36.                 counter++;
  37.             }
  38.  
  39.  
  40.             for (Player player : players) {
  41.  
  42.                 for (Object packet : packets)
  43.                     Reflection.sendPacket(player, packet);
  44.  
  45.             }
  46.         } catch (Exception e) {
  47.             e.printStackTrace();
  48.         }
  49.     }
  50.  
  51.     public void hideHologram(Player... players) {
  52.         try {
  53.  
  54.             Constructor<?> packetConstructor = Reflection.getNMSClass("PacketPlayOutEntityDestroy").getConstructor(int[].class);
  55.  
  56.  
  57.             Object[] packets = new Object[entites.size()];
  58.             int counter = 0;
  59.             for (Object entity : entites) {
  60.                 Object entityID = entity.getClass().getMethod("getId").invoke(entity);
  61.                 packets[counter] = packetConstructor.newInstance(new int[]{(int) entityID});
  62.                 counter++;
  63.             }
  64.  
  65.             for (Player player : players) {
  66.  
  67.                 for (Object packet : packets)
  68.                     Reflection.sendPacket(player, packet);
  69.  
  70.             }
  71.         } catch (Exception e) {
  72.             e.printStackTrace();
  73.         }
  74.  
  75.  
  76.     }
  77.  
  78.     public Hologram addLine(String line) {
  79.         lines.add(line);
  80.         return this;
  81.     }
  82.  
  83.     public Hologram addLine(String line, int index) {
  84.         lines.add(index, line);
  85.         return this;
  86.     }
  87.  
  88.     public Hologram removeLine(String line) {
  89.         lines.remove(line);
  90.         return this;
  91.     }
  92.  
  93.     public Hologram removeLine(int index) {
  94.         lines.remove(index);
  95.         return this;
  96.     }
  97.  
  98.     public Hologram rebuild() {
  99.         entites.clear();
  100.         build();
  101.         return this;
  102.     }
  103.  
  104.     public Hologram build() {
  105.         try {
  106.  
  107.             Constructor entity = Reflection.getNMSClass("EntityArmorStand")
  108.                     .getConstructor(Reflection.getNMSClass("World"));
  109.  
  110.  
  111.             Object world = Reflection.getCraftbukkitClass("CraftWorld").cast(location.getWorld());
  112.  
  113.             for (String line : lines) {
  114.  
  115.                 Object entityStand = entity.newInstance(world.getClass().getMethod("getHandle").invoke(world));
  116.  
  117.                 entityStand.getClass().getMethod("setCustomName", String.class).invoke(entityStand, line);
  118.                 entityStand.getClass().getMethod("setCustomNameVisible", boolean.class).invoke(entityStand, true);
  119.                 entityStand.getClass().getMethod("setInvisible", boolean.class).invoke(entityStand, true);
  120.                 entityStand.getClass().getMethod("setGravity", boolean.class).invoke(entityStand, false);
  121.                 entityStand.getClass().getMethod("setLocation", double.class, double.class, double.class, float.class, float.class)
  122.                         .invoke(entityStand, location.getX(), location.getY(), location.getZ(), 0.0F, 0.0F);
  123.                 entites.add(entityStand);
  124.  
  125.                 location.subtract(0, 0.25, 0);
  126.  
  127.  
  128.             }
  129.  
  130.             location.add(0, lines.size() * 0.25, 0);
  131.  
  132.         } catch (Exception e) {
  133.             e.printStackTrace();
  134.         }
  135.         return this;
  136.     }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement