Solved Adding a Potion Effect to a player when they kill someone. [BROKEN CODE NEED FIX]

Discussion in 'Plugin Development' started by Jamie Marks, Dec 24, 2012.

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

    Jamie Marks

    Hey. i have this code
    Code:
        @EventHandler(priority=EventPriority.MONITOR, ignoreCancelled=true)
          public void onPlayerDeath(PlayerDeathEvent event){
              if (event.getEntity().getLastDamageCause() == null) {
                return;
              }
     
              Player dead = event.getEntity();
              Player killer = event.getEntity().getKiller();
              ItemStack itemInHand = killer.getItemInHand();
             
     
              EntityDamageEvent.DamageCause cause = event.getEntity().getLastDamageCause().getCause();
              if ((cause == EntityDamageEvent.DamageCause.ENTITY_ATTACK || cause == EntityDamageEvent.DamageCause.PROJECTILE) && (killer != null) && (CustomItem.getLore(itemInHand).toString().equalsIgnoreCase("Life Steal I"))){
                  killer.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 10, 10), true);
                  }
             
          }
    what it should do is apply regeneration to the killer if there item lore is "Life Steal I". looking at it im not 100% sure why it doesn't work. Help would be appreciated.

    In real need of help for this, need it done before tonight. that code should work, I am so confused XD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
    ITZ_SHIVA likes this.
  2. Offline

    fireblast709

    getKiller() returns null if the player died due another cause then a player
     
  3. Offline

    Jamie Marks

    yeah, i use this code to drop skulls from players when they are killed by other players. woks perfectly i did some changes to the code
    Code:
        @EventHandler(priority=EventPriority.MONITOR, ignoreCancelled=true)
          public void onPlayerDeath(PlayerDeathEvent event){
              if (event.getEntity().getLastDamageCause() == null) {
                return;
              }
     
     
              Player dead = event.getEntity();
              Player killer = event.getEntity().getKiller();
              ItemStack itemInHand = killer.getItemInHand();
              String[] lore = CustomItem.getLore(itemInHand);
              String lores = lore.toString();
             
     
              EntityDamageEvent.DamageCause cause = event.getEntity().getLastDamageCause().getCause();
              if ((cause == EntityDamageEvent.DamageCause.ENTITY_ATTACK || cause == EntityDamageEvent.DamageCause.PROJECTILE) && (killer != null)){
                  if (lores.equalsIgnoreCase("Life Steal I")){ // without this line it works perfectly 
                  killer.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 10, 10), true);
                  }
                  }
             
          }
    it works fine if I don't have the lore stuff there but im not sure. why it wont work.
     
  4. Offline

    skipperguy12

    Code:
        @EventHandler(priority=EventPriority.MONITOR, ignoreCancelled=true)
          public void onPlayerDeath(PlayerDeathEvent event){
              if (event.getEntity().getLastDamageCause() == null||event.getEntity().getKiller() == null) {
                return;
              }
     
              Player dead = event.getEntity();
              Player killer = event.getEntity().getKiller();
              ItemStack itemInHand = killer.getItemInHand();
             
     
              EntityDamageEvent.DamageCause cause = event.getEntity().getLastDamageCause().getCause();
              if ((cause == EntityDamageEvent.DamageCause.ENTITY_ATTACK || cause == EntityDamageEvent.DamageCause.PROJECTILE) && (killer != null) && (CustomItem.getLore(itemInHand).toString().equalsIgnoreCase("Life Steal I"))){
                  killer.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 10, 10), true);
                  }
             
          }
    Just did what fire said. Might not work, I'm on iPad and it's early in the morning (can't think 3:)
     
  5. Offline

    pzxc

    You did a good job checking if there was a killer (killer != null) before adding the potion effect, but you forgot to check it before you look for the killer's item in hand:

    ItemStack itemInHand = killer == null ? null : killer.getItemInHand();
     
  6. Offline

    fireblast709

    Or to prevent any future NPEs,
    Code:
    ItemStack itemInHand = killer == null ? new ItemStack(Material.AIR, 0) : killer.getItemInHand();
     
  7. Offline

    Jamie Marks

    Thanks for your input guys! really helped me out. just updated the code to the latest Beta, love the new API stuff :p
     
Thread Status:
Not open for further replies.

Share This Page