Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
8 257
src/client/java/com/kodari/keystrokes/client/KeystrokesClient.java
package com.kodari.keystrokes.client;
import io.wispforest.owo.ui.component.LabelComponent;
import io.wispforest.owo.ui.component.UIComponents;
import io.wispforest.owo.ui.container.FlowLayout;
import io.wispforest.owo.ui.container.GridLayout;
import io.wispforest.owo.ui.container.UIContainers;
import io.wispforest.owo.ui.core.Color;
import io.wispforest.owo.ui.core.HorizontalAlignment;
import io.wispforest.owo.ui.core.Insets;
import io.wispforest.owo.ui.core.Positioning;
import io.wispforest.owo.ui.core.Sizing;
import io.wispforest.owo.ui.core.Surface;
import io.wispforest.owo.ui.core.VerticalAlignment;
import io.wispforest.owo.ui.hud.Hud;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.function.Supplier;
public final class KeystrokesClient implements ClientModInitializer {
private static final Identifier HUD_ID = Identifier.of("keystrokes", "keystrokes_hud");
private static final Deque<Long> LEFT_CLICKS = new ArrayDeque<>();
private static final Deque<Long> RIGHT_CLICKS = new ArrayDeque<>();
private static LabelComponent leftCps;
private static LabelComponent rightCps;
@Override
public void onInitializeClient() {
Hud.add(HUD_ID, KeystrokesClient::buildHud);
ClientTickEvents.END_CLIENT_TICK.register(client -> tick());
}
private static FlowLayout buildHud() {
MinecraftClient client = MinecraftClient.getInstance();
var options = client.options;
FlowLayout panel = UIContainers.verticalFlow(Sizing.content(), Sizing.content());
panel.padding(Insets.of(6));
panel.gap(4);
panel.positioning(Positioning.absolute(8, 8));
panel.surface(Surface.flat(0xE610151D).and(Surface.outline(0xFF344354)));
LabelComponent title = label("KEYSTROKES", 0x89D7FF);
title.shadow(true);
panel.child(title);
GridLayout movement = UIContainers.grid(Sizing.fixed(72), Sizing.fixed(44), 2, 3);
movement.child(keyTile("W", 22, 20, options.forwardKey::isPressed), 0, 1);
movement.child(keyTile("A", 22, 20, options.leftKey::isPressed), 1, 0);
movement.child(keyTile("S", 22, 20, options.backKey::isPressed), 1, 1);
movement.child(keyTile("D", 22, 20, options.rightKey::isPressed), 1, 2);
panel.child(movement);
FlowLayout mouseButtons = UIContainers.horizontalFlow(Sizing.content(), Sizing.content());
mouseButtons.gap(4);
mouseButtons.child(mouseColumn("LMB", options.attackKey));
mouseButtons.child(mouseColumn("RMB", options.useKey));
panel.child(mouseButtons);
panel.child(keyTile("SPACE", 72, 18, options.jumpKey::isPressed));
FlowLayout movementModes = UIContainers.horizontalFlow(Sizing.content(), Sizing.content());
movementModes.gap(4);
movementModes.child(keyTile("SNEAK", 40, 18, options.sneakKey::isPressed));
movementModes.child(keyTile("SPRINT", 40, 18, options.sprintKey::isPressed));
panel.child(movementModes);
return panel;
}
private static FlowLayout mouseColumn(String name, KeyBinding binding) {
FlowLayout column = UIContainers.verticalFlow(Sizing.content(), Sizing.content());
column.gap(2);
column.child(keyTile(name, 52, 20, binding::isPressed));
LabelComponent cps = label("0 CPS", 0xB8C6D6);
cps.horizontalSizing(Sizing.fixed(52));
cps.horizontalTextAlignment(HorizontalAlignment.CENTER);
column.child(cps);
if (name.equals("LMB")) {
leftCps = cps;
} else {
rightCps = cps;
}
return column;
}
private static FlowLayout keyTile(String text, int width, int height, Supplier<Boolean> pressed) {
FlowLayout tile = UIContainers.horizontalFlow(Sizing.fixed(width), Sizing.fixed(height));
tile.alignment(HorizontalAlignment.CENTER, VerticalAlignment.CENTER);
tile.surface((graphics, component) -> {
boolean active = pressed.get();
int x = component.x();
int y = component.y();
int right = x + component.width();
int bottom = y + component.height();
graphics.fill(x, y, right, bottom, active ? 0xFF246FA8 : 0xD91A2430);
graphics.drawRectOutline(x, y, component.width(), component.height(), active ? 0xFF83D5FF : 0xFF34475A);
});
LabelComponent caption = label(text, 0xF1F5FB);
caption.shadow(true);
tile.child(caption);
return tile;
}
private static LabelComponent label(String text, int rgb) {
LabelComponent label = UIComponents.label(Text.literal(text));
label.color(Color.ofRgb(rgb));
return label;
}
private static void tick() {
long now = System.currentTimeMillis();
trimClicks(LEFT_CLICKS, now);
trimClicks(RIGHT_CLICKS, now);
updateCps(leftCps, LEFT_CLICKS.size());
updateCps(rightCps, RIGHT_CLICKS.size());
}
public static void recordMousePress(int button) {
if (button == 0) {
LEFT_CLICKS.addLast(System.currentTimeMillis());
} else if (button == 1) {
RIGHT_CLICKS.addLast(System.currentTimeMillis());
}
}
private static void trimClicks(Deque<Long> clicks, long now) {
while (!clicks.isEmpty() && now - clicks.peekFirst() > 1000) {
clicks.removeFirst();
}
}
private static void updateCps(LabelComponent label, int clicksPerSecond) {
if (label == null) {
return;
}
String value = clicksPerSecond + " CPS";
if (!label.text().getString().equals(value)) {
label.text(Text.literal(value));
}
}
}