Skip to content

Commit

Permalink
use reflections to reload modules
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Mar 21, 2024
1 parent 41b4854 commit a6af3c5
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 391 deletions.
1 change: 1 addition & 0 deletions AnarchyExploitFixesFolia/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ tasks.shadowJar {
"com/cryptomorin/xseries/NoteBlockMusic*",
"com/cryptomorin/xseries/SkullCacheListener*"
)
relocate("org.reflections", "me.moomoo.anarchyexploitfixes.libs.reflections")
relocate("org.apache.commons.math3", "me.moomoo.anarchyexploitfixes.libs.fastmath")
relocate("com.github.benmanes.caffeine", "me.moomoo.anarchyexploitfixes.libs.caffeine")
relocate("io.papermc.lib", "me.moomoo.anarchyexploitfixes.libs.paperlib")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public void onEnable() {

@Override
public void onDisable() {
AEFModule.modules.forEach(AEFModule::disable);
AEFModule.modules.clear();
AEFModule.enabledModules.forEach(AEFModule::disable);
AEFModule.enabledModules.clear();
PacketEvents.getAPI().terminate();
if (metrics != null) {
metrics.shutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void disableCommand(
final CommandSender sender
) {
sender.sendMessage(Component.text("Disabling plugin.").color(NamedTextColor.RED));
AEFModule.modules.forEach(AEFModule::disable);
AEFModule.enabledModules.forEach(AEFModule::disable);
sender.sendMessage(Component.text("All enabled plugin features have been disabled.").color(NamedTextColor.GREEN));
sender.sendMessage(Component.text("Use /aef reload to enable the plugin again. " +
"You can also use third party options like plugman or serverutils.").color(NamedTextColor.WHITE));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,11 @@
package me.moomoo.anarchyexploitfixes.modules;

import me.moomoo.anarchyexploitfixes.modules.bedrock.*;
import me.moomoo.anarchyexploitfixes.modules.chat.PreventPluginScanning;
import me.moomoo.anarchyexploitfixes.modules.chat.CommandWhitelist;
import me.moomoo.anarchyexploitfixes.modules.chunklimits.*;
import me.moomoo.anarchyexploitfixes.modules.combat.*;
import me.moomoo.anarchyexploitfixes.modules.dupepreventions.*;
import me.moomoo.anarchyexploitfixes.modules.elytra.*;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.*;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.enchantments.HigherEnchants;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.enchantments.InapplicableEnchants;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.enchantments.IncompatibleEnchants;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.nbt.CustomNBTFilter;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.nbt.NBTFilledStorageItem;
import me.moomoo.anarchyexploitfixes.modules.illegals.placedblocks.PeriodicallyRemoveIllegalBlocks;
import me.moomoo.anarchyexploitfixes.modules.illegals.placedblocks.RemoveIllegalBlocksOnChunkload;
import me.moomoo.anarchyexploitfixes.modules.illegals.placedblocks.RemoveUnnaturalSpawners;
import me.moomoo.anarchyexploitfixes.modules.lagpreventions.*;
import me.moomoo.anarchyexploitfixes.modules.lagpreventions.lowtpsphysics.*;
import me.moomoo.anarchyexploitfixes.modules.misc.FirstJoinMessages;
import me.moomoo.anarchyexploitfixes.modules.misc.JoinLeaveMessages;
import me.moomoo.anarchyexploitfixes.modules.misc.MaskKickMessages;
import me.moomoo.anarchyexploitfixes.modules.misc.PreventMessageKick;
import me.moomoo.anarchyexploitfixes.modules.packets.AntiBoatFly;
import me.moomoo.anarchyexploitfixes.modules.packets.AntiTagParserCrash;
import me.moomoo.anarchyexploitfixes.modules.packets.AntiWindowClickCrash;
import me.moomoo.anarchyexploitfixes.modules.patches.*;
import me.moomoo.anarchyexploitfixes.modules.preventions.BedTrap;
import me.moomoo.anarchyexploitfixes.modules.preventions.NetherRoof;
import me.moomoo.anarchyexploitfixes.modules.preventions.PreventNonSurvival;
import me.moomoo.anarchyexploitfixes.modules.preventions.PreventOppedPlayers;
import me.moomoo.anarchyexploitfixes.modules.preventions.blockbreak.PistonExplodePermBlockRemoval;
import me.moomoo.anarchyexploitfixes.modules.preventions.blockbreak.PistonPlaceWhileRetractPermBlockRemoval;
import me.moomoo.anarchyexploitfixes.modules.preventions.blockbreak.StructureGrowPermBlockRemoval;
import me.moomoo.anarchyexploitfixes.modules.preventions.portals.*;
import me.moomoo.anarchyexploitfixes.modules.preventions.withers.*;
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import org.reflections.Reflections;
import org.reflections.scanners.Scanners;

import java.util.HashSet;
import java.util.Set;

public interface AEFModule {

Expand All @@ -46,159 +15,25 @@ public interface AEFModule {
boolean shouldEnable();
void disable();

HashSet<AEFModule> modules = new HashSet<>();
Set<AEFModule> enabledModules = new HashSet<>();
Reflections reflections = new Reflections(AEFModule.class.getPackage().getName());

static void reloadModules() {
modules.forEach(AEFModule::disable);
modules.clear();

/*
Bedrock
*/
modules.add(new FillNetherCeilingOnChunkload());
modules.add(new FillNetherFloorOnChunkload());
modules.add(new FillOverworldFloorOnChunkload());
modules.add(new PeriodicallyFillNetherCeiling());
modules.add(new PeriodicallyFillNetherFloor());
modules.add(new PeriodicallyFillOverworldFloor());
modules.add(new PreventGoingBelowBedrockFloor());
/*
Chat
*/
modules.add(new CommandWhitelist());
modules.add(new PreventPluginScanning());
/*
Chunk Limits
*/
modules.add(new BlockLimit());
modules.add(new CustomEntityLimit());
modules.add(new ExpBottleLimit());
modules.add(new FallingBlockLimit());
modules.add(new MinecartLimit());
modules.add(new VehicleLimit());
modules.add(new NonLivingEntityLimit());
modules.add(new DroppedItemLimit());
modules.add(new VillagerLimit());
/*
Dupe Preventions
*/
modules.add(new ChestedEntitiesInPortals());
modules.add(new ChestsOnEntities());
modules.add(new CloseEntitiyInventoriesOnLogout());
modules.add(new CloseEntityInventoryOnChunkUnload());
modules.add(new EndPortalDupe());
/*
Elytra
*/
modules.add(new ElytraHelper());
modules.add(new ElytraAtSpawn());
modules.add(new ElytraGlobal());
modules.add(new ElytraOnCeiling());
modules.add(new ElytraPacketFly());
/*
Illegals
*/
// Blocks
modules.add(new RemoveIllegalBlocksOnChunkload());
modules.add(new PeriodicallyRemoveIllegalBlocks());
modules.add(new RemoveUnnaturalSpawners());
// Items
// Enchantments
modules.add(new HigherEnchants());
modules.add(new InapplicableEnchants());
modules.add(new IncompatibleEnchants());
// Other
modules.add(new OverstackedItems());
modules.add(new UnbreakableItems());
modules.add(new BannedMaterials());
modules.add(new SpawnEggItems());
modules.add(new PlayerHeadItems());
modules.add(new BannedItemNames());
// NBT
modules.add(new NBTFilledStorageItem());
modules.add(new CustomNBTFilter());

/*
Lag Preventions
*/
modules.add(new LeverSpam());
modules.add(new SnowballExploit());
modules.add(new FallingBlockStasis());
modules.add(new InventoryActionLag());
modules.add(new LiquidUpdateLag());
modules.add(new PathfindingLimits());
modules.add(new PreventFloodingMachines());
modules.add(new TargetDistanceLimit());
modules.add(new Explosions());
modules.add(new BlockPhysics());
modules.add(new LiquidSpread());
modules.add(new BlockMelting());
modules.add(new Redstone());
modules.add(new Noteblocks());
modules.add(new FireSpread());
modules.add(new GrassSpread());
modules.add(new LeaveDecay());
/*
Misc
*/
modules.add(new FirstJoinMessages());
modules.add(new JoinLeaveMessages());
modules.add(new MaskKickMessages());
modules.add(new PreventMessageKick());
/*
Patches
*/
modules.add(new BookBan());
modules.add(new MapCursorLag());
modules.add(new CommandSign());
modules.add(new GodMode());
modules.add(new TeleportCoordExploit());
modules.add(new BeehiveCoordinates());
modules.add(new WorldChangeCrash());
/*
Combat
*/
modules.add(new Burrow());
modules.add(new CrystalAuraDelay());
modules.add(new AnchorAuraDelay());
modules.add(new BedAuraDelay());
modules.add(new PistonAuraDelay());
modules.add(new CrystalAuraHotbarSwitchDelay());
modules.add(new BowBomb());
/*
Preventions
*/
modules.add(new BedTrap());
modules.add(new NetherRoof());
modules.add(new PreventNonSurvival());
modules.add(new PreventOppedPlayers());
// Blockbreak
modules.add(new PistonExplodePermBlockRemoval());
modules.add(new PistonPlaceWhileRetractPermBlockRemoval());
modules.add(new StructureGrowPermBlockRemoval());
// Portals
modules.add(new EndPortalDestruction());
modules.add(new PreventAllEntitiesInPortals());
modules.add(new PreventPortalTraps());
modules.add(new PreventProjectilesInPortals());
modules.add(new PreventSpecificEntitiesInPortals());
// Withers
modules.add(new DisableWitherSkulls());
modules.add(new RateLimitWitherSkulls());
modules.add(new RemoveAllSkullsPeriodically());
modules.add(new RemoveSkullsAfterXTicks());
modules.add(new RemoveSkullsOnChunkload());
modules.add(new RemoveSkullsOnChunkunload());
modules.add(new WitherSpawningAtSpawn());
/*
Packet Modules
*/
modules.add(new AntiBoatFly());
modules.add(new AntiTagParserCrash());
modules.add(new AntiWindowClickCrash());

for (AEFModule module : modules) {
if (module.shouldEnable()) module.enable();
enabledModules.forEach(AEFModule::disable);
enabledModules.clear();

for (Class<?> clazz : reflections.get(Scanners.SubTypes.of(AEFModule.class).asClass())) {
if (clazz.isInterface()) continue;

try {
AEFModule module = (AEFModule) clazz.getDeclaredConstructor().newInstance();
if (module.shouldEnable()) {
module.enable();
enabledModules.add(module);
}
} catch (Exception e) {
AnarchyExploitFixes.getPrefixedLogger().warn("Failed to load module " + clazz.getSimpleName());
}
}
}
}
1 change: 1 addition & 0 deletions AnarchyExploitFixesLegacy/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ tasks.shadowJar {
"com/cryptomorin/xseries/NoteBlockMusic*",
"com/cryptomorin/xseries/SkullCacheListener*"
)
relocate("org.reflections", "me.moomoo.anarchyexploitfixes.libs.reflections")
relocate("com.zaxxer", "me.moomoo.anarchyexploitfixes.libs.zaxxer")
relocate("org.apache.commons.math3", "me.moomoo.anarchyexploitfixes.libs.fastmath")
relocate("com.github.benmanes.caffeine", "me.moomoo.anarchyexploitfixes.libs.caffeine")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public void onEnable() {
@Override
public void onDisable() {
AEFModule.disableAll();
AEFListener.enabledListeners.forEach(AEFListener::disable);
PacketEvents.getAPI().terminate();
if (metrics != null) {
metrics.shutdown();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package me.moomoo.anarchyexploitfixes.listener;

import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import org.reflections.Reflections;
import org.reflections.scanners.Scanners;

import java.util.HashSet;
import java.util.Set;

Expand All @@ -9,18 +13,24 @@ public interface AEFListener {
void enable();
void disable();

Set<AEFListener> LISTENERS = new HashSet<>();
Set<AEFListener> enabledListeners = new HashSet<>();
Reflections reflections = new Reflections(AEFListener.class.getPackage().getName());

static void reloadListeners() {
LISTENERS.forEach(AEFListener::disable);
LISTENERS.clear();
enabledListeners.forEach(AEFListener::disable);
enabledListeners.clear();

LISTENERS.add(new InteractEntityPacketListener());
LISTENERS.add(new UseItemPacketListener());
for (Class<?> clazz : reflections.get(Scanners.SubTypes.of(AEFListener.class).asClass())) {
if (clazz.isInterface()) continue;

for (AEFListener listener : LISTENERS) {
if (listener.shouldEnable()) {
listener.enable();
try {
AEFListener listener = (AEFListener) clazz.getDeclaredConstructor().newInstance();
if (listener.shouldEnable()) {
listener.enable();
enabledListeners.add(listener);
}
} catch (Exception e) {
AnarchyExploitFixes.getPrefixedLogger().warning("Failed to load listener " + clazz.getSimpleName());
}
}
}
Expand Down
Loading

0 comments on commit a6af3c5

Please sign in to comment.