Merge pull request #3788 from ZacSharp/pr/detectBrokenLoadHook

Make the world cache work even when mods break our world load hook
This commit is contained in:
leijurv 2023-01-12 11:39:41 -08:00 committed by GitHub
commit ba0ca0cc65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 0 deletions

View File

@ -24,6 +24,7 @@ import baritone.utils.accessor.IAnvilChunkLoader;
import baritone.utils.accessor.IChunkProviderServer;
import net.minecraft.server.integrated.IntegratedServer;
import net.minecraft.world.WorldServer;
import net.minecraft.world.World;
import org.apache.commons.lang3.SystemUtils;
import java.io.File;
@ -44,9 +45,11 @@ public class WorldProvider implements IWorldProvider, Helper {
private static final Map<Path, WorldData> worldCache = new HashMap<>(); // this is how the bots have the same cached world
private WorldData currentWorld;
private World mcWorld; // this let's us detect a broken load/unload hook
@Override
public final WorldData getCurrentWorld() {
detectAndHandleBrokenLoading();
return this.currentWorld;
}
@ -82,7 +85,9 @@ public class WorldProvider implements IWorldProvider, Helper {
folderName = mc.getCurrentServerData().serverIP;
} else {
//replaymod causes null currentServerData and false singleplayer.
System.out.println("World seems to be a replay. Not loading Baritone cache.");
currentWorld = null;
mcWorld = mc.world;
return;
}
if (SystemUtils.IS_OS_WINDOWS) {
@ -110,11 +115,13 @@ public class WorldProvider implements IWorldProvider, Helper {
synchronized (worldCache) {
this.currentWorld = worldCache.computeIfAbsent(dir, d -> new WorldData(d, dimension));
}
this.mcWorld = mc.world;
}
public final void closeWorld() {
WorldData world = this.currentWorld;
this.currentWorld = null;
this.mcWorld = null;
if (world == null) {
return;
}
@ -122,8 +129,25 @@ public class WorldProvider implements IWorldProvider, Helper {
}
public final void ifWorldLoaded(Consumer<WorldData> currentWorldConsumer) {
detectAndHandleBrokenLoading();
if (this.currentWorld != null) {
currentWorldConsumer.accept(this.currentWorld);
}
}
private final void detectAndHandleBrokenLoading() {
if (this.mcWorld != mc.world) {
if (this.currentWorld != null) {
System.out.println("mc.world unloaded unnoticed! Unloading Baritone cache now.");
closeWorld();
}
if (mc.world != null) {
System.out.println("mc.world loaded unnoticed! Loading Baritone cache now.");
initWorld(mc.world.provider.getDimensionType().getId());
}
} else if (currentWorld == null && mc.world != null && (mc.isSingleplayer() || mc.getCurrentServerData() != null)) {
System.out.println("Retrying to load Baritone cache");
initWorld(mc.world.provider.getDimensionType().getId());
}
}
}