Handle mods breaking our world loading hook

This commit is contained in:
ZacSharp 2023-01-10 17:25:32 +01:00
parent 145a944860
commit 1680eeb80d
No known key found for this signature in database
GPG Key ID: 9453647B005083A3

View File

@ -24,6 +24,7 @@ import baritone.utils.accessor.IAnvilChunkLoader;
import baritone.utils.accessor.IChunkProviderServer; import baritone.utils.accessor.IChunkProviderServer;
import net.minecraft.server.integrated.IntegratedServer; import net.minecraft.server.integrated.IntegratedServer;
import net.minecraft.world.WorldServer; import net.minecraft.world.WorldServer;
import net.minecraft.world.World;
import org.apache.commons.lang3.SystemUtils; import org.apache.commons.lang3.SystemUtils;
import java.io.File; 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 static final Map<Path, WorldData> worldCache = new HashMap<>(); // this is how the bots have the same cached world
private WorldData currentWorld; private WorldData currentWorld;
private World mcWorld; // this let's us detect a broken load/unload hook
@Override @Override
public final WorldData getCurrentWorld() { public final WorldData getCurrentWorld() {
detectAndHandleBrokenLoading();
return this.currentWorld; return this.currentWorld;
} }
@ -83,6 +86,7 @@ public class WorldProvider implements IWorldProvider, Helper {
} else { } else {
//replaymod causes null currentServerData and false singleplayer. //replaymod causes null currentServerData and false singleplayer.
currentWorld = null; currentWorld = null;
mcWorld = mc.world;
return; return;
} }
if (SystemUtils.IS_OS_WINDOWS) { if (SystemUtils.IS_OS_WINDOWS) {
@ -110,11 +114,13 @@ public class WorldProvider implements IWorldProvider, Helper {
synchronized (worldCache) { synchronized (worldCache) {
this.currentWorld = worldCache.computeIfAbsent(dir, d -> new WorldData(d, dimension)); this.currentWorld = worldCache.computeIfAbsent(dir, d -> new WorldData(d, dimension));
} }
this.mcWorld = mc.world;
} }
public final void closeWorld() { public final void closeWorld() {
WorldData world = this.currentWorld; WorldData world = this.currentWorld;
this.currentWorld = null; this.currentWorld = null;
this.mcWorld = null;
if (world == null) { if (world == null) {
return; return;
} }
@ -122,8 +128,20 @@ public class WorldProvider implements IWorldProvider, Helper {
} }
public final void ifWorldLoaded(Consumer<WorldData> currentWorldConsumer) { public final void ifWorldLoaded(Consumer<WorldData> currentWorldConsumer) {
detectAndHandleBrokenLoading();
if (this.currentWorld != null) { if (this.currentWorld != null) {
currentWorldConsumer.accept(this.currentWorld); currentWorldConsumer.accept(this.currentWorld);
} }
} }
private final void detectAndHandleBrokenLoading() {
if (this.mcWorld != mc.world) {
if (this.currentWorld != null) {
closeWorld();
}
if (mc.world != null) {
initWorld(mc.world.provider.getDimensionType().getId());
}
}
}
} }