Solved Stopping Block From Dropping Item

Discussion in 'Plugin Development' started by KeybordPiano459, Feb 18, 2013.

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

    KeybordPiano459

    So here's what I'm trying to do. When there's an explosion, I want to check if a block blown up is snow or a snow block, and then if not, set it back to what it was. This code below works for just that, but I noticed that if the block isn't snow/snowblock, it still drops the item as if it were an explosion. Also, it doesn't reset signs. How do I fix those two problems?
    Code:java
    1. @EventHandler
    2. public void onEntityExplode(EntityExplodeEvent event) {
    3. if (event.getEntityType() == EntityType.PRIMED_TNT) {
    4. List<Block> blist = event.blockList();
    5. for (final Block block : blist) {
    6. final Material type = block.getType();
    7. if (type != Material.SNOW_BLOCK && type != Material.SNOW) {
    8. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    9. @Override
    10. public void run() {
    11. block.setType(type);
    12. }
    13. }, 1);
    14. } else {
    15. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    16. @Override
    17. public void run() {
    18. block.setType(type);
    19. }
    20. }, 2400L);
    21. }
    22. }
    23. }
    24. }
     
  2. Offline

    Zarius

    Rather than setting the block type back to normal, try:
    Code:
      if (..some check to see if we want to deny the block exploding...) {
        event.blockList().remove(block);
      }
    
    Hmm, just noticed you're scheduling the replacements - not sure how to work this into it. Perhaps in the main thread, remove the block as per above, then set it to air (to emulate the explosion) and let the scheduled task set it back to the original type?
     
  3. Offline

    KeybordPiano459

    That didn't work, I already tried that. It still blew the block up.
     
  4. Offline

    Zarius

    Hmm, looking at your code, when you use the blockList.remove() does it not give you a ConcurrentModificationException?

    I just doubled checked my code (here) and I take a copy of the list first to iterate over (as you can't remove items from the list you are currently iterating over).

    Eg:
    Code:
      List<Block> blockListCopy = new ArrayList<Block>();
      blockListCopy.addAll(event.blockList());
     
      for(Block block : blockListCopy) {
        if (drop.isDenied()) event.blockList().remove(block);
      }
    
     
  5. Offline

    KeybordPiano459

    It does =3
    But I fixed the huge problem thing awhile ago
    Code:java
    1. @EventHandler
    2. public void onEntityExplode(EntityExplodeEvent event) {
    3. Entity entity = event.getEntity();
    4. if (event.getEntityType() == EntityType.PRIMED_TNT) {
    5. List<Block> blist = event.blockList();
    6. Location loc = entity.getLocation();
    7. event.setCancelled(true);
    8. loc.getWorld().createExplosion(loc, 0);
    9. for (final Block block : blist) {
    10. final Material type = block.getType();
    11. if (block.getType() == Material.SNOW_BLOCK || block.getType() == Material.SNOW) {
    12. block.setType(Material.AIR);
    13. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    14. @Override
    15. public void run() {
    16. block.setType(type);
    17. }
    18. }, 2400L);
    19. }
    20. }
    21. }
    22. }
     
Thread Status:
Not open for further replies.

Share This Page