From bde0c620ad6270cbd363140ab6be8aa49370644d Mon Sep 17 00:00:00 2001 From: Brady Date: Wed, 14 Jun 2023 16:11:11 -0500 Subject: [PATCH 1/3] Allow freeLook when using elytra --- .../api/event/events/RotationMoveEvent.java | 38 ++++++++- .../launch/mixins/MixinEntityLivingBase.java | 85 +++++++++++++++---- .../launch/mixins/MixinEntityPlayerSP.java | 16 ---- .../launch/mixins/MixinMinecraft.java | 15 ++++ src/main/java/baritone/Elytra.java | 6 +- .../java/baritone/behavior/LookBehavior.java | 1 + 6 files changed, 121 insertions(+), 40 deletions(-) diff --git a/src/api/java/baritone/api/event/events/RotationMoveEvent.java b/src/api/java/baritone/api/event/events/RotationMoveEvent.java index 109061c7e..a2ab17ed6 100644 --- a/src/api/java/baritone/api/event/events/RotationMoveEvent.java +++ b/src/api/java/baritone/api/event/events/RotationMoveEvent.java @@ -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; } diff --git a/src/launch/java/baritone/launch/mixins/MixinEntityLivingBase.java b/src/launch/java/baritone/launch/mixins/MixinEntityLivingBase.java index 0fd2436c9..f8544dd2f 100644 --- a/src/launch/java/baritone/launch/mixins/MixinEntityLivingBase.java +++ b/src/launch/java/baritone/launch/mixins/MixinEntityLivingBase.java @@ -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 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 getBaritone() { + // noinspection ConstantConditions + if (EntityPlayerSP.class.isInstance(this)) { + return Optional.ofNullable(BaritoneAPI.getProvider().getBaritoneForPlayer((EntityPlayerSP) (Object) this)); + } else { + return Optional.empty(); + } } } diff --git a/src/launch/java/baritone/launch/mixins/MixinEntityPlayerSP.java b/src/launch/java/baritone/launch/mixins/MixinEntityPlayerSP.java index 7c1225b9b..3f34c0e77 100644 --- a/src/launch/java/baritone/launch/mixins/MixinEntityPlayerSP.java +++ b/src/launch/java/baritone/launch/mixins/MixinEntityPlayerSP.java @@ -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( diff --git a/src/launch/java/baritone/launch/mixins/MixinMinecraft.java b/src/launch/java/baritone/launch/mixins/MixinMinecraft.java index 097c72905..edc1e3fcc 100644 --- a/src/launch/java/baritone/launch/mixins/MixinMinecraft.java +++ b/src/launch/java/baritone/launch/mixins/MixinMinecraft.java @@ -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,21 @@ 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) { + baritone.getGameEventHandler().onPlayerUpdate(new PlayerUpdateEvent(EventState.POST)); + } } @Inject( diff --git a/src/main/java/baritone/Elytra.java b/src/main/java/baritone/Elytra.java index 2f724c692..35ea70b95 100644 --- a/src/main/java/baritone/Elytra.java +++ b/src/main/java/baritone/Elytra.java @@ -264,17 +264,19 @@ 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(); +// 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; +// 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; } } diff --git a/src/main/java/baritone/behavior/LookBehavior.java b/src/main/java/baritone/behavior/LookBehavior.java index 99e496e6f..d9a06b086 100644 --- a/src/main/java/baritone/behavior/LookBehavior.java +++ b/src/main/java/baritone/behavior/LookBehavior.java @@ -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()); } } From 91bf7d726b30add13deaf14094a15b58f93c8b70 Mon Sep 17 00:00:00 2001 From: Brady Date: Wed, 14 Jun 2023 16:32:56 -0500 Subject: [PATCH 2/3] Un-scuff and add setting --- src/api/java/baritone/api/Settings.java | 1 + .../java/baritone/launch/mixins/MixinMinecraft.java | 2 ++ src/main/java/baritone/Elytra.java | 4 +--- src/main/java/baritone/behavior/LookBehavior.java | 12 ++++++++---- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/api/java/baritone/api/Settings.java b/src/api/java/baritone/api/Settings.java index 30506fd08..f5cf3c77a 100644 --- a/src/api/java/baritone/api/Settings.java +++ b/src/api/java/baritone/api/Settings.java @@ -53,6 +53,7 @@ public final class Settings { public final Setting elytraFireworkSpeed = new Setting<>(0.6); public final Setting wasteFireworks = new Setting<>(false); public final Setting renderRaytraces = new Setting<>(false); + public final Setting elytraFreeLook = new Setting<>(false); /** * Allow Baritone to break blocks diff --git a/src/launch/java/baritone/launch/mixins/MixinMinecraft.java b/src/launch/java/baritone/launch/mixins/MixinMinecraft.java index edc1e3fcc..19d4741ca 100644 --- a/src/launch/java/baritone/launch/mixins/MixinMinecraft.java +++ b/src/launch/java/baritone/launch/mixins/MixinMinecraft.java @@ -98,6 +98,8 @@ public class MixinMinecraft { 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)); } } diff --git a/src/main/java/baritone/Elytra.java b/src/main/java/baritone/Elytra.java index 35ea70b95..b8a05010b 100644 --- a/src/main/java/baritone/Elytra.java +++ b/src/main/java/baritone/Elytra.java @@ -48,7 +48,7 @@ public class Elytra extends Behavior implements Helper { static { try { - DataInputStream in = new DataInputStream(new FileInputStream(new File("/Users/leijurv/Dropbox/nether-pathfinder/build/test"))); + DataInputStream in = new DataInputStream(new FileInputStream(new File("E:/Brady/Documents/Java/baritone/test"))); int count = in.readInt(); System.out.println("Count: " + count); for (int i = 0; i < count; i++) { @@ -264,7 +264,6 @@ 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) { @@ -272,7 +271,6 @@ public class Elytra extends Behavior implements Helper { 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); diff --git a/src/main/java/baritone/behavior/LookBehavior.java b/src/main/java/baritone/behavior/LookBehavior.java index d9a06b086..1b85ac7e3 100644 --- a/src/main/java/baritone/behavior/LookBehavior.java +++ b/src/main/java/baritone/behavior/LookBehavior.java @@ -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 @@ -186,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 { @@ -207,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; From 06d11c187417374103a6e4b0ed162e1dfbfe4083 Mon Sep 17 00:00:00 2001 From: Brady Date: Wed, 14 Jun 2023 16:59:03 -0500 Subject: [PATCH 3/3] Revert file path --- src/main/java/baritone/Elytra.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/baritone/Elytra.java b/src/main/java/baritone/Elytra.java index b8a05010b..6d12e9f0d 100644 --- a/src/main/java/baritone/Elytra.java +++ b/src/main/java/baritone/Elytra.java @@ -48,7 +48,7 @@ public class Elytra extends Behavior implements Helper { static { try { - DataInputStream in = new DataInputStream(new FileInputStream(new File("E:/Brady/Documents/Java/baritone/test"))); + DataInputStream in = new DataInputStream(new FileInputStream(new File("/Users/leijurv/Dropbox/nether-pathfinder/build/test"))); int count = in.readInt(); System.out.println("Count: " + count); for (int i = 0; i < count; i++) {