Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
10 422
src/client/java/dev/kodari/hasteclient/HasteClient.java
package dev.kodari.hasteclient;
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.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.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.hud.VanillaHudElements;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.network.PlayerListEntry;
import net.minecraft.client.render.RenderTickCounter;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import java.util.ArrayDeque;
import java.util.Deque;
public final class HasteClient implements ClientModInitializer {
public static final String MOD_ID = "hasteclient";
private static final int ACCENT = 0x6BFFBA;
private static final int TEXT = 0xE7EEF8;
private static final int MUTED = 0xA9B5C6;
private static final Identifier HUD_ID = Identifier.of(MOD_ID, "hud");
private static final Identifier CROSSHAIR_ID = Identifier.of(MOD_ID, "custom_crosshair");
private static final KeyBinding.Category KEY_CATEGORY = KeyBinding.Category.create(Identifier.of(MOD_ID, "main"));
private static final Deque<Long> LEFT_CLICKS = new ArrayDeque<>();
private static final Deque<Long> RIGHT_CLICKS = new ArrayDeque<>();
private static KeyBinding toggleHudKey;
private static boolean hudEnabled = true;
private static int ticks;
private static LabelComponent fpsLabel;
private static LabelComponent cpsLabel;
private static LabelComponent pingLabel;
private static LabelComponent coordinatesLabel;
private static LabelComponent armorLabel;
private static LabelComponent forwardLabel;
private static LabelComponent leftLabel;
private static LabelComponent backLabel;
private static LabelComponent rightLabel;
@Override
public void onInitializeClient() {
toggleHudKey = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.hasteclient.toggle_hud", 72, KEY_CATEGORY)
);
Hud.add(HUD_ID, HasteClient::createHud);
ClientTickEvents.END_CLIENT_TICK.register(HasteClient::onClientTick);
HudElementRegistry.attachElementAfter(VanillaHudElements.CROSSHAIR, CROSSHAIR_ID,
(context, tickCounter) -> renderCustomCrosshair(context));
}
public static boolean isHudEnabled() {
return hudEnabled;
}
public static void recordMouseClick(int button) {
long now = System.currentTimeMillis();
if (button == 0) {
LEFT_CLICKS.addLast(now);
} else if (button == 1) {
RIGHT_CLICKS.addLast(now);
}
}
private static void onClientTick(MinecraftClient client) {
while (toggleHudKey.wasPressed()) {
setHudEnabled(!hudEnabled);
}
long cutoff = System.currentTimeMillis() - 1000L;
pruneClicks(LEFT_CLICKS, cutoff);
pruneClicks(RIGHT_CLICKS, cutoff);
if (hudEnabled && ++ticks % 5 == 0 && fpsLabel != null) {
updateHud(client);
}
}
private static void setHudEnabled(boolean enabled) {
if (hudEnabled == enabled) {
return;
}
hudEnabled = enabled;
clearHudComponents();
if (enabled) {
Hud.add(HUD_ID, HasteClient::createHud);
} else {
Hud.remove(HUD_ID);
}
}
private static void clearHudComponents() {
fpsLabel = null;
cpsLabel = null;
pingLabel = null;
coordinatesLabel = null;
armorLabel = null;
forwardLabel = null;
leftLabel = null;
backLabel = null;
rightLabel = null;
}
private static FlowLayout createHud() {
clearHudComponents();
FlowLayout panel = UIContainers.verticalFlow(Sizing.content(), Sizing.content());
panel.padding(Insets.of(6));
panel.gap(2);
panel.surface(Surface.DARK_PANEL);
panel.positioning(Positioning.absolute(8, 8));
LabelComponent title = label("HASTE CLIENT", ACCENT);
title.shadow(true);
panel.child(title);
fpsLabel = addLabel(panel, "FPS: --", TEXT);
cpsLabel = addLabel(panel, "CPS L: 0 R: 0", TEXT);
pingLabel = addLabel(panel, "Ping: -- ms", TEXT);
coordinatesLabel = addLabel(panel, "XYZ: --, --, --", TEXT);
armorLabel = addLabel(panel, "Armor durability: --", TEXT);
FlowLayout keyPanel = UIContainers.verticalFlow(Sizing.content(), Sizing.content());
keyPanel.gap(1);
FlowLayout topKeys = UIContainers.horizontalFlow(Sizing.content(), Sizing.content());
topKeys.gap(2);
LabelComponent spacer = keyLabel(" ");
topKeys.child(spacer);
forwardLabel = keyLabel("W");
topKeys.child(forwardLabel);
FlowLayout bottomKeys = UIContainers.horizontalFlow(Sizing.content(), Sizing.content());
bottomKeys.gap(2);
leftLabel = keyLabel("A");
backLabel = keyLabel("S");
rightLabel = keyLabel("D");
bottomKeys.child(leftLabel);
bottomKeys.child(backLabel);
bottomKeys.child(rightLabel);
keyPanel.child(topKeys);
keyPanel.child(bottomKeys);
panel.child(label("MOVEMENT", MUTED));
panel.child(keyPanel);
return panel;
}
private static LabelComponent addLabel(FlowLayout parent, String text, int color) {
LabelComponent label = label(text, color);
parent.child(label);
return label;
}
private static LabelComponent label(String text, int color) {
LabelComponent label = UIComponents.label(Text.literal(text));
label.color(Color.ofRgb(color));
label.shadow(true);
return label;
}
private static LabelComponent keyLabel(String text) {
LabelComponent label = label(text, MUTED);
label.sizing(Sizing.fixed(18), Sizing.fixed(16));
label.horizontalTextAlignment(HorizontalAlignment.CENTER);
label.verticalTextAlignment(VerticalAlignment.CENTER);
return label;
}
private static void updateHud(MinecraftClient client) {
ClientPlayerEntity player = client.player;
setText(fpsLabel, "FPS: " + client.getCurrentFps());
setText(cpsLabel, "CPS L: " + LEFT_CLICKS.size() + " R: " + RIGHT_CLICKS.size());
int ping = -1;
if (player != null && client.getNetworkHandler() != null) {
PlayerListEntry entry = client.getNetworkHandler().getPlayerListEntry(player.getUuid());
if (entry != null) {
ping = entry.getLatency();
}
}
setText(pingLabel, "Ping: " + (ping < 0 ? "--" : ping) + " ms");