From 56263718d7da5c85118d827ddde39c20b4c5a698 Mon Sep 17 00:00:00 2001 From: Chiss Date: Thu, 11 Sep 2014 13:17:57 +1000 Subject: [PATCH] Fix for BossBar --- .../core/common/util/UtilDisplay.java | 148 ++++++++---------- 1 file changed, 62 insertions(+), 86 deletions(-) diff --git a/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/UtilDisplay.java b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/UtilDisplay.java index 0c12e38cd..8c2777a03 100644 --- a/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/UtilDisplay.java +++ b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/UtilDisplay.java @@ -12,6 +12,7 @@ import net.minecraft.server.v1_7_R4.PacketPlayOutEntityDestroy; import net.minecraft.server.v1_7_R4.PacketPlayOutEntityMetadata; import net.minecraft.server.v1_7_R4.PacketPlayOutSpawnEntityLiving; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.craftbukkit.v1_7_R4.CraftWorld; @@ -20,11 +21,14 @@ import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.util.Vector; public class UtilDisplay { public static final int ENTITY_ID = 777777; - + public static final EntityType ENTITY_TYPE = EntityType.WITHER; + public static final boolean HALF_HEALTH = true; + private static HashMap hasHealthBar = new HashMap(); public static void sendPacket(Player player, Packet packet){ @@ -34,11 +38,12 @@ public class UtilDisplay } //Accessing packets - public static PacketPlayOutSpawnEntityLiving getMobPacket(String text, double healthPercent, Location loc){ + public static PacketPlayOutSpawnEntityLiving getMobPacket(String text, double healthPercent, Location loc) + { PacketPlayOutSpawnEntityLiving mobPacket = new PacketPlayOutSpawnEntityLiving(); mobPacket.a = (int) ENTITY_ID; //Entity ID - mobPacket.b = (byte) EntityType.WITHER.getTypeId(); //Mob type (ID: 64) + mobPacket.b = (byte) ENTITY_TYPE.getTypeId(); //Mob type (ID: 64) mobPacket.c = (int) Math.floor(loc.getBlockX() * 32.0D); //X position mobPacket.d = (int) MathHelper.floor(loc.getBlockY() * 32.0D); //Y position mobPacket.e = (int) Math.floor(loc.getBlockZ() * 32.0D); //Z position @@ -48,31 +53,53 @@ public class UtilDisplay mobPacket.i = (short) 0; //X velocity mobPacket.j = (short) 0; //Y velocity mobPacket.k = (short) 0; //Z velocity - - DataWatcher watcher = getWatcher(text, 101 + healthPercent * 99, loc.getWorld()); + + //Dragon or Wither + double health; + if (ENTITY_TYPE == EntityType.WITHER) + { + if (HALF_HEALTH) + health = healthPercent * 149 + 151; + else + health = healthPercent * 299.9 + 0.1; + } + else + { + if (HALF_HEALTH) + health = healthPercent * 99 + 101; + else + health = healthPercent * 199.9 + 0.1; + } + + DataWatcher watcher = getWatcher(text, health, loc.getWorld()); mobPacket.l = watcher; return mobPacket; } - public static PacketPlayOutEntityDestroy getDestroyEntityPacket(){ + public static PacketPlayOutEntityDestroy getDestroyEntityPacket() + { PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(ENTITY_ID); return packet; } - public static PacketPlayOutEntityMetadata getMetadataPacket(DataWatcher watcher){ + public static PacketPlayOutEntityMetadata getMetadataPacket(DataWatcher watcher) + { PacketPlayOutEntityMetadata metaPacket = new PacketPlayOutEntityMetadata(); metaPacket.a = (int) ENTITY_ID; - try{ + try + { Field b = PacketPlayOutEntityMetadata.class.getDeclaredField("b"); b.setAccessible(true); b.set(metaPacket, watcher.c()); - } catch(Exception e){ + } + catch(Exception e) + { e.printStackTrace(); } @@ -83,16 +110,14 @@ public class UtilDisplay { DataWatcher watcher = new DataWatcher(new DummyEntity(((CraftWorld)world).getHandle())); - health = Math.max(151, health); - watcher.a(0, (Byte) (byte) 0); //Flags, 0x20 = invisible watcher.a(6, (Float) (float) health); watcher.a(2, (String) text); //Entity name watcher.a(10, (String) text); //Entity name watcher.a(3, (Byte) (byte) 0); //Show name, 1 = show, 0 = don't show watcher.a(11, (Byte) (byte) 0); //Show name, 1 = show, 0 = don't show - watcher.a(16, (Integer) (int) health); //Wither health, 200 = full health - watcher.a(20, (Integer) (int) 0); //Wither invuln, 200 = full health + watcher.a(16, (Integer) (int) health); //Wither health, 300 = full health + watcher.a(20, (Integer) (int) 0); //Wither invuln, int i1 = watcher.getInt(0); watcher.watch(0, Byte.valueOf((byte)(i1 | 1 << 5))); @@ -101,93 +126,44 @@ public class UtilDisplay return watcher; } - //Other methods + //Display public static void displayTextBar(JavaPlugin plugin, final Player player, double healthPercent, String text) { PacketPlayOutEntityDestroy destroyEntityPacket = getDestroyEntityPacket(); sendPacket(player, destroyEntityPacket); - PacketPlayOutSpawnEntityLiving mobPacket = getMobPacket(text, healthPercent, player.getEyeLocation().getDirection().multiply(24).toLocation(player.getWorld()).add(player.getLocation())); + Location loc = player.getEyeLocation().add(player.getLocation().getDirection().multiply(18)); + + /* + //Downward + if (player.getLocation().getPitch() > 0) + { + loc = player.getLocation().subtract(0, 10, 0); + } + //Upward + else + { + loc.add(player.getLocation().getDirection().multiply(12)); + loc.add(UtilAlg.getDown(player.getLocation().getDirection()).multiply(12)); + } + */ + + + PacketPlayOutSpawnEntityLiving mobPacket = getMobPacket(text, healthPercent, loc); sendPacket(player, mobPacket); hasHealthBar.put(player.getName(), true); - new BukkitRunnable(){ - @Override - public void run(){ + Bukkit.getServer().getScheduler().runTaskLater(plugin, new Runnable() + { + public void run() + { PacketPlayOutEntityDestroy destroyEntityPacket = getDestroyEntityPacket(); sendPacket(player, destroyEntityPacket); hasHealthBar.put(player.getName(), false); } - }.runTaskLater(plugin, 120L); - } - - public static void displayLoadingBar(final String text, final String completeText, final Player player, final int healthAdd, final long delay, final boolean loadUp, final JavaPlugin plugin){ - PacketPlayOutEntityDestroy destroyEntityPacket = getDestroyEntityPacket(); - - sendPacket(player, destroyEntityPacket); - PacketPlayOutSpawnEntityLiving mobPacket = getMobPacket(text, 0, player.getEyeLocation().getDirection().multiply(24).toLocation(player.getWorld()).add(player.getLocation())); - - sendPacket(player, mobPacket); - hasHealthBar.put(player.getName(), true); - - new BukkitRunnable(){ - int health = (loadUp ? 150 : 300); - - @Override - public void run(){ - if((loadUp ? health < 300 : health > 150)){ - DataWatcher watcher = getWatcher(text, health, player.getWorld()); - PacketPlayOutEntityMetadata metaPacket = getMetadataPacket(watcher); - - sendPacket(player, metaPacket); - - if(loadUp){ - health += healthAdd; - } else { - health -= healthAdd; - } - } else { - DataWatcher watcher = getWatcher(text, (loadUp ? 300 : 150), player.getWorld()); - PacketPlayOutEntityMetadata metaPacket = getMetadataPacket(watcher); - PacketPlayOutEntityDestroy destroyEntityPacket = getDestroyEntityPacket(); - - sendPacket(player, metaPacket); - sendPacket(player, destroyEntityPacket); - hasHealthBar.put(player.getName(), false); - - //Complete text - PacketPlayOutSpawnEntityLiving mobPacket = getMobPacket(completeText, 150, player.getLocation()); - - sendPacket(player, mobPacket); - hasHealthBar.put(player.getName(), true); - - DataWatcher watcher2 = getWatcher(completeText, 300, player.getWorld()); - PacketPlayOutEntityMetadata metaPacket2 = getMetadataPacket(watcher2); - - sendPacket(player, metaPacket2); - - new BukkitRunnable(){ - @Override - public void run(){ - PacketPlayOutEntityDestroy destroyEntityPacket = getDestroyEntityPacket(); - - sendPacket(player, destroyEntityPacket); - hasHealthBar.put(player.getName(), false); - } - }.runTaskLater(plugin, 40L); - - this.cancel(); - } - } - }.runTaskTimer(plugin, delay, delay); - } - - public static void displayLoadingBar(final String text, final String completeText, final Player player, final int secondsDelay, final boolean loadUp, JavaPlugin plugin){ - final int healthChangePerSecond = 150 / secondsDelay; - - displayLoadingBar(text, completeText, player, healthChangePerSecond, 20L, loadUp, plugin); + }, 20); } } \ No newline at end of file