Reputation: 23
I was going to regenerate all blocks that were destroyed by an TNT. I did that with the EntityExplosionEvent but when I get the blocks from the event.blockList(), their type is AIR.
Anyone who can help?
My EntityExplosionEvent:
@EventHandler
public void onExplode(EntityExplodeEvent e) {
Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new Runnable() {
@Override
public void run() {
for(Block b : e.blockList()) {
b.getLocation().getBlock().setType(b.getType());
b.getState().update();
}
}
}, 20*3);
}
Upvotes: 2
Views: 247
Reputation:
You're calling e.blockList
after x ticks, this will be called after the event. When you do, all blocks will have changed to air. A solution is to store all blocks outside the scheduler like this:
@EventHandler
public void onExplode(EntityExplodeEvent e) {
List<Block> blocks = e.blockList();
HashMap<Block, Material> blockMap = new HashMap<Block, Material>();
for(Block b : e.blockList()) {
blockMap.put(b, b.getType());
}
Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new Runnable() {
@Override
public void run() {
for(Block b : blocks) {
b.getLocation().getBlock().setType(blockMap.get(b));
b.getState().update();
}
}
}, 20*3);
}
Update: I changed to using a HashMap to store each block's previous material. Reason why is that block references remain consistent and block objects aren't cloneable. Note that my method only saves the previous material, not its data or state, you have to add that support yourself. Hope it helps!
Upvotes: 3