Solved Can't set an entities health?

Discussion in 'Spigot Plugin Development' started by Igasi, Apr 17, 2017.

  1. So uh.. A while back I made a custom enchants plugin, right? And it included a lifesteal enchantment (very original) which worked along the lines of

    double newHealth = e.getDamage / 5
    double Health = e.getEntity.getHealth + newHealth;
    e.getEntity().setHealth(health);

    And I'm not gonna lie it worked, however, if I do this:

    public void kkk(EntitySpawnEvent e){
    if(e.getEntity().getName().equals(MobEngine.getWeakzombie())){
    int health = 5;
    e.getEntity().WHY
    }
    }

    where it says WHY the method setHealth doesn't work (In this case I was going to do setMaxHealth()) so uhh..

    How do I make it work?

    also "LEARN JAVA" incoming
     
  2. I believe EntitySpawnEvent.getEntity() returns a simple Entity class. You must check if the returned object is an instanceof Damageable and then cast it to see health methods. In newer spigot builds, getMaxHealth() is deprecated for Damageable entities. To appropriately get the max health, you'll need to instead check if the entity is an instanceof LivingEntity, then get the max health attribute value using LivingEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue().
     
    • Like Like x 1
  3. Thanks! Let me check if it works
     
  4. Could you use code tags, and show the entire method please?

    Edit: ^^ Entire method code to make sure that you are performing the right checks on the entity.
     
  5. ugh it still doesnt work...
     
  6. I mentioned the instanceof checking, but I forgot to say that you need to cast afterwards:
    Code (Java):
    if(event.getEntity() instanceof LivingEntity){
        ((LivingEntity)event.getEntity()).getHealth();
    }
     
    • Like Like x 1
  7. That is literally the entire class besides

    public class yadayada extends JavaPlugin implements Listener
     
  8. Oh, ok. Thanks!
     
  9. Unrelated but you seem to know your stuff.

    I continued with this:
    @EventHandler
    public void kkk(EntitySpawnEvent e){
    if(e.getEntity().getName().equals(MobEngine.getWeakzombie())){
    if(e.getEntity() instanceof LivingEntity){
    ((LivingEntity)e.getEntity()).setHealth(5);
    if(e.getEntity()instanceof Monster){
    ((Monster)e.getEntity()).setDamage(3);
    }
    }
    }
    } and set damage isn't a method(I already knew) so uh

    How does one set a mobs damage?
     
  10. That's another value accessible through attributes.
    Code (Java):
    //Get the entity's GENERIC_ATTACK_DAMAGE attribute
    AttributeInstance dmgAttribute = livingEntity.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE);
    //Not all living entities have this attribute, so a null check is necessary. Example: Pigs
    if(dmgAttribute != null){
       //To completely override its damage (instead of modifying the current value), change the base value.
       dmgAttribute.setBaseValue(3.0);
    }
     
    • Like Like x 1
  11. Thanks! You are the man
     
    • Friendly Friendly x 2