(Help) BarAPI Player Health

Discussion in 'Plugin Development' started by Samstrongman, Apr 12, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Samstrongman

    So I want to make a plugin that uses the wither/enderman boss health bar but for players. I am using eclipse and the BarAPI file to do this. But when I try and run the plugin, it enables but nothing shows up in the game. I check the server bat window and it just gives a long error saying that the events won't pass through. Please help if you can. Thank you in advanced.
    Code:java
    1. package me.myname.healthbar;
    2.  
    3. import me.confuser.barapi.BarAPI;
    4.  
    5. import org.bukkit.entity.Damageable;
    6. import org.bukkit.entity.EntityType;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerJoinEvent;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class HealthMain extends JavaPlugin implements Listener{
    14.  
    15. public void onEnable() {
    16. getServer().getPluginManager().registerEvents(this, this);
    17. }
    18.  
    19. @EventHandler
    20. public void onPlayerJoinEvent(PlayerJoinEvent event) {
    21. if(event.getPlayer().equals(EntityType.PLAYER)) {
    22. Player p = (Player) event.getPlayer();
    23. Damageable player = p ;
    24. BarAPI.setHealth(p, (float) (((player.getHealth() + 1) / player.getMaxHealth())) * 100);
    25. }
    26.  
    27. }
    28. }
    29.  
     
  2. Offline

    Dahwn

    Samstrongman
    Give us the error. That will help US alot ;)!

    -Dahwn
     
  3. Offline

    Samstrongman

    Code:
    [10:10:54 ERROR]: Could not pass event EntityRegainHealthEvent to HealthBar v1.0
     
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:294) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.EntityLiving.heal(EntityLiving.java:598)
    [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.MobEffectList.tick(MobEffectList.java:86
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.MobEffect.b(SourceFile:103) [craftbukkit
    .jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.MobEffect.tick(SourceFile:90) [craftbukk
    it.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.EntityLiving.aN(EntityLiving.java:443) [
    craftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.EntityLiving.B(EntityLiving.java:232) [c
    raftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.Entity.h(Entity.java:240) [craftbukkit.j
    ar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.EntityLiving.h(EntityLiving.java:1266) [
    craftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.EntityHuman.h(EntityHuman.java:162) [cra
    ftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.EntityPlayer.i(EntityPlayer.java:245) [c
    raftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :343) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInFlying.a(SourceFile:137) [cr
    aftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInFlying.handle(SourceFile:8)
    [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:148
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
    Caused by: java.lang.NoClassDefFoundError: me/confuser/barapi/BarAPI
            at me.samuel.healthbar.HealthMain.onHealthUpEvent(HealthMain.java:24) ~[
    ?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _51]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _51]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_51]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-18-g3c3980f-b3046jnks]
            ... 23 more
    Here give me a few minutes. Warning, it's super long.

    Yeah, that's it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Offline

    skyrimfan1

    You don't have BarAPI in your server's plugin directory hence the NoClassDefFoundError.
     
  5. Offline

    Samstrongman

    Thank you. I'll fix that.

    Okay, so that removed the error. But the ender/wither boss bar aren't appearing in the game as the player health. Do you have any idea what's wrong with the code?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  6. Offline

    jimbo8

    Because you only set it every time the player joins. You'll have to make it change every time you lose a heart.
     
  7. Offline

    Samstrongman

    Well I want the health bar to appear by default when players join the server, and the health bar acts as a health bar when they lose and gain health points/hearts.
     
  8. Offline

    jimbo8

    Samstrongman

    Yeah, but you only check it once, and that is when the player joins the server. You need to update the bar whenever the player loses/gains health.
     
  9. Offline

    Samstrongman

    Okay, thank you. I'll see if that works.

    Nothing is working. Plugin is enabled, but no matter which event I use, nothing appears. This is really getting frustrating. I place the BarAPI jar in the plugins folder to remove one error and it still doesn't work. I change the events from playerLogin to EntityRegainHealthEvent, and it still doesn't work. So I have no clue what to do now.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  10. Offline

    jimbo8

    Samstrongman

    Hmm, this is weird. Can you show me the full code? :)
     
  11. Offline

    Samstrongman


    Code:java
    1. package me.samuel.healthbar;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import me.confuser.barapi.BarAPI;
    6.  
    7. import org.bukkit.entity.Damageable;
    8. import org.bukkit.entity.EntityType;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.EntityRegainHealthEvent;
    13. import org.bukkit.plugin.PluginDescriptionFile;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class HealthMain extends JavaPlugin implements Listener{
    17.  
    18. Logger logger = Logger.getLogger("Minecraft");
    19.  
    20. @Override
    21. public void onEnable() {
    22. PluginDescriptionFile pdfFile = this.getDescription();
    23. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + "Has Been Enabled!");
    24. getServer().getPluginManager().registerEvents(this, this);
    25.  
    26. }
    27. @Override
    28. public void onDisable() {
    29. PluginDescriptionFile pdfFile = this.getDescription();
    30. this.logger.info(pdfFile.getName() + "Has Been Disabled!");
    31.  
    32. }
    33.  
    34. @EventHandler
    35. public void onHealthUpEvent(EntityRegainHealthEvent event) {
    36. if(event.getEntityType().equals(EntityType.PLAYER)) {
    37. Player p = (Player) event.getEntity();
    38. Damageable player = p ;
    39. BarAPI.setHealth(p, (float) (((player.getHealth() + 1) / player.getMaxHealth())) * 100);
    40. }
    41. }
    42.  
    43. }
    44.  

    Thanks for helping me.
     
  12. Offline

    GeorgeeeHD

    Use your same code in a EntityDamageEvent and a EntityRegainHealthEvent. You can keep the PlayerJoinEvent in there too
     
  13. Offline

    Samstrongman


    Code:java
    1. @EventHandler
    2. public void onHealthUpEvent(EntityRegainHealthEvent event) {
    3. if(event.getEntityType().equals(EntityType.PLAYER)) {
    4. Player p = (Player) event.getEntity();
    5. Damageable player = p ;
    6. BarAPI.setHealth(p, (float) (((player.getHealth() + 1) / player.getMaxHealth())) * 100);
    7. }
    8. }
    9. public void onEntityDamageEvent(EntityDamageEvent event) {
    10. if(event.getEntityType().equals(EntityType.PLAYER)) {
    11. Player p = (Player) event.getEntity();
    12. Damageable player = p ;
    13. BarAPI.setHealth(p, (float) (((player.getHealth() + 1) / player.getMaxHealth())) * 100);
    14.  
    15. }
    16. }

    Like this? I'm sorta new to plugin developing, but I want to start somewhere to learn.
     
  14. Offline

    jimbo8

    Samstrongman

    Yeah, everyone got to start somewhere :)

    Forgot @Eventhandler over the DamageEvent, by the way.

    Also, of my own experience, using "==" is better than ".equals".
     
  15. Offline

    Samstrongman


    Code:java
    1. package me.samuel.healthbar;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import me.confuser.barapi.BarAPI;
    6.  
    7. import org.bukkit.entity.Damageable;
    8. import org.bukkit.entity.EntityType;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.EntityDamageEvent;
    13. import org.bukkit.event.entity.EntityRegainHealthEvent;
    14. import org.bukkit.plugin.PluginDescriptionFile;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class HealthMain extends JavaPlugin implements Listener{
    18.  
    19. Logger logger = Logger.getLogger("Minecraft");
    20.  
    21. @Override
    22. public void onEnable() {
    23. PluginDescriptionFile pdfFile = this.getDescription();
    24. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + "Has Been Enabled!");
    25. getServer().getPluginManager().registerEvents(this, this);
    26.  
    27. }
    28. @Override
    29. public void onDisable() {
    30. PluginDescriptionFile pdfFile = this.getDescription();
    31. this.logger.info(pdfFile.getName() + "Has Been Disabled!");
    32.  
    33. }
    34.  
    35. @EventHandler
    36. public void onHealthUpEvent(EntityRegainHealthEvent event) {
    37. if(event.getEntityType()==(EntityType.PLAYER)) {
    38. Player p = (Player) event.getEntity();
    39. Damageable player = p ;
    40. BarAPI.setHealth(p, (float) (((player.getHealth() + 1) / player.getMaxHealth())) * 100);
    41. }
    42. }
    43. @EventHandler
    44. public void onEntityDamageEvent(EntityDamageEvent event) {
    45. if(event.getEntityType()==(EntityType.PLAYER)) {
    46. Player p = (Player) event.getEntity();
    47. Damageable player = p ;
    48. BarAPI.setHealth(p, (float) (((player.getHealth() + 1) / player.getMaxHealth())) * 100);
    49.  
    50. }
    51. }
    52. }
    53.  


    So it's still not working, so I'm not entirely sure what is wrong.
    Sorry for the annoyance, but yeah, I just want to expand my knowledge in plugin developing.
     
  16. Offline

    jimbo8

    Samstrongman

    Haha, you're not annoying at all, i know how it is :)

    You might want to try to remove the parantheses around EntityType.
     
    Samstrongman likes this.
  17. Offline

    Dahwn

    Samstrongman
    Why don't you just make a method which sets the bar to the health the player has every tick?
    Code:java
    1. public void healthBar(Player p) {
    2. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask("Your Mainclass", new Runnable() {
    3. public void run() {
    4. //set your bar
    5. }
    6. }, 0L, 20L);
    7. }

    -Dahwn
     
  18. Offline

    Samstrongman

    Honestly, I have no idea how to do that. I am quite new to developing plugins. Would you care to share what I should do?
     
  19. Offline

    jimbo8

    Dahwn
    This might cause lag, i would not recommend doing this.

    Samstrongman
    As i said above, this might cause lag, but you would do it like this:

    Code:java
    1. public void healthBar(Player player) {
    2. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask("Your Mainclass", new Runnable() {
    3. public void run() {
    4. BarAPI.setHealth(p, (float) (((player.getHealth() + 1) / player.getMaxHealth())) * 100);
    5. }
    6. }, 0L, 20L);
    7. }


    Edit:

    and then call the function method.
     
  20. Offline

    Dahwn

    jimbo8
    Really man, I have a lot of tasks like this running EVERY tick on my localhost with only 4GB ram and it's not lagging! :)
    If every RepeatingTask would lag, why would they have been added then? :D

    -Dahwn
     
  21. Offline

    Samstrongman

    I'm so lost... once this plugin gets solved and finished, I'm gonna be so happy.
    Anyways,
    scheduleSyncRepeatingTask is always underlined in red and when I change it to scheduleASyncRepeatingTask, it gets crossed out and suggests it to change back to scheduleSyncRepeatingTask...
    Plus, the player line in player.getHealth and etc are always underlined with no fix I can think of or see.

    So lost. I feel so bad having you guys constantly try and help me. It's cool if you guys want to stop.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  22. Offline

    yupie_123

    The thing is, doesn't BarAPI show the bar for all the players on the server? Or all the player nearby?
    I used it once and that was the issue with me.
     
  23. Offline

    Samstrongman

    It depends how you have it setup. I want it so it shows up for all players but it'll have their health status on it individually.
     
  24. Offline

    yupie_123


    Yeah I might have a different setup.
    Anyways, I haven't used it much, so can't really help.
    Good luck!
     
  25. Offline

    Samstrongman

    Code:java
    1. package me.samuel.healthbar;
    2.  
    3. import me.confuser.barapi.BarAPI;
    4.  
    5. import org.bukkit.entity.Damageable;
    6. import org.bukkit.entity.EntityType;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.entity.EntityDamageEvent;
    11. import org.bukkit.event.entity.EntityRegainHealthEvent;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class HealthMain extends JavaPlugin implements Listener{
    15.  
    16. public void onEnable() {
    17. getServer().getPluginManager().registerEvents(this, this);
    18. }
    19.  
    20. @EventHandler
    21. public void onHealthUpEvent(EntityRegainHealthEvent event) {
    22. if(event.getEntity().equals(EntityType.PLAYER)) {
    23. Player p = (Player) event.getEntity();
    24. Damageable player = p ;
    25. BarAPI.setMessage(p, "Health Bar",(float) ((((player.getHealth() + 1) / player.getMaxHealth())) * 100));
    26. }
    27. }
    28.  
    29. @EventHandler
    30. public void onDamageEvent(EntityDamageEvent event) {
    31. if(event.getEntity().equals(EntityType.PLAYER)) {
    32. Player p = (Player) event.getEntity();
    33. Damageable player = p ;
    34. BarAPI.setMessage(p, "Health Bar",(float) ((((player.getHealth() + 1) / player.getMaxHealth())) * 100));
    35. }
    36. }
    37.  
    38. }
    39.  


    Okay I rewrote some parts of the code, but it still doesn't work. Does anyone see an error in the code I wrote? I would like to get this plugin functional as soon as possible.
     
  26. Offline

    Squid_Boss

    Just a tip, I would make sure the mob is an enderdragon and not a wither, as wither boss bar's are kind of weird having a short radius and all, whereas with an enderdragon the radius is pretty good, reaching a few hundred blocks. Also, an enderdragon's max health is 200.0, and a player has a max health of 20; so you'd just have to use the right events, take the player's health multiply by ten, and then set it to the boss bar.
     
  27. Offline

    Samstrongman

    The BarAPI interface is a enderdragon bar. I'm trying to manipulate it to become a player's health bar but the bar won't show up no matter what I do.
     
  28. Offline

    Squid_Boss

    I could send you the class I use for a dragon bar if you'd like.
     
  29. Offline

    Samstrongman

    If you don't mind can you please? Or just inbox the code to me.
     
  30. Offline

    Squid_Boss

    Here it is:
    http://pastebin.com/jt6dw5Jb
    WARNING: The location parameter is the location of the enderdragon, I'd put it like at a negative y or something.

    Here is an example of how to use it:
    Code:java
    1. DragonBar.displayDragonBar(*Bar's Name*, *A player*, *A location*, *Health of the Dragon*);
     
Thread Status:
Not open for further replies.

Share This Page