No Idea Why This Is Giving Error...

Discussion in 'Plugin Development' started by Evonoucono, Aug 27, 2015.

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

    Evonoucono

    Code:
        @EventHandler
        public void onEntityDeath (EntityDeathEvent event){
         
            Entity entity = (Entity) event.getEntity();
         
            Entity killer = (Entity) entity.getLastDamageCause();
    I looked at the error when an entity died, and tracked it back to this last line of code... I have no idea why it throws an error.... Any help/suggestions?
    edit It says Caused by: java.lang.ClassCastException:
     
    Last edited: Aug 27, 2015
  2. Offline

    Boomer

    Would help if you indicated what error it did throw...
     
  3. Offline

    Elimnator

    The error is caused by:
    Code:
    Entity killer = (Entity) entity.getLastDamageCause();
    getLastDamageCause() get the cause, not the entity that did the damage.
    So, it could be fall, or drown, ect.
     
  4. Offline

    au2001

    @Evonoucono Check if the EntityDamageEvent is an EntityDamageByEntityEvent.
    If so, use EntityDamageByEntityEvent#getDamager().
     
  5. Offline

    Evonoucono

    I need to get the killer when an entity dies, not the damager when an entity gets hurt
     
  6. Offline

    au2001

    @Evonoucono Entity#getLastDamageCause() returns an EntityDamageEvent though.
     
  7. Offline

    Gonmarte

    @Evonoucono u should use the EntityDamageByEntityEvent instead of EntityDamageEvent.
    So u need to check if the killer is a player and if the entity that got hitted is a player, and then u create the variables and cast!
     
  8. Offline

    mrgreen33gamer

    @Gonmarte

    Check for player death should go like this:

    Code:java
    1.  
    2. @EventHandler
    3. public void playerDeath(PlayerDeathEvent event){
    4. if(event.getEntity() instanceof Player && event.getEntity().getKiller() instanceof Player){
    5. Player died = (Player) event.getEntity();
    6. Player killer = (Player) died.getKiller();
    7. }
    8. }
    9.  


    EDIT:

    I think the event should cast even when they die from fall, lava, etc.

    Getting last damager, just cast EntityDamageByEntityEvent and add damager and player to HashMap.

    Then check for the DeathEvent, get the killer/damager name, cast.
     
  9. Offline

    xMrPoi

    If you're going for a player kill event, go for the PlayerDeathEvent. You can get the victim and the killer by checking if the event.getEntity().getKiller() instanceof Player.
     
Thread Status:
Not open for further replies.

Share This Page