Skip to content

Commit

Permalink
Refactor Sprint (#5078)
Browse files Browse the repository at this point in the history
Co-authored-by: Wide_Cat <[email protected]>
  • Loading branch information
machiecodes and Wide-Cat authored Jan 1, 2025
1 parent 967d584 commit 42ab944
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ private boolean modifyMovement(boolean original) {

@WrapWithCondition(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;setSprinting(Z)V", ordinal = 3))
private boolean wrapSetSprinting(ClientPlayerEntity instance, boolean b) {
return !Modules.get().get(Sprint.class).rageSprint();
Sprint s = Modules.get().get(Sprint.class);

return !s.rageSprint() || s.unsprintInWater() && isTouchingWater();
}

// Rotations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,19 @@ private void spawnItemParticles(ItemStack stack, int count, CallbackInfo info) {

@Inject(method = "onEquipStack", at = @At("HEAD"), cancellable = true)
private void onEquipStack(EquipmentSlot slot, ItemStack oldStack, ItemStack newStack, CallbackInfo info) {
if ((Object) this == mc.player && Modules.get().get(OffhandCrash.class).isAntiCrash()) {
if ((Object) this != mc.player) return;

if (Modules.get().get(OffhandCrash.class).isAntiCrash()) {
info.cancel();
}
}

@ModifyArg(method = "swingHand(Lnet/minecraft/util/Hand;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;swingHand(Lnet/minecraft/util/Hand;Z)V"))
private Hand setHand(Hand hand) {
if ((Object) this != mc.player) return hand;

HandView handView = Modules.get().get(HandView.class);
if ((Object) this == mc.player && handView.isActive()) {
if (handView.isActive()) {
if (handView.swingMode.get() == HandView.SwingMode.None) return hand;
return handView.swingMode.get() == HandView.SwingMode.Offhand ? Hand.OFF_HAND : Hand.MAIN_HAND;
}
Expand All @@ -87,12 +91,15 @@ private Hand setHand(Hand hand) {
@ModifyConstant(method = "getHandSwingDuration", constant = @Constant(intValue = 6))
private int getHandSwingDuration(int constant) {
if ((Object) this != mc.player) return constant;

return Modules.get().get(HandView.class).isActive() && mc.options.getPerspective().isFirstPerson() ? Modules.get().get(HandView.class).swingSpeed.get() : constant;
}

@ModifyReturnValue(method = "isGliding", at = @At("RETURN"))
private boolean isGlidingHook(boolean original) {
if ((Object) this == mc.player && Modules.get().get(ElytraFly.class).canPacketEfly()) {
if ((Object) this != mc.player) return original;

if (Modules.get().get(ElytraFly.class).canPacketEfly()) {
return true;
}

Expand Down Expand Up @@ -122,9 +129,7 @@ private boolean hasStatusEffect(boolean original, RegistryEntry<StatusEffect> ef
@ModifyExpressionValue(method = "jump", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;getYaw()F"))
private float modifyGetYaw(float original) {
if ((Object) this != mc.player) return original;

Sprint s = Modules.get().get(Sprint.class);
if (!s.rageSprint() || !s.jumpFix.get()) return original;
if (!Modules.get().get(Sprint.class).rageSprint()) return original;

float forward = Math.signum(mc.player.input.movementForward);
float strafe = 90 * Math.signum(mc.player.input.movementSideways);
Expand All @@ -138,7 +143,8 @@ private float modifyGetYaw(float original) {

@ModifyExpressionValue(method = "jump", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;isSprinting()Z"))
private boolean modifyIsSprinting(boolean original) {
if ((Object) this != mc.player || !Modules.get().get(Sprint.class).rageSprint()) return original;
if ((Object) this != mc.player) return original;
if (!Modules.get().get(Sprint.class).rageSprint()) return original;

// only add the extra velocity if you're actually moving, otherwise you'll jump in place and move forward
return original && (Math.abs(mc.player.input.movementForward) > 1.0E-5F || Math.abs(mc.player.input.movementSideways) > 1.0E-5F);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,58 +30,58 @@ public enum Mode {
}

public final Setting<Mode> mode = sgGeneral.add(new EnumSetting.Builder<Mode>()
.name("speed-mode")
.name("sprint-mode")
.description("What mode of sprinting.")
.defaultValue(Mode.Strict)
.build()
);

public final Setting<Boolean> jumpFix = sgGeneral.add(new BoolSetting.Builder()
.name("jump-fix")
.description("Whether to correct jumping directions.")
.defaultValue(true)
.visible(() -> mode.get() == Mode.Rage)
.build()
);

private final Setting<Boolean> keepSprint = sgGeneral.add(new BoolSetting.Builder()
.name("keep-sprint")
.description("Whether to keep sprinting after attacking an entity.")
.description("Whether to keep sprinting after attacking.")
.defaultValue(false)
.build()
);

private final Setting<Boolean> unsprintOnHit = sgGeneral.add(new BoolSetting.Builder()
.name("unsprint-on-hit")
.description("Whether to stop sprinting when attacking, to ensure you get crits and sweep attacks.")
.description("Whether to stop sprinting before attacking, to ensure you get crits and sweep attacks.")
.defaultValue(false)
.build()
);

private final Setting<Boolean> unsprintInWater = sgGeneral.add(new BoolSetting.Builder()
public final Setting<Boolean> unsprintInWater = sgGeneral.add(new BoolSetting.Builder()
.name("unsprint-in-water")
.description("Whether to stop sprinting when in water.")
.defaultValue(true)
.visible(() -> mode.get() == Mode.Rage)
.build()
);

private final Setting<Boolean> permaSprint = sgGeneral.add(new BoolSetting.Builder()
.name("sprint-while-stationary")
.description("Sprint even when not moving.")
.defaultValue(false)
.visible(() -> mode.get() == Mode.Rage)
.build()
);

public Sprint() {
super(Categories.Movement, "sprint", "Automatically sprints.");
}

@Override
public void onDeactivate() {
mc.player.setSprinting(false);
}

@EventHandler
@EventHandler(priority = EventPriority.HIGH)
private void onTickMovement(TickEvent.Post event) {
if (shouldSprint()) mc.player.setSprinting(true);
if (unsprintInWater.get() && mc.player.isTouchingWater()) return;

mc.player.setSprinting(shouldSprint());
}

@EventHandler(priority = EventPriority.HIGH)
private void onPacketSend(PacketEvent.Send event) {
if (!unsprintOnHit.get() || !(event.packet instanceof IPlayerInteractEntityC2SPacket packet) || packet.meteor$getType() != PlayerInteractEntityC2SPacket.InteractType.ATTACK) return;
if (!unsprintOnHit.get()) return;
if (!(event.packet instanceof IPlayerInteractEntityC2SPacket packet)
|| packet.meteor$getType() != PlayerInteractEntityC2SPacket.InteractType.ATTACK) return;

mc.getNetworkHandler().sendPacket(new ClientCommandC2SPacket(mc.player, ClientCommandC2SPacket.Mode.STOP_SPRINTING));
mc.player.setSprinting(false);
Expand All @@ -90,29 +90,41 @@ private void onPacketSend(PacketEvent.Send event) {
@EventHandler
private void onPacketSent(PacketEvent.Sent event) {
if (!unsprintOnHit.get() || !keepSprint.get()) return;
if (!(event.packet instanceof IPlayerInteractEntityC2SPacket packet) || packet.meteor$getType() != PlayerInteractEntityC2SPacket.InteractType.ATTACK) return;
if (!(event.packet instanceof IPlayerInteractEntityC2SPacket packet)
|| packet.meteor$getType() != PlayerInteractEntityC2SPacket.InteractType.ATTACK) return;

if (shouldSprint() && !mc.player.isSprinting()) {
mc.getNetworkHandler().sendPacket(new ClientCommandC2SPacket(mc.player, ClientCommandC2SPacket.Mode.START_SPRINTING));
mc.player.setSprinting(true);
}
if (!shouldSprint() || mc.player.isSprinting()) return;

mc.getNetworkHandler().sendPacket(new ClientCommandC2SPacket(mc.player, ClientCommandC2SPacket.Mode.START_SPRINTING));
mc.player.setSprinting(true);
}

public boolean shouldSprint() {
if (unsprintInWater.get() && (mc.player.isTouchingWater() || mc.player.isSubmergedInWater())) return false;
if (mc.currentScreen != null && !Modules.get().get(GUIMove.class).sprint.get()) return false;

float movement = mode.get() == Mode.Rage
? (Math.abs(mc.player.input.movementForward) + Math.abs(mc.player.input.movementSideways))
: mc.player.input.movementForward;

if (movement <= (mc.player.isSubmergedInWater() ? 1.0E-5F : 0.8)) {
if (mode.get() == Mode.Strict || !permaSprint.get()) return false;
}

boolean strictSprint = mc.player.forwardSpeed > 1.0E-5F
boolean strictSprint = !(mc.player.isTouchingWater() && !mc.player.isSubmergedInWater())
&& ((ClientPlayerEntityAccessor) mc.player).invokeCanSprint()
&& (!mc.player.horizontalCollision || mc.player.collidedSoftly)
&& !(mc.player.isTouchingWater() && !mc.player.isSubmergedInWater());
&& (!mc.player.horizontalCollision || mc.player.collidedSoftly);

return isActive() && (mode.get() == Mode.Rage || strictSprint) && (mc.currentScreen == null || Modules.get().get(GUIMove.class).sprint.get());
return isActive() && (mode.get() == Mode.Rage || strictSprint);
}

public boolean rageSprint() {
return isActive() && mode.get() == Mode.Rage;
}

public boolean unsprintInWater() {
return isActive() && unsprintInWater.get();
}

public boolean stopSprinting() {
return !isActive() || !keepSprint.get();
}
Expand Down

0 comments on commit 42ab944

Please sign in to comment.