diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/AnarchyExploitFixes.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/AnarchyExploitFixes.java index a660856c5..a28086ec9 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/AnarchyExploitFixes.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/AnarchyExploitFixes.java @@ -13,8 +13,6 @@ import me.xginko.aef.utils.tickdata.TickReporter; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.logger.slf4j.ComponentLogger; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.core.config.Configurator; import org.bstats.bukkit.Metrics; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -26,7 +24,12 @@ import java.io.IOException; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; -import java.util.*; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; import java.util.jar.JarFile; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -48,9 +51,6 @@ public final class AnarchyExploitFixes extends JavaPlugin { @Override public void onLoad() { PlatformUtil.load(); - // Disable logging for some shaded libraries as those can get very verbose - String shadedLibs = getClass().getPackageName() + ".libs"; - Configurator.setLevel(shadedLibs + ".reflections.Reflections", Level.OFF); isPacketEventsInstalled = getServer().getPluginManager().getPlugin("packetevents") != null; if (isPacketEventsInstalled) { // Configure and load packetevents @@ -147,8 +147,7 @@ public void onEnable() { @Override public void onDisable() { if (isPacketEventsInstalled) { - AEFModule.ENABLED_MODULES.forEach(AEFModule::disable); - AEFModule.ENABLED_MODULES.clear(); + AEFModule.disableAll(); PacketEvents.getAPI().terminate(); } if (languageCacheMap != null) { diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/commands/aef/subcommands/DisableSubCmd.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/commands/aef/subcommands/DisableSubCmd.java index 36e23550c..b24d8401d 100644 --- a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/commands/aef/subcommands/DisableSubCmd.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/commands/aef/subcommands/DisableSubCmd.java @@ -37,8 +37,8 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String commandLab sender.sendMessage(Component.empty()); sender.sendMessage(Component.text(" Disabling plugin.").color(KyoriUtil.AEF_WHITE)); AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); - plugin.getServer().getGlobalRegionScheduler().execute(plugin, () -> { - AEFModule.ENABLED_MODULES.forEach(AEFModule::disable); + plugin.getServer().getAsyncScheduler().runNow(plugin, disable -> { + AEFModule.disableAll(); sender.sendMessage(Component.text(" All enabled plugin features have been disabled.").color(KyoriUtil.AEF_BLUE)); sender.sendMessage(Component.text(" Use /aef reload to enable the plugin again.").color(KyoriUtil.AEF_BLUE)); sender.sendMessage(Component.empty()); diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/commands/aef/subcommands/ReloadSubCmd.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/commands/aef/subcommands/ReloadSubCmd.java index c13653a82..a56abf649 100644 --- a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/commands/aef/subcommands/ReloadSubCmd.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/commands/aef/subcommands/ReloadSubCmd.java @@ -36,7 +36,7 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String commandLab sender.sendMessage(Component.empty()); sender.sendMessage(Component.text(" Reloading AnarchyExploitFixes...").color(KyoriUtil.AEF_WHITE)); AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); - plugin.getServer().getGlobalRegionScheduler().execute(plugin, () -> { + plugin.getServer().getAsyncScheduler().runNow(plugin, reload -> { plugin.reloadPlugin(); sender.sendMessage(Component.text(" Reload complete.").color(KyoriUtil.AEF_BLUE)); sender.sendMessage(Component.empty()); diff --git a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/AEFModule.java b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/AEFModule.java index dd77b19dd..6540f93b5 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/AEFModule.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/AEFModule.java @@ -1,26 +1,43 @@ package me.xginko.aef.modules; +import com.google.common.collect.ImmutableSet; import me.xginko.aef.AnarchyExploitFixes; import me.xginko.aef.config.Config; import me.xginko.aef.modules.packets.PacketModule; import me.xginko.aef.utils.models.ConditionalEnableable; import me.xginko.aef.utils.models.Disableable; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.config.Configurator; import org.reflections.Reflections; import org.reflections.scanners.Scanners; import java.lang.reflect.Modifier; +import java.util.Comparator; import java.util.HashSet; import java.util.Set; +import java.util.stream.Collectors; public abstract class AEFModule implements ConditionalEnableable, Disableable { - private static final Reflections MODULES_PACKAGE = new Reflections(AEFModule.class.getPackage().getName()); - public static final Set ENABLED_MODULES = new HashSet<>(); + protected static final Set> AVAILABLE_MODULES; + protected static final Set ENABLED_MODULES; + + static { + // Disable reflection logging for this operation because its just confusing and provides no value. + Configurator.setLevel(AEFModule.class.getPackage().getName() + ".libs.reflections.Reflections", Level.OFF); + AVAILABLE_MODULES = new Reflections(AEFModule.class.getPackage().getName()) + .get(Scanners.SubTypes.of(AEFModule.class).asClass()) + .stream() + .filter(clazz -> !clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers())) + .map(clazz -> (Class) clazz) + .sorted(Comparator.comparing(Class::getSimpleName)) + .collect(Collectors.collectingAndThen(Collectors.toList(), ImmutableSet::copyOf)); + ENABLED_MODULES = new HashSet<>(); + } protected final AnarchyExploitFixes plugin; protected final Config config; - public final String configPath; - private final String logFormat; + protected final String configPath, logFormat; public AEFModule(String configPath) { this.plugin = AnarchyExploitFixes.getInstance(); @@ -35,26 +52,27 @@ public AEFModule(String configPath) { } } - public static void reloadModules() { - ENABLED_MODULES.forEach(AEFModule::disable); + public static void disableAll() { + ENABLED_MODULES.forEach(Disableable::disable); ENABLED_MODULES.clear(); + } - for (Class clazz : MODULES_PACKAGE.get(Scanners.SubTypes.of(AEFModule.class).asClass())) { - if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) continue; + public static void reloadModules() { + disableAll(); + for (Class moduleClass : AVAILABLE_MODULES) { try { - AEFModule module = (AEFModule) clazz.getDeclaredConstructor().newInstance(); + AEFModule module = moduleClass.getDeclaredConstructor().newInstance(); if (module.shouldEnable()) { if (module instanceof PacketModule && AnarchyExploitFixes.config().packets_disabled) { module.warn("Cannot enable because you disabled packets in config!"); continue; } - module.enable(); ENABLED_MODULES.add(module); } - } catch (Throwable t) { - AnarchyExploitFixes.getPrefixedLogger().error("Failed to load module {}", clazz.getSimpleName(), t); + } catch (Throwable t) { // We want to catch everything here if it fails to init + AnarchyExploitFixes.getPrefixedLogger().warn("Failed initialising module class '{}'.", moduleClass.getSimpleName(), t); } } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/AnarchyExploitFixes.java b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/AnarchyExploitFixes.java index 7253574de..fd7262249 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/AnarchyExploitFixes.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/AnarchyExploitFixes.java @@ -54,11 +54,8 @@ public final class AnarchyExploitFixes extends JavaPlugin { @Override public void onLoad() { PlatformUtil.load(); - prefixedLogger = LoggerFactory.getLogger(getLogger().getName()); - unPrefixedLogger = LoggerFactory.getLogger(""); // Disable logging for some shaded libraries as those can get very verbose String shadedLibs = getClass().getPackage().getName() + ".libs"; - Configurator.setLevel(shadedLibs + ".reflections.Reflections", Level.OFF); Configurator.setLevel(shadedLibs + ".zaxxer.hikari.pool.PoolBase", Level.OFF); Configurator.setLevel(shadedLibs + ".zaxxer.hikari.pool.HikariPool", Level.OFF); Configurator.setLevel(shadedLibs + ".zaxxer.hikari.HikariDataSource", Level.OFF); @@ -75,6 +72,9 @@ public void onLoad() { @Override public void onEnable() { + prefixedLogger = LoggerFactory.getLogger(getLogger().getName()); + unPrefixedLogger = LoggerFactory.getLogger(""); + if (!isPacketEventsInstalled) { Stream.of(" ", " _ _ _ _ _ ", @@ -155,8 +155,7 @@ public void onEnable() { @Override public void onDisable() { if (isPacketEventsInstalled) { - AEFModule.ENABLED_MODULES.forEach(AEFModule::disable); - AEFModule.ENABLED_MODULES.clear(); + AEFModule.disableAll(); PacketEvents.getAPI().terminate(); } if (languageCacheMap != null) { diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/commands/aef/subcommands/DisableSubCmd.java b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/commands/aef/subcommands/DisableSubCmd.java index 1d3e1cf5a..ee9b19f8a 100644 --- a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/commands/aef/subcommands/DisableSubCmd.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/commands/aef/subcommands/DisableSubCmd.java @@ -36,9 +36,8 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String commandLab sender.sendMessage(""); sender.sendMessage(ChatColor.WHITE + " Disabling plugin."); AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); - plugin.getServer().getScheduler().runTask(plugin, () -> { - AEFModule.ENABLED_MODULES.forEach(AEFModule::disable); - AEFModule.ENABLED_MODULES.clear(); + plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> { + AEFModule.disableAll(); sender.sendMessage(ChatColor.AQUA + " All enabled plugin features have been disabled."); sender.sendMessage(ChatColor.AQUA + " Use /aef reload to enable the plugin again."); sender.sendMessage(""); diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/commands/aef/subcommands/ReloadSubCmd.java b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/commands/aef/subcommands/ReloadSubCmd.java index 3bcf97e7d..ec45d202f 100644 --- a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/commands/aef/subcommands/ReloadSubCmd.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/commands/aef/subcommands/ReloadSubCmd.java @@ -36,9 +36,9 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String commandLab sender.sendMessage(""); sender.sendMessage(" Reloading AnarchyExploitFixes..."); - plugin.getServer().getScheduler().runTask(plugin, () -> { + plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> { plugin.reloadPlugin(); - sender.sendMessage(ChatColor.AQUA+" Reload complete."); + sender.sendMessage(ChatColor.AQUA + " Reload complete."); sender.sendMessage(""); }); diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/AEFModule.java b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/AEFModule.java index 092826bb8..0df5bc3b8 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/AEFModule.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/AEFModule.java @@ -1,26 +1,43 @@ package me.xginko.aef.modules; +import com.google.common.collect.ImmutableSet; import me.xginko.aef.AnarchyExploitFixes; import me.xginko.aef.config.Config; import me.xginko.aef.modules.packets.PacketModule; import me.xginko.aef.utils.models.ConditionalEnableable; import me.xginko.aef.utils.models.Disableable; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.config.Configurator; import org.reflections.Reflections; import org.reflections.scanners.Scanners; import java.lang.reflect.Modifier; +import java.util.Comparator; import java.util.HashSet; import java.util.Set; +import java.util.stream.Collectors; public abstract class AEFModule implements ConditionalEnableable, Disableable { - private static final Reflections MODULES_PACKAGE = new Reflections(AEFModule.class.getPackage().getName()); - public static final Set ENABLED_MODULES = new HashSet<>(); + protected static final Set> AVAILABLE_MODULES; + protected static final Set ENABLED_MODULES; + + static { + // Disable reflection logging for this operation because its just confusing and provides no value. + Configurator.setLevel(AEFModule.class.getPackage().getName() + ".libs.reflections.Reflections", Level.OFF); + AVAILABLE_MODULES = new Reflections(AEFModule.class.getPackage().getName()) + .get(Scanners.SubTypes.of(AEFModule.class).asClass()) + .stream() + .filter(clazz -> !clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers())) + .map(clazz -> (Class) clazz) + .sorted(Comparator.comparing(Class::getSimpleName)) + .collect(Collectors.collectingAndThen(Collectors.toList(), ImmutableSet::copyOf)); + ENABLED_MODULES = new HashSet<>(); + } protected final AnarchyExploitFixes plugin; protected final Config config; - public final String configPath; - private final String logFormat; + protected final String configPath, logFormat; public AEFModule(String configPath) { this.plugin = AnarchyExploitFixes.getInstance(); @@ -35,26 +52,27 @@ public AEFModule(String configPath) { } } - public static void reloadModules() { - ENABLED_MODULES.forEach(AEFModule::disable); + public static void disableAll() { + ENABLED_MODULES.forEach(Disableable::disable); ENABLED_MODULES.clear(); + } - for (Class clazz : MODULES_PACKAGE.get(Scanners.SubTypes.of(AEFModule.class).asClass())) { - if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) continue; + public static void reloadModules() { + disableAll(); + for (Class moduleClass : AVAILABLE_MODULES) { try { - AEFModule module = (AEFModule) clazz.getDeclaredConstructor().newInstance(); + AEFModule module = moduleClass.getDeclaredConstructor().newInstance(); if (module.shouldEnable()) { if (module instanceof PacketModule && AnarchyExploitFixes.config().packets_disabled) { module.warn("Cannot enable because you disabled packets in config!"); continue; } - module.enable(); ENABLED_MODULES.add(module); } - } catch (Throwable t) { - AnarchyExploitFixes.prefixedLogger().error("Failed to load module {}", clazz.getSimpleName(), t); + } catch (Throwable t) { // We want to catch everything here if it fails to init + AnarchyExploitFixes.prefixedLogger().warn("Failed initialising module class '{}'.", moduleClass.getSimpleName(), t); } } }