Solved EntityExplodeEvent - Only destroy specific blocks?

Discussion in 'Plugin Development' started by TutorialMakerHD, Jun 15, 2013.

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

    TutorialMakerHD

    I want that creepers can destroy every block except glass.
    Code:
    @EventHandler
    public void onEntityExplode(EntityExplodeEvent event) {
        if (event.getEntity() instanceof Creeper) {
            for (Block block : event.blockList())
                event.blockList().remove(block);
        }
    }
    This is what i have at the moment, but it doesn't work!

    (Sorry for my bad english!)
    TutorialMakerHD | Lukas
     
  2. Offline

    Hoolean

    Code:
    @EventHandler
    public void onEntityExplode(EntityExplodeEvent event) {
        if (event.getEntity() instanceof Creeper) {
            for (Block block : event.blockList())
                if(block.getType() == Material.GLASS) {
                    event.blockList().remove(block);
                }
            }
        }
    }
    There ya' go! :p
     
  3. Offline

    xzKinGzxBuRnzx

    Simply check if the block is glass then. Like so...

    Code:
    for (Block block : event.blockList()) {
        if (block.getType() != Material.GLASS) {
            continue;
        }
        event.blockList().remove(block);
    }
     
  4. Offline

    Hoolean

  5. Offline

    TutorialMakerHD

    Oh i forgot to check if it is glass, but it doesn't work though?
    throws a ConcurrentModificationException ... :(
     
  6. Offline

    Hoolean

    TutorialMakerHD

    Oop! Of course it would, my bad! Let me just fix it :p

    Code:java
    1. @EventHandler
    2. public void onEntityExplode(EntityExplodeEvent event) {
    3. if (event.getEntity() instanceof Creeper) {
    4. for (Block block : new ArrayList<Block>(event.blockList()))
    5. if(block.getType() == Material.GLASS) {
    6. event.blockList().remove(block);
    7. }
    8. }
    9. }
    10. }


    Fixed! A concurrent modification exception occurs when you edit a list whilst looping through it :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
  7. Offline

    TutorialMakerHD

    Ah thank you! :D
     
    Hoolean likes this.
Thread Status:
Not open for further replies.

Share This Page