Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. public class BarUtil {
  2.  
  3. private static Map<String, EntityEnderDragon> dragons = new ConcurrentHashMap<>();
  4.  
  5. public static void setBar(Player p, String text, float healthPercent) {
  6. Location loc = p.getLocation();
  7. WorldServer world = ((CraftWorld) p.getLocation().getWorld()).getHandle();
  8.  
  9. EntityEnderDragon dragon = new EntityEnderDragon(world);
  10. dragon.setLocation(loc.getX(), loc.getY() - 100, loc.getZ(), 0, 0);
  11.  
  12. PacketPlayOutSpawnEntityLiving packet = new PacketPlayOutSpawnEntityLiving(dragon);
  13.  
  14. DataWatcher watcher = new DataWatcher(null);
  15. watcher.a(0, (byte) 0x20);
  16. watcher.a(6, (healthPercent * 200) / 100);
  17. watcher.a(10, text);
  18. watcher.a(2, text);
  19. watcher.a(11, (byte) 1);
  20. watcher.a(3, (byte) 1);
  21.  
  22. try{
  23. Field t = PacketPlayOutSpawnEntityLiving.class.getDeclaredField("l");
  24. t.setAccessible(true);
  25. t.set(packet, watcher);
  26. } catch(Exception ex){
  27. ex.printStackTrace();
  28. }
  29.  
  30. dragons.put(p.getName(), dragon);
  31. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
  32. }
  33.  
  34. public static void removeBar(Player p) {
  35. if(dragons.containsKey(p.getName())) {
  36. PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(dragons.get(p.getName()).getId());
  37. dragons.remove(p.getName());
  38. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
  39. }
  40. }
  41.  
  42. public static void teleportBar(Player p) {
  43. if(dragons.containsKey(p.getName())) {
  44. Location loc = p.getLocation();
  45. PacketPlayOutEntityTeleport packet = new PacketPlayOutEntityTeleport(dragons.get(p.getName()).getId(),
  46. (int) loc.getX() * 32, (int) (loc.getY() - 100) * 32, (int) loc.getZ() * 32,
  47. (byte) ((int) loc.getYaw() * 256 / 360), (byte) ((int) loc.getPitch() * 256 / 360), false);
  48. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
  49. }
  50. }
  51.  
  52. public static void updateText(Player p, String text) {
  53. updateBar(p, text, -1);
  54. }
  55.  
  56. public static void updateHealth(Player p, float healthPercent) {
  57. updateBar(p, null, healthPercent);
  58. }
  59.  
  60. public static void updateBar(Player p, String text, float healthPercent) {
  61. if(dragons.containsKey(p.getName())) {
  62. DataWatcher watcher = new DataWatcher(null);
  63. watcher.a(0, (byte) 0x20);
  64. if (healthPercent != -1) watcher.a(6, (healthPercent * 200) / 100);
  65. if (text != null) {
  66. watcher.a(10, text);
  67. watcher.a(2, text);
  68. }
  69. watcher.a(11, (byte) 1);
  70. watcher.a(3, (byte) 1);
  71.  
  72. PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(dragons.get(p.getName()).getId(), watcher, true);
  73. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
  74. }
  75. }
  76.  
  77. public static Set<String> getPlayers() {
  78. Set<String> set = new HashSet<>();
  79.  
  80. for(Map.Entry<String, EntityEnderDragon> entry : dragons.entrySet()) {
  81. set.add(entry.getKey());
  82. }
  83.  
  84. return set;
  85. }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement