Remove fallen block after it has landed

Discussion in 'Spigot Plugin Development' started by DotRar, May 26, 2016.

  1. I have created a system that turns exploded blocks into fallingblocks and send them flying. However, when they land, I want to remove them. How would I do this?
     
    #1 DotRar, May 26, 2016
    Last edited: May 26, 2016
  2. Code (Java):
    @EventHandler
        public void onChange(EntityChangeBlockEvent e) {
            if (is explosion block)
                e.setCancelled(true);
        }
     
  3. Would this work?

    Code (Text):
    public static List<Block> blocks = new ArrayList<Block>();

        @EventHandler
        public void changeEvent(EntityChangeBlockEvent e) {
            Block b = e.getBlock();
            if(blocks.contains(b)) {
                b.setType(Material.AIR);
                blocks.remove(b);
            }
        }
    And in the explode event, add the block to blocks
     
  4. Well, no, you want to check the entity, not the landing block.
     
  5. So this?:

    Code (Text):
    public static List<Entity> entities = new ArrayList<Entity>();

        @EventHandler
        public void changeEvent(EntityChangeBlockEvent e) {
            Entity ent = e.getEntity();
            if(entities.contains(ent)) {
                e.setCancelled(true);
                entities.remove(ent);
            }
        }
    and add each FallingBlock to entities
     
  6. That should work

    Code (Text):
        public static List<Entity> entities = new ArrayList<Entity>();

        @EventHandler
        public void killFallingBlocks(EntityChangeBlockEvent e) {

            if (e.getEntityType() == EntityType.FALLING_BLOCK) {

                if (entities.contains(e.getEntity())) {
                    System.out.println("2");

                    final FallingBlock fl = (FallingBlock) e.getEntity();

                    if (fl.isOnGround()) {
                        Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {

                            @Override
                            public void run() {
                                fl.getWorld().getBlockAt(fl.getLocation()).setType(Material.AIR);

                            }
                        }, 1l);

                    }
                }
            }
        }
     
  7. joehot200

    Supporter

    Quick heads up, be wary of turning it into air: If the falling block lands in water or lava and then turns it into air, it can cause problems.