simplify management of elytra state

This commit is contained in:
Babbaj 2023-07-17 22:57:24 -04:00
parent 2f0497756b
commit ff1b3e7c5f
No known key found for this signature in database
GPG Key ID: F044309848A07CAC
8 changed files with 76 additions and 139 deletions

View File

@ -175,8 +175,8 @@ dependencies {
transitive = false
}
launchAnnotationProcessor 'org.spongepowered:mixin:0.8.4-SNAPSHOT:processor'
launchImplementation('dev.babbaj:nether-pathfinder:0.39')
implementation 'dev.babbaj:nether-pathfinder:0.39'
launchImplementation('dev.babbaj:nether-pathfinder:0.41')
implementation 'dev.babbaj:nether-pathfinder:0.41'
testImplementation 'junit:junit:4.12'
}

View File

@ -19,24 +19,21 @@ package baritone.api.process;
import net.minecraft.util.math.BlockPos;
import java.util.concurrent.CompletableFuture;
public interface IElytraProcess extends IBaritoneProcess {
/**
* Marks the nether pathfinder context to be reset when it is safe to do so. Because this operation is not
* immediate, a {@link CompletableFuture} is returned that will complete after the context has been reset.
*
* @return A {@link CompletableFuture} that is completed when the context is reset
*/
@Deprecated
CompletableFuture<Void> resetContext();
void repackChunks();
/**
* @return Where it is currently flying to, null if not active
*/
BlockPos currentDestination();
void pathTo(BlockPos destination);
void cancel();
/**
* Resets the state of the process but will maintain the same destination and will try to keep flying
*/
void resetState();
/**
* @return {@code true} if the native library loaded and elytra is actually usable

View File

@ -85,10 +85,11 @@ public class ElytraCommand extends Command {
final String action = args.getString();
switch (action) {
case "reset": {
elytra.resetContext().whenComplete((result, ex) -> {
logDirect("Context reset, repacking chunks");
elytra.repackChunks();
});
BlockPos destination = elytra.currentDestination();
elytra.onLostControl();
elytra.pathTo(destination);
elytra.repackChunks();
logDirect("Reset state but still flying to same goal");
break;
}
case "repack": {

View File

@ -180,7 +180,6 @@ public class ExecutionControlCommands {
paused[0] = false;
}
baritone.getPathingBehavior().cancelEverything();
baritone.getElytraProcess().cancel(); // TODO: this shouldnt be necessary
logDirect("ok canceled");
}

View File

@ -39,7 +39,7 @@ public class ForceCancelCommand extends Command {
IPathingBehavior pathingBehavior = baritone.getPathingBehavior();
pathingBehavior.cancelEverything();
pathingBehavior.forceCancel();
baritone.getElytraProcess().cancel();
baritone.getElytraProcess().onLostControl(); // is this necessary?
logDirect("ok force canceled");
}

View File

@ -19,6 +19,7 @@ package baritone.process;
import baritone.Baritone;
import baritone.api.event.events.*;
import baritone.api.event.events.type.EventState;
import baritone.api.event.listener.AbstractGameEventListener;
import baritone.api.pathing.goals.Goal;
import baritone.api.pathing.goals.GoalYLevel;
@ -42,9 +43,6 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import java.util.concurrent.*;
import java.util.function.Supplier;
public class ElytraProcess extends BaritoneProcessHelper implements IBaritoneProcess, IElytraProcess, AbstractGameEventListener {
public State state;
private Goal goal;
@ -63,14 +61,32 @@ public class ElytraProcess extends BaritoneProcessHelper implements IBaritonePro
: new NullElytraProcess(baritone));
}
@Override
public boolean isActive() {
return behavior != null;
}
@Override
public void resetState() {
BlockPos destination = this.currentDestination();
this.onLostControl();
this.pathTo(destination);
this.repackChunks();
}
@Override
public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) {
this.behavior.onTick();
final long seedSetting = Baritone.settings().elytraNetherSeed.value;
if (this.behavior != null && seedSetting != behavior.context.getSeed()) {
logDirect("Nether seed changed, recalculating path");
this.resetState();
}
if (this.behavior != null) {
this.behavior.onTick();
}
if (calcFailed) {
onLostControl();
logDirect("Failed to get to jump off spot, canceling");
@ -184,38 +200,13 @@ public class ElytraProcess extends BaritoneProcessHelper implements IBaritonePro
public void onLostControl() {
this.goal = null;
this.state = State.START_FLYING; // TODO: null state?
if (this.behavior != null) this.behavior.cancel();
if (this.behavior != null) this.behavior.destroy();
this.behavior = null;
}
@Override
public String displayName0() {
final Supplier<String> status = () -> {
switch (this.state) {
case LOCATE_JUMP:
return "Finding spot to jump off";
case PAUSE:
return "Waiting for elytra path";
case GET_TO_JUMP:
return "Walking to takeoff";
case START_FLYING:
return "Begin flying";
case FLYING:
return "Flying";
case LANDING:
return "Landing";
default:
return "Unknown";
}
};
return "Elytra - " + status.get();
}
@Override
public CompletableFuture<Void> resetContext() {
return behavior.resetContext();
return "Elytra - " + this.state.description;
}
@Override
@ -223,6 +214,11 @@ public class ElytraProcess extends BaritoneProcessHelper implements IBaritonePro
this.behavior.repackChunks();
}
@Override
public BlockPos currentDestination() {
return this.behavior != null ? this.behavior.destination : null;
}
@Override
public void pathTo(BlockPos destination) {
this.behavior = new LegacyElytraBehavior(this.baritone, this);
@ -232,12 +228,6 @@ public class ElytraProcess extends BaritoneProcessHelper implements IBaritonePro
this.behavior.pathTo(destination);
}
@Override
public void cancel() {
if (this.behavior != null) this.behavior.cancel();
this.behavior = null;
}
@Override
public boolean isLoaded() {
return true;
@ -249,13 +239,19 @@ public class ElytraProcess extends BaritoneProcessHelper implements IBaritonePro
}
public enum State {
LOCATE_JUMP,
VALIDATE_PATH,
PAUSE,
GET_TO_JUMP,
START_FLYING,
FLYING,
LANDING
LOCATE_JUMP("Finding spot to jump off"),
VALIDATE_PATH("Validating path"),
PAUSE("Waiting for elytra path"),
GET_TO_JUMP("Walking to takeoff"),
START_FLYING("Begin flying"),
FLYING("Flying"),
LANDING("Landing");
public String description;
State(String desc) {
this.description = desc;
}
}
@Override
@ -265,7 +261,13 @@ public class ElytraProcess extends BaritoneProcessHelper implements IBaritonePro
@Override
public void onWorldEvent(WorldEvent event) {
if (this.behavior != null) this.behavior.onWorldEvent(event);
if (event.getWorld() != null && event.getState() == EventState.POST) {
// Exiting the world, just destroy
if (this.behavior != null) {
this.behavior.destroy();
this.behavior = new LegacyElytraBehavior(baritone, this);
}
}
}
@Override

View File

@ -23,7 +23,6 @@ import baritone.api.Settings;
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.GoalBlock;
import baritone.api.utils.*;
import baritone.pathing.movement.CalculationContext;
@ -50,7 +49,6 @@ 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.*;
@ -76,8 +74,7 @@ public final class LegacyElytraBehavior implements Helper {
public List<BetterBlockPos> visiblePath;
// :sunglasses:
private NetherPathfinderContext context; // TODO: make this final
private CompletableFuture<Void> forceResetContext;
public final NetherPathfinderContext context; // TODO: make this final
public final PathManager pathManager;
private final ElytraProcess process;
@ -102,8 +99,8 @@ public final class LegacyElytraBehavior implements Helper {
private final int[] nextTickBoostCounter;
private BlockStateInterface bsi;
private BlockStateOctreeInterface boi;
public BlockPos destination;
private final BlockStateOctreeInterface boi;
public BlockPos destination; // TODO: make this final?
private final ExecutorService solverExecutor;
private Future<Solution> solver;
@ -127,6 +124,7 @@ public final class LegacyElytraBehavior implements Helper {
this.nextTickBoostCounter = new int[2];
this.context = new NetherPathfinderContext(Baritone.settings().elytraNetherSeed.value);
this.boi = new BlockStateOctreeInterface(context);
}
public final class PathManager {
@ -411,24 +409,6 @@ public final class LegacyElytraBehavior implements Helper {
}
}
// TODO: move this logic to ElytraProcess
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.resetContext();
}
} 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;
}
}
}
}
public void onChunkEvent(ChunkEvent event) {
if (event.isPostPopulate() && this.context != null) {
final Chunk chunk = ctx.world().getChunk(event.getX(), event.getZ());
@ -436,12 +416,6 @@ public final class LegacyElytraBehavior implements Helper {
}
}
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);
}
@ -461,24 +435,11 @@ public final class LegacyElytraBehavior implements Helper {
}
}
public void cancel() {
this.destination = null;
this.pathManager.clear();
this.remainingFireworkTicks = 0;
this.remainingSetBackTicks = 0;
public void destroy() {
if (this.solver != null) {
this.solver.cancel(true);
this.solver = null;
}
this.pendingSolution = null;
Arrays.fill(this.nextTickBoostCounter, 0);
}
public CompletableFuture<Void> resetContext() {
if (this.forceResetContext == null) {
this.forceResetContext = new CompletableFuture<>();
}
return this.forceResetContext;
this.context.destroy();
}
public void repackChunks() {
@ -486,11 +447,6 @@ public final class LegacyElytraBehavior implements Helper {
.forEach(this.context::queueForPacking);
}
public boolean isActive() {
return baritone.getPathingControlManager().mostRecentInControl()
.filter(process -> this.process == process).isPresent();
}
public void onTick() {
// Fetch the previous solution, regardless of if it's going to be used
this.pendingSolution = null;
@ -504,23 +460,6 @@ public final class LegacyElytraBehavior implements Helper {
}
}
// Setup/reset context
final long netherSeed = Baritone.settings().elytraNetherSeed.value;
if (this.context == null || this.context.getSeed() != netherSeed || this.forceResetContext != null) {
if (this.context != null) {
this.context.destroy();
}
this.context = new NetherPathfinderContext(netherSeed);
if (this.forceResetContext != null) {
this.forceResetContext.complete(null);
this.forceResetContext = null;
}
if (this.context.getSeed() != netherSeed && this.isActive()) {
logDirect("Nether seed changed, recalculating path");
this.pathManager.pathToDestination();
}
}
tickInventoryTransactions();
// Certified mojang employee incident
@ -554,7 +493,6 @@ public final class LegacyElytraBehavior implements Helper {
// ctx AND context???? :DDD
this.bsi = new BlockStateInterface(ctx);
this.boi = new BlockStateOctreeInterface(context);
this.pathManager.tick();
final int playerNear = this.pathManager.getNear();

View File

@ -35,13 +35,13 @@ public final class NullElytraProcess extends BaritoneProcessHelper implements IE
}
@Override
public CompletableFuture<Void> resetContext() {
throw new UnsupportedOperationException("Called resetContext() on NullElytraBehavior");
public void repackChunks() {
throw new UnsupportedOperationException("Called repackChunks() on NullElytraBehavior");
}
@Override
public void repackChunks() {
throw new UnsupportedOperationException("Called repackChunks() on NullElytraBehavior");
public BlockPos currentDestination() {
return null;
}
@Override
@ -50,8 +50,8 @@ public final class NullElytraProcess extends BaritoneProcessHelper implements IE
}
@Override
public void cancel() {
throw new UnsupportedOperationException("Called cancel() on NullElytraBehavior");
public void resetState() {
}
@Override