Skip to content

Commit

Permalink
speed up reload
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Nov 5, 2024
1 parent a3b10af commit fcec699
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<AEFModule> ENABLED_MODULES = new HashSet<>();
protected static final Set<Class<AEFModule>> AVAILABLE_MODULES;
protected static final Set<AEFModule> 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<AEFModule>) 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();
Expand All @@ -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<AEFModule> 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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -75,6 +72,9 @@ public void onLoad() {

@Override
public void onEnable() {
prefixedLogger = LoggerFactory.getLogger(getLogger().getName());
unPrefixedLogger = LoggerFactory.getLogger("");

if (!isPacketEventsInstalled) {
Stream.of(" ",
" _ _ _ _ _ ",
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
});

Expand Down
Original file line number Diff line number Diff line change
@@ -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<AEFModule> ENABLED_MODULES = new HashSet<>();
protected static final Set<Class<AEFModule>> AVAILABLE_MODULES;
protected static final Set<AEFModule> 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<AEFModule>) 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();
Expand All @@ -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<AEFModule> 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);
}
}
}
Expand Down

0 comments on commit fcec699

Please sign in to comment.