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

129 lines
4.1 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;
import baritone.Settings;
2018-09-17 02:50:07 +00:00
import baritone.api.behavior.Behavior;
2018-08-28 00:37:21 +00:00
import baritone.api.event.events.PlayerUpdateEvent;
import baritone.api.event.events.RotationMoveEvent;
2018-09-12 22:53:29 +00:00
import baritone.utils.Helper;
2018-08-22 20:15:56 +00:00
import baritone.utils.Rotation;
public final class LookBehavior extends Behavior implements Helper {
public static final LookBehavior INSTANCE = new LookBehavior();
/**
* 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-09-17 03:16:05 +00:00
private LookBehavior() {}
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
boolean silent = Baritone.settings().antiCheatCompatibility.get();
switch (event.getState()) {
case PRE: {
if (this.force) {
player().rotationYaw = this.target.getFirst();
float oldPitch = player().rotationPitch;
float desiredPitch = this.target.getSecond();
player().rotationPitch = desiredPitch;
if (desiredPitch == oldPitch) {
nudgeToLevel();
}
this.target = null;
} else if (silent) {
this.lastYaw = player().rotationYaw;
player().rotationYaw = this.target.getFirst();
}
break;
}
case POST: {
if (!this.force && silent) {
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
}
2018-09-17 02:56:33 +00:00
new Thread().start();
2018-08-21 23:19:20 +00:00
}
@Override
public void onPlayerRotationMove(RotationMoveEvent event) {
if (this.target != null && !this.force) {
2018-08-21 23:19:20 +00:00
switch (event.getState()) {
case PRE:
this.lastYaw = player().rotationYaw;
player().rotationYaw = this.target.getFirst();
break;
case POST:
player().rotationYaw = this.lastYaw;
// 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-09-08 04:32:25 +00:00
}
2018-08-21 23:19:20 +00:00
break;
2018-09-17 02:56:33 +00:00
default:
break;
2018-08-21 23:19:20 +00:00
}
}
}
2018-08-10 17:45:13 +00:00
private void nudgeToLevel() {
if (player().rotationPitch < -20) {
player().rotationPitch++;
} else if (player().rotationPitch > 10) {
player().rotationPitch--;
}
}
}