Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
7 700
src/client/java/com/kodari/ultrahub/UltrahubHelperClient.java
package com.kodari.ultrahub;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.mojang.authlib.GameProfile;
import io.wispforest.owo.ui.base.BaseOwoScreen;
import io.wispforest.owo.ui.component.ButtonComponent;
import io.wispforest.owo.ui.component.LabelComponent;
import io.wispforest.owo.ui.component.TextBoxComponent;
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.OwoUIAdapter;
import io.wispforest.owo.ui.core.Sizing;
import io.wispforest.owo.ui.core.Surface;
import io.wispforest.owo.ui.core.VerticalAlignment;
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.message.v1.ClientSendMessageEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.text.Text;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.WeekFields;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
public final class UltrahubHelperClient implements ClientModInitializer {
private static final int ACCENT = 0xA020F0;
private static final int INSPECTION_SECONDS = 180;
private static final int MAX_CHAT_LINES = 6;
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static final HttpClient HTTP = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(8)).build();
private static final Path CONFIG_PATH = Path.of("config", "ultrahub_admin_config.json");
private static final Path STATS_PATH = Path.of("config", "ultrahub_admin_stats.json");
private static final Deque<String> TARGET_CHAT = new ArrayDeque<>();
private static final KeyBinding DASHBOARD_KEY = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.ultrahub_helper.dashboard", InputUtil.Type.KEYSYM,
InputUtil.GLFW_KEY_RIGHT_SHIFT, KeyBinding.Category.MISC));
private static AdminConfig config;
private static StatsData stats;
private static String targetPlayer;
private static Instant inspectionStartedAt;
private static int lastInspectionSeconds;
private static int tickCounter;
@Override
public void onInitializeClient() {
config = load(CONFIG_PATH, AdminConfig.class, new AdminConfig());
stats = load(STATS_PATH, StatsData.class, new StatsData());
if (stats.events == null) stats.events = new ArrayList<>();
save(CONFIG_PATH, config);
save(STATS_PATH, stats);
ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (DASHBOARD_KEY.wasPressed()) client.setScreen(new DashboardScreen());
if (++tickCounter % 20 == 0) {
updateInspectionTimer();
checkSummaryPeriods();
}
});
ClientSendMessageEvents.COMMAND.register(UltrahubHelperClient::observeCommand);
ClientReceiveMessageEvents.CHAT.register((message, signedMessage, sender, params, receptionTimestamp) -> {
if (sender != null && targetPlayer != null && targetPlayer.equalsIgnoreCase(sender.name())) {
TARGET_CHAT.addLast(sender.name() + ": " + message.getString());
while (TARGET_CHAT.size() > MAX_CHAT_LINES) TARGET_CHAT.removeFirst();
}
});
}
private static <T> T load(Path path, Class<T> type, T fallback) {
try {
if (Files.exists(path)) {
T loaded = GSON.fromJson(Files.readString(path), type);
if (loaded != null) return loaded;
}
} catch (IOException | RuntimeException exception) {
System.err.println("Could not read " + path + ": " + exception.getMessage());
}
return fallback;
}
private static void save(Path path, Object value) {
try {
Files.createDirectories(path.getParent());
Files.writeString(path, GSON.toJson(value));
} catch (IOException exception) {
System.err.println("Could not write " + path + ": " + exception.getMessage());
}
}
private static void observeCommand(String command) {
String[] parts = command.trim().split("\\s+");
if (parts.length < 2 || parts[0].isEmpty()) return;
if (parts[0].equalsIgnoreCase("sprawdz")) {
startInspection(parts[1]);
return;
}
if (!parts[0].equalsIgnoreCase("tempmute")) return;
int playerIndex = parts.length > 1 && parts[1].equalsIgnoreCase("IP") ? 2 : 1;
int timeIndex = playerIndex + 1;
int reasonIndex = playerIndex + 2;
if (parts.length <= timeIndex) return;
String player = parts[playerIndex];
String duration = parts[timeIndex];
String reason = parts.length > reasonIndex
? String.join(" ", java.util.Arrays.copyOfRange(parts, reasonIndex, parts.length))
: "";
recordEvent("MUTE", player, reason, duration, inspectionDurationSeconds());
submitModeration("MUTE", player, reason, duration, formatDuration(inspectionDurationSeconds()));
}
private static void startInspection(String player) {
if (inspectionStartedAt != null) finishInspection(false);
targetPlayer = player;
TARGET_CHAT.clear();
inspectionStartedAt = Instant.now();
lastInspectionSeconds = 0;
recordEvent("INSPECTION_START", player, "", "", 0);
}
private static void updateInspectionTimer() {
if (inspectionStartedAt != null && inspectionDurationSeconds() >= INSPECTION_SECONDS) {
finishInspection(false);
}
}
private static int inspectionDurationSeconds() {
if (inspectionStartedAt == null) return lastInspectionSeconds;
return (int) Math.max(0, Duration.between(inspectionStartedAt, Instant.now()).toSeconds());
}
private static int remainingInspectionSeconds() {
if (inspectionStartedAt == null) return targetPlayer == null ? INSPECTION_SECONDS : 0;
return Math.max(0, INSPECTION_SECONDS - inspectionDurationSeconds());
}
private static void finishInspection(boolean clearTarget) {
if (inspectionStartedAt != null) {
lastInspectionSeconds = inspectionDurationSeconds();
recordEvent("INSPECTION_END", targetPlayer == null ? "" : targetPlayer, "", "", lastInspectionSeconds);
inspectionStartedAt = null;
}
if (clearTarget) {
targetPlayer = null;
TARGET_CHAT.clear();
}
}
private static void banTarget(String reason) {
if (targetPlayer == null || targetPlayer.isBlank()) return;
String player = targetPlayer;
String inspectionDuration = formatDuration(inspectionDurationSeconds());
ClientPlayNetworkHandler network = MinecraftClient.getInstance().getNetworkHandler();
if (network == null) return;
network.sendChatCommand("ban " + player + " 30d " + reason);
recordEvent("BAN", player, reason, "30d", inspectionDurationSeconds());
submitModeration("BAN", player, reason, "30d", inspectionDuration);
finishInspection(true);
}
private static void clearAsClean() {
if (targetPlayer == null) return;
String player = targetPlayer;
finishInspection(true);
ClientPlayNetworkHandler network = MinecraftClient.getInstance().getNetworkHandler();
if (network != null) {
network.sendChatMessage(config.moderatorName + " (" + config.adminRank + "): " + player + " jest czysty po sprawdzeniu.");
}