Solved Remove drops

Discussion in 'Plugin Development' started by markototh123, Jul 21, 2015.

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

    markototh123

    Hey, i'm currently working on a plugin, and one part of it i can't seem to figure out . When you throw a snowball and it hits the ground it explodes, giving poison to any player standing within a radius of 10 blocks for 10 seconds.
    The part that is causing difficulties is that i would like to remove the item drops caused to drop by the explosion.

    Any ideas?

    My Code:
    Code:
    public class BombListener implements Listener {
    
        @EventHandler
        public void onProjectileHit(ProjectileHitEvent e) {
    
            Projectile po = e.getEntity();
            if ( !(po instanceof Snowball) ) return;
    
            Snowball so = (Snowball) po;
    
            so.getWorld().createExplosion(so.getLocation(), 2.0F);
    
            for (Entity en : so.getNearbyEntities(10, 10, 10)) {
    
                if (en instanceof Player) {
    
                    Player p = (Player) en;
    
                    p.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 120, 1));
    
                    // TODO: Add item drop remove
                }
            }
        }
    }
     
  2. @markototh123
    Perhaps try setting the explosion yield to 0, and damage the players manually. Or listen for EntityExplodeEvent, and clear the block list.
     
  3. Offline

    Agentleader1

    Do you want the explosion to create a griefing explosion? There is actually a method that creates an explosion with damage that doesn't grief. createExplosion(x, y, z, power, false, false); Otherwise, you can grab the blocks and it's drops and in a delayed task; grab all nearby entities and see if they are items and see if the items are similar enough as the block drops before the explosion.
     
  4. Offline

    markototh123

    @Agentleader1 , @Assist thanks for the tips. I actually combined the suggestions.
    I grabbed the entities in the range of the explosion and than placed them in a list( List<Entity> ), checked if they where items, and removed them.

    New code:
    Code:
    package me.markototh.kit;
    
    import org.bukkit.entity.*;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    import java.util.List;
    
    public class BombListener implements Listener {
    
        @EventHandler
        public void onProjectileHit(ProjectileHitEvent e) {
    
            Projectile po = e.getEntity();
            if ( !(po instanceof Snowball) ) return;
    
            Snowball so = (Snowball) po;
    
            so.getWorld().createExplosion(so.getLocation(), 2.0F);
    
            for (Entity en : so.getNearbyEntities(10, 10, 10)) {
    
                if (en instanceof Player) {
    
                    Player p = (Player) en;
    
                    p.addPotionEffect(new PotionEffect(PotionEffectType.HARM, 3, 2));
    
                    p.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 120, 1));
    
                    // clear entities
    
                    List<Entity> entList = en.getNearbyEntities(10, 10, 10);
    
                    for(Entity current : entList) {
    
                        if (current instanceof Item) {
    
                            current.remove();
                        }
                    }
                }
            }
        }
    }
    
     
Thread Status:
Not open for further replies.

Share This Page