Get Entity Killer onEntityDeath

Discussion in 'Plugin Development' started by Pavilliox, Oct 2, 2011.

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

    Pavilliox

    I'm new to Plugin Development and Java (I'm a PHP Programmer so I know programming concepts, OOP, etc.

    I'm developing a novelty thing for practice which makes Creepers drop a cake when you kill them, and I'm trying to figure out how to get the person who killed the creeper so I can show a message on their screen.

    My current code is:
    Code:
    public void onEntityDeath(EntityDeathEvent event) {
            if(event.getEntity() instanceof Creeper) {
                LivingEntity monster = (LivingEntity) event.getEntity();
                monster.getWorld().dropItemNaturally(monster.getLocation(), new ItemStack(Material.CAKE, 1));
                plugin.logInfo(cause.toString());
                plugin.logInfo("Creeper dropped Cake at X:" + monster.getLocation().getX() + " Z:" + monster.getLocation().getZ());
            }
        }
     
  2. Offline

    Darkman2412

    I think you need to use EntityDamageEvent.
    Then add
    Code:
    if(!(event instanceof EntityDamageByEntityEvent)||(event.getDamage()<event.getEntity.getHealth())
        return;
    
    EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) event;
    Then change every 'event.*' underneath that code with 'e'.
     
  3. Offline

    Pavilliox

    That just seemed to throw me errors about data types etc. D:
     
  4. Offline

    Darkman2412

    Can you post the error + code?
     
  5. Offline

    Pavilliox

    I've changed it back now D:
    any chance you can change my original code to what it should be and I'll try it?
     
  6. Offline

    Darkman2412

    Code:
    public void onEntityDamage(EntityDamageEvent e) {
        if(!(e instanceof EntityDamageByEntityEvent)||(e.getDamage()<e.getEntity().getHealth()) // Check if a entity damaged it and that the entity died
            return;
    
        EntityDamageByEntityEvent event = (EntityDamageByEntityEvent) e;
        if(!(event.getDamager instanceof Player)) // Check if the damager is a player.
            return;
    
        Player player = (Player) event.getPlayer();
        if(event.getEntity() instanceof Creeper) {
            LivingEntity monster = (LivingEntity) event.getEntity();
            monster.getWorld().dropItemNaturally(monster.getLocation(), new ItemStack(Material.CAKE, 1));
            player.sendMessage("Happy birthday"); // Message ;)
            plugin.logInfo(cause.toString());
            plugin.logInfo("Creeper dropped Cake at X:" + monster.getLocation().getX() + " Z:" + monster.getLocation().getZ());
        }
    }
     
  7. Offline

    Pavilliox

    I've had to make a few changes, but there's an error with the getHealth() method - it isn't an available method.
     
  8. Offline

    qsysmine

    For the getHealth() method, it is part of the Damageable entity class, and you will have to cast e.getEntity() to Damageable
     
  9. Offline

    kreashenz

    qsysmine .. Why..? 2 year old post, mate. Check the dates.
     
  10. Offline

    qsysmine

    Oh. Sorry.
     
  11. Offline

    Levi2251

    get the killer by:
    Code:
    public void onEntityDeath(EntityDeathEvent event) {
            if(event.getEntity() instanceof Creeper) {
                String KillerName = event.getEntity().getKiller().getName();
                Entity ent = event.getEntity().getKiller();
            }
        }
     
Thread Status:
Not open for further replies.

Share This Page