(Help) BarAPI Player Health

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

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

    Samstrongman

    Thanks Man :D
     
  2. Offline

    Squid_Boss

    Np
     
  3. 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.  

    Does anyone know why this code won't run? The plugin enables and everything, but the boss bar won't show up. I've tried all that I could, but it won't work. I'm using Eclipse IDE and BarAPI to make this code. I want to use the enderdragons health bar as a player health bar.

    bump

    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

    I don't know if this will help, but maybe set 100 to 100f? That way it really recognizes it as a float.
     
  5. Offline

    Samstrongman

    I'll give that a shot when I have the chance. I'll let you know if it works. Thanks for the help.
     
  6. Offline

    jimbo8

    Samstrongman

    Sorry for not answering, I'm at the cabin and the internet here is not too good :p

    I'll test it out myself and find a solution for you, give me a few minutes.

    Samstrongman
    I had to make a new one from scratch, but this one works like a charm:
    Code:java
    1. public class Main extends JavaPlugin implements Listener{
    2.  
    3. public void onEnable(){
    4. PluginManager pm = Bukkit.getPluginManager();
    5. pm.registerEvents(this, this);
    6. System.out.print("Enabled!");
    7. }
    8. @EventHandler
    9. public void onJoin(PlayerJoinEvent event){
    10. Player player = event.getPlayer();
    11. BarAPI.setMessage(player, "Health Bar",(float) (player.getHealth() * 5));
    12. }
    13. @EventHandler
    14. public void onDamage(EntityDamageEvent event){
    15. Entity e = event.getEntity();
    16. if(e instanceof Player){
    17. Player player = (Player) e;
    18. BarAPI.setMessage(player, "Health Bar",(float) (player.getHealth() * 5));
    19. }
    20. }
    21. @EventHandler
    22. public void onHeal(EntityRegainHealthEvent event){
    23. Entity e = event.getEntity();
    24. if(e instanceof Player){
    25. Player player = (Player) e;
    26. BarAPI.setMessage(player, "Health Bar",(float) (player.getHealth() * 5));
    27. }
    28. }
    29. @EventHandler
    30. public void onRespawn(final PlayerRespawnEvent event){
    31. Bukkit.getScheduler().runTaskLaterAsynchronously(this, new Runnable() {
    32. @Override
    33. public void run() {
    34. Player player = event.getPlayer();
    35. BarAPI.setMessage(player, "Health Bar",(float) (player.getHealth() * 5));
    36. }
    37. }, 0*1);
    38.  
    39. }
    40. }
    41.  



    Dahwn

    The reason why it generates "lag" is because you check for something every tick. Maybe you don't lag now, but if you get, let's say a few houndred people online, then it would lag and possibly crash the server. It is not recommended to run a task that often.

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

    Dahwn

    jimbo8 I have a server with 20 players online all day, and everyone has 4+ RepeatingTasks and it doesn't lag! :)
     
  8. Offline

    jimbo8

    Dahwn

    Still not recommended, you're using more resources than needed. It's always better to check for the event that is getting fired instead of running a task to check it :)
     
    teozfrank likes this.
  9. Offline

    Samstrongman

    Thanks for the code. I'll test it out later.
     
  10. Offline

    jimbo8

  11. Offline

    Samstrongman

    Lol sorry no. I'm probably just to stupid to get it to work. I'll have to still study more Java and hopefully become better, if that's possible lol, but for now I'll try and get it to work, and I'll start from square one from the basics.
     
  12. Offline

    Dahwn

    Samstrongman
    I'll start coding this now! I think that shouldn't be hard.
     
  13. Offline

    jimbo8

    Samstrongman

    Alright :)

    If you need more help, my PM is always open.
     
Thread Status:
Not open for further replies.

Share This Page