EntityExplodeEvent - Looking for a way to protect entities

Discussion in 'Plugin Development' started by Maxx_Qc, Jan 4, 2016.

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

    Maxx_Qc

    Hi guys, I have a problem.
    Show Spoiler

    Code:
    package com.maxx.events;
    
    import java.util.Iterator;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.block.Block;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityExplodeEvent;
    
    import com.maxx.abc.ABC;
    import com.maxx.abc.utils.Util;
    
    public class EntExplodeEvent implements Listener {
        public EntExplodeEvent(ABC plugin) {
            Bukkit.getPluginManager().registerEvents(this, plugin);
        }
       
        @EventHandler
        public void onCreeperExplode(EntityExplodeEvent e) {
            List<Block> destroyed = e.blockList();
            Iterator<Block> it = destroyed.iterator();
            while(it.hasNext()) {
                Block block = it.next();
                if(Util.isInTemple(block.getLocation()))
                    it.remove();
            }
        }
    }

    When a creeper explodes (or tnt), it doesn't destroy any block in the temple BUT entities get killed.
    Before the explosion: http://i.imgur.com/ShJxZ1N.png
    After the explosion: http://prntscr.com/9m7qse
    I've been trying to find a way to protect entities in the temple but I couldn't find one.
    Please help, thank you!
     
  2. Offline

    Zombie_Striker

    @Maxx_Qc
    • Use EntityDamageEvent to check when the entity is damages
    • If the damage cause is Creeper Explosion
    • Cancel the event.
     
    Maxx_Qc likes this.
  3. Offline

    elian1203

    Well if you can't remove them from the list of blocks to destroy, you could run a for loop: for all the blocks broken, check if they are in the template, and place them back.
     
    Maxx_Qc likes this.
  4. Offline

    Maxx_Qc

    Show Spoiler

    Code:
    package com.maxx.events;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    
    import com.maxx.abc.ABC;
    import com.maxx.abc.utils.Util;
    
    public class EntDamageEvent implements Listener {
        private ABC plugin;
     
        public EntDamageEvent(ABC plugin) {
            this.plugin = plugin;
            Bukkit.getPluginManager().registerEvents(this, plugin);
        }
     
        @EventHandler
        public void onDamage(EntityDamageEvent e) {
            if(!plugin.gameInProgress && e.getEntity() instanceof Player)
                e.setCancelled(true);
            if(Util.isInTemple(e.getEntity().getLocation()) && e.getEntity() instanceof Player)
                e.setCancelled(true);
            if((e.getCause() == DamageCause.BLOCK_EXPLOSION || e.getCause() == DamageCause.ENTITY_EXPLOSION) && Util.isInTemple(e.getEntity().getLocation()))
                e.setCancelled(true);
        }
    }

    Thanks for your help :)
    Really appreciated

    EDIT: The player doesn't take damage but the paints and others entities do
    Help!
     
    Last edited: Jan 4, 2016
  5. Offline

    elian1203

    Oh my bad I misread, thought you were trying to prevent blocks from being broken.
     
  6. Offline

    Maxx_Qc

  7. Offline

    Zombie_Striker

    Well then, why can't you just use:
    Any just apply it to Paintings and other entities?
     
  8. Offline

    Maxx_Qc

    @Zombie_Striker I don't want to do this method with every entities...
    It's not optimized
     
  9. Offline

    Zombie_Striker

    @Maxx_Qc
    If you don't want to do that for every entity, then why not reverse it:
    Code:
    if ( ! e.getEntity() instanceof ENITY_YOU_DON'T_WANT)
     
  10. Offline

    Maxx_Qc

    @Zombie_Striker
    Because it's not working.
    I want it to do it with EVERY entities, including paints, players, armor stands, etc. but it's only working with players actually and that's the problem
     
  11. Offline

    Zombie_Striker

    @Maxx_Qc
    So you want this to apply to all entity types? If so, then just remove that boolean. That line is meant to get specific entities (whether it is to include or exclude them)
     
  12. Offline

    Maxx_Qc

    Show Spoiler

    Code:
    package com.maxx.events;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import com.maxx.abc.ABC;
    import com.maxx.abc.utils.Util;
    public class EntDamageEvent implements Listener {
        private ABC plugin;
        public EntDamageEvent(ABC plugin) {
            this.plugin = plugin;
            Bukkit.getPluginManager().registerEvents(this, plugin);
        }
        @EventHandler
        public void onDamage(EntityDamageEvent e) {
            if(!plugin.gameInProgress && e.getEntity() instanceof Player)
                e.setCancelled(true);
            if(Util.isInTemple(e.getEntity().getLocation()) && e.getEntity() instanceof Player)
                e.setCancelled(true);
            if((e.getCause() == DamageCause.BLOCK_EXPLOSION || e.getCause() == DamageCause.ENTITY_EXPLOSION) && Util.isInTemple(e.getEntity().getLocation()))
                e.setCancelled(true);
        }
    }

    This is my actual code.
    @Zombie_Striker What boolean do you want me to get rid of?
     
  13. Offline

    Zombie_Striker

    This statement is a boolean. If the entity is a player, it returns true. If not, it returns false. If you do not want if to check what the entity implements, then don't add that statement.
     
  14. Offline

    Maxx_Qc

    @Zombie_Striker Good point, it means it's not working at all...
    Is there any entity kill event?
     
  15. Offline

    mythbusterma

    @Maxx_Qc

    There's an EntityDeathEvent.
     
    Maxx_Qc likes this.
  16. Offline

    Maxx_Qc

  17. Offline

    mythbusterma

  18. Offline

    Maxx_Qc

  19. Offline

    Maxx_Qc

  20. Offline

    Zombie_Striker

  21. Offline

    Maxx_Qc

    @mythbusterma
    @Zombie_Striker
    Thank you guys but there's the solution:
    Code:
    public class HangingEvent implements Listener {
        public HangingEvent(ABC plugin) {
            Bukkit.getPluginManager().registerEvents(this, plugin);
        }
       
        @EventHandler
        public void onHangingBreak(HangingBreakEvent e) {
            if(Util.isInTemple(e.getEntity().getLocation()))
                e.setCancelled(true);
        }
    }
     
Thread Status:
Not open for further replies.

Share This Page