baritone/src/main/java/baritone/behavior/LookBehavior.java

123 lines
3.9 KiB
Java
Raw Normal View History

2018-08-08 03:16:53 +00:00
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
2018-08-08 03:16:53 +00:00
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
2018-08-08 03:16:53 +00:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
2018-08-08 03:16:53 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
2018-08-08 03:16:53 +00:00
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.behavior;
2018-08-22 20:15:56 +00:00
import baritone.Baritone;
2018-09-23 23:35:55 +00:00
import baritone.api.Settings;
import baritone.api.behavior.ILookBehavior;
2018-08-28 00:37:21 +00:00
import baritone.api.event.events.PlayerUpdateEvent;
import baritone.api.event.events.RotationMoveEvent;
import baritone.api.utils.Rotation;
2018-11-13 22:33:45 +00:00
public final class LookBehavior extends Behavior implements ILookBehavior {
2018-11-05 22:05:25 +00:00
/**
* Target's values are as follows:
2018-08-10 17:45:13 +00:00
* <p>
* getFirst() -> yaw
* getSecond() -> pitch
*/
2018-08-06 04:42:17 +00:00
private Rotation target;
2018-08-21 23:19:20 +00:00
/**
* Whether or not rotations are currently being forced
*/
private boolean force;
/**
* The last player yaw angle. Used when free looking
*
* @see Settings#freeLook
*/
private float lastYaw;
2018-10-28 01:45:17 +00:00
public LookBehavior(Baritone baritone) {
super(baritone);
}
2018-09-17 03:16:05 +00:00
@Override
2018-08-21 23:19:20 +00:00
public void updateTarget(Rotation target, boolean force) {
2018-08-06 04:42:17 +00:00
this.target = target;
2018-08-21 23:19:20 +00:00
this.force = force || !Baritone.settings().freeLook.get();
}
@Override
public void onPlayerUpdate(PlayerUpdateEvent event) {
2018-09-08 04:32:25 +00:00
if (this.target == null) {
return;
2018-09-08 04:32:25 +00:00
}
// Whether or not we're going to silently set our angles
2018-11-05 22:05:25 +00:00
boolean silent = Baritone.settings().antiCheatCompatibility.get() && !this.force;
switch (event.getState()) {
case PRE: {
if (this.force) {
ctx.player().rotationYaw = this.target.getYaw();
float oldPitch = ctx.player().rotationPitch;
2018-09-25 14:39:59 +00:00
float desiredPitch = this.target.getPitch();
ctx.player().rotationPitch = desiredPitch;
if (desiredPitch == oldPitch) {
nudgeToLevel();
}
this.target = null;
2018-11-05 22:05:25 +00:00
}
if (silent) {
this.lastYaw = ctx.player().rotationYaw;
ctx.player().rotationYaw = this.target.getYaw();
}
break;
}
case POST: {
2018-11-05 22:05:25 +00:00
if (silent) {
ctx.player().rotationYaw = this.lastYaw;
this.target = null;
}
break;
2018-08-10 17:45:13 +00:00
}
2018-09-17 02:50:07 +00:00
default:
break;
2018-08-21 23:19:20 +00:00
}
}
@Override
public void onPlayerRotationMove(RotationMoveEvent event) {
if (this.target != null && !this.force) {
2018-11-05 19:53:26 +00:00
event.setYaw(this.target.getYaw());
// If we have antiCheatCompatibility on, we're going to use the target value later in onPlayerUpdate()
// Also the type has to be MOTION_UPDATE because that is called after JUMP
if (!Baritone.settings().antiCheatCompatibility.get() && event.getType() == RotationMoveEvent.Type.MOTION_UPDATE) {
this.target = null;
2018-08-21 23:19:20 +00:00
}
}
}
2018-08-10 17:45:13 +00:00
2018-11-05 22:05:25 +00:00
/**
* Nudges the player's pitch to a regular level. (Between {@code -20} and {@code 10}, increments are by {@code 1})
*/
2018-08-10 17:45:13 +00:00
private void nudgeToLevel() {
if (ctx.player().rotationPitch < -20) {
ctx.player().rotationPitch++;
} else if (ctx.player().rotationPitch > 10) {
ctx.player().rotationPitch--;
2018-08-10 17:45:13 +00:00
}
}
}