diff --git a/src/api/java/baritone/api/process/IElytraProcess.java b/src/api/java/baritone/api/process/IElytraProcess.java index 06c658f42..b1bd3d833 100644 --- a/src/api/java/baritone/api/process/IElytraProcess.java +++ b/src/api/java/baritone/api/process/IElytraProcess.java @@ -29,6 +29,7 @@ public interface IElytraProcess extends IBaritoneProcess { * * @return A {@link CompletableFuture} that is completed when the context is reset */ + @Deprecated CompletableFuture resetContext(); void repackChunks(); diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 0404921ae..9b15b88ba 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -33,6 +33,7 @@ import baritone.pathing.calc.AbstractNodeCostSearch; import baritone.pathing.movement.CalculationContext; import baritone.pathing.movement.MovementHelper; import baritone.pathing.path.PathExecutor; +import baritone.process.ElytraProcess; import baritone.utils.PathRenderer; import baritone.utils.PathingCommandContext; import baritone.utils.pathing.Favoring; @@ -93,6 +94,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, @Override public void onTick(TickEvent event) { + ((ElytraProcess) baritone.getElytraProcess()).onTickBeforePathingBehavior(event); dispatchEvents(); if (event.getType() == TickEvent.Type.OUT) { secretInternalSegmentCancel(); diff --git a/src/main/java/baritone/process/ElytraProcess.java b/src/main/java/baritone/process/ElytraProcess.java index 121b37b8a..11f4fdeaf 100644 --- a/src/main/java/baritone/process/ElytraProcess.java +++ b/src/main/java/baritone/process/ElytraProcess.java @@ -18,6 +18,8 @@ package baritone.process; import baritone.Baritone; +import baritone.api.event.events.*; +import baritone.api.event.listener.AbstractGameEventListener; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.goals.GoalYLevel; import baritone.api.pathing.movement.IMovement; @@ -43,7 +45,7 @@ import net.minecraft.util.math.Vec3d; import java.util.concurrent.*; import java.util.function.Supplier; -public class ElytraProcess extends BaritoneProcessHelper implements IBaritoneProcess, IElytraProcess { +public class ElytraProcess extends BaritoneProcessHelper implements IBaritoneProcess, IElytraProcess, AbstractGameEventListener { public State state; private Goal goal; private LegacyElytraBehavior behavior; @@ -51,7 +53,7 @@ public class ElytraProcess extends BaritoneProcessHelper implements IBaritonePro private ElytraProcess(Baritone baritone) { super(baritone); this.behavior = new LegacyElytraBehavior(baritone, this); - baritone.getGameEventHandler().registerEventListener(this.behavior); + baritone.getGameEventHandler().registerEventListener(this); } public static T create(final Baritone baritone) { @@ -62,7 +64,7 @@ public class ElytraProcess extends BaritoneProcessHelper implements IBaritonePro @Override public boolean isActive() { - return behavior.destination != null; + return behavior != null; } @Override @@ -176,16 +178,12 @@ public class ElytraProcess extends BaritoneProcessHelper implements IBaritonePro return new PathingCommand(null, PathingCommandType.CANCEL_AND_SET_GOAL); } - @Override - public boolean isTemporary() { - return false; - } - @Override public void onLostControl() { this.goal = null; - this.state = State.START_FLYING; - behavior.cancel(); + this.state = State.START_FLYING; // TODO: null state? + if (this.behavior != null) this.behavior.cancel(); + this.behavior = null; } @@ -220,17 +218,22 @@ public class ElytraProcess extends BaritoneProcessHelper implements IBaritonePro @Override public void repackChunks() { - behavior.repackChunks(); + this.behavior.repackChunks(); } @Override public void pathTo(BlockPos destination) { - behavior.pathTo(destination); + this.behavior = new LegacyElytraBehavior(this.baritone, this); + if (ctx.world() != null) { + this.behavior.repackChunks(); + } + this.behavior.pathTo(destination); } @Override public void cancel() { - behavior.cancel(); + if (this.behavior != null) this.behavior.cancel(); + this.behavior = null; } @Override @@ -240,7 +243,7 @@ public class ElytraProcess extends BaritoneProcessHelper implements IBaritonePro @Override public boolean isSafeToCancel() { - return behavior.isSafeToCancel(); + return !this.isActive() || !(this.state == State.FLYING || this.state == State.START_FLYING); } public enum State { @@ -252,4 +255,38 @@ public class ElytraProcess extends BaritoneProcessHelper implements IBaritonePro FLYING, LANDING } + + @Override + public void onRenderPass(RenderEvent event) { + if (this.behavior != null) this.behavior.onRenderPass(event); + } + + @Override + public void onWorldEvent(WorldEvent event) { + if (this.behavior != null) this.behavior.onWorldEvent(event); + } + + @Override + public void onChunkEvent(ChunkEvent event) { + if (this.behavior != null) this.behavior.onChunkEvent(event); + } + + @Override + public void onBlockChange(BlockChangeEvent event) { + if (this.behavior != null) this.behavior.onBlockChange(event); + } + + @Override + public void onReceivePacket(PacketEvent event) { + if (this.behavior != null) this.behavior.onReceivePacket(event); + } + + public void onTickBeforePathingBehavior(final TickEvent event) { + if (this.behavior != null) this.behavior.onTick(event); + } + + @Override + public void onPostTick(TickEvent event) { + if (this.behavior != null) this.behavior.onPostTick(event); + } } diff --git a/src/main/java/baritone/process/elytra/LegacyElytraBehavior.java b/src/main/java/baritone/process/elytra/LegacyElytraBehavior.java index 48066f3ea..6553f1ef8 100644 --- a/src/main/java/baritone/process/elytra/LegacyElytraBehavior.java +++ b/src/main/java/baritone/process/elytra/LegacyElytraBehavior.java @@ -24,7 +24,6 @@ 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.event.listener.AbstractGameEventListener; import baritone.api.pathing.goals.GoalBlock; import baritone.api.utils.*; import baritone.pathing.movement.CalculationContext; @@ -51,6 +50,7 @@ import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.Vec3d; +import net.minecraft.world.World; import net.minecraft.world.chunk.Chunk; import java.awt.*; @@ -63,7 +63,7 @@ import static baritone.api.pathing.movement.ActionCosts.COST_INF; import static baritone.utils.BaritoneMath.fastCeil; import static baritone.utils.BaritoneMath.fastFloor; -public final class LegacyElytraBehavior implements AbstractGameEventListener, Helper { +public final class LegacyElytraBehavior implements Helper { private final Baritone baritone; private final IPlayerContext ctx; @@ -76,7 +76,7 @@ public final class LegacyElytraBehavior implements AbstractGameEventListener, H public List visiblePath; // :sunglasses: - private NetherPathfinderContext context; + private NetherPathfinderContext context; // TODO: make this final private CompletableFuture forceResetContext; public final PathManager pathManager; private final ElytraProcess process; @@ -125,6 +125,8 @@ public final class LegacyElytraBehavior implements AbstractGameEventListener, H this.process = process; this.solverExecutor = Executors.newSingleThreadExecutor(); this.nextTickBoostCounter = new int[2]; + + this.context = new NetherPathfinderContext(Baritone.settings().elytraNetherSeed.value); } public final class PathManager { @@ -370,7 +372,6 @@ public final class LegacyElytraBehavior implements AbstractGameEventListener, H } } - @Override public void onRenderPass(RenderEvent event) { final Settings settings = Baritone.settings(); if (this.visiblePath != null) { @@ -409,7 +410,7 @@ public final class LegacyElytraBehavior implements AbstractGameEventListener, H } } - @Override + // TODO: move this logic to ElytraProcess public void onWorldEvent(WorldEvent event) { if (event.getWorld() != null) { if (event.getState() == EventState.PRE) { @@ -427,7 +428,6 @@ public final class LegacyElytraBehavior implements AbstractGameEventListener, H } } - @Override public void onChunkEvent(ChunkEvent event) { if (event.isPostPopulate() && this.context != null) { final Chunk chunk = ctx.world().getChunk(event.getX(), event.getZ()); @@ -435,12 +435,16 @@ public final class LegacyElytraBehavior implements AbstractGameEventListener, H } } - @Override + public void uploadRenderDistance(World world) { + ((IChunkProviderClient) world.getChunkProvider()).loadedChunks().forEach((l, chunk) -> { + this.context.queueForPacking(chunk); + }); + } + public void onBlockChange(BlockChangeEvent event) { this.context.queueBlockUpdate(event); } - @Override public void onReceivePacket(PacketEvent event) { if (event.getPacket() instanceof SPacketPlayerPosLook) { ctx.minecraft().addScheduledTask(() -> { @@ -486,11 +490,6 @@ public final class LegacyElytraBehavior implements AbstractGameEventListener, H .filter(process -> this.process == process).isPresent(); } - public boolean isSafeToCancel() { - return !this.isActive() || !(this.process.state == ElytraProcess.State.FLYING || this.process.state == ElytraProcess.State.START_FLYING); - } - - @Override public void onTick(final TickEvent event) { if (event.getType() == TickEvent.Type.OUT) { return; @@ -629,7 +628,6 @@ public final class LegacyElytraBehavior implements AbstractGameEventListener, H ); } - @Override public void onPostTick(TickEvent event) { if (event.getType() == TickEvent.Type.IN && this.solveNextTick) { // We're at the end of the tick, the player's position likely updated and the closest path node could've diff --git a/src/main/java/baritone/process/elytra/NetherPathfinderContext.java b/src/main/java/baritone/process/elytra/NetherPathfinderContext.java index 46a840981..69ac41992 100644 --- a/src/main/java/baritone/process/elytra/NetherPathfinderContext.java +++ b/src/main/java/baritone/process/elytra/NetherPathfinderContext.java @@ -52,7 +52,6 @@ public final class NetherPathfinderContext { final long context; private final long seed; private final ExecutorService executor; - private final AtomicInteger packQueueSize = new AtomicInteger(); public NetherPathfinderContext(long seed) { this.context = NetherPathfinder.newContext(seed);