Files
src
build.gradle
gradle.properties
settings.gradle
Code
9 354
src/client/java/com/kodari/lookangle/LookAngleMod.java
package com.kodari.lookangle;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Items;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.RaycastContext;
import org.lwjgl.glfw.GLFW;
import java.util.concurrent.ThreadLocalRandom;
public class LookAngleMod implements ClientModInitializer {
private static boolean hasPreviousItemCounts;
private static int previousPearlCount;
private static int previousWindChargeCount;
private static boolean pearlFreezeArmed;
private static boolean pearlFreezeReached;
private static float pearlFreezeDegrees;
private static boolean waitingForDownwardLook;
private static float pearlThrowDegrees;
private static float postThrowFreezeDegrees;
private static boolean postThrowFreezeActive;
private static int postThrowFreezeTicks;
private static KeyBinding pearlSnapKey;
@Override
public void onInitializeClient() {
pearlSnapKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.lookangle.snap_pearl_angle",
GLFW.GLFW_KEY_Z,
KeyBinding.Category.MISC
));
ClientTickEvents.END_CLIENT_TICK.register(LookAngleMod::tick);
HudElementRegistry.addLast(Identifier.of("lookangle", "angle"), (context, tickCounter) -> {
var cameraEntity = MinecraftClient.getInstance().getCameraEntity();
if (cameraEntity == null) {
return;
}
int degrees = Math.round(90.0f - cameraEntity.getPitch());
String label = "Look: " + degrees + "°";
int x = (context.getScaledWindowWidth() - MinecraftClient.getInstance().textRenderer.getWidth(label)) / 2;
int y = context.getScaledWindowHeight() / 2 + 8;
context.drawText(
MinecraftClient.getInstance().textRenderer,
Text.literal(label),
x,
y,
0xFFFFFFFF,
true
);
});
}
private static void onPearlThrown(PlayerEntity player) {
pearlThrowDegrees = 90.0f - player.getPitch();
pearlFreezeArmed = false;
pearlFreezeReached = false;
waitingForDownwardLook = false;
postThrowFreezeActive = false;
postThrowFreezeTicks = 0;
if (pearlThrowDegrees < 125.0f || pearlThrowDegrees > 145.0f) {
return;
}
postThrowFreezeDegrees = Math.max(0.0f, pearlThrowDegrees - ThreadLocalRandom.current().nextInt(3, 7));
waitingForDownwardLook = false;
postThrowFreezeActive = true;
postThrowFreezeTicks = 10;
freezeAtDegrees(player, postThrowFreezeDegrees);
}
private static void onWindChargeThrown() {
pearlFreezeArmed = false;
pearlFreezeReached = false;
waitingForDownwardLook = false;
postThrowFreezeActive = false;
postThrowFreezeTicks = 0;
}
public static void handleMouseLook(PlayerEntity player, double cursorDeltaX, double cursorDeltaY) {
if (pearlFreezeReached || postThrowFreezeActive) {
return;
}
float currentDegrees = 90.0f - player.getPitch();
float nextDegrees = currentDegrees - (float) cursorDeltaY * 0.15f;
if (pearlFreezeArmed && nextDegrees > currentDegrees && nextDegrees >= pearlFreezeDegrees) {
freezeAtDegrees(player, Math.max(currentDegrees, pearlFreezeDegrees));
pearlFreezeReached = true;
return;
}
if (waitingForDownwardLook && nextDegrees < currentDegrees && nextDegrees <= postThrowFreezeDegrees) {
freezeAtDegrees(player, Math.min(currentDegrees, postThrowFreezeDegrees));
waitingForDownwardLook = false;
postThrowFreezeActive = true;
postThrowFreezeTicks = 10;
return;
}
player.changeLookDirection(cursorDeltaX, cursorDeltaY);
}
public static boolean shouldBlockWindCharge(PlayerEntity player, Hand hand) {
return waitingForDownwardLook && player.getStackInHand(hand).isOf(Items.WIND_CHARGE);
}
private static void freezeAtDegrees(PlayerEntity player, float degrees) {
float targetPitch = 90.0f - degrees;
double clampedDeltaY = (targetPitch - player.getPitch()) / 0.15f;
player.changeLookDirection(0.0, clampedDeltaY);
}
private static boolean isAtLeastTwentyBlocksAboveGround(PlayerEntity player) {
double x = player.getX();
double z = player.getZ();
double y = player.getY();
Vec3d start = new Vec3d(x, y, z);
Vec3d end = new Vec3d(x, y - 20.0, z);
HitResult hit = player.getEntityWorld().raycast(new RaycastContext(
start,
end,
RaycastContext.ShapeType.COLLIDER,
RaycastContext.FluidHandling.NONE,
player
));
return hit.getType() == HitResult.Type.MISS || hit.getPos().y <= end.y;
}
private static void tick(MinecraftClient client) {
boolean snapRequested = false;
while (pearlSnapKey.wasPressed()) {
snapRequested = true;
}
PlayerEntity player = client.player;
Entity cameraEntity = client.getCameraEntity();
if (player == null || cameraEntity == null) {
hasPreviousItemCounts = false;
pearlFreezeArmed = false;
pearlFreezeReached = false;
waitingForDownwardLook = false;
postThrowFreezeActive = false;
postThrowFreezeTicks = 0;
return;
}
int pearlCount = player.getInventory().count(Items.ENDER_PEARL);
int windChargeCount = player.getInventory().count(Items.WIND_CHARGE);
if (hasPreviousItemCounts) {
if (pearlCount < previousPearlCount) {
onPearlThrown(player);
}
if (windChargeCount < previousWindChargeCount) {
onWindChargeThrown();
}
} else {
hasPreviousItemCounts = true;
}
previousPearlCount = pearlCount;
previousWindChargeCount = windChargeCount;
boolean holdingPearl = player.getStackInHand(Hand.MAIN_HAND).isOf(Items.ENDER_PEARL)
|| player.getStackInHand(Hand.OFF_HAND).isOf(Items.ENDER_PEARL);
if (snapRequested && holdingPearl && isAtLeastTwentyBlocksAboveGround(player)) {
pearlFreezeDegrees = ThreadLocalRandom.current().nextInt(125, 146);
pearlFreezeArmed = true;
pearlFreezeReached = true;
freezeAtDegrees(player, pearlFreezeDegrees);
}
if (!holdingPearl) {
pearlFreezeArmed = false;
pearlFreezeReached = false;
} else if (!pearlFreezeArmed && !waitingForDownwardLook && !postThrowFreezeActive) {
float currentDegrees = 90.0f - player.getPitch();
pearlFreezeDegrees = 125.0f;
pearlFreezeArmed = true;
if (currentDegrees >= pearlFreezeDegrees) {
pearlFreezeDegrees = currentDegrees;
pearlFreezeReached = true;
}
}
if (postThrowFreezeActive && --postThrowFreezeTicks <= 0) {
postThrowFreezeActive = false;
}
}