Skip to content

Instantly share code, notes, and snippets.

@jan-krueger
Last active August 29, 2015 14:01
Show Gist options
  • Save jan-krueger/0e1fbe2c664876229949 to your computer and use it in GitHub Desktop.
Save jan-krueger/0e1fbe2c664876229949 to your computer and use it in GitHub Desktop.
Regenerate Explosion
package <your-package>;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
public class ExplosionListener implements Listener {
private List<Location> physicBlock = new ArrayList<Location>();
@EventHandler
public void onExplode(final EntityExplodeEvent e) {
if(!(e.getEntity() instanceof TNTPrimed)) {
return;
}
final List<ExplodedBlock> blocks = new ArrayList<ExplodedBlock>();
for(Block b : e.blockList()) {
blocks.add(new ExplodedBlock(b));
if(b.getType() == Material.SAND || b.getType() == Material.GRAVEL) {
physicBlock.add(b.getLocation());
}
}
Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
private int count = 0;
@Override
public void run() {
if(blocks.size() > count) {
blocks.get(count).reset();
count++;
}
}
}, 120L, 5L);
}
@EventHandler
public void preventSandFall(BlockPhysicsEvent e){
if(!(physicBlock.contains(e.getBlock().getLocation()))) {
return;
}
e.setCancelled(true);
/*
* Ab hier bin ich mir nicht sicher, ob das dann noch funktioniert, falls nein einfach mal auskommentieren die Zeile
*
*/
physicBlock.remove(e.getBlock().getLocation());
}
}
class ExplodedBlock {
private Material m;
private Location l;
public ExplodedBlock(Block b) {
this.m = b.getType();
this.l = b.getLocation();
}
public Location getLocation() {
return this.l;
}
public Material getMaterial() {
return this.m;
}
public void reset() {
l.getBlock().setType(m);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment