No more setSprinting

This commit is contained in:
Brady 2019-01-18 13:43:19 -06:00
parent 979b8b2663
commit 20b03d0004
No known key found for this signature in database
GPG Key ID: 73A788379A197567
7 changed files with 97 additions and 27 deletions

View File

@ -0,0 +1,42 @@
/*
* 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
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.event.events;
import baritone.api.event.events.type.ManagedPlayerEvent;
import net.minecraft.client.entity.EntityPlayerSP;
/**
* @author Brady
* @since 1/18/2019
*/
public class SprintStateEvent extends ManagedPlayerEvent {
private Boolean state;
public SprintStateEvent(EntityPlayerSP player) {
super(player);
}
public final void setState(boolean state) {
this.state = state;
}
public final Boolean getState() {
return this.state;
}
}

View File

@ -57,6 +57,9 @@ public interface AbstractGameEventListener extends IGameEventListener {
@Override
default void onPlayerRotationMove(RotationMoveEvent event) {}
@Override
default void onPlayerSprintState(SprintStateEvent event) {}
@Override
default void onBlockInteract(BlockInteractEvent event) {}

View File

@ -109,6 +109,14 @@ public interface IGameEventListener {
*/
void onPlayerRotationMove(RotationMoveEvent event);
/**
* Called whenever the sprint keybind state is checked in {@link EntityPlayerSP#onLivingUpdate}
*
* @param event The event
* @see EntityPlayerSP#onLivingUpdate()
*/
void onPlayerSprintState(SprintStateEvent event);
/**
* Called when the local player interacts with a block, whether it is breaking or opening/placing.
*

View File

@ -21,8 +21,10 @@ import baritone.api.BaritoneAPI;
import baritone.api.behavior.IPathingBehavior;
import baritone.api.event.events.ChatEvent;
import baritone.api.event.events.PlayerUpdateEvent;
import baritone.api.event.events.SprintStateEvent;
import baritone.api.event.events.type.EventState;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.entity.player.PlayerCapabilities;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
@ -87,4 +89,18 @@ public class MixinEntityPlayerSP {
IPathingBehavior pathingBehavior = BaritoneAPI.getProvider().getBaritoneForPlayer((EntityPlayerSP) (Object) this).getPathingBehavior();
return !pathingBehavior.isPathing() && capabilities.allowFlying;
}
@Redirect(
method = "onLivingUpdate",
at = @At(
value = "INVOKE",
target = "net/minecraft/client/settings/KeyBinding.isKeyDown()Z"
)
)
private boolean isKeyDown(KeyBinding keyBinding) {
EntityPlayerSP self = (EntityPlayerSP) (Object) this;
SprintStateEvent event = new SprintStateEvent(self);
BaritoneAPI.getProvider().getBaritoneForPlayer(self).getGameEventHandler().onPlayerSprintState(event);
return event.getState() == null ? keyBinding.isKeyDown() : event.getState();
}
}

View File

@ -19,10 +19,7 @@ package baritone.behavior;
import baritone.Baritone;
import baritone.api.behavior.IPathingBehavior;
import baritone.api.event.events.PathEvent;
import baritone.api.event.events.PlayerUpdateEvent;
import baritone.api.event.events.RenderEvent;
import baritone.api.event.events.TickEvent;
import baritone.api.event.events.*;
import baritone.api.pathing.calc.IPath;
import baritone.api.pathing.goals.Goal;
import baritone.api.pathing.goals.GoalXZ;
@ -98,6 +95,13 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior,
dispatchEvents();
}
@Override
public void onPlayerSprintState(SprintStateEvent event) {
if (current != null) {
event.setState(current.isSprinting());
}
}
private void tickPath() {
if (pauseRequestedLastTick && safeToCancel) {
pauseRequestedLastTick = false;

View File

@ -121,6 +121,11 @@ public final class GameEventHandler implements IEventBus, Helper {
listeners.forEach(l -> l.onPlayerRotationMove(event));
}
@Override
public void onPlayerSprintState(SprintStateEvent event) {
listeners.forEach(l -> l.onPlayerSprintState(event));
}
@Override
public void onBlockInteract(BlockInteractEvent event) {
listeners.forEach(l -> l.onBlockInteract(event));

View File

@ -79,6 +79,8 @@ public class PathExecutor implements IPathExecutor, Helper {
private PathingBehavior behavior;
private IPlayerContext ctx;
private boolean sprintNextTick;
public PathExecutor(PathingBehavior behavior, IPath path) {
this.behavior = behavior;
this.ctx = behavior.ctx;
@ -269,7 +271,7 @@ public class PathExecutor implements IPathExecutor, Helper {
onTick();
return true;
} else {
sprintIfRequested();
sprintNextTick = shouldSprintNextTick();
ticksOnCurrent++;
if (ticksOnCurrent > currentMovementOriginalCostEstimate + Baritone.settings().movementTimeoutTicks.get()) {
// only cancel if the total time has exceeded the initial estimate
@ -371,21 +373,17 @@ public class PathExecutor implements IPathExecutor, Helper {
return true;
}
private void sprintIfRequested() {
private boolean shouldSprintNextTick() {
// first and foremost, if allowSprint is off, or if we don't have enough hunger, don't try and sprint
if (!new CalculationContext(behavior.baritone).canSprint) {
behavior.baritone.getInputOverrideHandler().setInputForceState(Input.SPRINT, false);
ctx.player().setSprinting(false);
return;
return false;
}
// if the movement requested sprinting, then we're done
if (behavior.baritone.getInputOverrideHandler().isInputForcedDown(Input.SPRINT)) {
behavior.baritone.getInputOverrideHandler().setInputForceState(Input.SPRINT, false);
if (!ctx.player().isSprinting()) {
ctx.player().setSprinting(true);
}
return;
return true;
}
// we'll take it from here, no need for minecraft to see we're holding down control and sprint for us
@ -397,30 +395,23 @@ public class PathExecutor implements IPathExecutor, Helper {
if (((MovementDescend) current).safeMode()) {
logDebug("Sprinting would be unsafe");
ctx.player().setSprinting(false);
return;
return false;
}
IMovement next = path.movements().get(pathPosition + 1);
if (next instanceof MovementAscend && current.getDirection().up().equals(next.getDirection().down())) {
// a descend then an ascend in the same direction
if (!ctx.player().isSprinting()) {
ctx.player().setSprinting(true);
}
pathPosition++;
// okay to skip clearKeys and / or onChangeInPathPosition here since this isn't possible to repeat, since it's asymmetric
logDebug("Skipping descend to straight ascend");
return;
return true;
}
if (canSprintInto(ctx, current, next)) {
if (ctx.playerFeet().equals(current.getDest())) {
pathPosition++;
onChangeInPathPosition();
}
if (!ctx.player().isSprinting()) {
ctx.player().setSprinting(true);
}
return;
return true;
}
//logDebug("Turning off sprinting " + movement + " " + next + " " + movement.getDirection() + " " + next.getDirection().down() + " " + next.getDirection().down().equals(movement.getDirection()));
}
@ -430,14 +421,11 @@ public class PathExecutor implements IPathExecutor, Helper {
BlockPos center = current.getSrc().up();
if (ctx.player().posY >= center.getY()) { // playerFeet adds 0.1251 to account for soul sand
behavior.baritone.getInputOverrideHandler().setInputForceState(Input.JUMP, false);
if (!ctx.player().isSprinting()) {
ctx.player().setSprinting(true);
}
return;
return true;
}
}
}
ctx.player().setSprinting(false);
return false;
}
private static boolean canSprintInto(IPlayerContext ctx, IMovement current, IMovement next) {
@ -532,4 +520,8 @@ public class PathExecutor implements IPathExecutor, Helper {
public Set<BlockPos> toWalkInto() {
return Collections.unmodifiableSet(toWalkInto);
}
public boolean isSprinting() {
return sprintNextTick;
}
}