Fix for BossBar

This commit is contained in:
Chiss 2014-09-11 13:17:57 +10:00
parent 7bbb7ca545
commit 56263718d7

View File

@ -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.PacketPlayOutEntityMetadata;
import net.minecraft.server.v1_7_R4.PacketPlayOutSpawnEntityLiving; import net.minecraft.server.v1_7_R4.PacketPlayOutSpawnEntityLiving;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.craftbukkit.v1_7_R4.CraftWorld; 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.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
public class UtilDisplay public class UtilDisplay
{ {
public static final int ENTITY_ID = 777777; 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<String, Boolean> hasHealthBar = new HashMap<String, Boolean>(); private static HashMap<String, Boolean> hasHealthBar = new HashMap<String, Boolean>();
public static void sendPacket(Player player, Packet packet){ public static void sendPacket(Player player, Packet packet){
@ -34,11 +38,12 @@ public class UtilDisplay
} }
//Accessing packets //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(); PacketPlayOutSpawnEntityLiving mobPacket = new PacketPlayOutSpawnEntityLiving();
mobPacket.a = (int) ENTITY_ID; //Entity ID 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.c = (int) Math.floor(loc.getBlockX() * 32.0D); //X position
mobPacket.d = (int) MathHelper.floor(loc.getBlockY() * 32.0D); //Y position mobPacket.d = (int) MathHelper.floor(loc.getBlockY() * 32.0D); //Y position
mobPacket.e = (int) Math.floor(loc.getBlockZ() * 32.0D); //Z 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.i = (short) 0; //X velocity
mobPacket.j = (short) 0; //Y velocity mobPacket.j = (short) 0; //Y velocity
mobPacket.k = (short) 0; //Z 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; mobPacket.l = watcher;
return mobPacket; return mobPacket;
} }
public static PacketPlayOutEntityDestroy getDestroyEntityPacket(){ public static PacketPlayOutEntityDestroy getDestroyEntityPacket()
{
PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(ENTITY_ID); PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(ENTITY_ID);
return packet; return packet;
} }
public static PacketPlayOutEntityMetadata getMetadataPacket(DataWatcher watcher){ public static PacketPlayOutEntityMetadata getMetadataPacket(DataWatcher watcher)
{
PacketPlayOutEntityMetadata metaPacket = new PacketPlayOutEntityMetadata(); PacketPlayOutEntityMetadata metaPacket = new PacketPlayOutEntityMetadata();
metaPacket.a = (int) ENTITY_ID; metaPacket.a = (int) ENTITY_ID;
try{ try
{
Field b = PacketPlayOutEntityMetadata.class.getDeclaredField("b"); Field b = PacketPlayOutEntityMetadata.class.getDeclaredField("b");
b.setAccessible(true); b.setAccessible(true);
b.set(metaPacket, watcher.c()); b.set(metaPacket, watcher.c());
} catch(Exception e){ }
catch(Exception e)
{
e.printStackTrace(); e.printStackTrace();
} }
@ -83,16 +110,14 @@ public class UtilDisplay
{ {
DataWatcher watcher = new DataWatcher(new DummyEntity(((CraftWorld)world).getHandle())); 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(0, (Byte) (byte) 0); //Flags, 0x20 = invisible
watcher.a(6, (Float) (float) health); watcher.a(6, (Float) (float) health);
watcher.a(2, (String) text); //Entity name watcher.a(2, (String) text); //Entity name
watcher.a(10, (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(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(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(16, (Integer) (int) health); //Wither health, 300 = full health
watcher.a(20, (Integer) (int) 0); //Wither invuln, 200 = full health watcher.a(20, (Integer) (int) 0); //Wither invuln,
int i1 = watcher.getInt(0); int i1 = watcher.getInt(0);
watcher.watch(0, Byte.valueOf((byte)(i1 | 1 << 5))); watcher.watch(0, Byte.valueOf((byte)(i1 | 1 << 5)));
@ -101,93 +126,44 @@ public class UtilDisplay
return watcher; return watcher;
} }
//Other methods //Display
public static void displayTextBar(JavaPlugin plugin, final Player player, double healthPercent, String text) public static void displayTextBar(JavaPlugin plugin, final Player player, double healthPercent, String text)
{ {
PacketPlayOutEntityDestroy destroyEntityPacket = getDestroyEntityPacket(); PacketPlayOutEntityDestroy destroyEntityPacket = getDestroyEntityPacket();
sendPacket(player, destroyEntityPacket); 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); sendPacket(player, mobPacket);
hasHealthBar.put(player.getName(), true); hasHealthBar.put(player.getName(), true);
new BukkitRunnable(){ Bukkit.getServer().getScheduler().runTaskLater(plugin, new Runnable()
@Override {
public void run(){ public void run()
{
PacketPlayOutEntityDestroy destroyEntityPacket = getDestroyEntityPacket(); PacketPlayOutEntityDestroy destroyEntityPacket = getDestroyEntityPacket();
sendPacket(player, destroyEntityPacket); sendPacket(player, destroyEntityPacket);
hasHealthBar.put(player.getName(), false); hasHealthBar.put(player.getName(), false);
} }
}.runTaskLater(plugin, 120L); }, 20);
}
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);
} }
} }