2019-12-17 12:37:59 +00:00
|
|
|
package pw.kaboom.extras.modules.block;
|
2019-07-30 17:14:24 +00:00
|
|
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Material;
|
2019-09-28 00:29:48 +00:00
|
|
|
import org.bukkit.entity.EntityType;
|
2019-07-30 17:14:24 +00:00
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.Listener;
|
2023-07-02 00:06:12 +00:00
|
|
|
import org.bukkit.event.block.BlockFormEvent;
|
2019-12-18 14:25:17 +00:00
|
|
|
import org.bukkit.event.block.BlockPhysicsEvent;
|
2019-07-30 17:14:24 +00:00
|
|
|
import org.bukkit.event.block.BlockRedstoneEvent;
|
2019-09-28 00:29:48 +00:00
|
|
|
import org.bukkit.event.entity.EntityChangeBlockEvent;
|
2023-03-11 13:09:26 +00:00
|
|
|
import org.bukkit.scheduler.BukkitScheduler;
|
|
|
|
import pw.kaboom.extras.Main;
|
2019-12-03 04:53:37 +00:00
|
|
|
|
2019-12-21 14:12:26 +00:00
|
|
|
public final class BlockPhysics implements Listener {
|
2023-07-02 00:06:12 +00:00
|
|
|
private static final double MINIMUM_TPS = 10;
|
2022-05-20 02:35:48 +00:00
|
|
|
// This class contains code to prevent large areas of non-solid blocks
|
|
|
|
// from crashing the server
|
2023-03-11 13:09:26 +00:00
|
|
|
private static double tps = 20;
|
2022-05-20 02:35:48 +00:00
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
void onBlockPhysics(final BlockPhysicsEvent event) {
|
|
|
|
try {
|
|
|
|
switch (event.getChangedType()) {
|
|
|
|
case COMMAND_BLOCK:
|
|
|
|
case CHAIN_COMMAND_BLOCK:
|
|
|
|
case REPEATING_COMMAND_BLOCK:
|
|
|
|
if (Material.STRUCTURE_BLOCK.equals(event.getSourceBlock().getType())) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (Exception | StackOverflowError e) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
void onBlockRedstone(final BlockRedstoneEvent event) {
|
2023-07-02 00:06:12 +00:00
|
|
|
if (tps < MINIMUM_TPS) {
|
2022-05-20 02:35:48 +00:00
|
|
|
event.setNewCurrent(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int fallingBlockCount;
|
|
|
|
|
2023-07-02 00:06:12 +00:00
|
|
|
@EventHandler
|
|
|
|
void onBlockForm(final BlockFormEvent event) {
|
|
|
|
if (tps < MINIMUM_TPS) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 02:35:48 +00:00
|
|
|
@EventHandler
|
|
|
|
void onEntityChangeBlock(final EntityChangeBlockEvent event) {
|
|
|
|
if (event.getEntityType() == EntityType.FALLING_BLOCK
|
|
|
|
&& event.getTo() == Material.AIR) {
|
|
|
|
fallingBlockCount++;
|
|
|
|
|
|
|
|
final int maxFallingBlockCount = 10;
|
|
|
|
|
|
|
|
if (fallingBlockCount == maxFallingBlockCount) {
|
|
|
|
event.setCancelled(true);
|
|
|
|
fallingBlockCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-11 13:09:26 +00:00
|
|
|
private static void updateTPS() {
|
|
|
|
final double[] tpsValues = Bukkit.getTPS();
|
|
|
|
|
|
|
|
tps = tpsValues[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void init(final Main main) {
|
|
|
|
final BukkitScheduler scheduler = Bukkit.getScheduler();
|
|
|
|
|
|
|
|
scheduler.runTaskTimer(main, BlockPhysics::updateTPS, 0L, 1200L); // 1 minute
|
|
|
|
}
|
2019-07-30 17:14:24 +00:00
|
|
|
}
|