How do I give a player EXP when they kill a zombie?

Discussion in 'Plugin Development' started by xDGaming, Jan 25, 2014.

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

    xDGaming

    Im making a plugin and when they kill a Zombie I want it to give them a certain ammount of EXP but not drop any xp how can I do this?
     
  2. Offline

    xDGaming

  3. Well, first off, you need to use a entitydeathevent and check if the entity dying is a zombie. If so, clear the drops(event.clearDrops() I think) and get the killer of the zombie(event.getKiller()) and then give the killer more xp(killer.setExp(killer.getExp + 40). Remember to add null checks for the death cause so that if a zombie dies from falling it does not through an NPE.

    NOTE: I don't think its .getExp but its something close.
     
  4. Offline

    xDGaming

    can you show me an example? (sorry I'm kinda lost)
     
  5. Offline

    NonameSL

    Its actually really simple:
    Code:java
    1. @EventHandler
    2. public void onEntityDeath(EntityDeathEvent e){
    3. if(e.getEntity() instanceof Zombie){
    4. Player killer = e.getEntity().getKiller();
    5. e.setDroppedExp(0);
    6. killer.setExp(killer.getExp()+40/*Insert amount instead of 40*/);
    7. }
    8. }
     
  6. You might want to add a check to make sure the killer is not null and is a player.
    Code:java
    1. @EventHandler
    2. public void onEntityDeath(EntityDeathEvent e){
    3. if(e.getEntity() instanceof Zombie){
    4. if (e.getEntity().getKiller() != null && e.getEntity().getKiller() instanceof Player) {
    5. Player killer = e.getEntity().getKiller();
    6. e.setDroppedExp(0);
    7. killer.setExp(killer.getExp() + 40/* Insert amount instead of 40 */);
    8. }
    9.  
    10. }
    11. }
     
  7. Offline

    NonameSL

    I am not sure but I think that getKiller() returns a Player, but you are right, didnt think of that.
     
  8. Offline

    bennie3211

    NonameSL that doesnt always returns a player, like when the entity falls down by water, then its null or if a creeper explodes and it kills the zombie, its not a player :D
     
Thread Status:
Not open for further replies.

Share This Page