Merge pull request #3990 from cabaletta/elytra-freelook

Merge `elytra-freelook` into `elytra`
This commit is contained in:
leijurv
2023-06-15 20:00:45 -07:00
committed by GitHub
7 changed files with 130 additions and 44 deletions

View File

@@ -53,6 +53,7 @@ public final class Settings {
public final Setting<Double> elytraFireworkSpeed = new Setting<>(0.6);
public final Setting<Boolean> wasteFireworks = new Setting<>(false);
public final Setting<Boolean> renderRaytraces = new Setting<>(false);
public final Setting<Boolean> elytraFreeLook = new Setting<>(false);
/**
* Allow Baritone to break blocks

View File

@@ -17,6 +17,7 @@
package baritone.api.event.events;
import baritone.api.utils.Rotation;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
@@ -31,14 +32,27 @@ public final class RotationMoveEvent {
*/
private final Type type;
private final Rotation original;
/**
* The yaw rotation
*/
private float yaw;
public RotationMoveEvent(Type type, float yaw) {
/**
* The pitch rotation
*/
private float pitch;
public RotationMoveEvent(Type type, float yaw, float pitch) {
this.type = type;
this.original = new Rotation(yaw, pitch);
this.yaw = yaw;
this.pitch = pitch;
}
public Rotation getOriginal() {
return this.original;
}
/**
@@ -46,21 +60,37 @@ public final class RotationMoveEvent {
*
* @param yaw Yaw rotation
*/
public final void setYaw(float yaw) {
public void setYaw(float yaw) {
this.yaw = yaw;
}
/**
* @return The yaw rotation
*/
public final float getYaw() {
public float getYaw() {
return this.yaw;
}
/**
* Set the pitch movement rotation
*
* @param pitch Pitch rotation
*/
public void setPitch(float pitch) {
this.pitch = pitch;
}
/**
* @return The pitch rotation
*/
public float getPitch() {
return pitch;
}
/**
* @return The type of the event
*/
public final Type getType() {
public Type getType() {
return this.type;
}

View File

@@ -25,11 +25,14 @@ import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.Optional;
import static org.spongepowered.asm.lib.Opcodes.GETFIELD;
/**
@@ -42,11 +45,14 @@ public abstract class MixinEntityLivingBase extends Entity {
/**
* Event called to override the movement direction when jumping
*/
@Unique
private RotationMoveEvent jumpRotationEvent;
public MixinEntityLivingBase(World worldIn, RotationMoveEvent jumpRotationEvent) {
@Unique
private RotationMoveEvent elytraRotationEvent;
public MixinEntityLivingBase(World worldIn) {
super(worldIn);
this.jumpRotationEvent = jumpRotationEvent;
}
@Inject(
@@ -54,14 +60,10 @@ public abstract class MixinEntityLivingBase extends Entity {
at = @At("HEAD")
)
private void preMoveRelative(CallbackInfo ci) {
// noinspection ConstantConditions
if (EntityPlayerSP.class.isInstance(this)) {
IBaritone baritone = BaritoneAPI.getProvider().getBaritoneForPlayer((EntityPlayerSP) (Object) this);
if (baritone != null) {
this.jumpRotationEvent = new RotationMoveEvent(RotationMoveEvent.Type.JUMP, this.rotationYaw);
baritone.getGameEventHandler().onPlayerRotationMove(this.jumpRotationEvent);
}
}
this.getBaritone().ifPresent(baritone -> {
this.jumpRotationEvent = new RotationMoveEvent(RotationMoveEvent.Type.JUMP, this.rotationYaw, this.rotationPitch);
baritone.getGameEventHandler().onPlayerRotationMove(this.jumpRotationEvent);
});
}
@Redirect(
@@ -79,6 +81,38 @@ public abstract class MixinEntityLivingBase extends Entity {
return self.rotationYaw;
}
@Inject(
method = "travel",
at = @At(
value = "INVOKE",
target = "net/minecraft/entity/EntityLivingBase.getLookVec()Lnet/minecraft/util/math/Vec3d;"
)
)
private void onPreElytraMove(float strafe, float vertical, float forward, CallbackInfo ci) {
this.getBaritone().ifPresent(baritone -> {
this.elytraRotationEvent = new RotationMoveEvent(RotationMoveEvent.Type.MOTION_UPDATE, this.rotationYaw, this.rotationPitch);
baritone.getGameEventHandler().onPlayerRotationMove(this.elytraRotationEvent);
this.rotationYaw = this.elytraRotationEvent.getYaw();
this.rotationPitch = this.elytraRotationEvent.getPitch();
});
}
@Inject(
method = "travel",
at = @At(
value = "INVOKE",
target = "net/minecraft/entity/EntityLivingBase.move(Lnet/minecraft/entity/MoverType;DDD)V",
shift = At.Shift.AFTER
)
)
private void onPostElytraMove(float strafe, float vertical, float forward, CallbackInfo ci) {
if (this.elytraRotationEvent != null) {
this.rotationYaw = this.elytraRotationEvent.getOriginal().getYaw();
this.rotationPitch = this.elytraRotationEvent.getOriginal().getPitch();
this.elytraRotationEvent = null;
}
}
@Redirect(
method = "travel",
at = @At(
@@ -86,17 +120,32 @@ public abstract class MixinEntityLivingBase extends Entity {
target = "net/minecraft/entity/EntityLivingBase.moveRelative(FFFF)V"
)
)
private void travel(EntityLivingBase self, float strafe, float up, float forward, float friction) {
// noinspection ConstantConditions
if (!EntityPlayerSP.class.isInstance(this) || BaritoneAPI.getProvider().getBaritoneForPlayer((EntityPlayerSP) (Object) this) == null) {
private void onMoveRelative(EntityLivingBase self, float strafe, float up, float forward, float friction) {
Optional<IBaritone> baritone = this.getBaritone();
if (!baritone.isPresent()) {
moveRelative(strafe, up, forward, friction);
return;
}
RotationMoveEvent motionUpdateRotationEvent = new RotationMoveEvent(RotationMoveEvent.Type.MOTION_UPDATE, this.rotationYaw);
BaritoneAPI.getProvider().getBaritoneForPlayer((EntityPlayerSP) (Object) this).getGameEventHandler().onPlayerRotationMove(motionUpdateRotationEvent);
float originalYaw = this.rotationYaw;
this.rotationYaw = motionUpdateRotationEvent.getYaw();
RotationMoveEvent event = new RotationMoveEvent(RotationMoveEvent.Type.MOTION_UPDATE, this.rotationYaw, this.rotationPitch);
baritone.get().getGameEventHandler().onPlayerRotationMove(event);
this.rotationYaw = event.getYaw();
this.rotationPitch = event.getPitch();
this.moveRelative(strafe, up, forward, friction);
this.rotationYaw = originalYaw;
this.rotationYaw = event.getOriginal().getYaw();
this.rotationPitch = event.getOriginal().getPitch();
}
@Unique
private Optional<IBaritone> getBaritone() {
// noinspection ConstantConditions
if (EntityPlayerSP.class.isInstance(this)) {
return Optional.ofNullable(BaritoneAPI.getProvider().getBaritoneForPlayer((EntityPlayerSP) (Object) this));
} else {
return Optional.empty();
}
}
}

View File

@@ -73,22 +73,6 @@ public class MixinEntityPlayerSP {
}
}
@Inject(
method = "onUpdate",
at = @At(
value = "INVOKE",
target = "net/minecraft/client/entity/EntityPlayerSP.onUpdateWalkingPlayer()V",
shift = At.Shift.BY,
by = 2
)
)
private void onPostUpdate(CallbackInfo ci) {
IBaritone baritone = BaritoneAPI.getProvider().getBaritoneForPlayer((EntityPlayerSP) (Object) this);
if (baritone != null) {
baritone.getGameEventHandler().onPlayerUpdate(new PlayerUpdateEvent(EventState.POST));
}
}
@Redirect(
method = "onLivingUpdate",
at = @At(

View File

@@ -20,6 +20,7 @@ package baritone.launch.mixins;
import baritone.api.BaritoneAPI;
import baritone.api.IBaritone;
import baritone.api.event.events.BlockInteractEvent;
import baritone.api.event.events.PlayerUpdateEvent;
import baritone.api.event.events.TickEvent;
import baritone.api.event.events.WorldEvent;
import baritone.api.event.events.type.EventState;
@@ -84,7 +85,23 @@ public class MixinMinecraft {
baritone.getGameEventHandler().onTick(tickProvider.apply(EventState.PRE, type));
}
}
@Inject(
method = "runTick",
at = @At(
value = "INVOKE",
target = "net/minecraft/client/multiplayer/WorldClient.updateEntities()V",
shift = At.Shift.AFTER
)
)
private void postUpdateEntities(CallbackInfo ci) {
IBaritone baritone = BaritoneAPI.getProvider().getBaritoneForPlayer(this.player);
if (baritone != null) {
// Intentionally call this after all entities have been updated. That way, any modification to rotations
// can be recognized by other entity code. (Fireworks and Pigs, for example)
baritone.getGameEventHandler().onPlayerUpdate(new PlayerUpdateEvent(EventState.POST));
}
}
@Inject(

View File

@@ -256,17 +256,17 @@ public class Elytra extends Behavior implements Helper {
}
if (requireClear ? isClear(start, dest) : clearView(start, dest)) {
Rotation rot = RotationUtils.calcRotationFromVec3d(start, dest, ctx.playerRotations());
ctx.player().rotationYaw = rot.getYaw();
long a = System.currentTimeMillis();
Float pitch = solvePitch(dest.subtract(start), steps, relaxation == 2);
if (pitch == null) {
baritone.getLookBehavior().updateTarget(new Rotation(rot.getYaw(), ctx.playerRotations().getPitch()), false);
continue;
}
long b = System.currentTimeMillis();
ctx.player().rotationPitch = pitch;
System.out.println("Solved pitch in " + (b - a) + " total time " + (b - t));
goingTo = i;
goal = path.get(i).add(0, dy, 0);
baritone.getLookBehavior().updateTarget(new Rotation(rot.getYaw(), pitch), false);
return;
}
}

View File

@@ -56,7 +56,7 @@ public final class LookBehavior extends Behavior implements ILookBehavior {
@Override
public void updateTarget(Rotation rotation, boolean blockInteract) {
this.target = new Target(rotation, blockInteract);
this.target = new Target(rotation, Target.Mode.resolve(ctx, blockInteract));
}
@Override
@@ -149,6 +149,7 @@ public final class LookBehavior extends Behavior implements ILookBehavior {
public void onPlayerRotationMove(RotationMoveEvent event) {
if (this.target != null) {
event.setYaw(this.target.rotation.getYaw());
event.setPitch(this.target.rotation.getPitch());
}
}
@@ -185,9 +186,9 @@ public final class LookBehavior extends Behavior implements ILookBehavior {
public final Rotation rotation;
public final Mode mode;
public Target(Rotation rotation, boolean blockInteract) {
public Target(Rotation rotation, Mode mode) {
this.rotation = rotation;
this.mode = Mode.resolve(blockInteract);
this.mode = mode;
}
enum Mode {
@@ -206,12 +207,16 @@ public final class LookBehavior extends Behavior implements ILookBehavior {
*/
NONE;
static Mode resolve(boolean blockInteract) {
static Mode resolve(IPlayerContext ctx, boolean blockInteract) {
final Settings settings = Baritone.settings();
final boolean antiCheat = settings.antiCheatCompatibility.value;
final boolean blockFreeLook = settings.blockFreeLook.value;
final boolean freeLook = settings.freeLook.value;
if (ctx.player().isElytraFlying()) {
return settings.elytraFreeLook.value ? SERVER : CLIENT;
}
if (!freeLook && !blockFreeLook) return CLIENT;
if (!blockFreeLook && blockInteract) return CLIENT;