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?
Code (Java): @EventHandler public void onChange(EntityChangeBlockEvent e) { if (is explosion block) e.setCancelled(true); }
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
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
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); } } } }
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.