Files
src
build.gradle
gradle.properties
manifest.kod
README.md
settings.gradle
Code
9 392
src/client/java/com/kodari/violethud/VioletHudClient.java
package com.kodari.violethud;
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.gui.DrawContext;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class VioletHudClient implements ClientModInitializer {
public static final String MOD_ID = "violethud";
private static KeyBinding openConfig;
private static double previousX;
private static double previousZ;
private static double blocksPerSecond;
private static boolean hasPreviousPosition;
@Override
public void onInitializeClient() {
VioletHudConfig.HANDLER.load();
KeyBinding.Category category = KeyBinding.Category.create(Identifier.of(MOD_ID, "main"));
openConfig = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.violethud.open_config", GLFW.GLFW_KEY_RIGHT_SHIFT, category));
ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (openConfig.wasPressed()) {
if (client.currentScreen == null) {
client.setScreen(VioletHudConfig.createScreen(null));
}
}
if (client.player == null) {
hasPreviousPosition = false;
blocksPerSecond = 0;
return;
}
double x = client.player.getX();
double z = client.player.getZ();
if (hasPreviousPosition) {
blocksPerSecond = Math.hypot(x - previousX, z - previousZ) * 20.0;
}
previousX = x;
previousZ = z;
hasPreviousPosition = true;
});
HudElementRegistry.addLast(Identifier.of(MOD_ID, "overlay"), VioletHudClient::renderHud);
}
private static void renderHud(DrawContext context, net.minecraft.client.render.RenderTickCounter tickCounter) {
MinecraftClient client = MinecraftClient.getInstance();
if (client.player == null || client.world == null || client.currentScreen != null) {
return;
}
VioletHudConfig config = VioletHudConfig.get();
int screenWidth = client.getWindow().getScaledWidth();
int screenHeight = client.getWindow().getScaledHeight();
float scale = config.scale;
int accent = config.accent.getRGB();
if (config.showStatus) {
int x = Math.round(screenWidth * config.statusX / 100.0f);
int y = Math.round(screenHeight * config.statusY / 100.0f);
List<String> lines = List.of(
"FPS " + client.getCurrentFps(),
String.format(Locale.ROOT, "BPS %.1f", blocksPerSecond));
drawPanel(context, client, "STATUS", x, y, scale, config.opacity, accent, 98, lines);
}
if (config.showModules) {
List<String> modules = new ArrayList<>();
if (config.showStatus) modules.add("Status HUD");
if (config.showModules) modules.add("Module List");
if (config.showLogo) modules.add("Client Logo");
if (config.showHotkeys) modules.add("Hotkeys");
int width = 116;
int x = screenWidth - Math.round(width * scale) - 8;
int y = Math.round(screenHeight * config.moduleY / 100.0f);
drawPanel(context, client, "ACTIVE", x, y, scale, config.opacity, accent, width, modules);
}
if (config.showLogo) {
int width = 132;
int x = (screenWidth - Math.round(width * scale)) / 2;
int y = screenHeight - Math.round(25 * scale) - config.logoBottom;
drawPanel(context, client, "VIOLET HUD", x, y, scale, config.opacity, accent, width, List.of("CLIENT HUD"));
}
if (config.showHotkeys) {
int width = 142;
int x = Math.round(screenWidth * config.hotkeyX / 100.0f);
int y = screenHeight - Math.round(37 * scale) - config.hotkeyBottom;
drawPanel(context, client, "SHORTCUTS", x, y, scale, config.opacity, accent, width,
List.of("Right Shift Settings"));
}
}
private static void drawPanel(DrawContext context, MinecraftClient client, String title, int x, int y,
float scale, float opacity, int accent, int width, List<String> lines) {
int height = 18 + lines.size() * 11;
var matrices = context.getMatrices();
matrices.pushMatrix();
matrices.translate(x, y);
matrices.scale(scale, scale);
context.fill(0, 0, width, height, withOpacity(0xFF09070D, opacity));
context.fill(0, 0, width, 1, accent);
context.fill(0, 0, 1, height, accent);
context.fill(width - 1, 0, width, height, accent);
context.fill(0, height - 1, width, height, accent);
context.drawTextWithShadow(client.textRenderer, Text.literal(title), 6, 4, accent);
for (int index = 0; index < lines.size(); index++) {
context.drawTextWithShadow(client.textRenderer, Text.literal(lines.get(index)), 6,
16 + index * 11, 0xFFF0EAF7);
}
matrices.popMatrix();
}
private static int withOpacity(int argb, float opacity) {
int alpha = Math.round(((argb >>> 24) & 0xFF) * Math.clamp(opacity, 0.0f, 1.0f));
return (argb & 0x00FFFFFF) | (alpha << 24);
}
}