diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java index 4337d8d66..cad31ec48 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/AnarchyExploitFixes.java @@ -5,19 +5,16 @@ import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.config.LanguageCache; import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; +import me.moomoo.anarchyexploitfixes.utils.TPSCache; import org.bstats.bukkit.Metrics; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.bukkit.event.HandlerList; import org.bukkit.plugin.java.JavaPlugin; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.util.*; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.logging.Logger; @@ -27,14 +24,12 @@ public class AnarchyExploitFixes extends JavaPlugin { private static AnarchyExploitFixes instance; - private static Logger logger; - private static Config config; private static HashMap languageCacheMap; - private static int minorMCVersion = 12; - - private static ScheduledFuture scheduled_tps_check; - public double tps = 20; + private static Config config; + private static Logger logger; + private static int minorMCVersion; + public TPSCache tpsCache; public final HashSet CONNECTION_MSG_TOGGLE = new HashSet<>(); public final HashSet NEW_CHUNK_PLAYERS = new HashSet<>(); @@ -94,11 +89,6 @@ public void onEnable() { logger.info("Loading metrics"); new Metrics(this, 8700); - // Scheduled TPS checker - scheduled_tps_check = Executors.newScheduledThreadPool(1).scheduleAtFixedRate( - () -> new Thread(() -> tps = getServer().getTPS()[0]).start(), 1, 1, TimeUnit.SECONDS - ); - logger.info("Done."); } @@ -107,21 +97,15 @@ public void onEnable() { public static AnarchyExploitFixes getInstance() { return instance; } - public static Config getConfiguration() { - return config; - } public static HashMap getLanguageCacheMap() { return languageCacheMap; } + public static Config getConfiguration() { + return config; + } public static Logger getLog() { return logger; } - public static int getMCVersion() { - return minorMCVersion; - } - public static boolean isProtocolLibInstalled() { - return instance.getServer().getPluginManager().isPluginEnabled("ProtocolLib"); - } public static LanguageCache getLang(Locale locale) { return getLang(locale.toString().toLowerCase()); } @@ -135,18 +119,14 @@ public static LanguageCache getLang(String lang) { return languageCacheMap.get(config.default_lang.toString().toLowerCase()); } } - - public void disablePlugin() { - HandlerList.unregisterAll(this); - getServer().getScheduler().cancelTasks(this); - AnarchyExploitFixesModule.unregisterPacketListeners(this); - scheduled_tps_check.cancel(true); + public static int getMCVersion() { + return minorMCVersion; + } + public static boolean isProtocolLibInstalled() { + return instance.getServer().getPluginManager().isPluginEnabled("ProtocolLib"); } public void reloadPlugin() { - if (scheduled_tps_check.isCancelled()) - scheduled_tps_check = Executors.newScheduledThreadPool(1).scheduleAtFixedRate(() -> - new Thread(() -> tps = getServer().getTPS()[0]).start(), 1, 1, TimeUnit.SECONDS); reloadLang(); reloadConfiguration(); } @@ -155,6 +135,7 @@ private void reloadConfiguration() { try { config = new Config(); AnarchyExploitFixesModule.reloadModules(); + this.tpsCache = TPSCache.create(config.max_tps_check_interval_millis); config.saveConfig(); } catch (Exception e) { logger.severe("Failed to load config file! - " + e.getLocalizedMessage()); diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/commands/AEFCommand.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/commands/AEFCommand.java index 62e13d66c..9c99480dd 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/commands/AEFCommand.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/commands/AEFCommand.java @@ -5,9 +5,11 @@ import cloud.commandframework.annotations.CommandMethod; import cloud.commandframework.annotations.CommandPermission; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; +import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; import org.bukkit.material.MaterialData; import org.bukkit.plugin.PluginDescriptionFile; @@ -46,7 +48,9 @@ public void disableCommand( final CommandSender sender ) { sender.sendMessage(ChatColor.RED + "Disabling plugin."); - plugin.disablePlugin(); + HandlerList.unregisterAll(plugin); + plugin.getServer().getScheduler().cancelTasks(plugin); + AnarchyExploitFixesModule.unregisterPacketListeners(plugin); sender.sendMessage(ChatColor.GREEN + "All enabled plugin features have been disabled."); sender.sendMessage(ChatColor.WHITE + "Use /aef reload to enable the plugin again. You can also use third party options like plugman or serverutils."); } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/config/Config.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/config/Config.java index 8ad413cef..50a408d4f 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/config/Config.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/config/Config.java @@ -13,6 +13,7 @@ public class Config { private final ConfigFile config; public final Locale default_lang; public final String cmd_say_format; + public final long max_tps_check_interval_millis; public final int nether_ceiling_max_y, nether_floor_min_y, overworld_floor_min_y; public final boolean auto_lang, protocolLib_IsDisabled, connectionMsgsAreOnByDefault, cmd_say_enabled, cmd_help_enabled, cmd_toggleConMsgs_enabled; @@ -32,6 +33,7 @@ public Config() throws Exception { this.auto_lang = getBoolean("language.auto-language", true, "If set to true, will display messages based on client language"); // General Settings + this.max_tps_check_interval_millis = getInt("general.max-tps-check-interval-in-ticks", 20, "How long a checked tps is cached to save resources in ticks (1 sec = 20 ticks)") * 50L; this.protocolLib_IsDisabled = getBoolean("general.disable-all-ProtocolLib", false, "Use only if you are having problems with ProtocolLib when starting the plugin."); this.nether_ceiling_max_y = getInt("general.nether-ceiling-y", 127, "The Y-level at which the nether ceiling generates the last layer of bedrock on your server."); this.nether_floor_min_y = getInt("general.nether-floor-y", 0, "The Y-level at which the nether floor generates the last layer of bedrock on your server."); diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherCeilingOnChunkload.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherCeilingOnChunkload.java index ea0ffeb0c..27e5d9de1 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherCeilingOnChunkload.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherCeilingOnChunkload.java @@ -55,7 +55,7 @@ public boolean shouldEnable() { @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) private void onChunkLoad(ChunkLoadEvent event) { - if (checkShouldPauseOnLowTPS && (plugin.tps <= pauseTPS)) return; + if (checkShouldPauseOnLowTPS && (plugin.tpsCache.getTPS() <= pauseTPS)) return; if (!alsoCheckNewChunks && event.isNewChunk()) return; World world = event.getWorld(); if (exemptedWorlds.contains(world.getName())) return; diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherFloorOnChunkload.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherFloorOnChunkload.java index 9c0d753f3..45c5caaa2 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherFloorOnChunkload.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillNetherFloorOnChunkload.java @@ -55,7 +55,7 @@ public boolean shouldEnable() { @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) private void onChunkLoad(ChunkLoadEvent event) { - if (checkShouldPauseOnLowTPS && (plugin.tps <= pauseTPS)) return; + if (checkShouldPauseOnLowTPS && (plugin.tpsCache.getTPS() <= pauseTPS)) return; if (!alsoCheckNewChunks && event.isNewChunk()) return; World world = event.getWorld(); if (exemptedWorlds.contains(world.getName())) return; diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillOverworldFloorOnChunkload.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillOverworldFloorOnChunkload.java index 13e51e269..aa5c82080 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillOverworldFloorOnChunkload.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/FillOverworldFloorOnChunkload.java @@ -55,7 +55,7 @@ public boolean shouldEnable() { @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) private void onChunkLoad(ChunkLoadEvent event) { - if (checkShouldPauseOnLowTPS && (plugin.tps <= pauseTPS)) return; + if (checkShouldPauseOnLowTPS && (plugin.tpsCache.getTPS() <= pauseTPS)) return; if (!alsoCheckNewChunks && event.isNewChunk()) return; World world = event.getWorld(); if (exemptedWorlds.contains(world.getName())) return; diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillNetherCeiling.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillNetherCeiling.java index 0f195a56a..5723f4bee 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillNetherCeiling.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillNetherCeiling.java @@ -53,7 +53,7 @@ public boolean shouldEnable() { @Override public void run() { - if (checkShouldPauseOnLowTPS && (plugin.tps <= pauseTPS)) return; + if (checkShouldPauseOnLowTPS && (plugin.tpsCache.getTPS() <= pauseTPS)) return; for (World world : plugin.getServer().getWorlds()) { if (world.getEnvironment().equals(World.Environment.NETHER)) { diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillNetherFloor.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillNetherFloor.java index 0c61a6b05..b2331016f 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillNetherFloor.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillNetherFloor.java @@ -53,7 +53,7 @@ public boolean shouldEnable() { @Override public void run() { - if (checkShouldPauseOnLowTPS && (plugin.tps <= pauseTPS)) return; + if (checkShouldPauseOnLowTPS && (plugin.tpsCache.getTPS() <= pauseTPS)) return; for (World world : plugin.getServer().getWorlds()) { if (world.getEnvironment().equals(World.Environment.NETHER)) { diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillOverworldFloor.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillOverworldFloor.java index 89760bc2b..7b633c331 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillOverworldFloor.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/bedrock/PeriodicallyFillOverworldFloor.java @@ -54,7 +54,7 @@ public boolean shouldEnable() { @Override public void run() { - if (checkShouldPauseOnLowTPS && (plugin.tps <= pauseTPS)) return; + if (checkShouldPauseOnLowTPS && (plugin.tpsCache.getTPS() <= pauseTPS)) return; for (World world : plugin.getServer().getWorlds()) { if (world.getEnvironment().equals(World.Environment.NORMAL)) { diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraAtSpawn.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraAtSpawn.java index a383c4f53..f1df48fc9 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraAtSpawn.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraAtSpawn.java @@ -90,7 +90,7 @@ private void onPlayerMove(PlayerMoveEvent event) { return; } - if (spawn_DenyOnLowTPS && plugin.tps <= spawn_DenyElytraTPS) { + if (spawn_DenyOnLowTPS && plugin.tpsCache.getTPS() <= spawn_DenyElytraTPS) { if (teleportBack) player.teleport(event.getFrom()); else event.setCancelled(true); diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraGlobal.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraGlobal.java index 5880d7d03..6b71c26dc 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraGlobal.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraGlobal.java @@ -98,7 +98,7 @@ private void onPlayerMove(PlayerMoveEvent event) { return; } - if (global_DenyOnLowTPS && plugin.tps <= global_DenyElytraTPS) { + if (global_DenyOnLowTPS && plugin.tpsCache.getTPS() <= global_DenyElytraTPS) { if (teleportBack) player.teleport(event.getFrom()); else event.setCancelled(true); @@ -124,7 +124,7 @@ private void onPlayerMove(PlayerMoveEvent event) { if (plugin.NEW_CHUNK_PLAYERS.contains(player.getUniqueId())) { // Speed New Chunks - if (global_EnableBursting && plugin.tps >= global_BurstNewChunk_TPS) { + if (global_EnableBursting && plugin.tpsCache.getTPS() >= global_BurstNewChunk_TPS) { // Burst Speed New Chunks if (flySpeed > global_BurstSpeedNewChunks+tolerance) { // Too fast @@ -204,7 +204,7 @@ private void onPlayerMove(PlayerMoveEvent event) { } } else { // Speed Old Chunks - if (global_EnableBursting && plugin.tps >= global_BurstOldChunk_TPS) { + if (global_EnableBursting && plugin.tpsCache.getTPS() >= global_BurstOldChunk_TPS) { // Burst Speed Old Chunks if (flySpeed > global_BurstSpeedOldChunks+tolerance) { if (teleportBack) player.teleport(event.getFrom()); diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraOnCeiling.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraOnCeiling.java index ae8405020..b2ea9d996 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraOnCeiling.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraOnCeiling.java @@ -95,7 +95,7 @@ private void onPlayerMove(PlayerMoveEvent event) { return; } - if (ceiling_DenyOnLowTPS && plugin.tps <= ceiling_DenyElytraTPS) { + if (ceiling_DenyOnLowTPS && plugin.tpsCache.getTPS() <= ceiling_DenyElytraTPS) { if (teleportBack) player.teleport(event.getFrom()); else event.setCancelled(true); @@ -121,7 +121,7 @@ private void onPlayerMove(PlayerMoveEvent event) { if (plugin.NEW_CHUNK_PLAYERS.contains(player.getUniqueId())) { // Speed New Chunks - if (ceiling_EnableBursting && plugin.tps >= ceiling_BurstNewChunk_TPS) { + if (ceiling_EnableBursting && plugin.tpsCache.getTPS() >= ceiling_BurstNewChunk_TPS) { // Burst Speed New Chunks if (flySpeed > ceiling_BurstSpeedNewChunks+tolerance) { if (teleportBack) player.teleport(event.getFrom()); @@ -200,7 +200,7 @@ private void onPlayerMove(PlayerMoveEvent event) { } } else { // Speed Old Chunks - if (ceiling_EnableBursting && plugin.tps >= ceiling_BurstOldChunk_TPS) { + if (ceiling_EnableBursting && plugin.tpsCache.getTPS() >= ceiling_BurstOldChunk_TPS) { // Burst Speed Old Chunks if (flySpeed > ceiling_BurstSpeedOldChunks+tolerance) { if (teleportBack) player.teleport(event.getFrom()); diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/PeriodicallyRemoveIllegalBlocks.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/PeriodicallyRemoveIllegalBlocks.java index 6658b058e..a40182f92 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/PeriodicallyRemoveIllegalBlocks.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/PeriodicallyRemoveIllegalBlocks.java @@ -90,7 +90,7 @@ public boolean shouldEnable() { @Override public void run() { - if (checkShouldPauseOnLowTPS && (plugin.tps <= pauseTPS)) return; + if (checkShouldPauseOnLowTPS && (plugin.tpsCache.getTPS() <= pauseTPS)) return; for (World world : plugin.getServer().getWorlds()) { if (!exemptedWorlds.contains(world.getName())) { diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/RemoveIllegalBlocksOnChunkload.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/RemoveIllegalBlocksOnChunkload.java index af7759983..fd0ff75af 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/RemoveIllegalBlocksOnChunkload.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/RemoveIllegalBlocksOnChunkload.java @@ -94,7 +94,7 @@ public boolean shouldEnable() { @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) private void onChunkLoad(ChunkLoadEvent event) { - if (event.isNewChunk() || checkShouldPauseOnLowTPS && (plugin.tps <= pauseTPS)) return; + if (event.isNewChunk() || checkShouldPauseOnLowTPS && (plugin.tpsCache.getTPS() <= pauseTPS)) return; Chunk chunk = event.getChunk(); World world = chunk.getWorld(); diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/RemoveUnnaturalSpawners.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/RemoveUnnaturalSpawners.java index 19af4326f..344fcf92b 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/RemoveUnnaturalSpawners.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/placedblocks/RemoveUnnaturalSpawners.java @@ -88,7 +88,7 @@ public boolean shouldEnable() { @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) private void onChunkLoad(ChunkLoadEvent event) { - if (event.isNewChunk() || (checkShouldPauseOnLowTPS && (plugin.tps <= pauseTPS))) return; + if (event.isNewChunk() || (checkShouldPauseOnLowTPS && (plugin.tpsCache.getTPS() <= pauseTPS))) return; Chunk chunk = event.getChunk(); World world = chunk.getWorld(); diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/BlockMelting.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/BlockMelting.java index c9c43f2a1..1f5e897c3 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/BlockMelting.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/BlockMelting.java @@ -48,7 +48,7 @@ public boolean shouldEnable() { @EventHandler(priority = EventPriority.LOWEST) private void onBlockFade(BlockFadeEvent event) { - if (plugin.tps <= disableMeltingTPS) { + if (plugin.tpsCache.getTPS() <= disableMeltingTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Stopped block melting because tps is lower than " + disableMeltingTPS); } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/BlockPhysics.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/BlockPhysics.java index 657a4669c..a5d2a8dfe 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/BlockPhysics.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/BlockPhysics.java @@ -48,7 +48,7 @@ public boolean shouldEnable() { @EventHandler(priority = EventPriority.LOWEST) private void onEntityChange(BlockPhysicsEvent event) { - if (plugin.tps <= disablePhysicsTPS) { + if (plugin.tpsCache.getTPS() <= disablePhysicsTPS) { event.setCancelled(true); if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Cancelled block physics because TPS is lower than " + disablePhysicsTPS); } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Explosions.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Explosions.java index 9c9971ec6..fdda4773a 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Explosions.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Explosions.java @@ -51,7 +51,7 @@ public boolean shouldEnable() { @EventHandler(priority = EventPriority.LOWEST) private void onEntityExplode(EntityExplodeEvent event) { - if (plugin.tps <= disableExplosionsTPS) { + if (plugin.tpsCache.getTPS() <= disableExplosionsTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Cancelled explosion because tps is lower than " + disableExplosionsTPS); } @@ -59,7 +59,7 @@ private void onEntityExplode(EntityExplodeEvent event) { @EventHandler(priority = EventPriority.LOWEST) private void onExplodePrime(ExplosionPrimeEvent event) { - if (plugin.tps <= disableExplosionsTPS) { + if (plugin.tpsCache.getTPS() <= disableExplosionsTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Cancelled explosion because tps is lower than " + disableExplosionsTPS); } @@ -67,7 +67,7 @@ private void onExplodePrime(ExplosionPrimeEvent event) { @EventHandler(priority = EventPriority.LOWEST) private void onBlockExplode(BlockExplodeEvent event) { - if (plugin.tps <= disableExplosionsTPS) { + if (plugin.tpsCache.getTPS() <= disableExplosionsTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Cancelled explosion because tps is lower than " + disableExplosionsTPS); } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/FireSpread.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/FireSpread.java index f3f5b9924..b836c70e8 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/FireSpread.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/FireSpread.java @@ -48,7 +48,7 @@ public boolean shouldEnable() { @EventHandler(priority = EventPriority.LOWEST) private void onLiquidSpread(BlockIgniteEvent event) { - if (plugin.tps <= disableFireTPS) { + if (plugin.tpsCache.getTPS() <= disableFireTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Stopped fire spread because tps is lower than " + disableFireTPS); } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/GrassSpread.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/GrassSpread.java index 4b0d6f221..dd1027b24 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/GrassSpread.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/GrassSpread.java @@ -49,7 +49,7 @@ public boolean shouldEnable() { @EventHandler(priority = EventPriority.LOWEST) private void onBlockForm(BlockFormEvent event) { - if (plugin.tps <= disableGrassTPS) { + if (plugin.tpsCache.getTPS() <= disableGrassTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Stopped grass spread because tps is lower than " + disableGrassTPS); } @@ -57,7 +57,7 @@ private void onBlockForm(BlockFormEvent event) { @EventHandler(priority = EventPriority.LOWEST) private void onBlockForm(BlockSpreadEvent event) { - if (plugin.tps <= disableGrassTPS) { + if (plugin.tpsCache.getTPS() <= disableGrassTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Stopped grass spread because tps is lower than " + disableGrassTPS); } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/LeaveDecay.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/LeaveDecay.java index 327c14679..600d60bba 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/LeaveDecay.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/LeaveDecay.java @@ -48,7 +48,7 @@ public boolean shouldEnable() { @EventHandler(priority = EventPriority.LOWEST) private void onLeaveDecay(LeavesDecayEvent event) { - if (plugin.tps <= disableLeaveDecayTPS) { + if (plugin.tpsCache.getTPS() <= disableLeaveDecayTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Cancelled leave decay because tps is lower than " + disableLeaveDecayTPS); } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/LiquidSpread.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/LiquidSpread.java index 9af456715..27af5722b 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/LiquidSpread.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/LiquidSpread.java @@ -48,7 +48,7 @@ public boolean shouldEnable() { @EventHandler(priority = EventPriority.LOWEST) private void onLiquidSpread(BlockFromToEvent event) { - if (plugin.tps <= disableLiquidsTPS) { + if (plugin.tpsCache.getTPS() <= disableLiquidsTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Stopped liquid spread because tps is lower than " + disableLiquidsTPS); } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Noteblocks.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Noteblocks.java index b6513a8bd..dcd80d333 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Noteblocks.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Noteblocks.java @@ -49,7 +49,7 @@ public boolean shouldEnable() { @EventHandler(priority = EventPriority.LOWEST) private void onNoteblockGetsPlayed(NotePlayEvent event) { - if (plugin.tps <= disableNoteblockTPS) { + if (plugin.tpsCache.getTPS() <= disableNoteblockTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Cancelled noteblocks playing because tps is lower than " + disableNoteblockTPS); } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Redstone.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Redstone.java index e1fbf77a0..4a935d7e8 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Redstone.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/lagpreventions/lowtpsphysics/Redstone.java @@ -52,7 +52,7 @@ public boolean shouldEnable() { @EventHandler(priority = EventPriority.LOWEST) private void onRedstoneEvent(BlockRedstoneEvent event) { - if (plugin.tps <= disableRedstoneTPS) { + if (plugin.tpsCache.getTPS() <= disableRedstoneTPS) { event.setNewCurrent(0); if (logIsEnabled) moduleLog(Level.INFO, name(),"Disabled redstone because tps is lower than " + disableRedstoneTPS); } @@ -60,7 +60,7 @@ private void onRedstoneEvent(BlockRedstoneEvent event) { @EventHandler(priority = EventPriority.LOWEST) private void onPistonExtendEvent(BlockPistonExtendEvent event) { - if (plugin.tps <= disableRedstoneTPS) { + if (plugin.tpsCache.getTPS() <= disableRedstoneTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Cancelled piston event because tps is lower than " + disableRedstoneTPS); } @@ -68,7 +68,7 @@ private void onPistonExtendEvent(BlockPistonExtendEvent event) { @EventHandler(priority = EventPriority.LOWEST) private void onPistonRetractEvent(BlockPistonRetractEvent event) { - if (plugin.tps <= disableRedstoneTPS) { + if (plugin.tpsCache.getTPS() <= disableRedstoneTPS) { event.setCancelled(true); if (logIsEnabled) moduleLog(Level.INFO, name(),"Cancelled piston event because tps is lower than " + disableRedstoneTPS); } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/utils/TPSCache.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/utils/TPSCache.java new file mode 100755 index 000000000..e42382c68 --- /dev/null +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/utils/TPSCache.java @@ -0,0 +1,32 @@ +package me.moomoo.anarchyexploitfixes.utils; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; +import org.bukkit.Server; + +import java.time.Duration; + +public final class TPSCache { + + private final Server server; + private final Cache cached_tps; + + TPSCache(Server server, long checkDelayMillis) { + this.server = server; + this.cached_tps = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(checkDelayMillis)).build(); + } + + public static TPSCache create(long checkDelayMillis) { + return new TPSCache(AnarchyExploitFixes.getInstance().getServer(), checkDelayMillis); + } + + public double getTPS() { + Double tps = this.cached_tps.getIfPresent(true); + if (tps == null) { + tps = server.getTPS()[0]; + this.cached_tps.put(true, tps); + } + return tps; + } +} \ No newline at end of file