Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
9 590
src/client/java/com/kodari/timerhud/client/TimerHudClient.java
package com.kodari.timerhud.client;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.ClientCommands;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keymapping.v1.KeyMappingHelper;
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.DeltaTracker;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import com.mojang.blaze3d.platform.InputConstants;
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.Locale;
import java.util.Properties;
import java.util.logging.Logger;
public class TimerHudClient implements ClientModInitializer {
private static final String DEFAULT_COLOR = "white";
private static final Path STATE_FILE = Path.of("config", "timerhud.properties");
private static final Logger LOGGER = Logger.getLogger("timerhud");
private static boolean active;
private static boolean paused;
private static boolean countingDown;
static boolean visible = true;
static boolean textShadow = true;
static int hudOffset;
static float gradientSpeed = 1.0f;
static GradientMode gradientMode = GradientMode.SOLID;
private static long startingSeconds;
private static long startedAtNanos;
private static long lastPersistedSeconds = Long.MIN_VALUE;
private static int startColor = 0xFFFFFFFF;
private static int endColor = 0xFFFFFFFF;
private static KeyMapping configKey;
private static boolean configOpenRequested;
enum GradientMode {
SOLID,
STATIC,
ANIMATED
}
@Override
public void onInitializeClient() {
loadTimerState();
configKey = KeyMappingHelper.registerKeyMapping(new KeyMapping(
"key.timerhud.config", keyboardType(), InputConstants.KEY_O, KeyMapping.Category.MISC
));
ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (configKey.consumeClick()) {
configOpenRequested = true;
}
if (configOpenRequested) {
configOpenRequested = false;
openConfigScreen();
}
});
HudElementRegistry.addLast(
Identifier.fromNamespaceAndPath("timerhud", "timer"),
TimerHudClient::renderHud
);
ClientCommandRegistrationCallback.EVENT.register((dispatcher, buildContext) -> dispatcher.register(
ClientCommands.literal("timer")
.executes(context -> {
context.getSource().sendFeedback(Component.literal("/timer up [color] [gradient] | /timer down <seconds> [color] [gradient] | /timer color <color> [gradient] | /timer gradient <off|static|animated> | /timer show | hide | toggle | config | pause | resume | stop"));
return 1;
})
.then(ClientCommands.literal("up")
.executes(context -> startTimer(context.getSource(), false, 0, DEFAULT_COLOR, null))
.then(ClientCommands.argument("color", StringArgumentType.word())
.executes(context -> startTimer(context.getSource(), false, 0, StringArgumentType.getString(context, "color"), null))
.then(ClientCommands.argument("gradient", StringArgumentType.word())
.executes(context -> startTimer(context.getSource(), false, 0, StringArgumentType.getString(context, "color"), StringArgumentType.getString(context, "gradient"))))))
.then(ClientCommands.literal("down")
.then(ClientCommands.argument("seconds", IntegerArgumentType.integer(1))
.executes(context -> startTimer(context.getSource(), true, IntegerArgumentType.getInteger(context, "seconds"), DEFAULT_COLOR, null))
.then(ClientCommands.argument("color", StringArgumentType.word())
.executes(context -> startTimer(context.getSource(), true, IntegerArgumentType.getInteger(context, "seconds"), StringArgumentType.getString(context, "color"), null))
.then(ClientCommands.argument("gradient", StringArgumentType.word())
.executes(context -> startTimer(context.getSource(), true, IntegerArgumentType.getInteger(context, "seconds"), StringArgumentType.getString(context, "color"), StringArgumentType.getString(context, "gradient")))))))
.then(ClientCommands.literal("color")
.then(ClientCommands.argument("color", StringArgumentType.word())
.executes(context -> setTimerColor(context.getSource(), StringArgumentType.getString(context, "color"), null))
.then(ClientCommands.argument("gradient", StringArgumentType.word())
.executes(context -> setTimerColor(context.getSource(), StringArgumentType.getString(context, "color"), StringArgumentType.getString(context, "gradient"))))))
.then(ClientCommands.literal("stop").executes(context -> {
long shownSeconds = active ? currentSeconds() : startingSeconds;
active = false;
paused = false;
saveTimerState(shownSeconds);
context.getSource().sendFeedback(Component.literal("Timer stopped."));
return 1;
}))
.then(ClientCommands.literal("show").executes(context -> setHudVisible(context.getSource(), true)))
.then(ClientCommands.literal("hide").executes(context -> setHudVisible(context.getSource(), false)))
.then(ClientCommands.literal("toggle").executes(context -> setHudVisible(context.getSource(), !visible)))
.then(ClientCommands.literal("config").executes(context -> requestConfigScreen(context.getSource())))
.then(ClientCommands.literal("settings").executes(context -> requestConfigScreen(context.getSource())))
.then(ClientCommands.literal("gradient")
.then(ClientCommands.literal("off").executes(context -> setGradientMode(context.getSource(), GradientMode.SOLID)))
.then(ClientCommands.literal("static").executes(context -> setGradientMode(context.getSource(), GradientMode.STATIC)))
.then(ClientCommands.literal("animated").executes(context -> setGradientMode(context.getSource(), GradientMode.ANIMATED))))
.then(ClientCommands.literal("pause").executes(context -> pauseTimer(context.getSource())))
.then(ClientCommands.literal("resume").executes(context -> resumeTimer(context.getSource())))
));
}
private static InputConstants.Type keyboardType() {
try {
return InputConstants.Type.valueOf("KEYBOARD");
} catch (IllegalArgumentException exception) {
return InputConstants.Type.valueOf("KEYSYM");
}
}
private static int startTimer(FabricClientCommandSource source, boolean down, long seconds, String colorName, String gradientName) {
Integer parsedStartColor = parseColor(colorName);
Integer parsedEndColor = gradientName == null ? parsedStartColor : parseColor(gradientName);
if (parsedStartColor == null || parsedEndColor == null) {
source.sendError(Component.literal("Unknown color. Use a color name or a hex value such as #33AAFF."));
return 0;
}
countingDown = down;
startingSeconds = seconds;
startedAtNanos = System.nanoTime();
startColor = parsedStartColor;
endColor = parsedEndColor;
gradientMode = gradientName == null ? GradientMode.SOLID : GradientMode.STATIC;
active = true;
paused = false;
lastPersistedSeconds = seconds;
saveTimerState(seconds);
source.sendFeedback(Component.literal(down ? "Countdown started." : "Count-up timer started."));
return 1;
}
private static int pauseTimer(FabricClientCommandSource source) {
if (!active) {
source.sendError(Component.literal("There is no active timer to pause."));
return 0;
}
if (paused) {
source.sendError(Component.literal("The timer is already paused."));
return 0;
}
startingSeconds = currentSeconds();
startedAtNanos = System.nanoTime();
paused = true;
lastPersistedSeconds = startingSeconds;
saveTimerState(startingSeconds);
source.sendFeedback(Component.literal("Timer paused."));
return 1;
}
private static int resumeTimer(FabricClientCommandSource source) {
if (!active) {
source.sendError(Component.literal("There is no active timer to resume."));
return 0;
}
if (!paused) {
source.sendError(Component.literal("The timer is not paused."));
return 0;
}
startedAtNanos = System.nanoTime();
paused = false;
lastPersistedSeconds = startingSeconds;
saveTimerState(startingSeconds);
source.sendFeedback(Component.literal("Timer resumed."));
return 1;
}
private static int setTimerColor(FabricClientCommandSource source, String colorName, String gradientName) {
if (!active) {
source.sendError(Component.literal("Start a timer before changing its color."));
return 0;
}
Integer parsedStartColor = parseColor(colorName);
Integer parsedEndColor = gradientName == null ? parsedStartColor : parseColor(gradientName);
if (parsedStartColor == null || parsedEndColor == null) {
source.sendError(Component.literal("Unknown color. Use a color name or a hex value such as #33AAFF."));
return 0;
}
