Solved Something I'm overlooking?

Discussion in 'Plugin Development' started by ForbiddenGaming, Jan 21, 2016.

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

    ForbiddenGaming

    So I'm trying to make a bossbar plugin showing a player's health. I am using the BossBar API and yes, I do have it installed on the server I'm testing it on. No errors are displayed so i'm guessing it is something I overlooked in the code but i can't seem to figure it out.
    Code:
            @SuppressWarnings("deprecation")
            Player dummy = Bukkit.getServer().getPlayer(args[0]);
              if (dummy == null) {
                  sender.sendMessage(ChatColor.RED + "Invalid Player Name: " + args[0] + "!" + ChatColor.DARK_RED + "(Either the player is offline or the player does not exist whatsoever)");
                      return true;
                         }
            if (label.equals(dummy)) {
          float health = BarAPI.getHealth(dummy);
          BarAPI.setMessage(dummy + "'s" + health);
          sender.sendMessage("Test");
          return true;
        }
             }
            return false;
    
    }}
    
    Okay this is just the bottom portion of the code but I know for a fact this is the only part of the code that needs fixing. The top part was just the has.permission and the public boolean. For the barapi setmessage i am using: player, string, float. Also the class implements commandexecutor
     
  2. Offline

    teej107

    @ForbiddenGaming I'm not sure because I haven't used the API but isn't there a send method to send the boss bar to the player?
     
  3. Offline

    ForbiddenGaming

  4. Offline

    teej107

    Have you looked at its documentation?
     
  5. Offline

    Zombie_Striker

    Due to lack of context, I will assume "label" is just a string if it is not also the command label. With that being the case, you are testing if "label" is equal to "dummy", or in other words, If a String is equal to a Player. I only assume label is a string because it is hard for me to think you would call a player a label.
     
  6. Offline

    ShowbizLocket61

    That would seem to be the problem, and explains why no errors are thrown.
     
  7. Offline

    ForbiddenGaming

    @ShowbizLocket61 @Zombie_Striker Thanks it rlly helped :D

    Also does anyone know why the health is displayed as {my username}-1.0
    Can anyone show me to get rid of the -

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

    Zombie_Striker

    @ForbiddenGaming
    Why would you want to remove the negative sign? The health is negative, so removing the '-' would mean the entity still has health. I think what you mean is displaying the health as "0" instead of a negative number, which you would use
    Code:
    "'s"+(health < 0 ? 0, health)
    This checks if the health is less than 0, and if so, it displays 0.
     
  9. Offline

    ShowbizLocket61

    @Zombie_Striker
    Should the player not be dead if the health was nonpositive?
     
  10. Offline

    Zombie_Striker

    @ShowbizLocket61
    True, but that is most likely the case. That is the only way to receive a negative sign unless the value it returns is "null" (numbers treat -1 as null)
     
    ShowbizLocket61 likes this.
  11. Offline

    ForbiddenGaming

    @Zombie_Striker @ShowbizLocket61 i don't understand, i am not dead, but i do not want it to display as 0. I want it to show my health so how do i fix the null?
     
  12. Offline

    Zombie_Striker

    @ForbiddenGaming
    Looking at the code, you may want to use this instead:
    Code:
    float health = Entity/Player.getHealth;
    //NOTE: You may have to cast the entity to a damagable.
     
  13. Offline

    ForbiddenGaming

    nvm i got it
     
    Last edited: Jan 22, 2016
  14. Offline

    Zombie_Striker

  15. Offline

    ForbiddenGaming

    @Zombie_Striker
    Code:
                   Player dummy = Bukkit.getServer().getPlayer(args[0]);
                     if (dummy == null) {
                         sender.sendMessage(ChatColor.RED + "Invalid Player Name: " + args[0] + "!" + ChatColor.DARK_RED + "(Either the player is offline or the player does not exist whatsoever)");
                             return true;
                                }
                     String dname = dummy.getName();
                     float health = (float) dummy.getHealth();
                 float hmax = (float) dummy.getMaxHealth();
                 //BarAPI.setMessage(dname + "'s" + health +"/" + hmax);
                for(Player p : Bukkit.getOnlinePlayers()){
                    if(BarAPI.hasBar(p)){
                        BarAPI.removeBar(p);
                    }
                   BarAPI.setMessage(dname + "'s" + " health " + health +"/" + hmax);
                 sender.sendMessage("Test");
                 return true;
               
                    }
                   
           
           }
            return false;}
    }
    
    So how do i check for the player's health every 3 seconds?
     
  16. Offline

    Zombie_Striker

  17. Offline

    ForbiddenGaming

    @Zombie_Striker I have no idea where to put the scheduler w/o breaking the plugin
     
  18. Offline

    Zombie_Striker

    @ForbiddenGaming
    Well, the only objectsmethods that would need to be updated or calledevery 3 seconds would be the "health", "maxhealth", and "setMessage". Just try the following:
    Code:
    Bukkit.getServer().getScheduler().schedulersyncrepeatingTask(Java Plugin Instance, new Runnable(){
    
    float health = (float) dummy.getHealth();
                 float hmax = (float) dummy.getMaxHealth();
                 //BarAPI.setMessage(dname + "'s" + health +"/" + hmax);
                for(Player p : Bukkit.getOnlinePlayers()){
                    if(BarAPI.hasBar(p)){
                        BarAPI.removeBar(p);
                    }
                   BarAPI.setMessage(dname + "'s" + " health " + health +"/" + hmax);
                 sender.sendMessage("Test");
                 return true;
               
                    }
    
    }, 0 /*delay*/, 3*20L /*seconds before repeat*/);
     
  19. Offline

    ForbiddenGaming

    @Zombie_Striker Java Plugin Instance cannot be resolved as a variable. Runnable cannot be instantiated, as for the delay and repeat, an "(" was expected instead. How to fix?
     
  20. Offline

    Zombie_Striker

    @ForbiddenGaming
    Replace "Java Plugin instance" with the instance of the main class (if this is in the main class, use "this") As for you other problem, they most likely "bug IDE errors". Try refreshing the project you're working on (press F5), and if that does not work, close the IDE and open it again.
     
  21. Offline

    ForbiddenGaming

    @Zombie_Striker how would i put replace java plugin instance with commandexecutor? Also i restarted my IDE, still didn't work
     
  22. Offline

    mythbusterma

Thread Status:
Not open for further replies.

Share This Page