extras/src/main/java/pw/kaboom/extras/modules/block/BlockPhysics.java

176 lines
4.7 KiB
Java
Raw Normal View History

2019-07-30 17:14:24 +00:00
package pw.kaboom.extras;
2019-11-13 20:50:09 +00:00
import java.util.ArrayList;
2019-07-30 17:14:24 +00:00
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
2019-11-30 23:04:08 +00:00
import org.bukkit.Nameable;
2019-07-30 17:14:24 +00:00
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
2019-11-30 23:04:08 +00:00
import org.bukkit.block.BlockState;
2019-11-15 15:30:36 +00:00
import org.bukkit.block.CommandBlock;
2019-07-30 17:14:24 +00:00
import org.bukkit.block.data.Levelled;
2019-09-28 00:29:48 +00:00
import org.bukkit.entity.EntityType;
import org.bukkit.entity.TNTPrimed;
2019-07-30 17:14:24 +00:00
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.block.BlockRedstoneEvent;
2019-09-28 00:29:48 +00:00
import org.bukkit.event.entity.EntityChangeBlockEvent;
2019-07-30 17:14:24 +00:00
2019-09-28 00:29:48 +00:00
class BlockPhysics implements Listener {
2019-07-30 17:14:24 +00:00
@EventHandler
void onBlockFromTo(BlockFromToEvent event) {
try {
event.getBlock().getState();
event.getToBlock().getState();
} catch (Exception exception) {
event.setCancelled(true);
return;
}
2019-09-28 00:29:48 +00:00
2019-07-30 17:14:24 +00:00
final double tps = Bukkit.getServer().getTPS()[0];
2019-09-28 00:29:48 +00:00
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) {
2019-09-28 00:29:48 +00:00
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) {
2019-09-28 00:29:48 +00:00
blockCount++;
}
continue;
2019-10-13 13:14:25 +00:00
} else {
event.setCancelled(true);
2019-09-28 00:29:48 +00:00
}
}
}
}
2019-07-30 17:14:24 +00:00
}
@EventHandler
void onBlockPhysics(BlockPhysicsEvent event) {
final Material material = event.getChangedType();
2019-11-30 23:04:08 +00:00
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);
}*/
2019-07-30 17:14:24 +00:00
2019-09-28 00:29:48 +00:00
if (material == Material.FARMLAND) {
2019-07-30 17:14:24 +00:00
event.setCancelled(true);
2019-11-30 23:04:08 +00:00
} else if (blockState instanceof CommandBlock) {
blockState.update();
} else if (blockStateSource instanceof CommandBlock) {
blockStateSource.update();
} else if (event.getBlock().isLiquid()) {
final Block block = event.getBlock();
2019-10-04 13:42:11 +00:00
final World world = block.getWorld();
final int radius = 5;
int blockCount = 0;
2019-10-13 13:14:25 +00:00
2019-10-04 13:42:11 +00:00
for (int x = -radius; x <= radius; x++) {
for (int y = -radius; y <= radius; y++) {
for (int z = -radius; z <= radius; z++) {
if (blockCount < 200) {
2019-10-04 13:42:11 +00:00
final Location blockLocation = new Location(world, block.getX() + x, block.getY() + y, block.getZ() + z);
final Block coordBlock = world.getBlockAt(blockLocation);
if ((coordBlock.isLiquid() ||
2019-10-04 13:42:11 +00:00
coordBlock.getType() == Material.OBSIDIAN) &&
block.getType() != coordBlock.getType()) {
blockCount++;
}
continue;
2019-10-13 13:14:25 +00:00
} else {
event.setCancelled(true);
return;
2019-10-03 16:30:18 +00:00
}
}
}
}
2019-10-13 13:14:25 +00:00
} else if (Main.nonSolidBlockList.contains(material) ||
material == Material.AIR ||
material == Material.CAVE_AIR) {
final Block block = event.getBlock();
2019-07-30 17:14:24 +00:00
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) {
2019-07-30 17:14:24 +00:00
final Location blockLocation = new Location(world, block.getX() + x, block.getY() + y, block.getZ() + z);
final Block coordBlock = world.getBlockAt(blockLocation);
2019-10-13 13:14:25 +00:00
if (Main.nonSolidBlockList.contains(coordBlock.getType())) {
2019-07-30 17:14:24 +00:00
blockCount++;
}
continue;
2019-10-13 13:14:25 +00:00
} else {
for (BlockFace face : BlockFace.values()) {
if (Main.nonSolidBlockList.contains(block.getRelative(face).getType())) {
event.setCancelled(true);
return;
}
}
return;
2019-07-30 17:14:24 +00:00
}
}
}
}
}
2019-07-30 17:14:24 +00:00
}
@EventHandler
void onBlockRedstone(BlockRedstoneEvent event) {
final double tps = Bukkit.getServer().getTPS()[0];
if (tps < 10) {
event.setNewCurrent(0);
}
}
2019-09-28 00:29:48 +00:00
@EventHandler
void onEntityChangeBlock(EntityChangeBlockEvent event) {
if (event.getEntityType() == EntityType.FALLING_BLOCK &&
event.getTo() == Material.AIR) {
Main.fallingBlockCount++;
if (Main.fallingBlockCount == 10) {
event.setCancelled(true);
Main.fallingBlockCount = 0;
}
}
}
2019-07-30 17:14:24 +00:00
}