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.
This commit is contained in:
əlemi 2023-03-08 17:22:00 +01:00
parent 1a189e9c8a
commit 7a6102dfbf
Signed by: alemi
GPG key ID: A4895B84D311642C
8 changed files with 44 additions and 32 deletions

View file

@ -77,7 +77,9 @@ public class Boscovicino implements ICommons {
LOGGER.info("Initializing modules"); LOGGER.info("Initializing modules");
for (IModule m : modManager.mods) { 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
}
} }
} }

View file

@ -8,10 +8,14 @@ public interface IModule extends ICommand {
ForgeConfigSpec.Builder getConfigBuilder(); ForgeConfigSpec.Builder getConfigBuilder();
boolean isEnabled();
default void toggle() { default void toggle() {
if (this.isEnabled()) if (this.isEnabled()) {
this.disable(); this.disable();
else this.enable(); } else {
this.enable();
}
} }
default void enable() { default void enable() {
@ -19,8 +23,6 @@ public interface IModule extends ICommand {
} }
default void disable() { default void disable() {
MinecraftForge.EVENT_BUS.register(this); MinecraftForge.EVENT_BUS.unregister(this);
} }
boolean isEnabled();
} }

View file

@ -7,7 +7,6 @@ import ftbsc.bscv.api.IModule;
import net.minecraft.command.CommandSource; import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands; import net.minecraft.command.Commands;
import net.minecraftforge.common.ForgeConfigSpec; import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.MinecraftForge;
import static ftbsc.bscv.Boscovicino.log; import static ftbsc.bscv.Boscovicino.log;
@ -70,24 +69,23 @@ public abstract class AbstractModule implements IModule {
); );
} }
protected void onEnabled() {} public void enable() {
protected void onDisabled() {} IModule.super.enable();
if (!this.enabled.get()) {
public final void enable() {
MinecraftForge.EVENT_BUS.register(this);
this.enabled.set(true); this.enabled.set(true);
this.enabled.save(); this.enabled.save();
this.onEnabled();
log(String.format("%s ON", this.getName())); log(String.format("%s ON", this.getName()));
} }
}
public final void disable() { public void disable() {
MinecraftForge.EVENT_BUS.unregister(this); IModule.super.disable();
if (this.enabled.get()) {
this.enabled.set(false); this.enabled.set(false);
this.enabled.save(); this.enabled.save();
this.onDisabled();
log(String.format("%s OFF", this.getName())); log(String.format("%s OFF", this.getName()));
} }
}
@Override @Override
public boolean isEnabled() { public boolean isEnabled() {

View file

@ -104,7 +104,8 @@ public class Aura extends QuickModule implements ICommons {
} }
@Override @Override
protected void onEnabled() { public void enable() {
super.enable();
this.autotool = (AutoTool) Boscovicino.modManager.get(AutoTool.class); this.autotool = (AutoTool) Boscovicino.modManager.get(AutoTool.class);
} }

View file

@ -43,7 +43,8 @@ public class Highlighter extends AbstractModule {
} }
@Override @Override
protected void onEnabled() { public void enable() {
super.enable();
this.pattern = Pattern.compile(this.query.get()); this.pattern = Pattern.compile(this.query.get());
} }

View file

@ -97,17 +97,18 @@ public class VanillaFlight extends QuickModule implements ICommons {
} }
@Override @Override
protected void onEnabled() { public void enable() {
super.enable();
this.tick = 0; this.tick = 0;
if (MC.player != null) { if (MC.player != null) {
this.couldFlyBefore = MC.player.abilities.mayfly; this.couldFlyBefore = MC.player.abilities.mayfly;
this.flyingSpeedBefore = MC.player.abilities.getFlyingSpeed(); this.flyingSpeedBefore = MC.player.abilities.getFlyingSpeed();
Boscovicino.log(String.format("Flying speed before = %f", this.flyingSpeedBefore));
} }
} }
@Override @Override
protected void onDisabled() { public void disable() {
super.disable();
ClientPlayerEntity player = MC.player; ClientPlayerEntity player = MC.player;
if (player != null) { if (player != null) {
player.abilities.mayfly = this.couldFlyBefore; player.abilities.mayfly = this.couldFlyBefore;

View file

@ -77,7 +77,8 @@ public class Freecam extends QuickModule implements ICommons {
} }
@Override @Override
protected void onEnabled() { public void enable() {
super.enable();
if (MC.player == null) { if (MC.player == null) {
Boscovicino.log("[!] Can only enable freecam while in-game"); Boscovicino.log("[!] Can only enable freecam while in-game");
this.disable(); this.disable();
@ -102,7 +103,8 @@ public class Freecam extends QuickModule implements ICommons {
} }
@Override @Override
protected void onDisabled() { public void disable() {
super.disable();
if (MC.player == null) return; if (MC.player == null) return;
MC.gameMode.setLocalMode(this.prev_gamemode); MC.gameMode.setLocalMode(this.prev_gamemode);
MC.player.noCulling = false; MC.player.noCulling = false;

View file

@ -13,6 +13,10 @@ import java.awt.event.KeyEvent;
@AutoService(ILoadable.class) @AutoService(ILoadable.class)
public class Fullbright extends QuickModule implements ICommons { 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 @Override
protected int getDefaultKey() { protected int getDefaultKey() {
return KeyEvent.VK_V; return KeyEvent.VK_V;
@ -22,11 +26,12 @@ public class Fullbright extends QuickModule implements ICommons {
public void onTick(TickEvent.ClientTickEvent event) { public void onTick(TickEvent.ClientTickEvent event) {
if (MC == null) return; if (MC == null) return;
if (MC.player == 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 @Override
protected void onDisabled() { public void disable() {
MC.player.removeEffect(Effect.byId(16)); super.disable();
MC.player.removeEffect(Effect.byId(NIGHT_VISION_ID));
} }
} }