Solved How to prevent specific block damaged by explosion

Discussion in 'Spigot Plugin Development' started by KohakuSaintCrown, May 27, 2017.

  1. I know i need use EntityExplodeEvent to prevent the damage by explotion, but for example, i need know how to prevent dont destroy a especific block in a explotion. for example, creeper or a TNT explode, destroy all exept a especific block (chest, cake, diamond block...)
     
    • Like Like x 1
    • Agree Agree x 1
  2. Code (Text):
    @EventHandler
        public void onEntityExplode(EntityExplodeEvent event) {
            if (event.getEntity() instanceof Creeper) {
                for (Block block : event.blockList()){
                    if(block.getType() == YOURTYPE){
                       
                    }
                }
            }
        }
     
    • Agree Agree x 1
  3. Never do that! It will lead to ConcurrentModificationExceptions.

    Use this:
    Code (Text):
    @EventHandler
        public void onEntityExplode(EntityExplodeEvent event) {
            if (event.getEntity() instanceof Creeper) {
                for (Block block : event.blockList().toArray(new Block[event.blockList().size()])){
                    if(block.getType() == YOURTYPE){
                     
                    }
                }
            }
        }
     
    • Like Like x 2