Enable explosion for only one block

Discussion in 'Plugin Development' started by Doknesss, Aug 12, 2019.

Thread Status:
Not open for further replies.
  1. Hi guys, thought about making plugin that will allow for exploding just one block type, I mean when
    when something explodes then it will not destroy anything pose diamond blocks and i wrote code like this


    @EventHandler(priority = EventPriority.HIGHEST)
    public void TylkoDiaxy(BlockExplodeEvent event) {
    Bukkit.broadcastMessage("1"); //debugs ofc
    for (Block block : event.blockList()) {
    Bukkit.broadcastMessage("2");
    if (block.getType() == Material.DIAMOND_BLOCK) {
    Bukkit.broadcastMessage("diament");
    e.setCancelled(true);
    }else {
    Bukkit.broadcastMessage("none");
    }
    }
    Bukkit.broadcastMessage("3");

    }

    and in this case i can see just number 1 and 3

    I dont know what can i do to save everything without diamond blocks in explosion
     
  2. Offline

    CraftCreeper6

    @Doknesss
    If you want to save everything EXCEPT Diamond Blocks then do the inverse of what you're doing now. Instead of
    Code:
    block.getType() == Material.DIAMOND_BLOCK
    Do:
    Code:
    block.getType() != Material.DIAMOND_BLOCK
    EDIT: Potential misunderstanding of OP, blocklist is empty if you can only see 1 and 3.
     
  3. Offline

    KarimAKL

    @Doknesss If you only see '1' and '3', then event.blockList() is empty. You can also break out of the loop after confirming that it contains a diamond.
     
  4. I don't know what block triggers the BlockExplodeEvent, maybe try the EntityExplodeEvent as it works for almost every type of explosion. And then modify the blockList instead of cancelling the event
     
  5. how its possible the list is empty?

    but explosion about we are talking Im making by createExplosion() so its undetectable for EntityExplodeEvent
     
  6. Then please post your createExplosion method

    EDIT: and the explosion does destroy blocks?
     
    Last edited: Aug 12, 2019
  7. yes


    @EventHandler
    public void onProjectileHit(ProjectileHitEvent e){
    final Projectile damager = e.getEntity();
    damager.getShooter();
    if(e.getEntity() instanceof Arrow){
    e.getEntity().getLocation().getWorld().createExplosion(e.getEntity().getLocation(), 5f);
    e.getEntity().remove();
     
  8. I tested your exact code, except I removed the blocks from the blockList instead of cancelling the event and it worked just fine. Do you have any other plugins on the server? If so, remove them since that is the only way the blockList can be empty in my opinion
     
  9. oh yes - another plugin was a problem you have right but now i have problem with not destroing diamond blocks

    replaced

    e.setCancelled(true);

    to
    event.blockList().remove(block);

    but this do nothing
     
  10. Take a look at the log. You probably got an CurrentModification exception. In the head of the for loop, create a new ArrayList. Do not modify the same instance of an List while looping over it.
     
Thread Status:
Not open for further replies.

Share This Page