From df8bcf1b1006e8cca9b20e43c67c8a2f0420eaa5 Mon Sep 17 00:00:00 2001 From: cvs0 <143862758+cvs0@users.noreply.github.com> Date: Sat, 6 Jul 2024 20:49:35 -0600 Subject: [PATCH] fix: Fixed addon system module + command loading --- src/main/java/net/aoba/AobaClient.java | 51 +++++++++---------- .../java/net/aoba/cmd/CommandManager.java | 16 +++++- .../java/net/aoba/module/ModuleManager.java | 23 ++++++++- 3 files changed, 60 insertions(+), 30 deletions(-) diff --git a/src/main/java/net/aoba/AobaClient.java b/src/main/java/net/aoba/AobaClient.java index 951caec8..62a18a5c 100644 --- a/src/main/java/net/aoba/AobaClient.java +++ b/src/main/java/net/aoba/AobaClient.java @@ -39,6 +39,8 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; public class AobaClient { @@ -76,15 +78,35 @@ public void loadAssets() { System.out.println("[Aoba] Starting Client"); eventManager = new EventManager(); + + LogUtils.getLogger().info("[Aoba] Starting addon initialization"); + List addons = new ArrayList<>(); + + for (EntrypointContainer entrypoint : FabricLoader.getInstance().getEntrypointContainers("aoba", IAddon.class)) { + IAddon addon = entrypoint.getEntrypoint(); + + try { + LogUtils.getLogger().info("[Aoba] Initializing addon: " + addon.getClass().getName()); + addon.onIntialize(); + LogUtils.getLogger().info("[Aoba] Addon initialized: " + addon.getClass().getName()); + } catch (Throwable e) { + LogUtils.getLogger().error("Error initializing addon: " + addon.getClass().getName(), e); + } + + addons.add(addon); + } + + LogUtils.getLogger().info("[Aoba] Addon initialization completed"); + renderUtils = new RenderUtils(); System.out.println("[Aoba] Reading Settings"); settingManager = new SettingManager(); System.out.println("[Aoba] Reading Friends List"); friendsList = new FriendsList(); System.out.println("[Aoba] Initializing Modules"); - moduleManager = new ModuleManager(); + moduleManager = new ModuleManager(addons); System.out.println("[Aoba] Initializing Commands"); - commandManager = new CommandManager(); + commandManager = new CommandManager(addons); System.out.println("[Aoba] Initializing Font Manager"); fontManager = new FontManager(); fontManager.Initialize(); @@ -99,31 +121,6 @@ public void loadAssets() { SettingManager.loadSettings(settingManager.modulesContainer); SettingManager.loadSettings(settingManager.hiddenContainer); - LogUtils.getLogger().info("[Aoba] Starting addon initialization"); - - for (EntrypointContainer entrypoint : FabricLoader.getInstance().getEntrypointContainers("aoba", IAddon.class)) { - IAddon addon = entrypoint.getEntrypoint(); - - try { - LogUtils.getLogger().info("[Aoba] Initializing addon: " + addon.getClass().getName()); - addon.onIntialize(); - } catch (Exception e) { - LogUtils.getLogger().error("Error initializing addon: " + addon.getClass().getName(), e); - } - - addon.modules().stream().filter(Objects::nonNull).forEach(module -> { - try { - moduleManager.modules.add(module); - LogUtils.getLogger().info("[Aoba] Successfully registered module: " + module.getClass().getName()); - } catch (Exception e) { - LogUtils.getLogger().error("Error registering module: " + module.getClass().getName() + " for addon: " + addon.getClass().getName(), e); - } - }); - } - - LogUtils.getLogger().info("[Aoba] Addon initialization completed"); - - globalChat = new GlobalChat(); diff --git a/src/main/java/net/aoba/cmd/CommandManager.java b/src/main/java/net/aoba/cmd/CommandManager.java index ea53ce85..57ef7b6b 100644 --- a/src/main/java/net/aoba/cmd/CommandManager.java +++ b/src/main/java/net/aoba/cmd/CommandManager.java @@ -23,7 +23,11 @@ import java.lang.reflect.Field; import java.util.HashMap; +import java.util.List; +import java.util.Objects; + import net.aoba.Aoba; +import net.aoba.api.IAddon; import net.aoba.cmd.commands.*; import net.aoba.settings.SettingManager; import net.aoba.settings.types.StringSetting; @@ -69,7 +73,7 @@ public class CommandManager { /** * Constructor for Command Manager. Initializes all commands. */ - public CommandManager() { + public CommandManager(List addons) { PREFIX = new StringSetting("Prefix", "Prefix", ".aoba"); @@ -84,6 +88,16 @@ public CommandManager() { Command cmd = (Command)field.get(this); commands.put(cmd.getName(), cmd); } + + addons.stream().filter(Objects::nonNull).forEach(addon -> { + addon.commands().forEach(command -> { + if (commands.containsKey(command.getName())) { + System.out.println("Warning: Duplicate command name \"" + command.getName() + "\" from addon. This command will not be registered."); + } else { + commands.put(command.getName(), command); + } + }); + }); }catch(Exception e) { System.out.println("Error initializing Aoba commands."); diff --git a/src/main/java/net/aoba/module/ModuleManager.java b/src/main/java/net/aoba/module/ModuleManager.java index 7a4fd7fc..ef7e166b 100644 --- a/src/main/java/net/aoba/module/ModuleManager.java +++ b/src/main/java/net/aoba/module/ModuleManager.java @@ -22,7 +22,12 @@ package net.aoba.module; import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import com.mojang.logging.LogUtils; import net.aoba.Aoba; +import net.aoba.api.IAddon; import net.aoba.event.events.KeyDownEvent; import net.aoba.event.events.RenderEvent; import net.aoba.event.listeners.KeyDownListener; @@ -105,8 +110,7 @@ public class ModuleManager implements KeyDownListener { public Module xray = new XRay(); public Module zoom = new Zoom(); - public ModuleManager() { - // Look at all these modules! + public ModuleManager(List addons) { addModule(aimbot); addModule(anticactus); addModule(antiinvis); @@ -166,6 +170,21 @@ public ModuleManager() { addModule(trajectory); addModule(xray); addModule(zoom); + + addons.stream().filter(Objects::nonNull).forEach(addon -> { + try { + addon.modules().forEach(module -> { + try { + addModule(module); + LogUtils.getLogger().info("[Aoba] Successfully added module: " + module.getClass().getName() + " from addon: " + addon.getClass().getName()); + } catch (Exception e) { + LogUtils.getLogger().error("Error adding module: " + module.getClass().getName() + " from addon: " + addon.getClass().getName(), e); + } + }); + } catch (Exception e) { + LogUtils.getLogger().error("Error processing modules from addon: " + addon.getClass().getName(), e); + } + }); for(Module module : modules) { for(Setting setting : module.getSettings()) {