From 5076e2b37c407f8ccdd4644688e87acb3bdd7787 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Wed, 10 Jan 2024 12:40:50 +0100 Subject: [PATCH] chore: renamed ModManager, created static instance, made main class fields nonstatic --- src/main/java/ftbsc/bscv/Boscovicino.java | 67 ++++++++++--------- .../ftbsc/bscv/commands/AbstractCommand.java | 2 +- .../java/ftbsc/bscv/commands/ModCommands.java | 6 +- .../ftbsc/bscv/modules/AbstractModule.java | 4 +- .../java/ftbsc/bscv/modules/defense/Aura.java | 4 +- .../ftbsc/bscv/modules/hud/ActiveModules.java | 2 +- .../ftbsc/bscv/modules/motion/GuiMove.java | 2 +- src/main/java/ftbsc/bscv/system/Bindings.java | 5 +- src/main/java/ftbsc/bscv/system/Friends.java | 4 +- src/main/java/ftbsc/bscv/system/Macros.java | 6 +- .../system/{ModManager.java => Modules.java} | 4 +- 11 files changed, 53 insertions(+), 53 deletions(-) rename src/main/java/ftbsc/bscv/system/{ModManager.java => Modules.java} (94%) diff --git a/src/main/java/ftbsc/bscv/Boscovicino.java b/src/main/java/ftbsc/bscv/Boscovicino.java index cf03753..b104f96 100644 --- a/src/main/java/ftbsc/bscv/Boscovicino.java +++ b/src/main/java/ftbsc/bscv/Boscovicino.java @@ -27,26 +27,26 @@ import org.apache.logging.log4j.Logger; import java.io.IOException; -@Mod("bscv") +@Mod(Boscovicino.MOD_ID) public class Boscovicino implements ICommons { - public static String MOD_ID = "bscv"; + public static final String MOD_ID = "bscv"; public static final Logger LOGGER = LogManager.getLogger(); - public static ModManager modManager; //todo rename - private final CommandDispatcher dispatcher = new CommandDispatcher<>(); - public static ForgeConfigSpec spec; + private static Boscovicino INSTANCE; + public static Boscovicino getInstance() { return INSTANCE; } - private static Friends friends; - public static Friends friends() { return Boscovicino.friends; } + public final ForgeConfigSpec spec; - @SuppressWarnings("unused") // it just needs to exist to be used by player - private static Ruler ruler; + public final Modules modules; + public final Friends friends; + public final Bindings bindings; + public final Macros macros; - public static Bindings bindings; - public static Macros macros; + @SuppressWarnings({"unused", "FieldCanBeLocal"}) // it just needs to exist to be used by player + private final Ruler ruler; //TODO remove public Boscovicino() throws IOException { FMLJavaModLoadingContext.get().getModEventBus().addListener(this::onSetupComplete); @@ -54,29 +54,31 @@ public class Boscovicino implements ICommons { ForgeConfigSpec.Builder cfg = new ForgeConfigSpec.Builder(); CommandDispatcher dp = this.dispatcher; - Boscovicino.modManager = new ModManager(cfg, dp); - Boscovicino.modManager.load(); + this.modules = new Modules(cfg, dp); + this.modules.load(); - Boscovicino.modManager.finish(); + this.modules.finish(); - Boscovicino.ruler = new Ruler(); + this.ruler = new Ruler(); //TODO remove ForgeConfigSpec.Builder bindingSpec = new ForgeConfigSpec.Builder(); - Boscovicino.bindings = new Bindings(bindingSpec); - Boscovicino.macros = new Macros(); - - Boscovicino.spec = cfg.build(); + this.bindings = new Bindings(bindingSpec); + this.macros = new Macros(this); ForgeConfigSpec.Builder friendSpec = new ForgeConfigSpec.Builder(); - Boscovicino.friends = new Friends(friendSpec, dp); + this.friends = new Friends(friendSpec, dp); - // register config handler - ModLoadingContext.get().registerConfig(Type.CLIENT, spec, "bscv.toml"); + // register config handlers + this.spec = cfg.build(); + ModLoadingContext.get().registerConfig(Type.CLIENT, this.spec, "bscv.toml"); ModLoadingContext.get().registerConfig(Type.CLIENT, friendSpec.build(), "friends.toml"); ModLoadingContext.get().registerConfig(Type.CLIENT, bindingSpec.build(), "bindings.toml"); - // Register ourselves for server and other game events we are interested in + // register ourselves for server and other game events we are interested in MinecraftForge.EVENT_BUS.register(this); + + // create instance + INSTANCE = this; } public static void log(String message, Object... args) { @@ -93,7 +95,7 @@ public class Boscovicino implements ICommons { private void onSetupComplete(final FMLLoadCompleteEvent event) { LOGGER.info("Initializing modules"); - for (IModule m : modManager.mods) { + for (IModule m : modules.mods) { if (m.isEnabled()) { m.enable(); // re-run enable() to register on the event bus and run enabled callbacks } @@ -109,8 +111,8 @@ public class Boscovicino implements ICommons { @SubscribeEvent public void onClientChatEvent(ClientChatEvent event) { - if (event.getMessage().startsWith("/")) { - CommandSource source = MC.player.createCommandSourceStack(); // TODO player could be NULL + if (MC.player != null && event.getMessage().startsWith("/")) { + CommandSource source = MC.player.createCommandSourceStack(); try { LOGGER.info(String.format("Running command %s", event.getMessage())); this.dispatcher.execute(event.getMessage().substring(1), source); @@ -123,14 +125,15 @@ public class Boscovicino implements ICommons { } @SubscribeEvent + // TODO de-jank this, maybe by extending IngameMenuScreen public void onPauseMenu(InitGuiEvent.Post event) { - if (event.getGui() instanceof IngameMenuScreen) { // TODO de-jank this, maybe by extending IngameMenuScreen + if (event.getGui() instanceof IngameMenuScreen) { IngameMenuScreen screen = (IngameMenuScreen) event.getGui(); screen.buttons.remove(3); screen.buttons.remove(3); screen.children.remove(3); screen.children.remove(3); - Button mods_btn = new Button( + Button modsBtn = new Button( screen.width / 2 + 4, screen.height / 4 + 72 + -16, 98, 20, new TranslationTextComponent("fml.menu.mods"), @@ -139,10 +142,10 @@ public class Boscovicino implements ICommons { } ); // need to add it twice for it to work once... - screen.buttons.add(3, mods_btn); - screen.buttons.add(3, mods_btn); - screen.children.add(3, mods_btn); - screen.children.add(3, mods_btn); + screen.buttons.add(3, modsBtn); + screen.buttons.add(3, modsBtn); + screen.children.add(3, modsBtn); + screen.children.add(3, modsBtn); } } } diff --git a/src/main/java/ftbsc/bscv/commands/AbstractCommand.java b/src/main/java/ftbsc/bscv/commands/AbstractCommand.java index 9568237..5fc1651 100644 --- a/src/main/java/ftbsc/bscv/commands/AbstractCommand.java +++ b/src/main/java/ftbsc/bscv/commands/AbstractCommand.java @@ -19,7 +19,7 @@ public abstract class AbstractCommand implements ICommand, ICommons { } public CommandDispatcher getDispatcher() { - return Boscovicino.modManager.getDispatcher(); + return Boscovicino.getInstance().modules.getDispatcher(); } public List> subcommands() { diff --git a/src/main/java/ftbsc/bscv/commands/ModCommands.java b/src/main/java/ftbsc/bscv/commands/ModCommands.java index 57e1887..b2836c4 100644 --- a/src/main/java/ftbsc/bscv/commands/ModCommands.java +++ b/src/main/java/ftbsc/bscv/commands/ModCommands.java @@ -22,7 +22,7 @@ public class ModCommands extends AbstractCommand { .then( Commands.literal("off") .executes(ctx -> { - for (IModule mod : Boscovicino.modManager.mods) { + for (IModule mod : Boscovicino.getInstance().modules.mods) { if (mod.isEnabled()) { mod.disable(); } @@ -33,7 +33,7 @@ public class ModCommands extends AbstractCommand { .then( Commands.literal("re-enable") // this is to fix some jankyness in event subscriptions .executes(ctx -> { - for (IModule mod : Boscovicino.modManager.mods) { + for (IModule mod : Boscovicino.getInstance().modules.mods) { if (mod.isEnabled()) { mod.disable(); mod.enable(); @@ -43,7 +43,7 @@ public class ModCommands extends AbstractCommand { }) ) .executes(ctx -> { - String mods = Boscovicino.modManager.mods.stream() + String mods = Boscovicino.getInstance().modules.mods.stream() .map(x -> x.getName()) .collect(Collectors.joining(",")); Boscovicino.log("[ %s ]", mods); diff --git a/src/main/java/ftbsc/bscv/modules/AbstractModule.java b/src/main/java/ftbsc/bscv/modules/AbstractModule.java index 7315ebc..aa992b4 100644 --- a/src/main/java/ftbsc/bscv/modules/AbstractModule.java +++ b/src/main/java/ftbsc/bscv/modules/AbstractModule.java @@ -27,12 +27,12 @@ public abstract class AbstractModule implements IModule, ICommons { @Override public ForgeConfigSpec.Builder getConfigBuilder() { - return Boscovicino.modManager.getCfgBuilder(); + return Boscovicino.getInstance().modules.getCfgBuilder(); } @Override public CommandDispatcher getDispatcher() { - return Boscovicino.modManager.getDispatcher(); + return Boscovicino.getInstance().modules.getDispatcher(); } public AbstractModule() { diff --git a/src/main/java/ftbsc/bscv/modules/defense/Aura.java b/src/main/java/ftbsc/bscv/modules/defense/Aura.java index e38be1e..79c16fc 100644 --- a/src/main/java/ftbsc/bscv/modules/defense/Aura.java +++ b/src/main/java/ftbsc/bscv/modules/defense/Aura.java @@ -105,7 +105,7 @@ public class Aura extends QuickModule { @Override public void enable() { super.enable(); - this.autotool = (AutoTool) Boscovicino.modManager.get(AutoTool.class); + this.autotool = (AutoTool) Boscovicino.getInstance().modules.get(AutoTool.class); } private void lookAtHidden(EntityAnchorArgument.Type anchor, Vector3d target) { @@ -139,7 +139,7 @@ public class Aura extends QuickModule { if (!this.neutral.get() && e.getClassification(false).isFriendly()) continue; if (e instanceof PlayerEntity) { PlayerEntity player = (PlayerEntity) e; - if (Boscovicino.friends().isFriend(player.getGameProfile().getId())) { + if (Boscovicino.getInstance().friends.isFriend(player.getGameProfile().getId())) { continue; } } diff --git a/src/main/java/ftbsc/bscv/modules/hud/ActiveModules.java b/src/main/java/ftbsc/bscv/modules/hud/ActiveModules.java index f2c9c3d..0886282 100644 --- a/src/main/java/ftbsc/bscv/modules/hud/ActiveModules.java +++ b/src/main/java/ftbsc/bscv/modules/hud/ActiveModules.java @@ -18,7 +18,7 @@ public class ActiveModules extends HudModule { if (event.getType() != ElementType.TEXT) return; if (this.shouldHide()) return; int offset = 0; - for (IModule m : Boscovicino.modManager.mods) { + for (IModule m : Boscovicino.getInstance().modules.mods) { if (m.isEnabled() && !m.getGroup().equalsIgnoreCase("HUD")) { TextBuilder() .txt(this.affixed(m.getName())) diff --git a/src/main/java/ftbsc/bscv/modules/motion/GuiMove.java b/src/main/java/ftbsc/bscv/modules/motion/GuiMove.java index 6f2da90..3732e37 100644 --- a/src/main/java/ftbsc/bscv/modules/motion/GuiMove.java +++ b/src/main/java/ftbsc/bscv/modules/motion/GuiMove.java @@ -44,7 +44,7 @@ public class GuiMove extends AbstractModule { @Override public void enable() { - this.autoWalk_mod = (AutoWalk) Boscovicino.modManager.get(AutoWalk.class); + this.autoWalk_mod = (AutoWalk) Boscovicino.getInstance().modules.get(AutoWalk.class); super.enable(); } diff --git a/src/main/java/ftbsc/bscv/system/Bindings.java b/src/main/java/ftbsc/bscv/system/Bindings.java index 7081d42..e5c5397 100644 --- a/src/main/java/ftbsc/bscv/system/Bindings.java +++ b/src/main/java/ftbsc/bscv/system/Bindings.java @@ -11,10 +11,7 @@ import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.client.registry.ClientRegistry; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; -import java.util.UUID; public class Bindings { public static final int UNBOUND = InputMappings.UNKNOWN.getValue(); @@ -43,7 +40,7 @@ public class Bindings { public KeyBinding registerBinding(String name, int key, String macroFileName) { KeyBinding kb = this.createBinding(name, key); this.executorMap.put(kb, () -> { - Boscovicino.macros.execute(macroFileName); + Boscovicino.getInstance().macros.execute(macroFileName); }); return kb; } diff --git a/src/main/java/ftbsc/bscv/system/Friends.java b/src/main/java/ftbsc/bscv/system/Friends.java index 322f604..9f7dd60 100644 --- a/src/main/java/ftbsc/bscv/system/Friends.java +++ b/src/main/java/ftbsc/bscv/system/Friends.java @@ -33,10 +33,10 @@ public class Friends implements ICommons { return null; } - private ForgeConfigSpec.ConfigValue> store; + public final ForgeConfigSpec.ConfigValue> store; public Friends(ForgeConfigSpec.Builder builder, CommandDispatcher dispatcher) { - this.store = builder.comment("actual friend list").defineList("data", () -> new ArrayList(), u -> { + this.store = builder.comment("actual friend list").defineList("data", ArrayList::new, u -> { try{ UUID.fromString(u.toString()); return true; diff --git a/src/main/java/ftbsc/bscv/system/Macros.java b/src/main/java/ftbsc/bscv/system/Macros.java index ca8d56f..302910e 100644 --- a/src/main/java/ftbsc/bscv/system/Macros.java +++ b/src/main/java/ftbsc/bscv/system/Macros.java @@ -15,9 +15,9 @@ public class Macros { private final Lua lua; private final HashMap macroCache; - public Macros() throws IOException { + public Macros(Boscovicino bscv) throws IOException { this.lua = new LuaJit(); - this.lua.pushJavaClass(Boscovicino.class); //TODO use instance + this.lua.pushJavaObject(bscv); //TODO use instance this.lua.setGlobal("BSCV"); this.macroCache = new HashMap<>(); @@ -34,7 +34,7 @@ public class Macros { if(name.endsWith(".lua")) { String code = String.join("\n", Files.readAllLines(macro)); this.macroCache.put(name, code); - Boscovicino.bindings.registerBinding(name, Bindings.UNBOUND, name); + Boscovicino.getInstance().bindings.registerBinding(name, Bindings.UNBOUND, name); } } } diff --git a/src/main/java/ftbsc/bscv/system/ModManager.java b/src/main/java/ftbsc/bscv/system/Modules.java similarity index 94% rename from src/main/java/ftbsc/bscv/system/ModManager.java rename to src/main/java/ftbsc/bscv/system/Modules.java index 9dea139..c2cb992 100644 --- a/src/main/java/ftbsc/bscv/system/ModManager.java +++ b/src/main/java/ftbsc/bscv/system/Modules.java @@ -13,7 +13,7 @@ import java.util.HashSet; import java.util.ServiceLoader; import java.util.Set; -public class ModManager { +public class Modules { private Builder cfgBuilder; private CommandDispatcher dispatcher; @@ -21,7 +21,7 @@ public class ModManager { public final Set mods; public final Set categories; - public ModManager(Builder cfgBuilder, CommandDispatcher dispatcher) { + public Modules(Builder cfgBuilder, CommandDispatcher dispatcher) { this.cfgBuilder = cfgBuilder; this.dispatcher = dispatcher; this.mods = new HashSet<>();