Skip to content

Commit

Permalink
Fix crashes + some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjabrain1 committed Sep 6, 2024
1 parent ab88a6b commit 9f1587d
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 22 deletions.
2 changes: 0 additions & 2 deletions src/main/java/ninjabrainbot/gui/frames/ThemeEditorDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,12 @@ public class ThemeEditorDialog extends ThemedDialog {
private FramePreviewPanel ninBotPreviewDetailed;

private final StyleManager previewStyleManager;
private final NinjabrainBotPreferences preferences;
private final CustomTheme customTheme; // theme that is saved
private final CustomTheme previewTheme; // used for previewing

public ThemeEditorDialog(StyleManager styleManager, NinjabrainBotPreferences preferences, JFrame owner, CustomTheme customTheme) {
super(styleManager, preferences, owner, I18n.get("settings.themeeditor.themeeditor"));
Assert.isTrue(SwingUtilities.isEventDispatchThread());
this.preferences = preferences;
this.customTheme = customTheme;
previewTheme = new CustomTheme();
previewTheme.setFromTheme(customTheme, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import ninjabrainbot.model.datastate.alladvancements.IAllAdvancementsDataState;
import ninjabrainbot.model.datastate.blind.BlindResult;
import ninjabrainbot.model.datastate.calculator.ICalculatorResult;
import ninjabrainbot.model.datastate.common.IPlayerPosition;
import ninjabrainbot.model.datastate.common.DetachedDomainModel;
import ninjabrainbot.model.datastate.common.IPlayerPosition;
import ninjabrainbot.model.datastate.common.ResultType;
import ninjabrainbot.model.datastate.divine.DivineContext;
import ninjabrainbot.model.datastate.divine.DivineResult;
Expand Down Expand Up @@ -63,7 +63,7 @@ public PreviewDataState() {
blindResult = new InferredComponent<>(domainModel);
divineResult = new InferredComponent<>(domainModel);

boatDataState = new BoatDataState(null);
boatDataState = new BoatDataState(domainModel);
allAdvancementsDataState = new PreviewAllAdvancementsDataState();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class Calibrator implements IDisposable {
private double lastX;
private double lastZ;

private final IDivineContext divineContext = new DivineContext(null);
private final IDivineContext divineContext = new DivineContext(new DetachedDomainModel());

private final DisposeHandler disposeHandler = new DisposeHandler();
private final ObservableProperty<Calibrator> whenModified = new ObservableProperty<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ public boolean isReset() {
}

@Override
public boolean isFullyInitialized() {
return false;
public boolean isExternalSubscriptionRegistrationAllowed() {
return true;
}

@Override
public boolean isInternalSubscriptionRegistrationAllowed() {
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,13 @@ public boolean isReset() {

@Override
public Subscription subscribeInternal(Consumer<T> subscriber) {
if (domainModel != null)
Assert.isFalse(domainModel.isFullyInitialized(), "Attempted to subscribe to internal events after domain model initialization has completed. External subscribers should use IDataComponent.subscribe().");
Assert.isTrue(domainModel.isInternalSubscriptionRegistrationAllowed(), "Attempted to subscribe to internal events after domain model initialization has completed. External subscribers should use IDataComponent.subscribe().");
return observableField.subscribe(subscriber);
}

@Override
public Subscription subscribe(Consumer<T> subscriber) {
if (domainModel != null)
Assert.isTrue(domainModel.isFullyInitialized(), "Attempted to subscribe to external events before domain model initialization has completed. Internal subscribers should use IDataComponent.subscribeInternal().");
Assert.isTrue(domainModel.isExternalSubscriptionRegistrationAllowed(), "Attempted to subscribe to external events before domain model initialization has completed. Internal subscribers should use IDataComponent.subscribeInternal().");
return externalEvent.subscribe(subscriber);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,15 @@ public boolean isReset() {
}

@Override
public boolean isFullyInitialized() {
public boolean isExternalSubscriptionRegistrationAllowed() {
return isFullyInitialized;
}

@Override
public boolean isInternalSubscriptionRegistrationAllowed() {
return !isFullyInitialized;
}

@Override
public ISubscribable<IDomainModel> whenModified() {
return whenModified;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ public T get() {
@Override
public Subscription subscribeInternal(Consumer<T> subscriber) {
if (domainModel != null)
Assert.isFalse(domainModel.isFullyInitialized(), "Attempted to subscribe to internal events after domain model initialization has completed. External subscribers should use IDataComponent.subscribe().");
Assert.isTrue(domainModel.isInternalSubscriptionRegistrationAllowed(), "Attempted to subscribe to internal events after domain model initialization has completed. External subscribers should use IDataComponent.subscribe().");
return observableField.subscribe(subscriber);
}

@Override
public Subscription subscribe(Consumer<T> subscriber) {
if (domainModel != null)
Assert.isTrue(domainModel.isFullyInitialized(), "Attempted to subscribe to external events before domain model initialization has completed. Internal subscribers should use IDataComponent.subscribeInternal().");
Assert.isTrue(domainModel.isExternalSubscriptionRegistrationAllowed(), "Attempted to subscribe to external events before domain model initialization has completed. Internal subscribers should use IDataComponent.subscribeInternal().");
return externalEvent.subscribe(subscriber);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public interface IDomainModel extends IWriteLock {

boolean isReset();

boolean isFullyInitialized();
boolean isExternalSubscriptionRegistrationAllowed();

boolean isInternalSubscriptionRegistrationAllowed();

ISubscribable<IDomainModel> whenModified();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public T get() {
@Override
public Subscription subscribeInternal(Consumer<T> subscriber) {
if (domainModel != null)
Assert.isFalse(domainModel.isFullyInitialized(), "Attempted to subscribe to internal events after domain model initialization has completed. External subscribers should use IInferredComponent.subscribe().");
Assert.isTrue(domainModel.isInternalSubscriptionRegistrationAllowed(), "Attempted to subscribe to internal events after domain model initialization has completed. External subscribers should use IInferredComponent.subscribe().");
return observableField.subscribe(subscriber);
}

@Override
public Subscription subscribe(Consumer<T> subscriber) {
if (domainModel != null)
Assert.isTrue(domainModel.isFullyInitialized(), "Attempted to subscribe to external events before domain model initialization has completed. Internal subscribers should use IInferredComponent.subscribeInternal().");
Assert.isTrue(domainModel.isExternalSubscriptionRegistrationAllowed(), "Attempted to subscribe to external events before domain model initialization has completed. Internal subscribers should use IInferredComponent.subscribeInternal().");
return externalEvent.subscribe(subscriber);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,13 @@ public Iterator<T> iterator() {

@Override
public Subscription subscribeInternal(Consumer<IReadOnlyList<T>> subscriber) {
if (domainModel != null)
Assert.isFalse(domainModel.isFullyInitialized(), "Attempted to subscribe to internal events after domain model initialization has completed. External subscribers should use IListComponent.subscribe().");
Assert.isTrue(domainModel.isInternalSubscriptionRegistrationAllowed(), "Attempted to subscribe to internal events after domain model initialization has completed. External subscribers should use IListComponent.subscribe().");
return observableList.subscribe(subscriber);
}

@Override
public Subscription subscribe(Consumer<IReadOnlyList<T>> subscriber) {
if (domainModel != null)
Assert.isTrue(domainModel.isFullyInitialized(), "Attempted to subscribe to external events before domain model initialization has completed. Internal subscribers should use IListComponent.subscribeInternal().");
Assert.isTrue(domainModel.isExternalSubscriptionRegistrationAllowed(), "Attempted to subscribe to external events before domain model initialization has completed. Internal subscribers should use IListComponent.subscribeInternal().");
return externalEvent.subscribe(subscriber);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ninjabrainbot.integrationtests;

import java.util.Locale;

import javax.swing.SwingUtilities;

import ninjabrainbot.gui.frames.CalibrationDialog;
import ninjabrainbot.model.datastate.calibrator.CalibratorFactory;
import ninjabrainbot.util.TestUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class CalibrationIntegrationTests {

@Test
public void canOpenDialog() {
// Arrange
IntegrationTestBuilder integrationTestBuilder = new IntegrationTestBuilder();
Locale.setDefault(Locale.US);
CalibratorFactory calibratorFactory = integrationTestBuilder.createCalibratorFactory();

// Act + Assert
try {
SwingUtilities.invokeAndWait(() -> {
CalibrationDialog dialog = new CalibrationDialog(TestUtils.createStyleManager(), integrationTestBuilder.preferences, calibratorFactory, integrationTestBuilder.actionExecutor, null, false);
});
} catch (Exception e) {
Assertions.fail(e);
}
}

@Test
public void canOpenDialog_manualCalibration() {
// Arrange
IntegrationTestBuilder integrationTestBuilder = new IntegrationTestBuilder();
Locale.setDefault(Locale.US);
CalibratorFactory calibratorFactory = integrationTestBuilder.createCalibratorFactory();

// Act + Assert
try {
SwingUtilities.invokeAndWait(() -> {
CalibrationDialog dialog = new CalibrationDialog(TestUtils.createStyleManager(), integrationTestBuilder.preferences, calibratorFactory, integrationTestBuilder.actionExecutor, null, true);
});
} catch (Exception e) {
Assertions.fail(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import ninjabrainbot.model.ModelState;
import ninjabrainbot.model.actions.IActionExecutor;
import ninjabrainbot.model.datastate.IDataState;
import ninjabrainbot.model.datastate.calibrator.CalibratorFactory;
import ninjabrainbot.model.datastate.common.IDetailedPlayerPosition;
import ninjabrainbot.model.datastate.endereye.CoordinateInputSource;
import ninjabrainbot.model.datastate.endereye.EnderEyeThrowFactory;
Expand Down Expand Up @@ -175,6 +176,12 @@ public NinjabrainBotFrame createNinjabrainBotFrame() {
return frame;
}

public CalibratorFactory createCalibratorFactory() {
if (fakeCoordinateInputSource == null)
fakeCoordinateInputSource = new FakeCoordinateInputSource();
return new CalibratorFactory(environmentState.calculatorSettings(), fakeCoordinateInputSource, preferences);
}

public BoatIcon createBoatIcon() {
if (styleManager == null) styleManager = TestUtils.createStyleManager();
return new BoatIcon(styleManager, dataState.boatDataState().boatState(), preferences, new DisposeHandler());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ninjabrainbot.integrationtests;

import java.util.Locale;

import javax.swing.SwingUtilities;

import ninjabrainbot.gui.frames.ThemeEditorDialog;
import ninjabrainbot.gui.frames.ThemedDialog;
import ninjabrainbot.gui.style.theme.CustomTheme;
import ninjabrainbot.util.TestUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ThemeEditorIntegrationTests {

@Test
public void canOpenDialog() {
// Arrange
IntegrationTestBuilder integrationTestBuilder = new IntegrationTestBuilder();
Locale.setDefault(Locale.US);

// Act + Assert
try {
SwingUtilities.invokeAndWait(() -> {
ThemedDialog dialog = new ThemeEditorDialog(TestUtils.createStyleManager(), integrationTestBuilder.preferences, null, new CustomTheme());
});
} catch (Exception e) {
Assertions.fail(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class OneEyeAccuracySimulation {
public static void main(String[] args) {
Logger.enabled = false;
calculator = new Calculator(new CalculatorSettings(true, McVersion.PRE_119), new StandardDeviationSettings(std, std, std, std));
divineContext = new DivineContext(null);
divineContext = new DivineContext(new DetachedDomainModel());

for (int r = 0; r < 3000; r += 100) {
int successes = 0;
Expand Down

0 comments on commit 9f1587d

Please sign in to comment.