Skip to content

Commit

Permalink
use tps cache in legacy as well
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Dec 24, 2023
1 parent fce6c0b commit 8b57479
Show file tree
Hide file tree
Showing 25 changed files with 83 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,14 +24,12 @@
public class AnarchyExploitFixes extends JavaPlugin {

private static AnarchyExploitFixes instance;
private static Logger logger;
private static Config config;
private static HashMap<String, LanguageCache> 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<UUID> CONNECTION_MSG_TOGGLE = new HashSet<>();
public final HashSet<UUID> NEW_CHUNK_PLAYERS = new HashSet<>();

Expand Down Expand Up @@ -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.");
}

Expand All @@ -107,21 +97,15 @@ public void onEnable() {
public static AnarchyExploitFixes getInstance() {
return instance;
}
public static Config getConfiguration() {
return config;
}
public static HashMap<String, LanguageCache> 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());
}
Expand All @@ -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();
}
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,23 @@ 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);
}
}

@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);
}
}

@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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ 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);
}
}

@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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading

0 comments on commit 8b57479

Please sign in to comment.