Added free look option

This commit is contained in:
Brady 2018-08-21 18:19:20 -05:00
parent 843bc17777
commit c96b9cb6af
No known key found for this signature in database
GPG Key ID: 73A788379A197567
10 changed files with 174 additions and 12 deletions

View File

@ -202,6 +202,11 @@ public class Settings {
*/
public Setting<Boolean> fadePath = new Setting<>(false);
/**
* Move without having to force the client-sided rotations
*/
public Setting<Boolean> freeLook = new Setting<>(true);
public final Map<String, Setting<?>> byLowerName;
public final List<Setting<?>> allSettings;
@ -210,6 +215,7 @@ public class Settings {
private String name;
private final Class<T> klass;
@SuppressWarnings("unchecked")
private Setting(T value) {
if (value == null) {
throw new IllegalArgumentException("Cannot determine value type class from null");
@ -218,6 +224,7 @@ public class Settings {
this.klass = (Class<T>) value.getClass();
}
@SuppressWarnings("unchecked")
public final <K extends T> K get() {
return (K) value;
}
@ -244,7 +251,7 @@ public class Settings {
try {
for (Field field : temp) {
if (field.getType().equals(Setting.class)) {
Setting<?> setting = (Setting<? extends Object>) field.get(this);
Setting<?> setting = (Setting<?>) field.get(this);
String name = field.getName();
setting.name = name;
name = name.toLowerCase();
@ -262,6 +269,7 @@ public class Settings {
allSettings = Collections.unmodifiableList(tmpAll);
}
@SuppressWarnings("unchecked")
public <T> List<Setting<T>> getByValueType(Class<T> klass) {
ArrayList<Setting<T>> result = new ArrayList<>();
for (Setting<?> setting : allSettings) {

View File

@ -17,8 +17,11 @@
package baritone.bot.behavior.impl;
import baritone.bot.Baritone;
import baritone.bot.Settings;
import baritone.bot.behavior.Behavior;
import baritone.bot.event.events.PlayerUpdateEvent;
import baritone.bot.event.events.RelativeMoveEvent;
import baritone.bot.event.events.type.EventState;
import baritone.bot.utils.Rotation;
@ -36,21 +39,50 @@ public class LookBehavior extends Behavior {
*/
private Rotation target;
public void updateTarget(Rotation target) {
/**
* 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;
public void updateTarget(Rotation target, boolean force) {
this.target = target;
this.force = force || !Baritone.settings().freeLook.get();
}
@Override
public void onPlayerUpdate(PlayerUpdateEvent event) {
if (event.getState() == EventState.PRE && target != null) {
player().rotationYaw = target.getFirst();
if (event.getState() == EventState.PRE && this.target != null && this.force) {
player().rotationYaw = this.target.getFirst();
float oldPitch = player().rotationPitch;
float desiredPitch = target.getSecond();
float desiredPitch = this.target.getSecond();
player().rotationPitch = desiredPitch;
if (desiredPitch == oldPitch) {
nudgeToLevel();
}
target = null;
this.target = null;
}
}
@Override
public void onPlayerRelativeMove(RelativeMoveEvent event) {
if (this.target != null && !force) {
switch (event.getState()) {
case PRE:
this.lastYaw = player().rotationYaw;
player().rotationYaw = this.target.getFirst();
break;
case POST:
player().rotationYaw = this.lastYaw;
this.target = null;
break;
}
}
}

View File

@ -156,17 +156,22 @@ public final class GameEventHandler implements IGameEventListener, Helper {
@Override
public final void onSendPacket(PacketEvent event) {
dispatch(behavior -> behavior.onSendPacket(event));
dispatch(listener -> listener.onSendPacket(event));
}
@Override
public final void onReceivePacket(PacketEvent event) {
dispatch(behavior -> behavior.onReceivePacket(event));
dispatch(listener -> listener.onReceivePacket(event));
}
@Override
public final void onQueryItemSlotForBlocks(ItemSlotEvent event) {
dispatch(behavior -> behavior.onQueryItemSlotForBlocks(event));
dispatch(listener -> listener.onQueryItemSlotForBlocks(event));
}
@Override
public void onPlayerRelativeMove(RelativeMoveEvent event) {
dispatch(listener -> listener.onPlayerRelativeMove(event));
}
public final void registerEventListener(IGameEventListener listener) {

View File

@ -40,5 +40,4 @@ public final class PlayerUpdateEvent {
public final EventState getState() {
return this.state;
}
}

View File

@ -0,0 +1,43 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* 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,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.bot.event.events;
import baritone.bot.event.events.type.EventState;
/**
* @author Brady
* @since 8/21/2018
*/
public final class RelativeMoveEvent {
/**
* The state of the event
*/
private final EventState state;
public RelativeMoveEvent(EventState state) {
this.state = state;
}
/**
* @return The state of the event
*/
public final EventState getState() {
return this.state;
}
}

View File

@ -77,4 +77,7 @@ public interface AbstractGameEventListener extends IGameEventListener {
@Override
default void onQueryItemSlotForBlocks(ItemSlotEvent event) {}
@Override
default void onPlayerRelativeMove(RelativeMoveEvent event) {}
}

View File

@ -42,6 +42,7 @@ import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.client.renderer.EntityRenderer;
import net.minecraft.client.settings.GameSettings;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
@ -60,7 +61,8 @@ public interface IGameEventListener {
void onTick(TickEvent event);
/**
* Run once per game tick from before the player rotation is sent to the server.
* Run once per game tick from before and after the player rotation is sent to the server.
*
* @see EntityPlayerSP#onUpdate()
*/
void onPlayerUpdate(PlayerUpdateEvent event);
@ -123,4 +125,11 @@ public interface IGameEventListener {
* @see InventoryPlayer#canHarvestBlock(IBlockState)
*/
void onQueryItemSlotForBlocks(ItemSlotEvent event);
/**
* Run once per game tick from before and after the player's moveRelative method is called
*
* @see Entity#moveRelative(float, float, float, float)
*/
void onPlayerRelativeMove(RelativeMoveEvent event);
}

View File

@ -105,7 +105,13 @@ public abstract class Movement implements Helper, MovementHelper {
if (BlockStateInterface.isLiquid(playerFeet())) {
latestState.setInput(Input.JUMP, true);
}
latestState.getTarget().getRotation().ifPresent(LookBehavior.INSTANCE::updateTarget);
// If the movement target has to force the new rotations, or we aren't using silent move, then force the rotations
latestState.getTarget().getRotation().ifPresent(rotation ->
LookBehavior.INSTANCE.updateTarget(
rotation,
latestState.getTarget().hasToForceRotations()));
// TODO: calculate movement inputs from latestState.getGoal().position
// latestState.getTarget().position.ifPresent(null); NULL CONSUMER REALLY SHOULDN'T BE THE FINAL THING YOU SHOULD REALLY REPLACE THIS WITH ALMOST ACTUALLY ANYTHING ELSE JUST PLEASE DON'T LEAVE IT AS IT IS THANK YOU KANYE
latestState.getInputStates().forEach((input, forced) -> {

View File

@ -0,0 +1,56 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* 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,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.launch.mixins;
import baritone.bot.Baritone;
import baritone.bot.event.events.RelativeMoveEvent;
import baritone.bot.event.events.type.EventState;
import baritone.bot.utils.Helper;
import net.minecraft.entity.Entity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
/**
* @author Brady
* @since 8/21/2018
*/
@Mixin(Entity.class)
public class MixinEntity {
@Inject(
method = "moveRelative",
at = @At("HEAD")
)
private void preMoveRelative(float strafe, float up, float forward, float friction, CallbackInfo ci) {
Entity _this = (Entity) (Object) this;
if (_this == Helper.mc.player)
Baritone.INSTANCE.getGameEventHandler().onPlayerRelativeMove(new RelativeMoveEvent(EventState.PRE));
}
@Inject(
method = "moveRelative",
at = @At("RETURN")
)
private void postMoveRelative(float strafe, float up, float forward, float friction, CallbackInfo ci) {
Entity _this = (Entity) (Object) this;
if (_this == Helper.mc.player)
Baritone.INSTANCE.getGameEventHandler().onPlayerRelativeMove(new RelativeMoveEvent(EventState.POST));
}
}

View File

@ -8,6 +8,7 @@
"maxShiftBy": 2
},
"client": [
"MixinEntity",
"MixinEntityPlayerSP",
"MixinEntityRenderer",
"MixinGameSettings",