Files
gradle
src
build.gradle
gradle.properties
gradlew
gradlew.bat
manifest.kod
settings.gradle
Code
12 493
src/client/java/com/kodari/hud/KodariHudClient.java
package com.kodari.hud;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.network.PlayerListEntry;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.render.RenderTickCounter;
import net.minecraft.text.Text;
public final class KodariHudClient implements ClientModInitializer {
public static final String MOD_ID = "turtleclient";
private static final int TEXT_COLOR = 0xFFFFFFFF;
private static final int PANEL_COLOR = 0xA0101018;
private static final int BORDER_COLOR = 0xA0606070;
private static final int PRESSED_COLOR = 0xC04A90E2;
private static final int RELEASED_COLOR = 0xA0282838;
private static final int CROSSHAIR_COLOR = 0xFFFFFFFF;
@Override
public void onInitializeClient() {
KodariHudConfig.load();
HudRenderCallback.EVENT.register(KodariHudClient::renderHud);
}
private static void renderHud(DrawContext context, RenderTickCounter tickCounter) {
MinecraftClient client = MinecraftClient.getInstance();
KodariHudConfig config = KodariHudConfig.get();
if (!config.enabled || client.player == null || client.options.hudHidden) {
return;
}
if (config.showFps || config.showPerformance) {
drawInfoPanel(context, client, config);
}
if (config.showCrosshair) {
drawCrosshair(context, client, config);
}
if (config.showKeystrokes) {
drawKeystrokes(context, client, config);
}
}
private static void drawInfoPanel(DrawContext context, MinecraftClient client, KodariHudConfig config) {
TextRenderer textRenderer = client.textRenderer;
String[] lines = new String[2];
int lineCount = 0;
if (config.showFps) {
lines[lineCount++] = "FPS: " + client.getCurrentFps();
}
if (config.showPerformance) {
long usedMemory = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (1024 * 1024);
long maxMemory = Runtime.getRuntime().maxMemory() / (1024 * 1024);
PlayerListEntry playerListEntry = client.getNetworkHandler() == null
? null
: client.getNetworkHandler().getPlayerListEntry(client.player.getUuid());
String ping = playerListEntry == null ? "--" : Integer.toString(playerListEntry.getLatency());
lines[lineCount++] = "RAM: " + usedMemory + "/" + maxMemory + " MB Ping: " + ping + " ms";
}
int width = 0;
for (int i = 0; i < lineCount; i++) {
width = Math.max(width, textRenderer.getWidth(lines[i]));
}
int x = config.hudX;
int y = config.hudY;
context.fill(x - 4, y - 4, x + width + 4, y + lineCount * 10 + 2, PANEL_COLOR);
context.fill(x - 4, y - 4, x + width + 4, y - 3, BORDER_COLOR);
context.fill(x - 4, y + lineCount * 10 + 1, x + width + 4, y + lineCount * 10 + 2, BORDER_COLOR);
context.fill(x - 4, y - 3, x - 3, y + lineCount * 10 + 1, BORDER_COLOR);
context.fill(x + width + 3, y - 3, x + width + 4, y + lineCount * 10 + 1, BORDER_COLOR);
for (int i = 0; i < lineCount; i++) {
context.drawTextWithShadow(textRenderer, lines[i], x, y + i * 10, TEXT_COLOR);
}
}
private static void drawCrosshair(DrawContext context, MinecraftClient client, KodariHudConfig config) {
int centerX = client.getWindow().getScaledWidth() / 2;
int centerY = client.getWindow().getScaledHeight() / 2;
int size = config.crosshairSize;
int gap = config.crosshairGap;
context.fill(centerX - size - gap, centerY - 1, centerX - gap, centerY + 1, CROSSHAIR_COLOR);
context.fill(centerX + gap, centerY - 1, centerX + size + gap, centerY + 1, CROSSHAIR_COLOR);
context.fill(centerX - 1, centerY - size - gap, centerX + 1, centerY - gap, CROSSHAIR_COLOR);
context.fill(centerX - 1, centerY + gap, centerX + 1, centerY + size + gap, CROSSHAIR_COLOR);
}
private static void drawKeystrokes(DrawContext context, MinecraftClient client, KodariHudConfig config) {
int x = config.keystrokesX;
int y = client.getWindow().getScaledHeight() - config.keystrokesY - 42;
int keySize = 20;
int spacing = 2;
drawKey(context, client.textRenderer, x + keySize + spacing, y, keySize, "W", client.options.forwardKey);
drawKey(context, client.textRenderer, x, y + keySize + spacing, keySize, "A", client.options.leftKey);
drawKey(context, client.textRenderer, x + keySize + spacing, y + keySize + spacing, keySize, "S", client.options.backKey);
drawKey(context, client.textRenderer, x + (keySize + spacing) * 2, y + keySize + spacing, keySize, "D", client.options.rightKey);
drawKey(context, client.textRenderer, x, y + (keySize + spacing) * 2, keySize * 2 + spacing, "LMB", client.options.attackKey);
drawKey(context, client.textRenderer, x + (keySize + spacing) * 2, y + (keySize + spacing) * 2, keySize * 2 + spacing, "RMB", client.options.useKey);
}
private static void drawKey(DrawContext context, TextRenderer textRenderer, int x, int y, int width,
String label, KeyBinding keyBinding) {
int background = keyBinding.isPressed() ? PRESSED_COLOR : RELEASED_COLOR;
context.fill(x, y, x + width, y + 20, background);
context.fill(x, y, x + width, y + 1, BORDER_COLOR);
context.fill(x, y + 19, x + width, y + 20, BORDER_COLOR);
context.fill(x, y + 1, x + 1, y + 19, BORDER_COLOR);
context.fill(x + width - 1, y + 1, x + width, y + 19, BORDER_COLOR);
int textWidth = textRenderer.getWidth(label);
context.drawTextWithShadow(textRenderer, Text.literal(label), x + (width - textWidth) / 2, y + 6, TEXT_COLOR);
}
}