Skip to content

Commit

Permalink
Fixed aa mode flashing if tabbing into an instance which previously h…
Browse files Browse the repository at this point in the history
…ad an aa world
  • Loading branch information
Ninjabrain1 committed Apr 26, 2023
1 parent 13feaea commit d7d130d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
public class MinecraftWorldFile implements IMinecraftWorldFile {

private final MinecraftInstance minecraftInstance;
private final String name;
// Name can be null if the name has not been found out yet
private String name;
private boolean hasEnteredEnd;

private File endDimensionFile;
Expand All @@ -31,8 +32,12 @@ public boolean hasEnteredEnd() {
return hasEnteredEnd;
}

public void setName(String name) {
this.name = name;
}

File getEndDimensionFile() {
if (endDimensionFile == null)
if (endDimensionFile == null && name != null)
endDimensionFile = new File(minecraftInstance.savesDirectory + "\\" + name + "\\DIM1\\region");
return endDimensionFile;
}
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/ninjabrainbot/io/mcinstance/SavesReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.HashMap;

import ninjabrainbot.event.IObservable;
import ninjabrainbot.event.ISubscribable;
Expand All @@ -20,14 +19,12 @@ public class SavesReader {

private final WatchService watcher;
private WatchedInstance currentWatchedInstance;
private final HashMap<MinecraftInstance, MinecraftWorldFile> activeWorldInEachInstance;

private final ObservableField<IMinecraftWorldFile> lastModifiedWorldFile;
private final ObservableProperty<IMinecraftWorldFile> whenActiveWorldFileModified;
private MinecraftWorldFile activeWorldFile;

public SavesReader(ISubscribable<MinecraftInstance> activeMinecraftInstance) throws IOException {
activeWorldInEachInstance = new HashMap<>();
lastModifiedWorldFile = new ObservableField<>(null);
whenActiveWorldFileModified = new ObservableProperty<>();
watcher = FileSystems.getDefault().newWatchService();
Expand Down Expand Up @@ -65,11 +62,11 @@ private void monitorMinecraftInstance(MinecraftInstance minecraftInstance) {
e.printStackTrace();
}

if (!activeWorldInEachInstance.containsKey(minecraftInstance)) {
MinecraftWorldFile worldFile = new MinecraftWorldFile(minecraftInstance, getLatestModifiedWorldNameInSavesDirectory(minecraftInstance));
activeWorldInEachInstance.put(minecraftInstance, worldFile);
}
activeWorldFile = activeWorldInEachInstance.get(minecraftInstance);
// if (!activeWorldInEachInstance.containsKey(minecraftInstance)) {
// MinecraftWorldFile worldFile = new MinecraftWorldFile(minecraftInstance, getLatestModifiedWorldNameInSavesDirectory(minecraftInstance));
// activeWorldInEachInstance.put(minecraftInstance, worldFile);
// }
activeWorldFile = new MinecraftWorldFile(minecraftInstance, null);
lastModifiedWorldFile.set(activeWorldFile);
onActiveMinecraftWorldFileModified();
}
Expand All @@ -92,9 +89,11 @@ private void pollEventsForActiveMinecraftInstance(WatchKey watchKey) {

private void whenActiveMinecraftInstanceSaveDirectoryModified(String worldName) {
if (isWorldFileDifferentFromTheLastModifiedWorldFile(worldName)) {
activeWorldFile = new MinecraftWorldFile(currentWatchedInstance.minecraftInstance, worldName);
if (activeWorldFile.name() == null)
activeWorldFile.setName(worldName);
else
activeWorldFile = new MinecraftWorldFile(currentWatchedInstance.minecraftInstance, worldName);
lastModifiedWorldFile.set(activeWorldFile);
activeWorldInEachInstance.put(currentWatchedInstance.minecraftInstance, activeWorldFile);
}
onActiveMinecraftWorldFileModified();
}
Expand All @@ -106,14 +105,15 @@ private boolean isWorldFileDifferentFromTheLastModifiedWorldFile(String worldNam
if (activeWorldFile.minecraftInstance() != currentWatchedInstance.minecraftInstance)
return true;

return !activeWorldFile.name().contentEquals(worldName);
return activeWorldFile.name() == null || !activeWorldFile.name().contentEquals(worldName);
}

private void onActiveMinecraftWorldFileModified() {
if (!activeWorldFile.hasEnteredEnd() && activeWorldFile.getEndDimensionFile().exists()) {
if (!activeWorldFile.hasEnteredEnd() && activeWorldFile.getEndDimensionFile() != null && activeWorldFile.getEndDimensionFile().exists()) {
activeWorldFile.setHasEnteredEnd(true);
}
whenActiveWorldFileModified.notifySubscribers(activeWorldFile);
System.out.println(activeWorldFile.name() + ", " + activeWorldFile.hasEnteredEnd());
}

private String getLatestModifiedWorldNameInSavesDirectory(MinecraftInstance minecraftInstance) {
Expand Down

0 comments on commit d7d130d

Please sign in to comment.