Check if Entity Has Potion Effects

Discussion in 'Plugin Development' started by 6ingie, May 22, 2020 at 4:08 AM.

  1. Offline

    6ingie

    I'm trying to see if a Mule has Slow Falling. I feel like I'm so close but yet so far away with this...

    Code:
    @EventHandlerpublic void onEntityDeath (EntityDeathEvent event) {
         for (EntityType e : EntityType.values()) {
              if (event.getEntity().getType().equals(EntityType.MULE)){
                  EntityPotionEffectEvent.Action.ADDED(SLOW_FALLING)//<-ERROR HERE = true;}
         //do stuff here
         }
    }
    
    I'd also like for the Mule to double what ever is in it's inventory on death. So if someone could help with that, I'd love you forever.
     
    Last edited: May 22, 2020 at 9:50 AM
  2. Offline

    Legendcreeper

    Code:
    event.getEntity().hasPotionEffect(PotionEffectType.SLOW_FALLING)
    That works for me

    And for
    You could try
    Code:
    for(ItemStack itemstack : event.getDrops())
         event.getEntity().getWorld().dropItemNaturally(event.getEntity().getLocation(), itemstack);
    
    (I am sure that that isn't the best way to do it, but it works)
     
    Last edited: May 22, 2020 at 10:38 AM
    6ingie likes this.
  3. Offline

    6ingie

    It works, but way too much xD.

    Code:
    @EventHandlerpublic void onEntityDeath (EntityDeathEvent event) {
         for (EntityType e : EntityType.values()) {
              if (event.getEntity().getType().equals(EntityType.MULE)){
              //EntityPotionEffectEvent.getEntity(SLOW_FALLING) = true;
              event.getEntity().hasPotionEffect(PotionEffectType.SLOW_FALLING);}
              for(ItemStack itemstack : event.getDrops())
              event.getEntity().getWorld().dropItemNaturally(event.getEntity().getLocation(), itemstack);}
    }
    }
    
    It doesn't detect if it's in a mules inventory(killed 1 horse and it dropped like 28k saddles) Also thats another issue lol. It drops like 20,000 items, not doubled. Maybe not 20k, but you get the point. It doesn't double...

    How would I go about just "technically" killing the entity twice? That seems like it'd be way easier.
     
    Last edited: May 22, 2020 at 12:41 PM
  4. Offline

    bowlerguy66

    @6ingie Why are you looping through all of the EntityTypes? Aren't you just trying to detect if the entity that died is a Mule?
     
  5. Offline

    6ingie

    I thought that's what this was doing:
    if (event.getEntity().getType().equals(EntityType.MULE))
     
  6. Offline

    Legendcreeper

    You don't have it in the If-Clause
    And also, put the for-Loop (The one for Item-Duplication) in the If-Clause, or else it will always be called.

    And because of the mass amount of items, removing the EntityType-Loop should fix that, an if it doesn't try dropItem(..)
     
    Last edited: May 22, 2020 at 4:07 PM
  7. Offline

    bowlerguy66

    That's why you're items are multiplying by way more than you want, since you're looping through each type of entity, all the code inside the loop gets called over and over again even if the EntityType has nothing to do with Mules. If you remove the for loop the code inside should be fine because it doesn't look like you're using the EntityType 'e' you declared.
     
  8. Offline

    6ingie

    Code:
    @EventHandler
    public void onEntityDeath (EntityDeathEvent event) {
    EntityType e :/*<-- ; expected there*/ EntityType.values(); {
    event.getEntity().getType().equals(EntityType.MULE);{
    event.getEntity().hasPotionEffect(PotionEffectType.WATER_BREATHING);}
    ItemStack itemstack :/*<-- ; expected there*/ event.getDrops();
    event.getEntity().getWorld().dropItemNaturally(event.getEntity().getLocation(), itemstack);
    }
    }
    }
    
    Ok so how is this looking? Also, thank you very much for helping me out so far.
     
  9. Offline

    Legendcreeper

    I think you just removed the 'for'-key and not the whole loop. You have to remove the whole loop.
    And the second 'for'-key should stay, but that loop should be in the If.
    Same with the If, that key can stay.
     
  10. Offline

    6ingie

    Code:
    @EventHandlerpublic void onEntityDeath (EntityDeathEvent event) {
         EntityType.values(); {
              event.getEntity().getType().equals(EntityType.MULE);{
              event.getEntity().hasPotionEffect(PotionEffectType.WATER_BREATHING);}
         ItemStack itemstack :/*<-- ; expected there*/ event.getDrops();
         event.getEntity().getWorld().dropItemNaturally(event.getEntity().getLocation(), itemstack);}
    }
    
    So would this be correct for the first one? The second one, I don't understand
     
  11. Offline

    Legendcreeper

    No, you would still have a part of the for-loop. And for the second one, i'm sorry, english is not my first language.
    I usually don't spoonfeed, but i think you don't understand what i mean.

    Code:
    @EventHandler
    public void onEntityDeath (EntityDeathEvent event) {
       if(event.getEntity().getType().equals(EntityType.MULE) & event.getEntity().hasPotionEffect(PotionEffectType.SLOW_FALLING)) {
         for(ItemStack item : event.getDrops()) {
           event.getEntity().getWorld().dropItem(event.getEntity().getLocation(), item);
         }
       }
    }
    
    This would be the result, try to understand what i did here.
     
    6ingie likes this.
  12. Offline

    6ingie

    I'm sorry that I couldn't really understand you. Thank you very much for helping me out. I understand what you mean now.
     

Share This Page