[Tutorial] Utilizing the Boss Health Bar

Discussion in 'Resources' started by chasechocolate, Jul 5, 2013.

Thread Status:
Not open for further replies.
  1. chasechocolate Why is the dragen spawned on the exactly same position as the player's? Why not far under bedrock?



    Well, you could spawn the invisible dragon under the player - not at the player.
     
  2. Offline

    peterlee

    Hi, I have a question here and I wonder how to generate barcode in JAVA. Here are the guide of it, but I am totally a green hand on barcode generating field. Any suggestion will be appreciated. Thanks in advance.
     
  3. Offline

    brord

    Hey guys, i need help.
    Whenever i try to remove the bar, it doesnt remove it, but instead shows the current bar with like 5% health on it 0.o

    Any ideas? I am using the edit from AmoebaMan
     
  4. Offline

    Excalibur

    Same problem here, I'm looking through the code and I cannot seem to find the problem. Does anyone know the problem with it? The rest works perfectly.
     
  5. Offline

    Lzp_Hiro

    Hello, i have some trouble using this class, here's my code:
    BossBar :
    Code:java
    1. package com.eysnetwork.api.bossbar;
    2.  
    3. import java.lang.reflect.Field;
    4. import java.util.HashMap;
    5.  
    6. import net.minecraft.server.v1_7_R4.DataWatcher;
    7. import net.minecraft.server.v1_7_R4.EntityPlayer;
    8. import net.minecraft.server.v1_7_R4.Packet;
    9. import net.minecraft.server.v1_7_R4.PacketPlayInClientCommand;
    10. import net.minecraft.server.v1_7_R4.PacketPlayOutEntityDestroy;
    11. import net.minecraft.server.v1_7_R4.PacketPlayOutEntityMetadata;
    12. import net.minecraft.server.v1_7_R4.PacketPlayOutSpawnEntityLiving;
    13.  
    14. import org.bukkit.Location;
    15. import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer;
    16. import org.bukkit.entity.EntityType;
    17. import org.bukkit.entity.Player;
    18. import org.bukkit.scheduler.BukkitRunnable;
    19.  
    20. import com.eysnetwork.api.EYSApi;
    21.  
    22. public class BossBar {
    23. public static final int ENTITY_ID = 1234;
    24.  
    25. private static HashMap<String, Boolean> hasHealthBar = new HashMap<String, Boolean>();
    26.  
    27. public static void sendPacket(Player player, Packet packet) {
    28. EntityPlayer entityPlayer = ((CraftPlayer) player).getHandle();
    29.  
    30. entityPlayer.playerConnection.sendPacket(packet);
    31. }
    32.  
    33. public static Field getField(Class<?> cl, String field_name) {
    34. try {
    35. Field field = cl.getDeclaredField(field_name);
    36. return field;
    37. } catch (SecurityException e) {
    38. e.printStackTrace();
    39. } catch (NoSuchFieldException e) {
    40. e.printStackTrace();
    41. }
    42. return null;
    43. }
    44.  
    45. // Accessing packets
    46. @SuppressWarnings("deprecation")
    47. public static PacketPlayOutSpawnEntityLiving getMobPacket(String text,
    48. Location loc) {
    49. PacketPlayOutSpawnEntityLiving mobPacket = new PacketPlayOutSpawnEntityLiving();
    50.  
    51. try {
    52. Field a = getField(mobPacket.getClass(), "a");
    53. a.setAccessible(true);
    54. a.set(mobPacket, (int) ENTITY_ID);
    55.  
    56. Field b = getField(mobPacket.getClass(), "b");
    57. b.setAccessible(true);
    58. b.set(mobPacket, (byte) EntityType.WITHER.getTypeId());
    59.  
    60. Field c = getField(mobPacket.getClass(), "c");
    61. c.setAccessible(true);
    62. c.set(mobPacket, (int) Math.floor(loc.getBlockX() * 32.0D));
    63.  
    64. Field d = getField(mobPacket.getClass(), "d");
    65. d.setAccessible(true);
    66. d.set(mobPacket, (int) Math.floor(loc.getBlockY() * 32.0D));
    67.  
    68. Field e = getField(mobPacket.getClass(), "e");
    69. e.setAccessible(true);
    70. e.set(mobPacket, (int) Math.floor(loc.getBlockZ() * 32.0D));
    71.  
    72. Field f = getField(mobPacket.getClass(), "f");
    73. f.setAccessible(true);
    74. f.set(mobPacket, (byte) 0);
    75.  
    76. Field g = getField(mobPacket.getClass(), "g");
    77. g.setAccessible(true);
    78. g.set(mobPacket, (byte) 0);
    79.  
    80. Field h = getField(mobPacket.getClass(), "h");
    81. h.setAccessible(true);
    82. h.set(mobPacket, (byte) 0);
    83.  
    84. Field i = getField(mobPacket.getClass(), "i");
    85. i.setAccessible(true);
    86. i.set(mobPacket, (byte) 0);
    87.  
    88. Field j = getField(mobPacket.getClass(), "j");
    89. j.setAccessible(true);
    90. j.set(mobPacket, (byte) 0);
    91.  
    92. Field k = getField(mobPacket.getClass(), "k");
    93. k.setAccessible(true);
    94. k.set(mobPacket, (byte) 0);
    95.  
    96. // TODO Auto-generated catch block
    97. e1.printStackTrace();
    98. } catch (IllegalAccessException e1) {
    99. // TODO Auto-generated catch block
    100. e1.printStackTrace();
    101. }
    102.  
    103. DataWatcher watcher = getWatcher(text, 300);
    104.  
    105. try {
    106. Field t = PacketPlayOutSpawnEntityLiving.class
    107. .getDeclaredField("l");
    108. t.setAccessible(true);
    109. t.set(mobPacket, watcher);
    110. } catch (Exception ex) {
    111. ex.printStackTrace();
    112. }
    113.  
    114. return mobPacket;
    115. }
    116.  
    117. public static PacketPlayOutEntityDestroy getDestroyEntityPacket() {
    118. PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy();
    119.  
    120. Field a = getField(packet.getClass(), "a");
    121. a.setAccessible(true);
    122. try {
    123. a.set(packet, new int[] { ENTITY_ID });
    124. e.printStackTrace();
    125. e.printStackTrace();
    126. }
    127.  
    128. return packet;
    129. }
    130.  
    131. public static PacketPlayOutEntityMetadata getMetadataPacket(
    132. DataWatcher watcher) {
    133. PacketPlayOutEntityMetadata metaPacket = new PacketPlayOutEntityMetadata();
    134.  
    135. Field a = getField(metaPacket.getClass(), "a");
    136. a.setAccessible(true);
    137. try {
    138. a.set(metaPacket, (int) ENTITY_ID);
    139. e1.printStackTrace();
    140. } catch (IllegalAccessException e1) {
    141. e1.printStackTrace();
    142. }
    143.  
    144. try {
    145. Field b = PacketPlayOutEntityMetadata.class.getDeclaredField("b");
    146. b.setAccessible(true);
    147. b.set(metaPacket, watcher.c());
    148. } catch (Exception e) {
    149. e.printStackTrace();
    150. }
    151.  
    152. return metaPacket;
    153. }
    154.  
    155. public static PacketPlayInClientCommand getRespawnPacket() {
    156. PacketPlayInClientCommand packet = new PacketPlayInClientCommand();
    157.  
    158. Field a = getField(packet.getClass(), "a");
    159. a.setAccessible(true);
    160. try {
    161. a.set(packet, (int) 1);
    162. e.printStackTrace();
    163. e.printStackTrace();
    164. }
    165.  
    166. return packet;
    167. }
    168.  
    169. public static DataWatcher getWatcher(String text, int health) {
    170. DataWatcher watcher = new DataWatcher(null);
    171.  
    172. watcher.a(0, (Byte) (byte) 0x20); // Flags, 0x20 = invisible
    173. watcher.a(6, (Float) (float) health);
    174. watcher.a(10, (String) text); // Entity name
    175. watcher.a(11, (Byte) (byte) 1); // Show name, 1 = show, 0 = don't show
    176. // watcher.a(16, (Integer) (int) health); //Wither health, 300 = full
    177. // health
    178.  
    179. return watcher;
    180. }
    181.  
    182. // Other methods
    183. public static void displayTextBar(String text, final Player player) {
    184. PacketPlayOutSpawnEntityLiving mobPacket = getMobPacket(text,
    185. player.getLocation());
    186.  
    187. sendPacket(player, mobPacket);
    188. hasHealthBar.put(player.getName(), true);
    189.  
    190. new BukkitRunnable() {
    191.  
    192. public void run() {
    193. PacketPlayOutEntityDestroy destroyEntityPacket = getDestroyEntityPacket();
    194.  
    195. sendPacket(player, destroyEntityPacket);
    196. hasHealthBar.put(player.getName(), false);
    197. }
    198. }.runTaskLater(EYSApi.instance, 120L);
    199. }
    200.  
    201. public static void displayLoadingBar(final String text,
    202. final String completeText, final Player player,
    203. final int healthAdd, final long delay, final boolean loadUp) {
    204. PacketPlayOutSpawnEntityLiving mobPacket = getMobPacket(text,
    205. player.getLocation());
    206.  
    207. sendPacket(player, mobPacket);
    208. hasHealthBar.put(player.getName(), true);
    209.  
    210. new BukkitRunnable() {
    211. int health = (loadUp ? 0 : 300);
    212.  
    213. public void run() {
    214. if ((loadUp ? health < 300 : health > 0)) {
    215. DataWatcher watcher = getWatcher(text, health);
    216. PacketPlayOutEntityMetadata metaPacket = getMetadataPacket(watcher);
    217.  
    218. sendPacket(player, metaPacket);
    219.  
    220. if (loadUp) {
    221. health += healthAdd;
    222. } else {
    223. health -= healthAdd;
    224. }
    225. } else {
    226. DataWatcher watcher = getWatcher(text, (loadUp ? 300 : 0));
    227. PacketPlayOutEntityMetadata metaPacket = getMetadataPacket(watcher);
    228. PacketPlayOutEntityDestroy destroyEntityPacket = getDestroyEntityPacket();
    229.  
    230. sendPacket(player, metaPacket);
    231. sendPacket(player, destroyEntityPacket);
    232. hasHealthBar.put(player.getName(), false);
    233.  
    234. // Complete text
    235. PacketPlayOutSpawnEntityLiving mobPacket = getMobPacket(
    236. completeText, player.getLocation());
    237.  
    238. sendPacket(player, mobPacket);
    239. hasHealthBar.put(player.getName(), true);
    240.  
    241. DataWatcher watcher2 = getWatcher(completeText, 300);
    242. PacketPlayOutEntityMetadata metaPacket2 = getMetadataPacket(watcher2);
    243.  
    244. sendPacket(player, metaPacket2);
    245.  
    246. new BukkitRunnable() {
    247.  
    248. public void run() {
    249. PacketPlayOutEntityDestroy destroyEntityPacket = getDestroyEntityPacket();
    250.  
    251. sendPacket(player, destroyEntityPacket);
    252. hasHealthBar.put(player.getName(), false);
    253. }
    254. }.runTaskLater(EYSApi.instance, 40L);
    255.  
    256. this.cancel();
    257. }
    258. }
    259. }.runTaskTimer(EYSApi.instance, delay, delay);
    260. }
    261.  
    262. public static void displayLoadingBar(final String text,
    263. final String completeText, final Player player,
    264. final int secondsDelay, final boolean loadUp) {
    265. final int healthChangePerSecond = 300 / secondsDelay;
    266.  
    267. displayLoadingBar(text, completeText, player, healthChangePerSecond,
    268. 20L, loadUp);
    269. }
    270. }
    271.  

    Main (another plugin)
    Code:java
    1. package com;
    2.  
    3. import org.bukkit.command.Command;
    4. import org.bukkit.command.CommandSender;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. import com.eysnetwork.api.bossbar.BossBar;
    9.  
    10. public class Main extends JavaPlugin {
    11.  
    12. public static Main instance;
    13.  
    14. public void onEnable() {
    15. instance = this;
    16. }
    17.  
    18. public void onDisable() {
    19.  
    20. }
    21.  
    22. public boolean onCommand(Command cmd, CommandSender sender, String label,
    23. String[] arsg) {
    24. if (label.equalsIgnoreCase("test")) {
    25. Player p = (Player) sender;
    26. BossBar.displayTextBar("TEST", p);
    27. }
    28. return false;
    29. }
    30.  
    31. }
    32.  
     
  6. Offline

    Jannik8500

    Any Codes for 1.8? :)
     
  7. Offline

    RingOfStorms

    Use the same code, just update the imports.
     
  8. Offline

    Jannik8500

    hmm, i tested so many classes, but nothing of this works.

    Can anyone please release an offical Class for the newest Patch?
     
  9. Offline

    AdamQpzm

    Jannik8500 This is a resource on the Bukkit forums, and there is no CraftBukkit 1.8.
     
    chasechocolate likes this.
Thread Status:
Not open for further replies.

Share This Page