No chat to show

This project's files are all here to explore.

Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 218
src/main/java/dev/kodari/prismhud/PrismHud.java
package dev.kodari.prismhud;

import net.fabricmc.api.ModInitializer;
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.HudRenderCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.network.ClientPlayerEntity;

import java.util.Locale;

public class PrismHud implements ModInitializer {
    private static final String CATEGORY = "key.categories.prismhud";
    private static final KeyBinding TOGGLE_HUD = new KeyBinding("key.prismhud.toggle_hud", 72, CATEGORY);
    private static final KeyBinding TOGGLE_INFO = new KeyBinding("key.prismhud.toggle_info", 73, CATEGORY);
    private static final KeyBinding TOGGLE_STATUS = new KeyBinding("key.prismhud.toggle_status", 79, CATEGORY);
    private static final KeyBinding TOGGLE_CLOCK = new KeyBinding("key.prismhud.toggle_clock", 67, CATEGORY);
    private static final KeyBinding TOGGLE_KEYS = new KeyBinding("key.prismhud.toggle_keys", 75, CATEGORY);

    private static final int PANEL = 0xD9101520;
    private static final int TEXT = 0xFFF1F4FA;
    private static final int MUTED = 0xFF9BA8BC;
    private static boolean hudEnabled = true;
    private static boolean infoEnabled = true;
    private static boolean statusEnabled = true;
    private static boolean clockEnabled = true;
    private static boolean keysEnabled = true;
    private static MinecraftClient clientInstance;

    @Override
    public void onInitialize() {
        KeyBindingHelper.registerKeyBinding(TOGGLE_HUD);
        KeyBindingHelper.registerKeyBinding(TOGGLE_INFO);
        KeyBindingHelper.registerKeyBinding(TOGGLE_STATUS);
        KeyBindingHelper.registerKeyBinding(TOGGLE_CLOCK);
        KeyBindingHelper.registerKeyBinding(TOGGLE_KEYS);

        ClientTickEvents.END_CLIENT_TICK.register(client -> {
            clientInstance = client;
            while (TOGGLE_HUD.wasPressed()) hudEnabled = !hudEnabled;
            while (TOGGLE_INFO.wasPressed()) infoEnabled = !infoEnabled;
            while (TOGGLE_STATUS.wasPressed()) statusEnabled = !statusEnabled;
            while (TOGGLE_CLOCK.wasPressed()) clockEnabled = !clockEnabled;
            while (TOGGLE_KEYS.wasPressed()) keysEnabled = !keysEnabled;
        });

        HudRenderCallback.EVENT.register((context, tickCounter) -> render(context));
    }

    private static void render(DrawContext context) {
        MinecraftClient client = clientInstance;
        if (!hudEnabled || client == null || client.player == null || client.world == null || client.currentScreen != null) {
            return;
        }

        ClientPlayerEntity player = client.player;
        int accent = pulseColor();
        int screenWidth = context.getScaledWindowWidth();
        int screenHeight = context.getScaledWindowHeight();

        if (infoEnabled) {
            String coordinates = String.format(Locale.ROOT, "XYZ  %.1f  %.1f  %.1f", player.getX(), player.getY(), player.getZ());
            drawPanel(context, 8, 8, 174, "PRISM  /  INFO", accent,
                    "FPS   " + client.getCurrentFps(),
                    coordinates,
                    "FACING   " + player.getHorizontalFacing().asString().toUpperCase(Locale.ROOT));
        }

        if (clockEnabled) {
            long time = client.world.getTimeOfDay();
            long dayTime = Math.floorMod(time, 24000L);
            int hour = (int) ((dayTime / 1000L + 6L) % 24L);
            int minute = (int) (dayTime % 1000L * 60L / 1000L);
            drawPanel(context, screenWidth - 136, 8, 128, "WORLD CLOCK", accent,
                    String.format(Locale.ROOT, "%02d:%02d   DAY %d", hour, minute, time / 24000L + 1L));
        }

        if (statusEnabled) {
            drawPanel(context, screenWidth - 146, 53, 138, "PLAYER STATUS", accent,
                    String.format(Locale.ROOT, "HEALTH   %.0f / %.0f", player.getHealth(), player.getMaxHealth()),
                    "ARMOR   " + player.getArmor(),
                    "FOOD   " + player.getHungerManager().getFoodLevel());
        }

        if (keysEnabled) {
            drawKeystrokes(context, 8, screenHeight - 101, client, accent);
        }
    }

    private static void drawPanel(DrawContext context, int x, int y, int width, String title, int accent, String... lines) {
        int height = 19 + lines.length * 12 + 4;
        context.fill(x, y, x + width, y + height, PANEL);
        context.fill(x, y, x + width, y + 2, accent);
        context.drawText(clientInstance.textRenderer, title, x + 7, y + 6, accent, false);

        for (int i = 0; i < lines.length; i++) {
            context.drawText(clientInstance.textRenderer, lines[i], x + 7, y + 20 + i * 12, i == 0 ? TEXT : MUTED, false);
        }
    }

    private static void drawKeystrokes(DrawContext context, int x, int y, MinecraftClient client, int accent) {
        context.fill(x, y, x + 116, y + 91, PANEL);
        context.fill(x, y, x + 116, y + 2, accent);
        context.drawText(client.textRenderer, "KEYS  /  INPUT", x + 7, y + 6, accent, false);

        drawKey(context, client, x + 45, y + 21, "W", client.options.forwardKey.isPressed(), accent);
        drawKey(context, client, x + 18, y + 40, "A", client.options.leftKey.isPressed(), accent);
        drawKey(context, client, x + 45, y + 40, "S", client.options.backKey.isPressed(), accent);
        drawKey(context, client, x + 72, y + 40, "D", client.options.rightKey.isPressed(), accent);
        context.drawText(client.textRenderer, "JUMP", x + 10, y + 64, MUTED, false);
        context.drawText(client.textRenderer, client.options.jumpKey.isPressed() ? "ON" : "OFF", x + 78, y + 64,
                client.options.jumpKey.isPressed() ? accent : MUTED, false);
        context.drawText(client.textRenderer, "SNEAK", x + 10, y + 77, MUTED, false);
        context.drawText(client.textRenderer, client.options.sneakKey.isPressed() ? "ON" : "OFF", x + 78, y + 77,
                client.options.sneakKey.isPressed() ? accent : MUTED, false);
    }

    private static void drawKey(DrawContext context, MinecraftClient client, int x, int y, String label, boolean pressed, int accent) {
        context.fill(x, y, x + 24, y + 16, pressed ? accent : 0xFF283142);
        context.drawText(client.textRenderer, label, x + 9, y + 4, TEXT, false);
    }

    private static int pulseColor() {
        double pulse = (Math.sin(System.currentTimeMillis() / 550.0) + 1.0) * 0.5;
        int red = (int) (72 + pulse * 26);
        int green = (int) (185 + pulse * 55);
        int blue = 255;
        return 0xFF000000 | red << 16 | green << 8 | blue;
    }
}