From d7d130d7110cded21353789dbfffdf8e66cd6f1b Mon Sep 17 00:00:00 2001 From: Filip Ryblad Date: Wed, 26 Apr 2023 20:33:23 +0200 Subject: [PATCH] Fixed aa mode flashing if tabbing into an instance which previously had an aa world --- .../io/mcinstance/MinecraftWorldFile.java | 9 +++++-- .../io/mcinstance/SavesReader.java | 24 +++++++++---------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/main/java/ninjabrainbot/io/mcinstance/MinecraftWorldFile.java b/src/main/java/ninjabrainbot/io/mcinstance/MinecraftWorldFile.java index 7aa4d9a6..011503bf 100644 --- a/src/main/java/ninjabrainbot/io/mcinstance/MinecraftWorldFile.java +++ b/src/main/java/ninjabrainbot/io/mcinstance/MinecraftWorldFile.java @@ -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; @@ -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; } diff --git a/src/main/java/ninjabrainbot/io/mcinstance/SavesReader.java b/src/main/java/ninjabrainbot/io/mcinstance/SavesReader.java index 19853391..92fc6eb6 100644 --- a/src/main/java/ninjabrainbot/io/mcinstance/SavesReader.java +++ b/src/main/java/ninjabrainbot/io/mcinstance/SavesReader.java @@ -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; @@ -20,14 +19,12 @@ public class SavesReader { private final WatchService watcher; private WatchedInstance currentWatchedInstance; - private final HashMap activeWorldInEachInstance; private final ObservableField lastModifiedWorldFile; private final ObservableProperty whenActiveWorldFileModified; private MinecraftWorldFile activeWorldFile; public SavesReader(ISubscribable activeMinecraftInstance) throws IOException { - activeWorldInEachInstance = new HashMap<>(); lastModifiedWorldFile = new ObservableField<>(null); whenActiveWorldFileModified = new ObservableProperty<>(); watcher = FileSystems.getDefault().newWatchService(); @@ -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(); } @@ -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(); } @@ -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) {