How to find out what player killed a mob?

Discussion in 'Plugin Development' started by Hoolean, Aug 15, 2012.

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

    Hoolean

    I need to know how to find out what player killed a mob, so I can then reward them with health. I just need the code to find out each time a mob dies, what player killed it. It is for a plugin I am working on. Thanks! :D
     
  2. Offline

    Michael Rhodes

    Hello,

    I have some code I use in my plugin that may help you.

    Code:
    @EventHandler
    public void onEntityDeath(final EntityDeathEvent event)
    {             
         if(event.getEntity() instanceof Monster)
         {       
             monsterEnt = (Monster) event.getEntity();
             mcPlayer = monsterEnt.getKiller();
             if(mcPlayer == null)
                 return;
             
             iStack = mcPlayer.getItemInHand();
             if(iStack.getType() == Material.FISHING_ROD)
             {    mcPlayer.sendMessage("[§2RPG§f] You can not gain anything by killing with a fishing rod.");
                 return;
             }
                ...
    
    So, basically Find out if your entity is a Monster first and then you can get that entities Killer. Make sure you check that it is in fact a player, otherwise this code will run ALOT as entities die,

    Hope this helps


    Michael
     
  3. Offline

    Hoolean

    I am trying to get it to add health when a player kills a monster. Something like this? Got some problem with permissions though... Any ideas? Eclipse suggested a few things to change, which are included in this:
    Code:
    public class MobListener implements Listener {
        @EventHandler
        public void onEntityDeath(EntityDeathEvent event) {
            if(event.getEntity() instanceof Monster)
            {     
                Monster monsterEnt = (Monster) event.getEntity();
                Object mcPlayer = monsterEnt.getKiller();
                if(mcPlayer == null)
                    return;
                if(((Object)mcPlayer).hasPermission("mobhealth.benefit")) {
                    if(((LivingEntity) mcPlayer).getHealth() < 20) {
                        int healthSet = ((LivingEntity) mcPlayer).getHealth()+2;
                        ((LivingEntity) mcPlayer).setHealth(healthSet);
                    }
              }
            }
        }
    }
     
  4. Offline

    Michael Rhodes

    Looks go so far.... there's just one thing... You are assuming that the mobs killer is Player, that's not always the case

    public class MobListener implements Listener {
    @EventHandler
    public void onEntityDeath(EntityDeathEvent event) {
    if(event.getEntity() instanceof Monster)
    {
    Monster monsterEnt = (Monster) event.getEntity();
    if(monsterEnt.getKiller() instanceof Player)
    {







    Player mcPlayer = (Player)monsterEnt.getKiller();
    if(mcPlayer == null)
    return;
    if(mcPlayer.hasPermission("mobhealth.benefit"))
    {
    if(mcPlayer.getHealth() < 20)
    {
    // int healthSet = mcPlayer.getHealth()+2;
    mcPlayer.setHealth( mcPlayer.getHealth()+2 ); <--- could do this as well
    }

    }
    }
    }


    Try that and see how it goes.

    I made the same mistake and ran into trouble when Zombies were killing villagers...

    Hope this helps, and feel free to ask any more questions.

    Michael
     
  5. Offline

    Hoolean

    Thanks! No errors from Eclipse now, so hopefully it will work perfectly. I'm 13 and this is one of my first plugins so thanks for helping me out.
     
  6. Offline

    Michael Rhodes

    Code:
    public class MobListener implements Listener {
     
        @EventHandler
        public void onEntityDeath(EntityDeathEvent event) {
            if(event.getEntity() instanceof Monster)
            {
                Monster monsterEnt = (Monster) event.getEntity();
                if(monsterEnt.getKiller() instanceof Player)
                {
                    Player mcPlayer = (Player)monsterEnt.getKiller();
                    if(mcPlayer == null)
                        return;
                    if(mcPlayer.hasPermission("mobhealth.benefit"))
                        if(mcPlayer.getHealth() < 20)
                            mcPlayer.setHealth( mcPlayer.getHealth()+2 );   
                }
            } // if(event.getEntity() instanceof Monster)
        } // public void onEntityDeath(EntityDeathEvent event)
    } // public class MobListener implements Listener

    There ya go. I removed some of the unneeded brackets and commented the closing brackets (that's just my style of coding....


    Let me know how it goes for you
     
  7. Offline

    Hoolean

    Didn't work. Think the main class was ignoring the listener so I implemented it into the main class. However, nothing happens when I kill a zombie. I added in some code that would tell me where it was going wrong by sending a message to the console but none of them displayed, so I assume that the listener is not working properly. Here is my main and only class file:
    Code:
    package com.puffybugs.MobMoney;
     
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Monster;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
     
    public class MobMoney extends org.bukkit.plugin.java.JavaPlugin implements Listener {
        public void onEnable() {
            getLogger().info("MobHealth has been enabled. :D");
            getServer().getPluginManager().registerEvents(this, this);
        }
        public void onEntityDeath(EntityDeathEvent event) {
            getLogger().info("Deathevent: Success");
            if(event.getEntity() instanceof Monster)
            {     
                Monster monsterEnt = (Monster) event.getEntity();
                Player mcPlayer = (Player)monsterEnt.getKiller();
                getLogger().info(mcPlayer+": Success");
                if(mcPlayer == null)
                    return;
                if(mcPlayer.hasPermission("mobhealth.benefit")) {
                    getLogger().info("Permissions: Sucess");
                    if(((LivingEntity) mcPlayer).getHealth() < 20) {
                        getLogger().info("Health Check: Success");
                        int healthSet = ((LivingEntity) mcPlayer).getHealth()+2;
                        ((LivingEntity) mcPlayer).setHealth(healthSet);
                    }
              }
            }
        }
        public void onDisable() {
            getLogger().info("MobHealth has been disabled. D:");
        }
    }
    
     
  8. Offline

    Michael Rhodes

    Hey, well I see one thing you are missing..... you need to put in "@EventHandler" right above the onEntityDeath

    You could have left it in it's own class and made it work, but for now just add the eventhandler above the method declaration...

    Also, try replacing all the code for that method with what I wrote for you... You have a lot of casting going on that is not needed
     
  9. Offline

    Hoolean

    OK thanks! I'm new to programming plugins so thank you for all of your help; I really value it. :D
     
  10. Offline

    piano9uber

    I'm using this, but I find that it says that OnDeathEvent doesn't work as a void. Can someone help me here?
     
Thread Status:
Not open for further replies.

Share This Page