Skip to content

Commit

Permalink
Make all advancements mode save correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjabrain1 committed Sep 9, 2024
1 parent 8b729fa commit f26667e
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/main/java/ninjabrainbot/gui/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private void initInputHandlers() {
IEnderEyeThrowFactory enderEyeThrowFactory = new EnderEyeThrowFactory(preferences, dataState.boatDataState());
disposeHandler.add(new PlayerPositionInputHandler(coordinateInputSource, dataState, actionExecutor, preferences, enderEyeThrowFactory));
disposeHandler.add(new F3ILocationInputHandler(coordinateInputSource, dataState, actionExecutor, preferences));
disposeHandler.add(new ActiveInstanceInputHandler(activeInstanceProvider, domainModel, dataState, environmentState, actionExecutor, preferences));
disposeHandler.add(new ActiveInstanceInputHandler(activeInstanceProvider, domainModel, dataState, actionExecutor, preferences));
disposeHandler.add(new HotkeyInputHandler(preferences, domainModel, dataState, actionExecutor));
buttonInputHandler = new ButtonInputHandler(domainModel, dataState, actionExecutor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public IDomainModelComponent<Boolean> allAdvancementsModeEnabled() {
return new DataComponent<>("", domainModel, false);
}

@Override
public IDataComponent<Boolean> hasEnteredEnd() {
return new DataComponent<>("", domainModel);
}

@Override
public IDataComponent<IAllAdvancementsPosition> spawnPosition() {
return new DataComponent<>("", domainModel);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ninjabrainbot/io/ErrorHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

public class ErrorHandler {

public void handleStartupException(Exception exception){
public void handleStartupException(Exception exception) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
exception.printStackTrace(printWriter);
exception.printStackTrace();
String stackTrace = stringWriter.toString();
JTextArea textArea = new JTextArea(stackTrace);
JOptionPane.showMessageDialog(null, textArea, "An error occurred during startup", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ninjabrainbot.model.actions.alladvancements;

import ninjabrainbot.model.actions.IAction;
import ninjabrainbot.model.datastate.alladvancements.IAllAdvancementsDataState;

public class SetHasEnteredEndAction implements IAction {

private final IAllAdvancementsDataState allAdvancementsDataState;
private final boolean hasEnteredEnd;

public SetHasEnteredEndAction(IAllAdvancementsDataState allAdvancementsDataState, boolean hasEnteredEnd) {
this.allAdvancementsDataState = allAdvancementsDataState;
this.hasEnteredEnd = hasEnteredEnd;
}

@Override
public void execute() {
allAdvancementsDataState.hasEnteredEnd().set(hasEnteredEnd);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import ninjabrainbot.event.DisposeHandler;
import ninjabrainbot.event.IDisposable;
import ninjabrainbot.event.IObservable;
import ninjabrainbot.model.datastate.common.IOverworldPosition;
import ninjabrainbot.event.Subscription;
import ninjabrainbot.model.datastate.common.IPlayerPosition;
import ninjabrainbot.model.datastate.common.StructureInformation;
import ninjabrainbot.model.datastate.stronghold.ChunkPrediction;
Expand All @@ -19,6 +19,7 @@ public class AllAdvancementsDataState implements IAllAdvancementsDataState, IDis
private final IObservable<IPlayerPosition> playerPosition;
private final IEnvironmentState environmentState;

private final DataComponent<Boolean> hasEnteredEnd;
private final DataComponent<IAllAdvancementsPosition> spawnPosition;
private final DataComponent<IAllAdvancementsPosition> outpostPosition;
private final DataComponent<IAllAdvancementsPosition> monumentPosition;
Expand All @@ -43,6 +44,7 @@ public AllAdvancementsDataState(IDomainModelComponent<ChunkPrediction> currentSt
this.playerPosition = playerPosition;
this.environmentState = environmentState;

hasEnteredEnd = new DataComponent<>("aa_toggle", domainModel, false);
spawnPosition = new DataComponent<>("aa_spawn", domainModel);
outpostPosition = new DataComponent<>("aa_outpost", domainModel);
monumentPosition = new DataComponent<>("aa_monument", domainModel);
Expand All @@ -62,32 +64,41 @@ public AllAdvancementsDataState(IDomainModelComponent<ChunkPrediction> currentSt
generalLocationInformation = new InferredComponent<>(domainModel);

disposeHandler.add(environmentState.allAdvancementsModeEnabled().subscribeInternal(this::updateAllAdvancementsMode));
disposeHandler.add(environmentState.hasEnteredEnd().subscribeInternal(this::updateAllAdvancementsMode));
disposeHandler.add(hasEnteredEnd.subscribeInternal(this::updateAllAdvancementsMode));
disposeHandler.add(currentStrongholdPrediction.subscribeInternal(strongholdInformation::set));
disposeHandler.add(spawnPosition.subscribeInternal(position -> updateStructureInformationComponent(spawnInformation, position)));
disposeHandler.add(outpostPosition.subscribeInternal(position -> updateStructureInformationComponent(outpostInformation, position)));
disposeHandler.add(monumentPosition.subscribeInternal(position -> updateStructureInformationComponent(monumentInformation, position)));
disposeHandler.add(deepDarkPosition.subscribeInternal(position -> updateStructureInformationComponent(deepDarkInformation, position)));
disposeHandler.add(cityQueryPosition.subscribeInternal(position -> updateStructureInformationComponent(cityQueryInformation, position)));
disposeHandler.add(shulkerTransportPosition.subscribeInternal(position -> updateStructureInformationComponent(shulkerTransportInformation, position)));
disposeHandler.add(generalLocationPosition.subscribeInternal(position -> updateStructureInformationComponent(generalLocationInformation, position)));

disposeHandler.add(createStructureInformationSubscription(spawnPosition, spawnInformation));
disposeHandler.add(createStructureInformationSubscription(outpostPosition, outpostInformation));
disposeHandler.add(createStructureInformationSubscription(monumentPosition, monumentInformation));
disposeHandler.add(createStructureInformationSubscription(deepDarkPosition, deepDarkInformation));
disposeHandler.add(createStructureInformationSubscription(cityQueryPosition, cityQueryInformation));
disposeHandler.add(createStructureInformationSubscription(shulkerTransportPosition, shulkerTransportInformation));
disposeHandler.add(createStructureInformationSubscription(generalLocationPosition, generalLocationInformation));
}

private void updateAllAdvancementsMode() {
allAdvancementsModeEnabled.set(environmentState.allAdvancementsModeEnabled().get() && environmentState.hasEnteredEnd().get());
private Subscription createStructureInformationSubscription(IDataComponent<IAllAdvancementsPosition> allAdvancementsPosition, InferredComponent<StructureInformation> structureInformation) {
return allAdvancementsPosition.subscribeInternal(overworldPosition ->
structureInformation.set(overworldPosition == null
? null
: new StructureInformation(overworldPosition, playerPosition)
)
);
}

private void updateStructureInformationComponent(InferredComponent<StructureInformation> structureInformationComponent, IOverworldPosition overworldPosition) {
structureInformationComponent.set(overworldPosition == null
? null
: new StructureInformation(overworldPosition, playerPosition));
private void updateAllAdvancementsMode() {
allAdvancementsModeEnabled.set(environmentState.allAdvancementsModeEnabled().get() && hasEnteredEnd.get());
}

@Override
public IDomainModelComponent<Boolean> allAdvancementsModeEnabled() {
return allAdvancementsModeEnabled;
}

@Override
public IDataComponent<Boolean> hasEnteredEnd() {
return hasEnteredEnd;
}

@Override
public IDataComponent<IAllAdvancementsPosition> spawnPosition() {
return spawnPosition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public interface IAllAdvancementsDataState {

IDomainModelComponent<Boolean> allAdvancementsModeEnabled();

IDataComponent<Boolean> hasEnteredEnd();

IDataComponent<IAllAdvancementsPosition> spawnPosition();

IDataComponent<IAllAdvancementsPosition> outpostPosition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public void triggerDeserialization() {
}
if (!domainModel.isReset())
throw new RuntimeException("Domain model is not in the default state after failed deserialization, crashing the application to avoid an inconsistent data state.");
} catch (Exception e) {
Logger.log("Unhandled exception during domain model deserialization: " + e);
try {
objectInputStream.close();
fileAccessor.deleteFile();
} catch (IOException ex) {
Logger.log("Failed to close ObjectInputStream after failed deserialization: " + ex);
fileAccessor.deleteFile();
}
throw e;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@
*/
public class EnvironmentState implements IEnvironmentState, IDisposable {

private final IDomainModel domainModel;
private final NinjabrainBotPreferences preferences;

private final IEnvironmentComponent<ICalculator> calculator;
private final IEnvironmentComponent<CalculatorSettings> calculatorSettings;
private final IEnvironmentComponent<StandardDeviationSettings> standardDeviationSettings;
private final IEnvironmentComponent<Boolean> allAdvancementsModeEnabled;
private final EnvironmentComponent<Boolean> hasEnteredEnd;

private final DisposeHandler disposeHandler = new DisposeHandler();

public EnvironmentState(IDomainModel domainModel, NinjabrainBotPreferences preferences) {
this.domainModel = domainModel;
this.preferences = preferences;

IObservable<CalculatorSettings> calculatorSettings = disposeHandler.add(Observable
Expand All @@ -52,8 +49,6 @@ public EnvironmentState(IDomainModel domainModel, NinjabrainBotPreferences prefe
.inferFrom(preferences.allAdvancements::get)
.dependsOn(preferences.allAdvancements));
this.allAdvancementsModeEnabled = EnvironmentComponent.of(domainModel, allAdvancementsModeEnabled, disposeHandler);

hasEnteredEnd = new EnvironmentComponent<>(domainModel, false);
}

@Override
Expand All @@ -76,21 +71,6 @@ public IEnvironmentComponent<Boolean> allAdvancementsModeEnabled() {
return allAdvancementsModeEnabled;
}

@Override
public IEnvironmentComponent<Boolean> hasEnteredEnd() {
return hasEnteredEnd;
}

@Override
public void setHasEnteredEnd(boolean hasEnteredEnd) {
domainModel.acquireWriteLock();
try {
this.hasEnteredEnd.set(hasEnteredEnd);
} finally {
domainModel.releaseWriteLock();
}
}

private ICalculator createCalculator() {
return new Calculator(calculatorSettings.get(), standardDeviationSettings.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,4 @@ public interface IEnvironmentState {

IEnvironmentComponent<Boolean> allAdvancementsModeEnabled();

IEnvironmentComponent<Boolean> hasEnteredEnd();

void setHasEnteredEnd(boolean hasEnteredEnd);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import ninjabrainbot.io.preferences.NinjabrainBotPreferences;
import ninjabrainbot.io.preferences.enums.AllAdvancementsToggleType;
import ninjabrainbot.model.actions.IActionExecutor;
import ninjabrainbot.model.actions.alladvancements.SetHasEnteredEndAction;
import ninjabrainbot.model.actions.common.ResetAction;
import ninjabrainbot.model.datastate.IDataState;
import ninjabrainbot.model.domainmodel.IDomainModel;
Expand All @@ -21,28 +22,22 @@ public class ActiveInstanceInputHandler implements IDisposable {
private final NinjabrainBotPreferences preferences;
private final IDomainModel domainModel;
private final IDataState dataState;
private final IEnvironmentState environmentState;
private final IActionExecutor actionExecutor;

private boolean userHasToggledAllAdvancementModeThroughHotkey = false;

private final DisposeHandler disposeHandler = new DisposeHandler();

private IMinecraftWorldFile lastActiveMinecraftWorldFile;

public ActiveInstanceInputHandler(IActiveInstanceProvider activeInstanceProvider, IDomainModel domainModel, IDataState dataState, IEnvironmentState environmentState, IActionExecutor actionExecutor, NinjabrainBotPreferences preferences) {
public ActiveInstanceInputHandler(IActiveInstanceProvider activeInstanceProvider, IDomainModel domainModel, IDataState dataState, IActionExecutor actionExecutor, NinjabrainBotPreferences preferences) {
this.activeInstanceProvider = activeInstanceProvider;
this.preferences = preferences;
this.domainModel = domainModel;
this.dataState = dataState;
this.environmentState = environmentState;
this.actionExecutor = actionExecutor;
lastActiveMinecraftWorldFile = activeInstanceProvider.activeMinecraftWorld().get();

disposeHandler.add(activeInstanceProvider.activeMinecraftWorld().subscribe(this::onActiveMinecraftWorldChanged));
disposeHandler.add(activeInstanceProvider.whenActiveMinecraftWorldModified().subscribe(this::updateHasEnteredEndState));
disposeHandler.add(preferences.allAdvancementsToggleType.whenModified().subscribe(this::updateHasEnteredEndState));
disposeHandler.add(preferences.hotkeyToggleAllAdvancementsMode.whenTriggered().subscribe(this::onToggleAllAdvancementsModeHotkeyTriggered));
}

private void onActiveMinecraftWorldChanged(IMinecraftWorldFile newWorldFile) {
Expand All @@ -51,20 +46,12 @@ private void onActiveMinecraftWorldChanged(IMinecraftWorldFile newWorldFile) {
lastActiveMinecraftWorldFile = newWorldFile;
}

private void onToggleAllAdvancementsModeHotkeyTriggered() {
if (preferences.allAdvancementsToggleType.get() == AllAdvancementsToggleType.Hotkey) {
userHasToggledAllAdvancementModeThroughHotkey = !userHasToggledAllAdvancementModeThroughHotkey;
updateHasEnteredEndState();
}
}

private void updateHasEnteredEndState() {
if (preferences.allAdvancementsToggleType.get() == AllAdvancementsToggleType.Hotkey) {
environmentState.setHasEnteredEnd(userHasToggledAllAdvancementModeThroughHotkey);
} else if (preferences.allAdvancementsToggleType.get() == AllAdvancementsToggleType.Automatic) {
userHasToggledAllAdvancementModeThroughHotkey = false;
if (preferences.allAdvancementsToggleType.get() == AllAdvancementsToggleType.Automatic) {
IMinecraftWorldFile worldFile = activeInstanceProvider.activeMinecraftWorld().get();
environmentState.setHasEnteredEnd(worldFile != null && worldFile.hasEnteredEnd());
if (worldFile != null && worldFile.hasEnteredEnd())
// Do not set to false if hasEnteredEnd() == false, it should not be possible for automatic detection to go from hasEnteredEnd = true to hasEnteredEnd = false.
actionExecutor.executeImmediately(new SetHasEnteredEndAction(dataState.allAdvancementsDataState(), true));
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/ninjabrainbot/model/input/HotkeyInputHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import ninjabrainbot.event.DisposeHandler;
import ninjabrainbot.event.IDisposable;
import ninjabrainbot.io.preferences.NinjabrainBotPreferences;
import ninjabrainbot.io.preferences.enums.AllAdvancementsToggleType;
import ninjabrainbot.model.actions.IActionExecutor;
import ninjabrainbot.model.actions.alladvancements.SetHasEnteredEndAction;
import ninjabrainbot.model.actions.boat.ResetBoatStateAction;
import ninjabrainbot.model.actions.boat.ToggleEnteringBoatAction;
import ninjabrainbot.model.actions.boat.ToggleMod360IndicatorAction;
Expand All @@ -12,6 +14,7 @@
import ninjabrainbot.model.actions.endereye.ChangeLastAngleAction;
import ninjabrainbot.model.actions.endereye.ToggleAltStdOnLastThrowAction;
import ninjabrainbot.model.datastate.IDataState;
import ninjabrainbot.model.datastate.alladvancements.IAllAdvancementsDataState;
import ninjabrainbot.model.datastate.highprecision.BoatState;
import ninjabrainbot.model.domainmodel.IDomainModel;

Expand Down Expand Up @@ -40,6 +43,7 @@ public HotkeyInputHandler(NinjabrainBotPreferences preferences, IDomainModel dom
disposeHandler.add(preferences.hotkeyMod360.whenTriggered().subscribe(this::toggleMod360IndicatorIfNotLocked));
disposeHandler.add(preferences.hotkeyLock.whenTriggered().subscribe(__ -> actionExecutor.executeImmediately(new ToggleLockedAction(dataState))));
disposeHandler.add(preferences.usePreciseAngle.whenModified().subscribe(this::resetBoatState));
disposeHandler.add(preferences.hotkeyToggleAllAdvancementsMode.whenTriggered().subscribe(this::onToggleAllAdvancementsModeHotkeyTriggered));
}

private void resetIfNotLocked() {
Expand Down Expand Up @@ -87,6 +91,13 @@ private void resetBoatState() {
actionExecutor.executeImmediately(new ResetBoatStateAction(dataState));
}

private void onToggleAllAdvancementsModeHotkeyTriggered() {
if (preferences.allAdvancementsToggleType.get() == AllAdvancementsToggleType.Hotkey) {
IAllAdvancementsDataState allAdvancementsDataState = dataState.allAdvancementsDataState();
actionExecutor.executeImmediately(new SetHasEnteredEndAction(allAdvancementsDataState, !allAdvancementsDataState.hasEnteredEnd().get()));
}
}

@Override
public void dispose() {
disposeHandler.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void uiUpdatesAsExpected_TypicalAASpeedrun(String f3c1, String f3c2, String f3c3

testBuilder.resetCalculator();
TestUtils.awaitSwingEvents();
Assertions.assertTrue(mainTextArea.isAllAdvancementsPanelVisible());
Assertions.assertFalse(mainTextArea.isAllAdvancementsPanelVisible());
Assertions.assertTrue(mainTextArea.getAllAdvancementsStructurePanelCoordinates(StructureType.Stronghold).isEmpty());
Assertions.assertTrue(mainTextArea.getAllAdvancementsStructurePanelCoordinates(StructureType.Spawn).isEmpty());
Assertions.assertTrue(mainTextArea.getAllAdvancementsStructurePanelCoordinates(StructureType.Outpost).isEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void triggerHotkey(HotkeyPreference hotkeyPreference) {

public void setActiveMinecraftWorld(IMinecraftWorldFile minecraftWorld) {
if (activeInstanceProvider == null) activeInstanceProvider = new MockedInstanceProvider();
if (activeInstanceInputHandler == null) activeInstanceInputHandler = new ActiveInstanceInputHandler(activeInstanceProvider, domainModel, dataState, environmentState, actionExecutor, preferences);
if (activeInstanceInputHandler == null) activeInstanceInputHandler = new ActiveInstanceInputHandler(activeInstanceProvider, domainModel, dataState, actionExecutor, preferences);
activeInstanceProvider.activeMinecraftWorld().set(minecraftWorld);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void changingMinecraftWorldExecutesResetAction(boolean preferenceEnabled) {
MinecraftInstance minecraftInstance = new MinecraftInstance("instance 1");
activeInstanceProvider.currentWorldFile.set(new MinecraftWorldFile(minecraftInstance, "world 1"));

ActiveInstanceInputHandler sut = new ActiveInstanceInputHandler(activeInstanceProvider, domainModel, dataState, environmentState, actionExecutor, preferences);
ActiveInstanceInputHandler sut = new ActiveInstanceInputHandler(activeInstanceProvider, domainModel, dataState, actionExecutor, preferences);

// Act
activeInstanceProvider.currentWorldFile.set(new MinecraftWorldFile(minecraftInstance, "world 2"));
Expand Down Expand Up @@ -74,7 +74,7 @@ void changingMinecraftWorld_FromNull_DoesNotExecuteResetAction(boolean preferenc
MinecraftInstance minecraftInstance = new MinecraftInstance("instance 1");
activeInstanceProvider.currentWorldFile.set(null);

ActiveInstanceInputHandler sut = new ActiveInstanceInputHandler(activeInstanceProvider, domainModel, dataState, environmentState, actionExecutor, preferences);
ActiveInstanceInputHandler sut = new ActiveInstanceInputHandler(activeInstanceProvider, domainModel, dataState, actionExecutor, preferences);

// Act
activeInstanceProvider.currentWorldFile.set(new MinecraftWorldFile(minecraftInstance, "world 1"));
Expand Down

0 comments on commit f26667e

Please sign in to comment.