Unable to get Player's Health

Discussion in 'Plugin Development' started by xXSilentYoshiXx, Jul 2, 2013.

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

    xXSilentYoshiXx

    From the title, I'm currently unable to get a Player's Health as of Craftbukkit's 1.6.1 release.

    Eclipse Error: The method getHealth() is ambiguous for the type Player

    Code:
    Code:
        @EventHandler
        public void onPlayerDeath(EntityDamageEvent e) {
            Entity entity = e.getEntity();
           
            if (entity instanceof Player) {
                Player player = (Player) e.getEntity();
                Double dmgDealt = e.getDamage();
                Integer playerHealth = (Integer) player.getHealth();
            }
        }
    Is there a way I can fix this? I've tried adding a cast, making "playerHealth" a double, and nothing's worked so far.
     
  2. Offline

    Alex5657

    Ambiguous reference usually means that your method call fits both overloaded methods. I really have no idea why there would be 2 getHealth() methods... perhaps it is a bug?
     
  3. Offline

    re4ly

    the same with: setDamage(x);
    Edit: e.getPlayer().damage(x);
     
  4. Offline

    Alex5657

    You can obviously use:
    Code:
    double health = 20-dmgDealt;
    
     
  5. Offline

    Jogy34

    The new health system uses doubles for storing health instead of integers but the bukkit developers didn't want to forcefully break a lot of plugins by forcing it to be only used with doubles so they left the previous integer methods in there.
     
  6. Offline

    AmShaegar

    Either stop using craftbukkit as build dependency instead of bukkit, or, if you really need craftbukkit for some reasons, reorder them in your build path. Bukkits need to be loaded first. See here:
    [​IMG]
     
    legostarwarszach likes this.
  7. Offline

    Jogy34

    The dual getHealth() problem is also in the Bukkit API. Also the Bukkit API is contained within the CraftBukkit.jar files so there is no need to have both the Bukkit.jar and the CraftBukkit.jar. I personally have only ever used just the CraftBukkit.jar
     
  8. Offline

    AmShaegar

    This means: Add bukkit API to your buildpath and you are done with that error.
     
  9. Offline

    microgeek

    Having the same issue.
     
  10. Offline

    AmShaegar

    microgeek likes this.
  11. Offline

    Jogy34

    it works fine for me. I just had to change a few calls around. Also, I make NMS and Craftbukkit calls a lot in a few of my plugins.
     
  12. Offline

    AmShaegar

    I use NMS by myself but there is no need for workarounds. They intended the plugins NOT to break. So if you just add bukkit, everything is fine without workarounds.
     
  13. Offline

    microgeek

  14. Offline

    Jogy34

    The way that I have my workarea set up for making bukkit plugins it's a lot easier, cleaner, and faster to just do a few 1-2 second changes than to get the Bukkit.jar
     
  15. Offline

    xXSilentYoshiXx

    Whoa, whoa, whoa. So I've been downloading the wrong .jar for almost a YEAR now? :O
     
  16. Offline

    Technius

    xXSilentYoshiXx
    I only use craftbukkit.jar when I use NMS code. It's doesn't really matter, though.
    Code:
    int i = player.getHealth();
    Integer playerHealth = Integer.valueOf(i); //Why would you need this?
    
     
  17. Offline

    Jogy34

    The Craftbukkit.jar contains the Bukkit API so it isn't really a big deal which one you use unless you are looking for using NMS or Craftbukkit code in which case you would have to use the Craftbukkit.jar but to fix your problem it would probably be easier for you to just use the Bukkit.jar.

    On your problem, currently there are two getHealth() (and a few other) methods, one that used to be used that returned an integer and one that should now be used that returns a double. I believe you can still run your plugin if you IDE is giving you the error as in the javadocs for bukkit, it says "It will not exist at runtime" which presumably means that the compiler won't get confused at which method to use. Your IDE just doesn't know what to do about it. (That was pretty much all just an educated guess so don't hold me to it)
     
Thread Status:
Not open for further replies.

Share This Page