EntityExplodeEvent remove block from explosion

Discussion in 'Spigot Plugin Development' started by Realm, Apr 28, 2017.

  1. Is there a way to stop a single block, or rather a type of block, from getting damaged in an explosion, even after they change type?
    This is my attempt at it and it doesnt seem to work...



    Code (Text):
        @EventHandler
        public void onBlockExplode(EntityExplodeEvent e) {
           Iterator<Block> iterator = e.blockList().iterator();

            while (iterator.hasNext()) {
                    Block block = iterator.next();

                   if (condition()) {
                        new BlockCountdown(block);
                        iterator.remove();
                     }
              }
    }
    BlockCountdown simply changes the type of the block, along with a few other unrelated things..
    calling iterator.remove() on that block then doesnt work, because the type has changed, any way around this? ;/
     
  2. Not sure if it works but I think it would be as simple as just checking the block type and then just canceling the event.
    Code (Text):
        @EventHandler
        public void onBlockExplode(EntityExplodeEvent e) {
            Iterator<Block> iterator = e.blockList().iterator();

            while (iterator.hasNext()) {
                Block block = iterator.next();
                if (block.getType().equals(Material.STONE)) {
                    e.setCancelled(true);
                }
            }
        }
     
  3. The block still should be the same, however if you don't use iterator and create a Set<Block> removed.

    Add any changed blocks to that list and before you return loop through the set of removed and remove those blocks from the block list then.


    Sent from my iPhone using Tapatalk
     
  4. WAS

    WAS

    Use it's location? Location doesn't care for block type. It's just a location of a block.