From bfb4ffcafcf5c51cfe25791f8e61f93e92baa088 Mon Sep 17 00:00:00 2001 From: Brady Date: Fri, 7 Jul 2023 11:19:18 -0700 Subject: [PATCH] Reset context on world load/unload --- .../baritone/behavior/ElytraBehavior.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/baritone/behavior/ElytraBehavior.java b/src/main/java/baritone/behavior/ElytraBehavior.java index edabf2912..98bcf7a3f 100644 --- a/src/main/java/baritone/behavior/ElytraBehavior.java +++ b/src/main/java/baritone/behavior/ElytraBehavior.java @@ -23,6 +23,7 @@ import baritone.api.behavior.IElytraBehavior; import baritone.api.behavior.look.IAimProcessor; import baritone.api.behavior.look.ITickableAimProcessor; import baritone.api.event.events.*; +import baritone.api.event.events.type.EventState; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.goals.GoalYLevel; import baritone.api.pathing.movement.IMovement; @@ -75,6 +76,7 @@ public final class ElytraBehavior extends Behavior implements IElytraBehavior, H // :sunglasses: private NetherPathfinderContext context; + private boolean forceResetContext; private final PathManager pathManager; private final ElytraProcess process; @@ -363,6 +365,24 @@ public final class ElytraBehavior extends Behavior implements IElytraBehavior, H } } + @Override + public void onWorldEvent(WorldEvent event) { + if (event.getWorld() != null) { + if (event.getState() == EventState.PRE) { + // Reset the context when it's safe to do so on the next game tick + this.forceResetContext = true; + } + } else { + if (event.getState() == EventState.POST) { + // Exiting the world, just destroy and invalidate the context + if (this.context != null) { + this.context.destroy(); + this.context = null; + } + } + } + } + @Override public void onChunkEvent(ChunkEvent event) { if (event.isPostPopulate()) { @@ -435,11 +455,12 @@ public final class ElytraBehavior extends Behavior implements IElytraBehavior, H // Setup/reset context final long netherSeed = Baritone.settings().elytraNetherSeed.value; - if (this.context == null || this.context.getSeed() != netherSeed) { + if (this.context == null || this.context.getSeed() != netherSeed || this.forceResetContext) { if (this.context != null) { this.context.destroy(); } this.context = new NetherPathfinderContext(netherSeed); + this.forceResetContext = false; if (this.isActive()) { // TODO: Re-pack chunks?