call elytra event handlers from ElytraProcess

This commit is contained in:
Babbaj 2023-07-17 00:34:48 -04:00
parent 9a6241af8a
commit dbc0a46b10
No known key found for this signature in database
GPG Key ID: F044309848A07CAC
5 changed files with 66 additions and 29 deletions

View File

@ -29,6 +29,7 @@ public interface IElytraProcess extends IBaritoneProcess {
* *
* @return A {@link CompletableFuture} that is completed when the context is reset * @return A {@link CompletableFuture} that is completed when the context is reset
*/ */
@Deprecated
CompletableFuture<Void> resetContext(); CompletableFuture<Void> resetContext();
void repackChunks(); void repackChunks();

View File

@ -33,6 +33,7 @@ import baritone.pathing.calc.AbstractNodeCostSearch;
import baritone.pathing.movement.CalculationContext; import baritone.pathing.movement.CalculationContext;
import baritone.pathing.movement.MovementHelper; import baritone.pathing.movement.MovementHelper;
import baritone.pathing.path.PathExecutor; import baritone.pathing.path.PathExecutor;
import baritone.process.ElytraProcess;
import baritone.utils.PathRenderer; import baritone.utils.PathRenderer;
import baritone.utils.PathingCommandContext; import baritone.utils.PathingCommandContext;
import baritone.utils.pathing.Favoring; import baritone.utils.pathing.Favoring;
@ -93,6 +94,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior,
@Override @Override
public void onTick(TickEvent event) { public void onTick(TickEvent event) {
((ElytraProcess) baritone.getElytraProcess()).onTickBeforePathingBehavior(event);
dispatchEvents(); dispatchEvents();
if (event.getType() == TickEvent.Type.OUT) { if (event.getType() == TickEvent.Type.OUT) {
secretInternalSegmentCancel(); secretInternalSegmentCancel();

View File

@ -18,6 +18,8 @@
package baritone.process; package baritone.process;
import baritone.Baritone; 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.Goal;
import baritone.api.pathing.goals.GoalYLevel; import baritone.api.pathing.goals.GoalYLevel;
import baritone.api.pathing.movement.IMovement; import baritone.api.pathing.movement.IMovement;
@ -43,7 +45,7 @@ import net.minecraft.util.math.Vec3d;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.function.Supplier; 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; public State state;
private Goal goal; private Goal goal;
private LegacyElytraBehavior behavior; private LegacyElytraBehavior behavior;
@ -51,7 +53,7 @@ public class ElytraProcess extends BaritoneProcessHelper implements IBaritonePro
private ElytraProcess(Baritone baritone) { private ElytraProcess(Baritone baritone) {
super(baritone); super(baritone);
this.behavior = new LegacyElytraBehavior(baritone, this); this.behavior = new LegacyElytraBehavior(baritone, this);
baritone.getGameEventHandler().registerEventListener(this.behavior); baritone.getGameEventHandler().registerEventListener(this);
} }
public static <T extends IElytraProcess> T create(final Baritone baritone) { public static <T extends IElytraProcess> T create(final Baritone baritone) {
@ -62,7 +64,7 @@ public class ElytraProcess extends BaritoneProcessHelper implements IBaritonePro
@Override @Override
public boolean isActive() { public boolean isActive() {
return behavior.destination != null; return behavior != null;
} }
@Override @Override
@ -176,16 +178,12 @@ public class ElytraProcess extends BaritoneProcessHelper implements IBaritonePro
return new PathingCommand(null, PathingCommandType.CANCEL_AND_SET_GOAL); return new PathingCommand(null, PathingCommandType.CANCEL_AND_SET_GOAL);
} }
@Override
public boolean isTemporary() {
return false;
}
@Override @Override
public void onLostControl() { public void onLostControl() {
this.goal = null; this.goal = null;
this.state = State.START_FLYING; this.state = State.START_FLYING; // TODO: null state?
behavior.cancel(); if (this.behavior != null) this.behavior.cancel();
this.behavior = null;
} }
@ -220,17 +218,22 @@ public class ElytraProcess extends BaritoneProcessHelper implements IBaritonePro
@Override @Override
public void repackChunks() { public void repackChunks() {
behavior.repackChunks(); this.behavior.repackChunks();
} }
@Override @Override
public void pathTo(BlockPos destination) { 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 @Override
public void cancel() { public void cancel() {
behavior.cancel(); if (this.behavior != null) this.behavior.cancel();
this.behavior = null;
} }
@Override @Override
@ -240,7 +243,7 @@ public class ElytraProcess extends BaritoneProcessHelper implements IBaritonePro
@Override @Override
public boolean isSafeToCancel() { public boolean isSafeToCancel() {
return behavior.isSafeToCancel(); return !this.isActive() || !(this.state == State.FLYING || this.state == State.START_FLYING);
} }
public enum State { public enum State {
@ -252,4 +255,38 @@ public class ElytraProcess extends BaritoneProcessHelper implements IBaritonePro
FLYING, FLYING,
LANDING 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);
}
} }

View File

@ -24,7 +24,6 @@ import baritone.api.behavior.look.IAimProcessor;
import baritone.api.behavior.look.ITickableAimProcessor; import baritone.api.behavior.look.ITickableAimProcessor;
import baritone.api.event.events.*; import baritone.api.event.events.*;
import baritone.api.event.events.type.EventState; import baritone.api.event.events.type.EventState;
import baritone.api.event.listener.AbstractGameEventListener;
import baritone.api.pathing.goals.GoalBlock; import baritone.api.pathing.goals.GoalBlock;
import baritone.api.utils.*; import baritone.api.utils.*;
import baritone.pathing.movement.CalculationContext; 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.BlockPos;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk; import net.minecraft.world.chunk.Chunk;
import java.awt.*; 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.fastCeil;
import static baritone.utils.BaritoneMath.fastFloor; 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 Baritone baritone;
private final IPlayerContext ctx; private final IPlayerContext ctx;
@ -76,7 +76,7 @@ public final class LegacyElytraBehavior implements AbstractGameEventListener, H
public List<BetterBlockPos> visiblePath; public List<BetterBlockPos> visiblePath;
// :sunglasses: // :sunglasses:
private NetherPathfinderContext context; private NetherPathfinderContext context; // TODO: make this final
private CompletableFuture<Void> forceResetContext; private CompletableFuture<Void> forceResetContext;
public final PathManager pathManager; public final PathManager pathManager;
private final ElytraProcess process; private final ElytraProcess process;
@ -125,6 +125,8 @@ public final class LegacyElytraBehavior implements AbstractGameEventListener, H
this.process = process; this.process = process;
this.solverExecutor = Executors.newSingleThreadExecutor(); this.solverExecutor = Executors.newSingleThreadExecutor();
this.nextTickBoostCounter = new int[2]; this.nextTickBoostCounter = new int[2];
this.context = new NetherPathfinderContext(Baritone.settings().elytraNetherSeed.value);
} }
public final class PathManager { public final class PathManager {
@ -370,7 +372,6 @@ public final class LegacyElytraBehavior implements AbstractGameEventListener, H
} }
} }
@Override
public void onRenderPass(RenderEvent event) { public void onRenderPass(RenderEvent event) {
final Settings settings = Baritone.settings(); final Settings settings = Baritone.settings();
if (this.visiblePath != null) { 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) { public void onWorldEvent(WorldEvent event) {
if (event.getWorld() != null) { if (event.getWorld() != null) {
if (event.getState() == EventState.PRE) { if (event.getState() == EventState.PRE) {
@ -427,7 +428,6 @@ public final class LegacyElytraBehavior implements AbstractGameEventListener, H
} }
} }
@Override
public void onChunkEvent(ChunkEvent event) { public void onChunkEvent(ChunkEvent event) {
if (event.isPostPopulate() && this.context != null) { if (event.isPostPopulate() && this.context != null) {
final Chunk chunk = ctx.world().getChunk(event.getX(), event.getZ()); 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) { public void onBlockChange(BlockChangeEvent event) {
this.context.queueBlockUpdate(event); this.context.queueBlockUpdate(event);
} }
@Override
public void onReceivePacket(PacketEvent event) { public void onReceivePacket(PacketEvent event) {
if (event.getPacket() instanceof SPacketPlayerPosLook) { if (event.getPacket() instanceof SPacketPlayerPosLook) {
ctx.minecraft().addScheduledTask(() -> { ctx.minecraft().addScheduledTask(() -> {
@ -486,11 +490,6 @@ public final class LegacyElytraBehavior implements AbstractGameEventListener, H
.filter(process -> this.process == process).isPresent(); .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) { public void onTick(final TickEvent event) {
if (event.getType() == TickEvent.Type.OUT) { if (event.getType() == TickEvent.Type.OUT) {
return; return;
@ -629,7 +628,6 @@ public final class LegacyElytraBehavior implements AbstractGameEventListener, H
); );
} }
@Override
public void onPostTick(TickEvent event) { public void onPostTick(TickEvent event) {
if (event.getType() == TickEvent.Type.IN && this.solveNextTick) { 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 // We're at the end of the tick, the player's position likely updated and the closest path node could've

View File

@ -52,7 +52,6 @@ public final class NetherPathfinderContext {
final long context; final long context;
private final long seed; private final long seed;
private final ExecutorService executor; private final ExecutorService executor;
private final AtomicInteger packQueueSize = new AtomicInteger();
public NetherPathfinderContext(long seed) { public NetherPathfinderContext(long seed) {
this.context = NetherPathfinder.newContext(seed); this.context = NetherPathfinder.newContext(seed);