Solved getHealth(), getMaxHealth()

Discussion in 'Plugin Development' started by CaptainWalrus, Nov 29, 2013.

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

    CaptainWalrus

    So, whenever I try to use getHealth(), or getMaxHealth() eclipse always tells me:
    The method getHealth() is ambiguous for the type Zombie.

    It does this for everything, not just zombie.
    some of my code (though it does this more than just here):
    Code:
    public static void applyZombieCharacteristics(Zombie z, int level) {
            if (level < 1) level = 1;
            if (level > 7) level = 7;
            if (Config.useBabies && nextInt(1, Config.babyZombieChance) == Config.babyZombieChance - 1) {
                if (Config.babiesAlwaysFast)
                    z.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, Config.babySpeedLevel));
                z.setBaby(true);
            }
            z.setMaxHealth(r.nextInt(20) + (nextInt(10, 20) * level));
            z.setHealth(z.getMaxHealth());
    AND
    Code:
    @EventHandler(priority = EventPriority.MONITOR)
        public void onZombieDamage(EntityDamageEvent e) {
            if (!Config.useNameplates) return;
            Entity ent = e.getEntity();
            if (ent.getType() != EntityType.ZOMBIE) return;
            if (!RUtils.isInInfectedWorld(ent)) return;
            final Zombie z = (Zombie) ent;
            Map<String, Object> data = ZombieSpawner.getSurvivorsData(z);
            if (data == null) {
                final Map<String, Object> info = new HashMap<String, Object>();
                info.put("level", ZombieSpawner.guessLevel(z.getMaxHealth()));
                z.setMetadata("minez", new FixedMetadataValue(RoyalSurvivors.instance, info));
                data = info;
    
    AND lastly:
    Code:
    if (to.getY() > from.getY() && !RUtils.isOnLadder(p)) thirst -= Config.thirstJump;
            if (thirst <= 0F) {
                p.sendMessage(ChatColor.BLUE + "You have died of dehydration.");
                p.setLastDamageCause(new EntityDamageEvent(p, EntityDamageEvent.DamageCause.CUSTOM, p.getHealth()));
                p.setHealth(0);
                p.setExp(1F);
                pcm.set("thirst", 1F);
                return;
    
    Not sure if I have supplied enough code here for anyone to help, but if you can, I would love it!
    Please helppp.
     
  2. Offline

    caseif

    The methods were changed after MC 1.6 to account for the changes made to health storage without breaking plugins. While I haven't had reason to deal with them since then, my recommendation would be to assign the return value to a variable of a specific type (I say that to reinforce why it must be done, even though it's redundant).
     
  3. Offline

    CaptainWalrus

    ShadyPotato
    I'm confused, so what did they change getHealth to?
     
  4. Offline

    caseif

  5. Offline

    CaptainWalrus

    Hmm, seems a bit more confusing.
    If I where to compile with 1.6 jar would it work fine on other versions of bukkit?
     
  6. Offline

    caseif

    Cross-compatibility usually isn't an issue with Bukkit, so you'll probably be fine. And to sum up the thread I linked to:

    Minecraft decided to store health values as integers instead of floats. This basically just means they changed the variable type for health values. Normally, this would require the Bukkit team to change the .getHealth() method to return a float. But, all plugins were dependent on that method returning an int. If it suddenly returned a float, there would be type errors all over the place due to the plugin trying to assign a float to an int. So, the Bukkit team made the method ambiguous. In other words, they made the method return either an int or a float, depending on the context. If the code was expecting an int, it would return an int. Likewise, if the code was expecting a float, it would return a float. Hope this clears things up. :)
     
  7. Offline

    CaptainWalrus

    ShadyPotato
    Nevermind, I got it!
    Thank you so much!
     
Thread Status:
Not open for further replies.

Share This Page