Get entity death from onEntityDamage hook

Discussion in 'Plugin Development' started by DiddiZ, Apr 12, 2011.

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

    DiddiZ

    I know, there are many posts about this, but I still have an issue.
    I try to figure out when an entitiy (monster or player) killed another entity (monster, player or animal). So far the code works fine, but when a grab a sword and hit a creeper (for example) a few times, the message (diddiz killed creeper) shows twice, when wait a bit between the hits, the message shows once as intended.
    Hits on a dead entity a catch with victim.getHealth() <= 0.
    Code:
    @Override
    public void onEntityDamage(EntityDamageEvent event) {
        if (event.isCancelled())
            return;
        if (!(event instanceof EntityDamageByEntityEvent))
            return;
        if (!(event.getEntity() instanceof LivingEntity))
            return;
        LivingEntity victim = (LivingEntity)event.getEntity();
        Entity killer = ((EntityDamageByEntityEvent)event).getDamager();
        if (!(victim instanceof Player) && !(killer instanceof Player)) //Neither victim nor killer are players
            return;
        if (victim.getHealth() - event.getDamage() > 0) //Victim survives the damage
            return;
        if (victim.getHealth() <= 0 ) //Victim was alredy dead
            return;
        log.info(killer.getEntityId() + " killed " + victim.getEntityId());
    }
    Does anyone know something about this issue?
     
  2. Offline

    phondeux

    If it helps i'm having the same experience - it seems like if you swing wildly at a creature and more than one hit 'kills it' then it registers each hit as putting it under zero and reports it. I'm doing this;
    Code:
    if (damager instanceof Player){
                 player = (Player) damager;
                 String killMessage = "";
                 if ((mobDamage >= ((LivingEntity) event.getEntity()).getHealth())){
                     if(event.getEntity() instanceof Sheep) {
                         killMessage = "You killed a Sheep";
                     }
                     if(event.getEntity() instanceof Pig) {
                         killMessage = "You killed a Pig";
                     }
                 }
                 player.sendMessage(killMessage);
                 return;
             }
     
  3. Offline

    oliverw92

    I had this issue. I got around it by tracking all damages in lists:

    Code:
        private HashMap<String, String> damageList = new HashMap<String, String>();
    
        public void onEntityDamage(EntityDamageEvent event) {
            if (event.isCancelled())
                return;
            if(event.getEntity() instanceof Player) {
                Player victim = (Player) event.getEntity();
                String attacker = null;
                switch (event.getCause()) {
                    case ENTITY_ATTACK:
                        EntityDamageByEntityEvent attackEvent = (EntityDamageByEntityEvent) event;
                        Entity attackEntity = attackEvent.getDamager();
                        if (attackEntity instanceof Player) {
                            Player attackPlayer = (Player) attackEntity;
                            attacker = attackPlayer.getName();
                        }
                        else
                            attacker = "default";
                        break;
                    default:
                        attacker = "default";
                        break;
                }
                if (damageList.get(victim.getName()) != null)
                    damageList.remove(victim.getName());
                damageList.put(victim.getName(), attacker);
            }
        }
    
        public void onEntityDeath(EntityDeathEvent event) {
            Entity entity = event.getEntity();
            if (entity instanceof Player) {
                Player victim   = (Player) entity;
                Location loc    = victim.getLocation();
                String attacker = damageList.get(victim.getName());
                if (attacker != "default")
                    //Do whatever you want with the info here
            }
        }
    When a player dies, you can find out what killed them. If you want to track more than just players and 'default' as attackers, add damage types to the cause switch and set 'attacker' to whatever you want.
     
  4. Offline

    DiddiZ

    Hm, good workaround. Not the solution I hoped for, but will still work.
    Thanks
     
Thread Status:
Not open for further replies.

Share This Page