Skip to content

Commit

Permalink
Add uniqueId and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjabrain1 committed Sep 6, 2024
1 parent 356690f commit ab88a6b
Show file tree
Hide file tree
Showing 18 changed files with 165 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,56 @@

import ninjabrainbot.model.datastate.alladvancements.IAllAdvancementsDataState;
import ninjabrainbot.model.datastate.alladvancements.IAllAdvancementsPosition;
import ninjabrainbot.model.datastate.common.DetachedDomainModel;
import ninjabrainbot.model.datastate.common.StructureInformation;
import ninjabrainbot.model.domainmodel.DataComponent;
import ninjabrainbot.model.domainmodel.IDataComponent;
import ninjabrainbot.model.domainmodel.IDomainModel;
import ninjabrainbot.model.domainmodel.IDomainModelComponent;
import ninjabrainbot.model.domainmodel.InferredComponent;

public class PreviewAllAdvancementsDataState implements IAllAdvancementsDataState {

IDomainModel domainModel = new DetachedDomainModel();

@Override
public IDomainModelComponent<Boolean> allAdvancementsModeEnabled() {
return new DataComponent<>(null, false);
return new DataComponent<>("", domainModel, false);
}

@Override
public IDataComponent<IAllAdvancementsPosition> spawnPosition() {
return new DataComponent<>(null);
return new DataComponent<>("", domainModel);
}

@Override
public IDataComponent<IAllAdvancementsPosition> outpostPosition() {
return new DataComponent<>(null);
return new DataComponent<>("", domainModel);
}

@Override
public IDataComponent<IAllAdvancementsPosition> monumentPosition() {
return new DataComponent<>(null);
return new DataComponent<>("", domainModel);
}

@Override
public IDataComponent<IAllAdvancementsPosition> deepDarkPosition() {
return new DataComponent<>(null);
return new DataComponent<>("", domainModel);
}

@Override
public IDataComponent<IAllAdvancementsPosition> cityQueryPosition() {
return new DataComponent<>(null);
return new DataComponent<>("", domainModel);
}

@Override
public IDataComponent<IAllAdvancementsPosition> shulkerTransportPosition() {
return new DataComponent<>(null);
return new DataComponent<>("", domainModel);
}

@Override
public IDataComponent<IAllAdvancementsPosition> generalLocationPosition() {
return new DataComponent<>(null);
return new DataComponent<>("", domainModel);
}

@Override
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/ninjabrainbot/gui/themeeditor/PreviewDataState.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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.ResultType;
import ninjabrainbot.model.datastate.divine.DivineContext;
import ninjabrainbot.model.datastate.divine.DivineResult;
Expand All @@ -18,6 +19,7 @@
import ninjabrainbot.model.datastate.stronghold.ChunkPrediction;
import ninjabrainbot.model.domainmodel.DataComponent;
import ninjabrainbot.model.domainmodel.IDataComponent;
import ninjabrainbot.model.domainmodel.IDomainModel;
import ninjabrainbot.model.domainmodel.IDomainModelComponent;
import ninjabrainbot.model.domainmodel.IListComponent;
import ninjabrainbot.model.domainmodel.InferredComponent;
Expand Down Expand Up @@ -50,15 +52,16 @@ public PreviewDataState(ICalculatorResult result, List<IEnderEyeThrow> eyeThrows
}

public PreviewDataState() {
divineContext = new DivineContext(null);
throwSet = new ListComponent<>(null, 10);
playerPosition = new DataComponent<>(null);
locked = new DataComponent<>(null, false);
resultType = new DataComponent<>(null, ResultType.NONE);
calculatorResult = new InferredComponent<>(null);
topPrediction = new InferredComponent<>(null);
blindResult = new InferredComponent<>(null);
divineResult = new InferredComponent<>(null);
IDomainModel domainModel = new DetachedDomainModel();
divineContext = new DivineContext(domainModel);
throwSet = new ListComponent<>("", domainModel, 10);
playerPosition = new DataComponent<>("", domainModel, null);
locked = new DataComponent<>("", domainModel, false);
resultType = new DataComponent<>("", domainModel, ResultType.NONE);
calculatorResult = new InferredComponent<>(domainModel);
topPrediction = new InferredComponent<>(domainModel);
blindResult = new InferredComponent<>(domainModel);
divineResult = new InferredComponent<>(domainModel);

boatDataState = new BoatDataState(null);
allAdvancementsDataState = new PreviewAllAdvancementsDataState();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ninjabrainbot/model/datastate/DataState.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public DataState(IDomainModel domainModel, IEnvironmentState environmentState) {

public DataState(IDomainModel domainModel, IEnvironmentState environmentState, DefaultBoatType defaultBoatType) {
divineContext = disposeHandler.add(new DivineContext(domainModel));
throwSet = new ListComponent<>(domainModel, 10);
playerPosition = new DataComponent<>(domainModel);
locked = new DataComponent<>(domainModel, false);
throwSet = new ListComponent<>("throw_set", domainModel, 10);
playerPosition = new DataComponent<>("player_position", domainModel);
locked = new DataComponent<>("is_locked", domainModel, false);

calculatorManager = disposeHandler.add(new CalculatorManager(domainModel, environmentState, throwSet, playerPosition, divineContext));
allAdvancementsDataState = disposeHandler.add(new AllAdvancementsDataState(calculatorManager.topPrediction(), playerPosition, domainModel, environmentState));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public AllAdvancementsDataState(IDomainModelComponent<ChunkPrediction> currentSt
this.playerPosition = playerPosition;
this.environmentState = environmentState;

spawnPosition = new DataComponent<>(domainModel);
outpostPosition = new DataComponent<>(domainModel);
monumentPosition = new DataComponent<>(domainModel);
deepDarkPosition = new DataComponent<>(domainModel);
cityQueryPosition = new DataComponent<>(domainModel);
shulkerTransportPosition = new DataComponent<>(domainModel);
generalLocationPosition = new DataComponent<>(domainModel);
spawnPosition = new DataComponent<>("aa_spawn", domainModel);
outpostPosition = new DataComponent<>("aa_outpost", domainModel);
monumentPosition = new DataComponent<>("aa_monument", domainModel);
deepDarkPosition = new DataComponent<>("aa_deep_dark", domainModel);
cityQueryPosition = new DataComponent<>("aa_city_query", domainModel);
shulkerTransportPosition = new DataComponent<>("aa_shulker_transport", domainModel);
generalLocationPosition = new DataComponent<>("aa_general_location", domainModel);

allAdvancementsModeEnabled = new InferredComponent<>(domainModel, false);
strongholdInformation = new InferredComponent<>(domainModel);
Expand All @@ -64,13 +64,13 @@ public AllAdvancementsDataState(IDomainModelComponent<ChunkPrediction> currentSt
disposeHandler.add(environmentState.allAdvancementsModeEnabled().subscribeInternal(this::updateAllAdvancementsMode));
disposeHandler.add(environmentState.hasEnteredEnd().subscribeInternal(this::updateAllAdvancementsMode));
disposeHandler.add(currentStrongholdPrediction.subscribeInternal(strongholdInformation::set));
disposeHandler.add(spawnPosition.subscribeInternal(overworldPosition -> updateStructureInformationComponent(spawnInformation, overworldPosition)));
disposeHandler.add(outpostPosition.subscribeInternal(overworldPosition -> updateStructureInformationComponent(outpostInformation, overworldPosition)));
disposeHandler.add(monumentPosition.subscribeInternal(overworldPosition -> updateStructureInformationComponent(monumentInformation, overworldPosition)));
disposeHandler.add(deepDarkPosition.subscribeInternal(overworldPosition -> updateStructureInformationComponent(deepDarkInformation, overworldPosition)));
disposeHandler.add(cityQueryPosition.subscribeInternal(overworldPosition -> updateStructureInformationComponent(cityQueryInformation, overworldPosition)));
disposeHandler.add(shulkerTransportPosition.subscribeInternal(overworldPosition -> updateStructureInformationComponent(shulkerTransportInformation, overworldPosition)));
disposeHandler.add(generalLocationPosition.subscribeInternal(overworldPosition -> updateStructureInformationComponent(generalLocationInformation, overworldPosition)));
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)));
}

private void updateAllAdvancementsMode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import ninjabrainbot.model.datastate.calculator.Calculator;
import ninjabrainbot.model.datastate.calculator.ICalculator;
import ninjabrainbot.model.datastate.calculator.ICalculatorResult;
import ninjabrainbot.model.datastate.common.DetachedDomainModel;
import ninjabrainbot.model.datastate.common.IDetailedPlayerPosition;
import ninjabrainbot.model.datastate.common.ILimitedPlayerPosition;
import ninjabrainbot.model.datastate.common.IPlayerPositionInputSource;
Expand Down Expand Up @@ -65,7 +66,7 @@ public Calibrator(CalculatorSettings calculatorSettings, IPlayerPositionInputSou
}
this.preferences = preferences;
calculator = new Calculator(calculatorSettings, new StandardDeviationSettings(0.2, 0.2, 0.2, 0.2));
throwList = new ListComponent<>(null, 100);
throwList = new ListComponent<>("throw_list", new DetachedDomainModel(), 100);
disposeHandler.add(playerPositionInputSource.whenNewLimitedPlayerPositionInputted().subscribe(this::onNewLimitedPlayerPositionInputted));
disposeHandler.add(playerPositionInputSource.whenNewDetailedPlayerPositionInputted().subscribe(this::onNewDetailedPlayerPositionInputted));
disposeHandler.add(preferences.hotkeyIncrement.whenTriggered().subscribe(__ -> new ChangeLastAngleAction(throwList, locked, preferences, 1).execute()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ninjabrainbot.model.datastate.common;

import ninjabrainbot.event.ISubscribable;
import ninjabrainbot.event.ObservableProperty;
import ninjabrainbot.model.domainmodel.IDomainModel;
import ninjabrainbot.model.domainmodel.IFundamentalComponent;
import ninjabrainbot.model.domainmodel.IInferredComponent;

public class DetachedDomainModel implements IDomainModel {

ISubscribable<IDomainModel> whenModified = new ObservableProperty<>();

@Override
public void registerFundamentalComponent(IFundamentalComponent<?, ?> fundamentalComponent) {
}

@Override
public void registerInferredComponent(IInferredComponent<?> inferredComponent) {
}

@Override
public <T> ISubscribable<T> createExternalEventFor(ISubscribable<T> subscribable) {
return subscribable;
}

@Override
public void checkWriteAccess() {
}

@Override
public void acquireWriteLock() {
}

@Override
public void releaseWriteLock() {
}

@Override
public void reset() {
}

@Override
public void undoUnderWriteLock() {
}

@Override
public void redoUnderWriteLock() {
}

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

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

@Override
public ISubscribable<IDomainModel> whenModified() {
return whenModified;
}

@Override
public Runnable applyWriteLock(Runnable runnable) {
return runnable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class DivineContext implements IDivineContext, IDisposable {
private final DisposeHandler disposeHandler = new DisposeHandler();

public DivineContext(IDomainModel domainModel) {
fossil = new DataComponent<>(domainModel);
fossil = new DataComponent<>("fossil", domainModel);
discretizedAngularDensity = new DiscretizedDensity(0, 2.0 * Math.PI);
disposeHandler.add(fossil.subscribeInternal(this::onFossilChanged));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ public BoatDataState(IDomainModel domainModel) {
}

public BoatDataState(IDomainModel domainModel, DefaultBoatType defaultBoatType) {
reducingModulo360 = new DataComponent<>(domainModel, false);
reducingModulo360 = new DataComponent<>("boat_reducing_mod_360", domainModel, false);

switch (defaultBoatType) {
case GREEN:
enteringBoat = new DataComponent<>(domainModel, false);
boatAngle = new DataComponent<>(domainModel, 0f);
boatState = new DataComponent<>(domainModel, BoatState.VALID);
enteringBoat = new DataComponent<>("boat_entering", domainModel, false);
boatAngle = new DataComponent<>("boat_angle", domainModel, 0f);
boatState = new DataComponent<>("boat_state", domainModel, BoatState.VALID);
break;
case BLUE:
enteringBoat = new DataComponent<>(domainModel, true);
boatAngle = new DataComponent<>(domainModel);
boatState = new DataComponent<>(domainModel, BoatState.MEASURING);
enteringBoat = new DataComponent<>("boat_entering", domainModel, true);
boatAngle = new DataComponent<>("boat_angle", domainModel);
boatState = new DataComponent<>("boat_state", domainModel, BoatState.MEASURING);
break;
default:
enteringBoat = new DataComponent<>(domainModel, false);
boatAngle = new DataComponent<>(domainModel);
boatState = new DataComponent<>(domainModel, BoatState.NONE);
enteringBoat = new DataComponent<>("boat_entering", domainModel, false);
boatAngle = new DataComponent<>("boat_angle", domainModel);
boatState = new DataComponent<>("boat_state", domainModel, BoatState.NONE);
}
}

Expand Down
19 changes: 13 additions & 6 deletions src/main/java/ninjabrainbot/model/domainmodel/DataComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ public class DataComponent<T extends Serializable> implements IDataComponent<T>
private final ObservableField<T> observableField;
private final ISubscribable<T> externalEvent;
private final T defaultValue;
private final String uniqueId;

public DataComponent(IDomainModel domainModel) {
this(domainModel, null);
public DataComponent(String uniqueId, IDomainModel domainModel) {
this(uniqueId, domainModel, null);
}

public DataComponent(IDomainModel domainModel, T defaultValue) {
public DataComponent(String uniqueId, IDomainModel domainModel, T defaultValue) {
Assert.isNotNull(domainModel, "Domain model cannot be null");
this.domainModel = domainModel;
this.uniqueId = uniqueId;
observableField = new ObservableField<>(defaultValue);
externalEvent = domainModel != null ? domainModel.createExternalEventFor(observableField) : observableField;
externalEvent = domainModel.createExternalEventFor(observableField);
this.defaultValue = defaultValue;
if (domainModel != null)
domainModel.registerFundamentalComponent(this);
domainModel.registerFundamentalComponent(this);
}

@Override
Expand Down Expand Up @@ -83,6 +85,11 @@ public Subscription subscribe(Consumer<T> subscriber) {
return externalEvent.subscribe(subscriber);
}

@Override
public String uniqueId() {
return uniqueId;
}

@Override
public T getAsSerializable() {
return get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface IFundamentalComponent<T, U extends Serializable> extends IDomai

boolean isReset();

// String uniqueId();
String uniqueId();

U getAsSerializable();

Expand Down
15 changes: 11 additions & 4 deletions src/main/java/ninjabrainbot/model/domainmodel/ListComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ public class ListComponent<T extends Serializable> implements IListComponent<T>
private final ObservableList<T> observableList;
private final ISubscribable<IReadOnlyList<T>> externalEvent;
private final int maxCapacity;
private final String uniqueId;

public ListComponent(IDomainModel domainModel, int maxCapacity) {
public ListComponent(String uniqueId, IDomainModel domainModel, int maxCapacity) {
Assert.isNotNull(domainModel, "Domain model cannot be null");
this.domainModel = domainModel;
this.maxCapacity = maxCapacity;
observableList = new ObservableList<>();
externalEvent = domainModel != null ? domainModel.createExternalEventFor(observableList) : observableList;
if (domainModel != null)
domainModel.registerFundamentalComponent(this);
externalEvent = domainModel.createExternalEventFor(observableList);
domainModel.registerFundamentalComponent(this);
this.uniqueId = uniqueId;
}

@Override
Expand Down Expand Up @@ -138,6 +140,11 @@ public Subscription subscribe(Consumer<IReadOnlyList<T>> subscriber) {
return externalEvent.subscribe(subscriber);
}

@Override
public String uniqueId() {
return uniqueId;
}

@Override
public ArrayList<T> getAsSerializable() {
ArrayList<T> arrayList = new ArrayList<>();
Expand Down
Loading

0 comments on commit ab88a6b

Please sign in to comment.