Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
11 261
src/client/java/com/kodari/orbithud/OrbitHudClient.java
package com.kodari.orbithud;
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.client.option.Perspective;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;
public class OrbitHudClient implements ClientModInitializer {
private static KeyBinding freelook;
private static KeyBinding toggleHud;
private static boolean hudEnabled = true;
private static Text fpsText = Text.literal("");
private static Text coordinatesText = Text.literal("");
public static boolean freelookEnabled = true;
public static boolean looking;
public static float yaw;
public static float pitch;
private static Perspective previousPerspective;
@Override
public void onInitializeClient() {
KeyBinding.Category category = KeyBinding.Category.create(Identifier.of("orbit_hud", "controls"));
freelook = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.orbit_hud.freelook", GLFW.GLFW_KEY_LEFT_ALT, category));
toggleHud = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.orbit_hud.toggle_hud", GLFW.GLFW_KEY_F6, category));
ClientTickEvents.END_CLIENT_TICK.register(OrbitHudClient::tick);
HudElementRegistry.addLast(Identifier.of("orbit_hud", "stats"), (context, counter) -> {
MinecraftClient client = MinecraftClient.getInstance();
if (!hudEnabled || client.options.hudHidden || client.player == null) return;
context.drawTextWithShadow(client.textRenderer, fpsText, 5, 5, 0xFFFFFFFF);
context.drawTextWithShadow(client.textRenderer, coordinatesText, 5, 16, 0xFFFFFFFF);
});
}
private static void tick(MinecraftClient client) {
while (toggleHud.wasPressed()) hudEnabled = !hudEnabled;
if (hudEnabled && client.player != null) {
fpsText = Text.literal("FPS: " + client.getCurrentFps());
coordinatesText = Text.literal(String.format(java.util.Locale.ROOT, "XYZ: %.1f %.1f %.1f", client.player.getX(), client.player.getY(), client.player.getZ()));
}
boolean shouldLook = freelookEnabled && client.player != null && client.currentScreen == null && freelook.isPressed();
if (shouldLook && !looking) {
yaw = client.player.getYaw();
pitch = client.player.getPitch();
previousPerspective = client.options.getPerspective();
client.options.setPerspective(Perspective.THIRD_PERSON_BACK);
looking = true;
} else if (!shouldLook && looking) {
looking = false;
client.options.setPerspective(previousPerspective);
}
}
public static void moveLook(double x, double y) {
yaw += (float) (x * 0.15);
pitch = Math.max(-90.0F, Math.min(90.0F, pitch + (float) (y * 0.15)));
}
public static boolean isHudEnabled() {
return hudEnabled;
}
public static void setHudEnabled(boolean enabled) {
hudEnabled = enabled;
}
}