From 1bc1e0d0c15d981d2a3f495f3a5440a76e9c6e67 Mon Sep 17 00:00:00 2001 From: xGinko Date: Wed, 17 Jan 2024 21:49:37 +0100 Subject: [PATCH] first attempt at tps check fixup --- .../AnarchyExploitFixes.java | 7 +- .../bedrock/FillNetherCeilingOnChunkload.java | 3 +- .../bedrock/FillNetherFloorOnChunkload.java | 3 +- .../FillOverworldFloorOnChunkload.java | 3 +- .../PeriodicallyFillNetherCeiling.java | 2 +- .../bedrock/PeriodicallyFillNetherFloor.java | 2 +- .../PeriodicallyFillOverworldFloor.java | 2 +- .../modules/elytra/ElytraAtSpawn.java | 2 +- .../modules/elytra/ElytraGlobal.java | 6 +- .../modules/elytra/ElytraOnCeiling.java | 6 +- .../PeriodicallyRemoveIllegalBlocks.java | 2 +- .../RemoveIllegalBlocksOnChunkload.java | 2 +- .../placedblocks/RemoveUnnaturalSpawners.java | 2 +- .../lowtpsphysics/BlockMelting.java | 5 +- .../lowtpsphysics/BlockPhysics.java | 5 +- .../lowtpsphysics/Explosions.java | 9 ++- .../lowtpsphysics/FireSpread.java | 5 +- .../lowtpsphysics/GrassSpread.java | 5 +- .../lowtpsphysics/LeaveDecay.java | 5 +- .../lowtpsphysics/LiquidSpread.java | 5 +- .../lowtpsphysics/Noteblocks.java | 5 +- .../lowtpsphysics/Redstone.java | 8 +-- .../utils/models/TPSCache.java | 64 +++++++++++++++---- 23 files changed, 94 insertions(+), 64 deletions(-) diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java index e7fa978bb..35a023dc9 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java @@ -28,10 +28,10 @@ public class AnarchyExploitFixes extends JavaPlugin { private static AnarchyExploitFixes instance; private static HashMap languageCacheMap; private static Config config; + private static TPSCache tpsCache; private static Logger logger; private static boolean isServerFolia, foundProtocolLib; - public TPSCache tpsCache; public final HashSet NEW_CHUNK_PLAYERS = new HashSet<>(); @Override @@ -97,6 +97,9 @@ public static AnarchyExploitFixes getInstance() { public static HashMap getLanguageCacheMap() { return languageCacheMap; } + public static TPSCache getTpsCache() { + return tpsCache; + } public static Config getConfiguration() { return config; } @@ -131,7 +134,7 @@ public void reloadPlugin() { private void reloadConfiguration() { try { config = new Config(); - this.tpsCache = TPSCache.create(config.max_tps_check_interval_millis); + tpsCache = TPSCache.create(config.max_tps_check_interval_millis); AnarchyExploitFixesModule.reloadModules(); config.saveConfig(); } catch (Exception e) { diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherCeilingOnChunkload.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherCeilingOnChunkload.java index d9650578a..bb01661fa 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherCeilingOnChunkload.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherCeilingOnChunkload.java @@ -69,11 +69,10 @@ private void onChunkLoad(ChunkLoadEvent event) { final World world = event.getWorld(); if (exemptedWorlds.contains(world.getName())) return; if (world.getEnvironment() != World.Environment.NETHER) return; + if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS(event) <= pauseTPS)) return; Chunk chunk = event.getChunk(); - if (checkShouldPauseOnLowTPS && (plugin.tpsCache.getTPS(world, chunk.getX(), chunk.getZ()) <= pauseTPS)) return; - plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), fixBedrock -> ChunkUtil.createBedrockLayer(chunk, ceilingY)); } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherFloorOnChunkload.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherFloorOnChunkload.java index eb7c6308b..fd6550d82 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherFloorOnChunkload.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherFloorOnChunkload.java @@ -65,11 +65,10 @@ private void onChunkLoad(ChunkLoadEvent event) { final World world = event.getWorld(); if (exemptedWorlds.contains(world.getName())) return; if (world.getEnvironment() != World.Environment.NETHER) return; + if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS(event) <= pauseTPS)) return; Chunk chunk = event.getChunk(); - if (checkShouldPauseOnLowTPS && (plugin.tpsCache.getTPS(world, chunk.getX(), chunk.getZ()) <= pauseTPS)) return; - plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), fixBedrock -> ChunkUtil.createBedrockLayer(chunk, world.getMinHeight())); } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillOverworldFloorOnChunkload.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillOverworldFloorOnChunkload.java index 41dd376ab..d8c34f537 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillOverworldFloorOnChunkload.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillOverworldFloorOnChunkload.java @@ -68,11 +68,10 @@ private void onChunkLoad(ChunkLoadEvent event) { final World world = event.getWorld(); if (exemptedWorlds.contains(world.getName())) return; if (world.getEnvironment() != World.Environment.NETHER) return; + if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS(event) <= pauseTPS)) return; Chunk chunk = event.getChunk(); - if (checkShouldPauseOnLowTPS && (plugin.tpsCache.getTPS(world, chunk.getX(), chunk.getZ()) <= pauseTPS)) return; - plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), fixBedrock -> ChunkUtil.createBedrockLayer(chunk, world.getMinHeight())); } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillNetherCeiling.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillNetherCeiling.java index f0387ea2c..6aa398159 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillNetherCeiling.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillNetherCeiling.java @@ -65,7 +65,7 @@ private void run() { for (Chunk chunk : world.getLoadedChunks()) { if (!chunk.isEntitiesLoaded()) continue; - if (checkShouldPauseOnLowTPS && (plugin.tpsCache.getTPS(world, chunk.getX(), chunk.getZ()) <= pauseTPS)) continue; + if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS(world, chunk.getX(), chunk.getZ()) <= pauseTPS)) continue; plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), fixBedrock -> ChunkUtil.createBedrockLayer(chunk, ceilingY)); diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillNetherFloor.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillNetherFloor.java index da4efdecd..7dd8d96d1 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillNetherFloor.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillNetherFloor.java @@ -63,7 +63,7 @@ private void run() { for (Chunk chunk : world.getLoadedChunks()) { if (!chunk.isEntitiesLoaded()) continue; - if (checkShouldPauseOnLowTPS && (plugin.tpsCache.getTPS(world, chunk.getX(), chunk.getZ()) <= pauseTPS)) continue; + if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS(world, chunk.getX(), chunk.getZ()) <= pauseTPS)) continue; plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), fixBedrock -> ChunkUtil.createBedrockLayer(chunk, world.getMinHeight())); diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillOverworldFloor.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillOverworldFloor.java index f6db82ce3..bcc9647b9 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillOverworldFloor.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillOverworldFloor.java @@ -64,7 +64,7 @@ private void run() { for (Chunk chunk : world.getLoadedChunks()) { if (!chunk.isEntitiesLoaded()) continue; - if (checkShouldPauseOnLowTPS && (plugin.tpsCache.getTPS(world, chunk.getX(), chunk.getZ()) <= pauseTPS)) continue; + if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS(world, chunk.getX(), chunk.getZ()) <= pauseTPS)) continue; plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), fixBedrock -> ChunkUtil.createBedrockLayer(chunk, world.getMinHeight())); diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraAtSpawn.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraAtSpawn.java index d936e7552..9cfde74d2 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraAtSpawn.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraAtSpawn.java @@ -92,7 +92,7 @@ private void onPlayerMove(PlayerMoveEvent event) { return; } - if (spawn_DenyOnLowTPS && plugin.tpsCache.getTPS(event.getTo()) <= spawn_DenyElytraTPS) { + if (spawn_DenyOnLowTPS && AnarchyExploitFixes.getTpsCache().getTPS(event) <= spawn_DenyElytraTPS) { if (teleportBack) player.teleportAsync(event.getFrom()); else event.setCancelled(true); diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraGlobal.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraGlobal.java index a2d5d1cd9..1762c5cd9 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraGlobal.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraGlobal.java @@ -101,7 +101,7 @@ private void onPlayerMove(PlayerMoveEvent event) { return; } - if (global_DenyOnLowTPS && plugin.tpsCache.getTPS(event.getTo()) <= global_DenyElytraTPS) { + if (global_DenyOnLowTPS && AnarchyExploitFixes.getTpsCache().getTPS(event.getTo()) <= global_DenyElytraTPS) { if (teleportBack) player.teleportAsync(event.getFrom()); else event.setCancelled(true); @@ -129,7 +129,7 @@ private void onPlayerMove(PlayerMoveEvent event) { if (plugin.NEW_CHUNK_PLAYERS.contains(player.getUniqueId())) { // Speed New Chunks - if (global_EnableBursting && plugin.tpsCache.getTPS(event.getTo()) >= global_BurstNewChunk_TPS) { + if (global_EnableBursting && AnarchyExploitFixes.getTpsCache().getTPS(event.getTo()) >= global_BurstNewChunk_TPS) { // Burst Speed New Chunks if (flySpeed > global_BurstSpeedNewChunks+tolerance) { if (teleportBack) player.teleportAsync(event.getFrom()); @@ -215,7 +215,7 @@ private void onPlayerMove(PlayerMoveEvent event) { } } else { // Speed Old Chunks - if (global_EnableBursting && plugin.tpsCache.getTPS(event.getTo()) >= global_BurstOldChunk_TPS) { + if (global_EnableBursting && AnarchyExploitFixes.getTpsCache().getTPS(event.getTo()) >= global_BurstOldChunk_TPS) { // Burst Speed Old Chunks if (flySpeed > global_BurstSpeedOldChunks+tolerance) { if (teleportBack) player.teleportAsync(event.getFrom()); diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraOnCeiling.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraOnCeiling.java index 721aaccda..a5e5fa1a7 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraOnCeiling.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraOnCeiling.java @@ -98,7 +98,7 @@ private void onPlayerMove(PlayerMoveEvent event) { return; } - if (ceiling_DenyOnLowTPS && plugin.tpsCache.getTPS(event.getTo()) <= ceiling_DenyElytraTPS) { + if (ceiling_DenyOnLowTPS && AnarchyExploitFixes.getTpsCache().getTPS(event.getTo()) <= ceiling_DenyElytraTPS) { if (teleportBack) player.teleportAsync(event.getFrom()); else event.setCancelled(true); @@ -126,7 +126,7 @@ private void onPlayerMove(PlayerMoveEvent event) { if (plugin.NEW_CHUNK_PLAYERS.contains(player.getUniqueId())) { // Speed New Chunks - if (ceiling_EnableBursting && plugin.tpsCache.getTPS(event.getTo()) >= ceiling_BurstNewChunk_TPS) { + if (ceiling_EnableBursting && AnarchyExploitFixes.getTpsCache().getTPS(event.getTo()) >= ceiling_BurstNewChunk_TPS) { // Burst Speed New Chunks if (flySpeed > ceiling_BurstSpeedNewChunks+tolerance) { if (teleportBack) player.teleportAsync(event.getFrom()); @@ -211,7 +211,7 @@ private void onPlayerMove(PlayerMoveEvent event) { } } else { // Speed Old Chunks - if (ceiling_EnableBursting && plugin.tpsCache.getTPS(event.getTo()) >= ceiling_BurstOldChunk_TPS) { + if (ceiling_EnableBursting && AnarchyExploitFixes.getTpsCache().getTPS(event.getTo()) >= ceiling_BurstOldChunk_TPS) { // Burst Speed Old Chunks if (flySpeed > ceiling_BurstSpeedOldChunks+tolerance) { if (teleportBack) player.teleportAsync(event.getFrom()); diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/PeriodicallyRemoveIllegalBlocks.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/PeriodicallyRemoveIllegalBlocks.java index 96a0955b9..b80cf9b7e 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/PeriodicallyRemoveIllegalBlocks.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/PeriodicallyRemoveIllegalBlocks.java @@ -84,7 +84,7 @@ private void run() { for (Chunk chunk : world.getLoadedChunks()) { if (!chunk.isEntitiesLoaded()) continue; - if (checkShouldPauseOnLowTPS && (plugin.tpsCache.getTPS(world, chunk.getX(), chunk.getZ()) <= pauseTPS)) continue; + if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS(world, chunk.getX(), chunk.getZ()) <= pauseTPS)) continue; plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), task -> { if (!chunk.isEntitiesLoaded()) return; diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/RemoveIllegalBlocksOnChunkload.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/RemoveIllegalBlocksOnChunkload.java index 69f17b614..ea75d7e79 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/RemoveIllegalBlocksOnChunkload.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/RemoveIllegalBlocksOnChunkload.java @@ -80,11 +80,11 @@ public void disable() { @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) private void onChunkLoad(ChunkLoadEvent event) { if (event.isNewChunk()) return; + if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS(event) <= pauseTPS)) return; Chunk chunk = event.getChunk(); World world = chunk.getWorld(); if (exemptedWorlds.contains(world.getName())) return; - if (checkShouldPauseOnLowTPS && (plugin.tpsCache.getTPS(world, chunk.getX(), chunk.getZ()) <= pauseTPS)) return; final int minY = world.getMinHeight(); final int maxY = world.getMaxHeight(); diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/RemoveUnnaturalSpawners.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/RemoveUnnaturalSpawners.java index 1c7bd9d59..59c794382 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/RemoveUnnaturalSpawners.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/RemoveUnnaturalSpawners.java @@ -87,11 +87,11 @@ public void disable() { @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) private void onChunkLoad(ChunkLoadEvent event) { if (event.isNewChunk()) return; + if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS(event) <= pauseTPS)) return; Chunk chunk = event.getChunk(); World world = chunk.getWorld(); if (!naturalSpawners.containsKey(world.getName())) return; - if (checkShouldPauseOnLowTPS && (plugin.tpsCache.getTPS(world, chunk.getX(), chunk.getZ()) <= pauseTPS)) return; final int minY = world.getMinHeight(); final int maxY = world.getMaxHeight(); diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/BlockMelting.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/BlockMelting.java index 4e6eefd68..e215b2edc 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/BlockMelting.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/BlockMelting.java @@ -15,13 +15,11 @@ public class BlockMelting implements AnarchyExploitFixesModule, Listener { - private final AnarchyExploitFixes plugin; private final double disableMeltingTPS; private final boolean logIsEnabled; public BlockMelting() { shouldEnable(); - this.plugin = AnarchyExploitFixes.getInstance(); Config config = AnarchyExploitFixes.getConfiguration(); this.disableMeltingTPS = config.getDouble("lag-preventions.disable-physics-during-low-tps.melting-blocks.disable-TPS", 16.0); this.logIsEnabled = config.getBoolean("lag-preventions.disable-physics-during-low-tps.melting-blocks.log", false); @@ -39,6 +37,7 @@ public String category() { @Override public void enable() { + AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); plugin.getServer().getPluginManager().registerEvents(this, plugin); } @@ -54,7 +53,7 @@ public void disable() { @EventHandler(priority = EventPriority.LOWEST) private void onBlockFade(BlockFadeEvent event) { - if (plugin.tpsCache.getTPS(event.getBlock().getLocation()) <= disableMeltingTPS) { + if (AnarchyExploitFixes.getTpsCache().getTPS(event) <= disableMeltingTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Stopped block melting because tps is lower than " + disableMeltingTPS); } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/BlockPhysics.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/BlockPhysics.java index 565bfd9aa..ed93b4f9e 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/BlockPhysics.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/BlockPhysics.java @@ -14,13 +14,11 @@ public class BlockPhysics implements AnarchyExploitFixesModule, Listener { - private final AnarchyExploitFixes plugin; private final double disablePhysicsTPS; private final boolean logIsEnabled; public BlockPhysics() { shouldEnable(); - this.plugin = AnarchyExploitFixes.getInstance(); Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("lag-preventions.disable-physics-during-low-tps.block-physics.enable", "Stop block physics (like falling blocks) when the TPS gets below a certain value."); this.disablePhysicsTPS = config.getDouble("lag-preventions.disable-physics-during-low-tps.block-physics.disable-TPS", 16.0); @@ -39,6 +37,7 @@ public String category() { @Override public void enable() { + AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); plugin.getServer().getPluginManager().registerEvents(this, plugin); } @@ -54,7 +53,7 @@ public void disable() { @EventHandler(priority = EventPriority.LOWEST) private void onEntityChange(BlockPhysicsEvent event) { - if (plugin.tpsCache.getTPS(event.getBlock().getLocation()) <= disablePhysicsTPS) { + if (AnarchyExploitFixes.getTpsCache().getTPS(event) <= disablePhysicsTPS) { event.setCancelled(true); if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Cancelled block physics because TPS is lower than " + disablePhysicsTPS); } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Explosions.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Explosions.java index cdccae55b..4ecc3ea0c 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Explosions.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Explosions.java @@ -17,13 +17,11 @@ public class Explosions implements AnarchyExploitFixesModule, Listener { - private final AnarchyExploitFixes plugin; private final double disableExplosionsTPS; private final boolean logIsEnabled; public Explosions() { shouldEnable(); - this.plugin = AnarchyExploitFixes.getInstance(); Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("lag-preventions.disable-physics-during-low-tps.explosions.enable", "Disable explosions during low tps to combat lag."); this.disableExplosionsTPS = config.getDouble("lag-preventions.disable-physics-during-low-tps.explosions.disable-TPS", 14.0); @@ -42,6 +40,7 @@ public String category() { @Override public void enable() { + AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); plugin.getServer().getPluginManager().registerEvents(this, plugin); } @@ -57,7 +56,7 @@ public void disable() { @EventHandler(priority = EventPriority.LOWEST) private void onEntityExplode(EntityExplodeEvent event) { - if (plugin.tpsCache.getTPS(event.getLocation()) <= disableExplosionsTPS) { + if (AnarchyExploitFixes.getTpsCache().getTPS(event) <= disableExplosionsTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Cancelled explosion because tps is lower than " + disableExplosionsTPS); } @@ -65,7 +64,7 @@ private void onEntityExplode(EntityExplodeEvent event) { @EventHandler(priority = EventPriority.LOWEST) private void onExplodePrime(ExplosionPrimeEvent event) { - if (plugin.tpsCache.getTPS(event.getEntity().getLocation()) <= disableExplosionsTPS) { + if (AnarchyExploitFixes.getTpsCache().getTPS(event) <= disableExplosionsTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Cancelled explosion because tps is lower than " + disableExplosionsTPS); } @@ -73,7 +72,7 @@ private void onExplodePrime(ExplosionPrimeEvent event) { @EventHandler(priority = EventPriority.LOWEST) private void onBlockExplode(BlockExplodeEvent event) { - if (plugin.tpsCache.getTPS(event.getBlock().getLocation()) <= disableExplosionsTPS) { + if (AnarchyExploitFixes.getTpsCache().getTPS(event) <= disableExplosionsTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Cancelled explosion because tps is lower than " + disableExplosionsTPS); } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/FireSpread.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/FireSpread.java index 4f997492f..24f6f0345 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/FireSpread.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/FireSpread.java @@ -15,13 +15,11 @@ public class FireSpread implements AnarchyExploitFixesModule, Listener { - private final AnarchyExploitFixes plugin; private final double disableFireTPS; private final boolean logIsEnabled; public FireSpread() { shouldEnable(); - this.plugin = AnarchyExploitFixes.getInstance(); Config config = AnarchyExploitFixes.getConfiguration(); this.disableFireTPS = config.getDouble("lag-preventions.disable-physics-during-low-tps.fire-spread.disable-TPS", 14.0); this.logIsEnabled = config.getBoolean("lag-preventions.disable-physics-during-low-tps.fire-spread.log", false); @@ -39,6 +37,7 @@ public String category() { @Override public void enable() { + AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); plugin.getServer().getPluginManager().registerEvents(this, plugin); } @@ -54,7 +53,7 @@ public void disable() { @EventHandler(priority = EventPriority.LOWEST) private void onLiquidSpread(BlockIgniteEvent event) { - if (plugin.tpsCache.getTPS(event.getBlock().getLocation()) <= disableFireTPS) { + if (AnarchyExploitFixes.getTpsCache().getTPS(event) <= disableFireTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Stopped fire spread because tps is lower than " + disableFireTPS); } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/GrassSpread.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/GrassSpread.java index ac81282cf..34a20612e 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/GrassSpread.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/GrassSpread.java @@ -40,6 +40,7 @@ public String category() { @Override public void enable() { + AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); plugin.getServer().getPluginManager().registerEvents(this, plugin); } @@ -55,7 +56,7 @@ public void disable() { @EventHandler(priority = EventPriority.LOWEST) private void onBlockForm(BlockFormEvent event) { - if (plugin.tpsCache.getTPS(event.getBlock().getLocation()) <= disableGrassTPS) { + if (AnarchyExploitFixes.getTpsCache().getTPS(event) <= disableGrassTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Stopped grass spread because tps is lower than " + disableGrassTPS); } @@ -63,7 +64,7 @@ private void onBlockForm(BlockFormEvent event) { @EventHandler(priority = EventPriority.LOWEST) private void onBlockForm(BlockSpreadEvent event) { - if (plugin.tpsCache.getTPS(event.getBlock().getLocation()) <= disableGrassTPS) { + if (AnarchyExploitFixes.getTpsCache().getTPS(event) <= disableGrassTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Stopped grass spread because tps is lower than " + disableGrassTPS); } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/LeaveDecay.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/LeaveDecay.java index 2607cecd2..f22ebcaca 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/LeaveDecay.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/LeaveDecay.java @@ -15,13 +15,11 @@ public class LeaveDecay implements AnarchyExploitFixesModule, Listener { - private final AnarchyExploitFixes plugin; private final double disableLeaveDecayTPS; private final boolean logIsEnabled; public LeaveDecay() { shouldEnable(); - this.plugin = AnarchyExploitFixes.getInstance(); Config config = AnarchyExploitFixes.getConfiguration(); this.disableLeaveDecayTPS = config.getDouble("lag-preventions.disable-physics-during-low-tps.leave-decay.disable-TPS", 14.0); this.logIsEnabled = config.getBoolean("lag-preventions.disable-physics-during-low-tps.leave-decay.log", false); @@ -39,6 +37,7 @@ public String category() { @Override public void enable() { + AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); plugin.getServer().getPluginManager().registerEvents(this, plugin); } @@ -54,7 +53,7 @@ public void disable() { @EventHandler(priority = EventPriority.LOWEST) private void onLeaveDecay(LeavesDecayEvent event) { - if (plugin.tpsCache.getTPS(event.getBlock().getLocation()) <= disableLeaveDecayTPS) { + if (AnarchyExploitFixes.getTpsCache().getTPS(event) <= disableLeaveDecayTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Cancelled leave decay because tps is lower than " + disableLeaveDecayTPS); } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/LiquidSpread.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/LiquidSpread.java index e20bb3d8d..11cd78cb0 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/LiquidSpread.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/LiquidSpread.java @@ -15,13 +15,11 @@ public class LiquidSpread implements AnarchyExploitFixesModule, Listener { - private final AnarchyExploitFixes plugin; private final double disableLiquidsTPS; private final boolean logIsEnabled; public LiquidSpread() { shouldEnable(); - this.plugin = AnarchyExploitFixes.getInstance(); Config config = AnarchyExploitFixes.getConfiguration(); this.disableLiquidsTPS = config.getDouble("lag-preventions.disable-physics-during-low-tps.liquid-spread.disable-TPS", 16.0); this.logIsEnabled = config.getBoolean("lag-preventions.disable-physics-during-low-tps.liquid-spread.log", false); @@ -39,6 +37,7 @@ public String category() { @Override public void enable() { + AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); plugin.getServer().getPluginManager().registerEvents(this, plugin); } @@ -54,7 +53,7 @@ public void disable() { @EventHandler(priority = EventPriority.LOWEST) private void onLiquidSpread(BlockFromToEvent event) { - if (plugin.tpsCache.getTPS(event.getBlock().getLocation()) <= disableLiquidsTPS) { + if (AnarchyExploitFixes.getTpsCache().getTPS(event) <= disableLiquidsTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Stopped liquid spread because tps is lower than " + disableLiquidsTPS); } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Noteblocks.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Noteblocks.java index 9ebab7d82..5fdebfad7 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Noteblocks.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Noteblocks.java @@ -15,13 +15,11 @@ public class Noteblocks implements AnarchyExploitFixesModule, Listener { - private final AnarchyExploitFixes plugin; private final double disableNoteblockTPS; private final boolean logIsEnabled; public Noteblocks() { shouldEnable(); - this.plugin = AnarchyExploitFixes.getInstance(); Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("lag-preventions.disable-physics-during-low-tps.noteblocks.enable", "Some lag machines use noteblocks to work around redstone limitations."); this.disableNoteblockTPS = config.getDouble("lag-preventions.disable-physics-during-low-tps.noteblocks.disable-TPS", 16.0); @@ -40,6 +38,7 @@ public String category() { @Override public void enable() { + AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); plugin.getServer().getPluginManager().registerEvents(this, plugin); } @@ -55,7 +54,7 @@ public void disable() { @EventHandler(priority = EventPriority.LOWEST) private void onNoteblockGetsPlayed(NotePlayEvent event) { - if (plugin.tpsCache.getTPS(event.getBlock().getLocation()) <= disableNoteblockTPS) { + if (AnarchyExploitFixes.getTpsCache().getTPS(event) <= disableNoteblockTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Cancelled noteblocks playing because tps is lower than " + disableNoteblockTPS); } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Redstone.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Redstone.java index c427f3938..bf5685bc1 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Redstone.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Redstone.java @@ -17,13 +17,11 @@ public class Redstone implements AnarchyExploitFixesModule, Listener { - private final AnarchyExploitFixes plugin; private final double disableRedstoneTPS; private final boolean logIsEnabled; public Redstone() { shouldEnable(); - this.plugin = AnarchyExploitFixes.getInstance(); Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("lag-preventions.disable-physics-during-low-tps.redstone.enable", "Disable redstone during low TPS to prevent some lag machines."); this.disableRedstoneTPS = config.getDouble("lag-preventions.disable-physics-during-low-tps.redstone.disable-TPS", 16.0); @@ -58,7 +56,7 @@ public void disable() { @EventHandler(priority = EventPriority.LOWEST) private void onRedstoneEvent(BlockRedstoneEvent event) { - if (plugin.tpsCache.getTPS(event.getBlock().getLocation()) <= disableRedstoneTPS) { + if (AnarchyExploitFixes.getTpsCache().getTPS(event) <= disableRedstoneTPS) { event.setNewCurrent(0); if (logIsEnabled) moduleLog(Level.INFO, name(),"Disabled redstone because tps is lower than " + disableRedstoneTPS); } @@ -66,7 +64,7 @@ private void onRedstoneEvent(BlockRedstoneEvent event) { @EventHandler(priority = EventPriority.LOWEST) private void onPistonExtendEvent(BlockPistonExtendEvent event) { - if (plugin.tpsCache.getTPS(event.getBlock().getLocation()) <= disableRedstoneTPS) { + if (AnarchyExploitFixes.getTpsCache().getTPS(event) <= disableRedstoneTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Cancelled piston event because tps is lower than " + disableRedstoneTPS); } @@ -74,7 +72,7 @@ private void onPistonExtendEvent(BlockPistonExtendEvent event) { @EventHandler(priority = EventPriority.LOWEST) private void onPistonRetractEvent(BlockPistonRetractEvent event) { - if (plugin.tpsCache.getTPS(event.getBlock().getLocation()) <= disableRedstoneTPS) { + if (AnarchyExploitFixes.getTpsCache().getTPS(event) <= disableRedstoneTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Cancelled piston event because tps is lower than " + disableRedstoneTPS); } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/utils/models/TPSCache.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/utils/models/TPSCache.java index a8eedca37..943c9be51 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/utils/models/TPSCache.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/utils/models/TPSCache.java @@ -2,7 +2,6 @@ import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; -import com.google.common.util.concurrent.AtomicDouble; import io.papermc.paper.threadedregions.RegionizedServer; import io.papermc.paper.threadedregions.ThreadedRegionizer; import io.papermc.paper.threadedregions.TickRegionScheduler; @@ -11,14 +10,18 @@ import org.bukkit.Location; import org.bukkit.Server; import org.bukkit.World; +import org.bukkit.event.Event; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import java.time.Duration; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; public interface TPSCache { double getGlobalTPS(); + double getTPS(Event event); double getTPS(Location location); double getTPS(World world, int chunkX, int chunkZ); @@ -51,6 +54,11 @@ public double getGlobalTPS() { return tps; } + @Override + public double getTPS(Event event) { + return getGlobalTPS(); + } + @Override public double getTPS(World world, int chunkX, int chunkZ) { return getGlobalTPS(); @@ -77,8 +85,30 @@ final class Folia implements TPSCache { @Override public double getGlobalTPS() { // Get region handle and check if there is already a cached tps for it - final TickRegionScheduler.RegionScheduleHandle - regionHandle = RegionizedServer.getGlobalTickData(); + final TickRegionScheduler.RegionScheduleHandle regionHandle = RegionizedServer.getGlobalTickData(); + Double tps = this.cached_tps.getIfPresent(regionHandle); + if (tps == null) { + // If nothing is cached yet, get tps and add to cache + tps = regionHandle.getTickReport5s(System.nanoTime()).tpsData().segmentAll().average(); + this.cached_tps.put(regionHandle, tps); + } + return tps; + } + + /** + * USE THIS METHOD INSIDE EVENTS + */ + @Override + public double getTPS(Event event) { + // Get the potential separate region that owns the location + final ThreadedRegionizer.ThreadedRegion + currentRegion = TickRegionScheduler.getCurrentRegion(); + // If not happening on a separate region, it must mean we're on the main region + if (currentRegion == null) { + return getGlobalTPS(); + } + // Get region handle and check if there is already a cached tps for it + final TickRegionScheduler.RegionScheduleHandle regionHandle = currentRegion.getData().getRegionSchedulingHandle(); Double tps = this.cached_tps.getIfPresent(regionHandle); if (tps == null) { // If nothing is cached yet, get tps and add to cache @@ -88,41 +118,49 @@ public double getGlobalTPS() { return tps; } + /** + * DO NOT USE THIS METHOD INSIDE EVENTS, THIS IS ONLY MEANT FOR SCHEDULED CHECKS! + * Uses region scheduler to get a TPS at a certain location, waiting for a result if necessary + */ @Override public double getTPS(Location location) { if (location == null) return getGlobalTPS(); return getTPS(location.getWorld(), location.getBlockX() >> 4, location.getBlockZ() >> 4); } + /** + * DO NOT USE THIS METHOD INSIDE EVENTS, THIS IS ONLY MEANT FOR SCHEDULED CHECKS! + * Uses region scheduler to get a TPS at a certain location, waiting for a result if necessary + */ @Override public double getTPS(World world, int chunkX, int chunkZ) { if (world == null) return getGlobalTPS(); - // Use AtomicDouble because we need a value from the lambda - // Init with 20 (perfect) tps, so we have a valid and safe fallback value - AtomicDouble atomic_tps = new AtomicDouble(20.0); - // Update atomic double via region scheduler execute method so usage of TickRegionScheduler.getCurrentRegion() - // happens faster and on the thread of the region that owns the location. + CompletableFuture result = new CompletableFuture<>(); this.server.getRegionScheduler().execute(this.plugin, world, chunkX, chunkZ, () -> { // Get the potential separate region that owns the location final ThreadedRegionizer.ThreadedRegion currentRegion = TickRegionScheduler.getCurrentRegion(); // If not happening on a separate region, it must mean we're on the main region if (currentRegion == null) { - atomic_tps.set(getGlobalTPS()); + result.complete(getGlobalTPS()); return; } // Get region handle and check if there is already a cached tps for it - final TickRegionScheduler.RegionScheduleHandle - regionHandle = currentRegion.getData().getRegionSchedulingHandle(); + final TickRegionScheduler.RegionScheduleHandle regionHandle = currentRegion.getData().getRegionSchedulingHandle(); Double tps = this.cached_tps.getIfPresent(regionHandle); if (tps == null) { // If nothing is cached yet, get tps and add to cache tps = regionHandle.getTickReport5s(System.nanoTime()).tpsData().segmentAll().average(); this.cached_tps.put(regionHandle, tps); } - atomic_tps.set(tps); + result.complete(tps); }); - return atomic_tps.get(); + + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return getGlobalTPS(); + } } } } \ No newline at end of file