diff options
author | alemi <me@alemi.dev> | 2023-03-08 17:22:00 +0100 |
---|---|---|
committer | alemi <me@alemi.dev> | 2023-03-08 17:22:00 +0100 |
commit | 7a6102dfbfbf8254b5beb14c4d1dd049a18ef1d4 (patch) | |
tree | ab1c957848a1b8217a32414d253d10cb73db6612 /src/main | |
parent | 1a189e9c8aeb5a6ee72b1385d1ac96220941fba1 (diff) |
fix: settings getting corrupted on load
bear with me: issue likely was that all mods were enabled and saved
concurrently while being loaded, messing up with config. Ok, must run
callbacks and register mod but not touch config file. Solved by removing
onEnabled/onDisabled callbacks, making mods directly override
enable/disable. Made the implementations in AbstractModule check
this.enabled before acting on config.
Diffstat (limited to 'src/main')
8 files changed, 44 insertions, 32 deletions
diff --git a/src/main/java/ftbsc/bscv/Boscovicino.java b/src/main/java/ftbsc/bscv/Boscovicino.java index a3a3f29..d1f4531 100644 --- a/src/main/java/ftbsc/bscv/Boscovicino.java +++ b/src/main/java/ftbsc/bscv/Boscovicino.java @@ -77,7 +77,9 @@ public class Boscovicino implements ICommons { LOGGER.info("Initializing modules"); for (IModule m : modManager.mods) { - if (m.isEnabled()) m.enable(); + if (m.isEnabled()) { + m.enable(); // re-run enable() to register on the event bus and run enabled callbacks + } } } diff --git a/src/main/java/ftbsc/bscv/api/IModule.java b/src/main/java/ftbsc/bscv/api/IModule.java index 2b0ea12..0219de3 100644 --- a/src/main/java/ftbsc/bscv/api/IModule.java +++ b/src/main/java/ftbsc/bscv/api/IModule.java @@ -8,10 +8,14 @@ public interface IModule extends ICommand { ForgeConfigSpec.Builder getConfigBuilder(); + boolean isEnabled(); + default void toggle() { - if (this.isEnabled()) + if (this.isEnabled()) { this.disable(); - else this.enable(); + } else { + this.enable(); + } } default void enable() { @@ -19,8 +23,6 @@ public interface IModule extends ICommand { } default void disable() { - MinecraftForge.EVENT_BUS.register(this); + MinecraftForge.EVENT_BUS.unregister(this); } - - boolean isEnabled(); } diff --git a/src/main/java/ftbsc/bscv/modules/AbstractModule.java b/src/main/java/ftbsc/bscv/modules/AbstractModule.java index 384d593..3dbd82e 100644 --- a/src/main/java/ftbsc/bscv/modules/AbstractModule.java +++ b/src/main/java/ftbsc/bscv/modules/AbstractModule.java @@ -7,7 +7,6 @@ import ftbsc.bscv.api.IModule; import net.minecraft.command.CommandSource; import net.minecraft.command.Commands; import net.minecraftforge.common.ForgeConfigSpec; -import net.minecraftforge.common.MinecraftForge; import static ftbsc.bscv.Boscovicino.log; @@ -70,23 +69,22 @@ public abstract class AbstractModule implements IModule { ); } - protected void onEnabled() {} - protected void onDisabled() {} - - public final void enable() { - MinecraftForge.EVENT_BUS.register(this); - this.enabled.set(true); - this.enabled.save(); - this.onEnabled(); - log(String.format("%s ON", this.getName())); + public void enable() { + IModule.super.enable(); + if (!this.enabled.get()) { + this.enabled.set(true); + this.enabled.save(); + log(String.format("%s ON", this.getName())); + } } - public final void disable() { - MinecraftForge.EVENT_BUS.unregister(this); - this.enabled.set(false); - this.enabled.save(); - this.onDisabled(); - log(String.format("%s OFF", this.getName())); + public void disable() { + IModule.super.disable(); + if (this.enabled.get()) { + this.enabled.set(false); + this.enabled.save(); + log(String.format("%s OFF", this.getName())); + } } @Override diff --git a/src/main/java/ftbsc/bscv/modules/defense/Aura.java b/src/main/java/ftbsc/bscv/modules/defense/Aura.java index 8c02656..290ab81 100644 --- a/src/main/java/ftbsc/bscv/modules/defense/Aura.java +++ b/src/main/java/ftbsc/bscv/modules/defense/Aura.java @@ -104,7 +104,8 @@ public class Aura extends QuickModule implements ICommons { } @Override - protected void onEnabled() { + public void enable() { + super.enable(); this.autotool = (AutoTool) Boscovicino.modManager.get(AutoTool.class); } diff --git a/src/main/java/ftbsc/bscv/modules/hud/Highlighter.java b/src/main/java/ftbsc/bscv/modules/hud/Highlighter.java index 25d5ac7..a5fa0e5 100644 --- a/src/main/java/ftbsc/bscv/modules/hud/Highlighter.java +++ b/src/main/java/ftbsc/bscv/modules/hud/Highlighter.java @@ -43,7 +43,8 @@ public class Highlighter extends AbstractModule { } @Override - protected void onEnabled() { + public void enable() { + super.enable(); this.pattern = Pattern.compile(this.query.get()); } diff --git a/src/main/java/ftbsc/bscv/modules/motion/VanillaFlight.java b/src/main/java/ftbsc/bscv/modules/motion/VanillaFlight.java index 9853595..2b238a4 100644 --- a/src/main/java/ftbsc/bscv/modules/motion/VanillaFlight.java +++ b/src/main/java/ftbsc/bscv/modules/motion/VanillaFlight.java @@ -97,17 +97,18 @@ public class VanillaFlight extends QuickModule implements ICommons { } @Override - protected void onEnabled() { + public void enable() { + super.enable(); this.tick = 0; if (MC.player != null) { this.couldFlyBefore = MC.player.abilities.mayfly; this.flyingSpeedBefore = MC.player.abilities.getFlyingSpeed(); - Boscovicino.log(String.format("Flying speed before = %f", this.flyingSpeedBefore)); } } @Override - protected void onDisabled() { + public void disable() { + super.disable(); ClientPlayerEntity player = MC.player; if (player != null) { player.abilities.mayfly = this.couldFlyBefore; diff --git a/src/main/java/ftbsc/bscv/modules/self/Freecam.java b/src/main/java/ftbsc/bscv/modules/self/Freecam.java index cd0c107..33c27ae 100644 --- a/src/main/java/ftbsc/bscv/modules/self/Freecam.java +++ b/src/main/java/ftbsc/bscv/modules/self/Freecam.java @@ -77,7 +77,8 @@ public class Freecam extends QuickModule implements ICommons { } @Override - protected void onEnabled() { + public void enable() { + super.enable(); if (MC.player == null) { Boscovicino.log("[!] Can only enable freecam while in-game"); this.disable(); @@ -102,7 +103,8 @@ public class Freecam extends QuickModule implements ICommons { } @Override - protected void onDisabled() { + public void disable() { + super.disable(); if (MC.player == null) return; MC.gameMode.setLocalMode(this.prev_gamemode); MC.player.noCulling = false; diff --git a/src/main/java/ftbsc/bscv/modules/vision/Fullbright.java b/src/main/java/ftbsc/bscv/modules/vision/Fullbright.java index f84a9e4..9569d44 100644 --- a/src/main/java/ftbsc/bscv/modules/vision/Fullbright.java +++ b/src/main/java/ftbsc/bscv/modules/vision/Fullbright.java @@ -13,6 +13,10 @@ import java.awt.event.KeyEvent; @AutoService(ILoadable.class) public class Fullbright extends QuickModule implements ICommons { + + private final static int NIGHT_VISION_ID = 16; + private final static int FOUR_MINUTES_TWENTY_SECONDS = 5204; + @Override protected int getDefaultKey() { return KeyEvent.VK_V; @@ -22,11 +26,12 @@ public class Fullbright extends QuickModule implements ICommons { public void onTick(TickEvent.ClientTickEvent event) { if (MC == null) return; if (MC.player == null) return; - MC.player.addEffect(new EffectInstance(Effect.byId(16), 5204)); + MC.player.addEffect(new EffectInstance(Effect.byId(NIGHT_VISION_ID), FOUR_MINUTES_TWENTY_SECONDS)); } @Override - protected void onDisabled() { - MC.player.removeEffect(Effect.byId(16)); + public void disable() { + super.disable(); + MC.player.removeEffect(Effect.byId(NIGHT_VISION_ID)); } } |