More efficient block optimizations

This commit is contained in:
mathiascode 2019-12-03 06:53:37 +02:00
parent 58e8197ef6
commit 6a06185eb5
2 changed files with 60 additions and 112 deletions

View file

@ -24,6 +24,8 @@ public class Main extends JavaPlugin {
static HashSet<Material> nonSolidSingularBlockList = new HashSet<>();
static HashSet<Material> nonSolidWallMountedBlockList = new HashSet<>();
static HashSet<Material> nonSolidWaterBlockList = new HashSet<>();
static HashSet<BlockFace> faces = new HashSet<>();
public void onLoad() {
/* Fill lists */
@ -652,6 +654,15 @@ public class Main extends JavaPlugin {
this.nonSolidBlockList.addAll(nonSolidSingularBlockList);
this.nonSolidBlockList.addAll(nonSolidWallMountedBlockList);
Collections.addAll(
faces,
BlockFace.NORTH,
BlockFace.SOUTH,
BlockFace.WEST,
BlockFace.EAST,
BlockFace.UP
);
saveResource("config.yml", false);
}

View file

@ -21,133 +21,70 @@ import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockFormEvent;
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.block.BlockRedstoneEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.plugin.java.JavaPlugin;
import com.destroystokyo.paper.event.block.BlockDestroyEvent;
class BlockPhysics implements Listener {
@EventHandler
void onBlockFromTo(BlockFromToEvent event) {
try {
event.getBlock().getState();
event.getToBlock().getState();
} catch (Exception exception) {
event.setCancelled(true);
return;
}
final double tps = Bukkit.getServer().getTPS()[0];
if (tps < 17) {
event.setCancelled(true);
return;
}
final Block block = event.getBlock();
final World world = block.getWorld();
final int radius = 5;
int blockCount = 0;
for (int x = -radius; x <= radius; x++) {
for (int y = -radius; y <= radius; y++) {
for (int z = -radius; z <= radius; z++) {
if (blockCount < 200) {
final Location blockLocation = new Location(world, block.getX() + x, block.getY() + y, block.getZ() + z);
final Block coordBlock = world.getBlockAt(blockLocation);
if (coordBlock.isLiquid() ||
coordBlock.getType() == Material.OBSIDIAN) {
blockCount++;
}
continue;
} else {
event.setCancelled(true);
}
void onBlockForm(BlockFormEvent event) {
if (event.getBlock().getType() == Material.LAVA ||
event.getBlock().getType() == Material.WATER) {
for (BlockFace face : JavaPlugin.getPlugin(Main.class).faces) {
if (event.getBlock().getRelative(face).getType() != Material.LAVA &&
event.getBlock().getRelative(face).getType() != Material.WATER) {
return;
}
event.setCancelled(true);
}
}
}
@EventHandler
void onBlockFromTo(BlockFromToEvent event) {
if (event.getBlock().getType() == Material.LAVA ||
event.getBlock().getType() == Material.WATER) {
boolean lavaFound = false;
boolean waterFound = false;
for (BlockFace face : JavaPlugin.getPlugin(Main.class).faces) {
if (event.getBlock().getRelative(face).getType() == Material.LAVA) {
lavaFound = true;
} else if (event.getBlock().getRelative(face).getType() == Material.WATER) {
waterFound = true;
}
if (lavaFound && waterFound) {
event.setCancelled(true);
}
}
}
}
@EventHandler
void onBlockDestroy(BlockDestroyEvent event) {
if (!event.getBlock().getType().isSolid()) {
for (BlockFace face : JavaPlugin.getPlugin(Main.class).faces) {
if (event.getBlock().getRelative(face).getType() != event.getBlock().getType()) {
return;
}
event.getBlock().setType(Material.AIR, false);
event.setCancelled(true);
}
}
}
@EventHandler
void onBlockPhysics(BlockPhysicsEvent event) {
final Material material = event.getChangedType();
final BlockState blockState = event.getSourceBlock().getState();
final BlockState blockStateSource = event.getSourceBlock().getState();
/*if (event.getSourceBlock().getBlockData().getAsString().length() > 3019) {
event.getSourceBlock().setType(Material.AIR);
} else if (event.getBlock().getBlockData().getAsString().length() > 3019) {
event.getBlock().setType(Material.AIR);
}*/
if (material == Material.FARMLAND) {
event.setCancelled(true);
} else if (blockState instanceof CommandBlock) {
blockState.update();
} else if (blockStateSource instanceof CommandBlock) {
blockStateSource.update();
} else if (event.getBlock().isLiquid()) {
final Block block = event.getBlock();
final World world = block.getWorld();
final int radius = 5;
int blockCount = 0;
for (int x = -radius; x <= radius; x++) {
for (int y = -radius; y <= radius; y++) {
for (int z = -radius; z <= radius; z++) {
if (blockCount < 200) {
final Location blockLocation = new Location(world, block.getX() + x, block.getY() + y, block.getZ() + z);
final Block coordBlock = world.getBlockAt(blockLocation);
if ((coordBlock.isLiquid() ||
coordBlock.getType() == Material.OBSIDIAN) &&
block.getType() != coordBlock.getType()) {
blockCount++;
}
continue;
} else {
event.setCancelled(true);
return;
}
}
}
}
} else if (Main.nonSolidBlockList.contains(material) ||
material == Material.AIR ||
material == Material.CAVE_AIR) {
final Block block = event.getBlock();
final World world = block.getWorld();
final int radius = 5;
int blockCount = 0;
for (int x = -radius; x <= radius; x++) {
for (int y = -radius; y <= radius; y++) {
for (int z = -radius; z <= radius; z++) {
if (blockCount < 100) {
final Location blockLocation = new Location(world, block.getX() + x, block.getY() + y, block.getZ() + z);
final Block coordBlock = world.getBlockAt(blockLocation);
if (Main.nonSolidBlockList.contains(coordBlock.getType())) {
blockCount++;
}
continue;
} else {
for (BlockFace face : BlockFace.values()) {
if (Main.nonSolidBlockList.contains(block.getRelative(face).getType())) {
event.setCancelled(true);
return;
}
}
return;
}
}
}
}
if (event.getSourceBlock().getState() instanceof CommandBlock) {
event.getSourceBlock().getState().update();
}
}