Skip to content

Commit

Permalink
YawLock won't spazout anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
fr1kin committed Oct 11, 2019
1 parent d3b8dd7 commit 2257b61
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 41 deletions.
60 changes: 29 additions & 31 deletions src/main/java/com/matt/forgehax/mods/YawLockMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.matt.forgehax.mods.managers.PositionRotationManager.RotationState;
import com.matt.forgehax.util.SafeConverter;
import com.matt.forgehax.util.command.Setting;
import com.matt.forgehax.util.common.PriorityEnum;
import com.matt.forgehax.util.entity.LocalPlayerUtils;
import com.matt.forgehax.util.math.Angle;
import com.matt.forgehax.util.math.AngleHelper;
Expand All @@ -27,30 +28,33 @@ public class YawLockMod extends ToggleMod
.defaultTo(true)
.build();

public final Setting<Double> angle =
public final Setting<Float> angle =
getCommandStub()
.builders()
.<Double>newSettingBuilder()
.<Float>newSettingBuilder()
.name("angle")
.description("Angle to snap too")
.defaultTo(0.0D)
.min(-180D)
.max(180D)
.defaultTo(0.f)
.min(-180.f)
.max(180.f)
.build();

public YawLockMod() {
super(Category.PLAYER, "YawLock", false, "Locks yaw to prevent moving into walls");
}

private double getYawDirection() {
return (int) (Math.round((LocalPlayerUtils.getViewAngles().getYaw() + 1.f) / 45.f) * 45.f);
private float getYawDirection(float yaw) {
return (int) (Math.round((yaw + 1.f) / 45.f) * 45.f);
}

private Angle getSnapAngle() {
return Angle.degrees(
LocalPlayerUtils.getViewAngles().getPitch(),
auto.get() ? getYawDirection() : angle.get())
.normalize();
Angle va = LocalPlayerUtils.getViewAngles();
return va.setYaw(auto.get() ? getYawDirection(va.getYaw()) : angle.get());
}

@Override
public String getDebugDisplayText() {
return super.getDebugDisplayText() + " [" + String.format("%.4f", getSnapAngle().getYaw()) + "]";
}

@Override
Expand All @@ -60,30 +64,24 @@ protected void onLoad() {
.newCommandBuilder()
.name("snap")
.description("Snap once to a certain direction")
.processor(
data -> {
if (getLocalPlayer() == null || getWorld() == null) {
return;
}

final double angle =
data.getArgumentCount() == 0
? getYawDirection()
: AngleHelper.normalizeInDegrees(
SafeConverter.toDouble(data.getArgumentAsString(0)));

PositionRotationManager.getManager()
.registerTemporary(
state ->
state.setViewAngles(
Angle.degrees(state.getClientAngles().getPitch(), angle)));
})
.processor(data -> MC.addScheduledTask(() -> {
if (getLocalPlayer() == null || getWorld() == null) {
return;
}

final float angle = data.getArgumentCount() == 0 ? getSnapAngle().getPitch()
: AngleHelper.normalizeInDegrees(
Float.parseFloat(data.getArgumentAsString(0)));

PositionRotationManager.getManager().registerTemporary(state
-> state.setViewAngles(Angle.degrees(state.getClientAngles().getPitch(), angle)));
}))
.build();
}

@Override
protected void onEnabled() {
PositionRotationManager.getManager().register(this);
PositionRotationManager.getManager().register(this, PriorityEnum.LOWEST);
}

@Override
Expand All @@ -93,6 +91,6 @@ protected void onDisabled() {

@Override
public void onLocalPlayerMovementUpdate(RotationState.Local state) {
state.setClientAngles(getSnapAngle());
state.setViewAngles(getSnapAngle());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static Angle getPlayerAngles(EntityPlayer player) {

private static void setPlayerAngles(EntityPlayerSP player, Angle angles) {
Angle original = getPlayerAngles(player);
Angle diff = angles.normalize().sub(original.normalize());
Angle diff = angles.normalize().sub(original.normalize()).normalize();
player.rotationPitch = Utils.clamp(original.getPitch() + diff.getPitch(), -90.f, 90.f);
player.rotationYaw = original.getYaw() + diff.getYaw();
}
Expand Down Expand Up @@ -103,6 +103,11 @@ public void onWorldLoad(WorldEvent.Load event) {
gState.setInitialized(false);
}

@SubscribeEvent
public void onWorldUnload(WorldEvent.Unload event) {
gState.setInitialized(false);
}

@SubscribeEvent
public void onMovementUpdatePre(LocalPlayerUpdateMovementEvent.Pre event) {
// updated view angles
Expand Down Expand Up @@ -248,11 +253,6 @@ public void onPacketReceived(PacketEvent.Incoming.Pre event) {
}
}

@SubscribeEvent
public void onWorldUnload(WorldEvent.Unload event) {
gState.setInitialized(false);
}

public interface MovementUpdateListener {

/**
Expand Down Expand Up @@ -296,8 +296,7 @@ default EntityPlayerSP getLocalPlayer() {
* @return angle in degrees
*/
default Angle getRenderClientViewAngles() {
return isActive()
? getClientAngles()
return isActive() ? getClientAngles()
: Angle.degrees(getLocalPlayer().rotationPitch, getLocalPlayer().rotationYaw);
}

Expand All @@ -308,8 +307,7 @@ default Angle getRenderClientViewAngles() {
* @return angle in degrees
*/
default Angle getRenderServerViewAngles() {
return isActive()
? getServerAngles()
return isActive() ? getServerAngles()
: Angle.degrees(getLocalPlayer().rotationPitch, getLocalPlayer().rotationYaw);
}

Expand Down

0 comments on commit 2257b61

Please sign in to comment.