Files
src
build.gradle
gradle.properties
README.md
settings.gradle
Code
9 342
src/client/java/com/kodari/balancehud/BalanceHudClient.java
package com.kodari.balancehud;
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.message.v1.ClientReceiveMessageEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class BalanceHudClient implements ClientModInitializer {
private static final String MOD_ID = "balancehud";
private static final String DEFAULT_COMMAND = "/pay .LeanderIQ_YT 1m";
private static final int DEFAULT_COOLDOWN_SECONDS = 5;
private static final int MAX_COOLDOWN_SECONDS = 3600;
private static final Path CONFIG_PATH = Path.of("config", "balancehud.properties");
private static final Pattern BALANCE_PATTERN = Pattern.compile("(?<![A-Za-z0-9_.])([0-9]+(?:[.,][0-9]+)?)\\s*([kKmMbB])\\b");
private static final KeyBinding.Category CATEGORY = KeyBinding.Category.create(Identifier.of(MOD_ID, "main"));
private static KeyBinding copyCommandKey;
private static KeyBinding sendCommandKey;
private static String preparedCommand = DEFAULT_COMMAND;
private static String detectedBalance;
private static String connectionStatus = "Not connected";
private static int cooldownSeconds = DEFAULT_COOLDOWN_SECONDS;
private static long cooldownUntilMillis;
@Override
public void onInitializeClient() {
loadConfig();
copyCommandKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.balancehud.copy_command", GLFW.GLFW_KEY_C, CATEGORY
));
sendCommandKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.balancehud.send_command", GLFW.GLFW_KEY_V, CATEGORY
));
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> {
preparedCommand = DEFAULT_COMMAND;
detectedBalance = null;
cooldownUntilMillis = 0L;
connectionStatus = client.isInSingleplayer() ? "Singleplayer" : "Multiplayer";
});
ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> connectionStatus = "Not connected");
ClientReceiveMessageEvents.GAME.register((message, overlay) -> {
if (overlay) {
return;
}
Matcher matcher = BALANCE_PATTERN.matcher(message.getString());
if (matcher.find()) {
detectedBalance = matcher.group(1) + matcher.group(2);
preparedCommand = "/pay .LeanderIQ_YT " + detectedBalance;
}
});
ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (client.world != null) {
connectionStatus = client.isInSingleplayer() ? "Singleplayer" : "Multiplayer";
}
while (copyCommandKey.wasPressed()) {
copyPreparedCommand(client);
}
while (sendCommandKey.wasPressed()) {
sendPreparedCommand(client);
}
});
HudElementRegistry.addLast(Identifier.of(MOD_ID, "overlay"), (context, tickCounter) -> {
MinecraftClient client = MinecraftClient.getInstance();
int x = 8;
int y = 8;
context.drawTextWithShadow(client.textRenderer, "BalanceHUD | " + connectionStatus, x, y, 0xFFFFFF);
context.drawTextWithShadow(client.textRenderer, "Prepared: " + preparedCommand, x, y + 12, 0xFFD166);
context.drawTextWithShadow(client.textRenderer, "[ COPY COMMAND ]", x, y + 26, 0x8FD3FF);
context.drawTextWithShadow(client.textRenderer, "[ SEND MANUALLY ]", x + 104, y + 26, 0x8FD3FF);
context.drawTextWithShadow(client.textRenderer,
"Detected balance: " + (detectedBalance == null ? "none" : detectedBalance), x, y + 40, 0xFFFFFF);
long remainingMillis = Math.max(0L, cooldownUntilMillis - System.currentTimeMillis());
String cooldownText = remainingMillis == 0L
? "Send cooldown: ready (" + cooldownSeconds + "s configured)"
: "Send cooldown: " + ((remainingMillis + 999L) / 1000L) + "s remaining";
context.drawTextWithShadow(client.textRenderer, cooldownText, x, y + 52, 0xFFFFFF);
});
}
public static boolean handleHudClick(double x, double y) {
if (y < 32.0 || y >= 46.0) {
return false;
}
MinecraftClient client = MinecraftClient.getInstance();
if (client.currentScreen != null) {
return false;
}
if (x >= 8.0 && x < 98.0) {
copyPreparedCommand(client);
return true;
}
if (x >= 104.0 && x < 230.0) {
sendPreparedCommand(client);
return true;
}
return false;
}
private static void copyPreparedCommand(MinecraftClient client) {
client.keyboard.setClipboard(preparedCommand);
if (client.player != null) {
client.player.sendMessage(Text.literal("BalanceHUD copied: " + preparedCommand), false);
}
}
private static void sendPreparedCommand(MinecraftClient client) {
if (client.player == null || client.getNetworkHandler() == null) {
return;
}
long remainingMillis = cooldownUntilMillis - System.currentTimeMillis();
if (remainingMillis > 0L) {
client.player.sendMessage(Text.literal("BalanceHUD: wait " + ((remainingMillis + 999L) / 1000L)
+ "s before sending another command."), false);
return;
}
cooldownUntilMillis = System.currentTimeMillis() + cooldownSeconds * 1000L;
client.getNetworkHandler().sendChatCommand(preparedCommand.substring(1));
}
private static void loadConfig() {
Properties properties = new Properties();
if (Files.exists(CONFIG_PATH)) {
try (InputStream input = Files.newInputStream(CONFIG_PATH)) {
properties.load(input);
} catch (IOException ignored) {
cooldownSeconds = DEFAULT_COOLDOWN_SECONDS;
}
}
try {
cooldownSeconds = Integer.parseInt(properties.getProperty("commandCooldownSeconds",
Integer.toString(DEFAULT_COOLDOWN_SECONDS)));
} catch (NumberFormatException ignored) {
cooldownSeconds = DEFAULT_COOLDOWN_SECONDS;
}
cooldownSeconds = Math.max(0, Math.min(MAX_COOLDOWN_SECONDS, cooldownSeconds));
saveConfig();
}
private static void saveConfig() {
Properties properties = new Properties();
properties.setProperty("commandCooldownSeconds", Integer.toString(cooldownSeconds));
try {
Files.createDirectories(CONFIG_PATH.getParent());
try (OutputStream output = Files.newOutputStream(CONFIG_PATH)) {
properties.store(output, "BalanceHUD settings. Set commandCooldownSeconds from 0 to 3600.");
}
} catch (IOException ignored) {
}
}
}