itzyashanxd
📎 1 file 2,353 chars
📄
pasted-code-1787915032176.txt
txt 30 lines
▶
Create a client-side Fabric mod for Minecraft 1.21.x that dynamically fetches and renders player PvP tiers across multiple gamemodes using a Supabase database.

The mod must support 10 competitive tiers: HT1, LT1, HT2, LT2, HT3, LT3, HT4, LT4, HT5, and LT5. Each tier has an assigned color (e.g., HT1 is Dark Red, LT1 is Red, HT2 is Gold, etc.).

Features & Technical Requirements:
1. Support for Premium and Cracked (Offline-Mode) Players:
   - The mod must work on both premium and cracked/offline servers. 
   - Because cracked players do not have official Mojang accounts, the mod must compute and use the player's offline-mode UUID (using UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes(StandardCharsets.UTF_8))) when querying the database or saving local configurations.

2. Dynamic Database Sync:
   - Connect to a Supabase database via lightweight HTTP GET requests using a configurable API URL and Anon/Bearer token. 
   - The database table must be queried by the computed UUID or username, checking for fields: 'uuid', 'username', 'gamemode' (e.g., Pot, Sword, Axe), and 'tier'.
   - When a player appears in the world or joins the Tab list, asynchronously fetch their data in a background thread. Do not block the main game thread.

3. Complete Visual Rendering (Nametags, Tab, & Chat):
   - Render the active tier tag prefix (e.g., "[Axe HT1]") next to player names in all three of these specific locations:
     * Directly above the player's physical character head (the Entity Nametag).
     * Inside the multiplayer Tab list overlay.
     * Within the global and private in-game multiplayer chat messages.

4. Smart Local Caching & Manual Overrides:
   - Cache fetched database entries in a memory HashMap for 15 minutes to prevent API rate-limiting.
   - Maintain a local 'config.json' file for manual client-side overrides. If a local override exists for a player's UUID, use that tag instead of the database tier.

5. In-Game Commands:
   - `/tiertag set   ` (Saves a local override tag for a player)
   - `/tiertag remove ` (Clears the local override and reverts to their database tier)
   - `/tiertag info ` (Prints all known gamemode tiers for that player to the client chat)
   - `/tiertag refresh` (Flushes the local cache and forces a re-fetch for all online players)
Kodari AI
KodariAug 28 at 11:11 am 3
PlayerTierTagRenderer
A client-side Fabric mod for Minecraft 1.21.x that fetches and displays player PvP tiers from a Supabase database. Supports 10 competitive tiers (HT1-LT5) with distinct colors. Works for premium and cracked players using offline-mode UUID computation. Renders tier tags above player heads, in Tab lists, and in chat messages. Features 15-minute memory caching, local config.json overrides, and commands for setting, removing, querying, and refreshing tier data. Uses asynchronous HTTP requests to avoid blocking the game thread.
  1. Set up the Fabric 1.21.11 client-only project and metadata.

  2. Implement Supabase fetching, UUID handling, 15-minute cache, and JSON-backed overrides.

  3. Add /tiertag commands and refresh behavior.

  4. Add nametag, tab-list, and chat tier rendering hooks.

Created the Fabric 1.21.11 client mod project with:

  • Fabric Loom 1.14, Yarn 1.21.11+build.5, Java 21 setup.

  • Supabase HTTP synchronization using configurable API URL and anon/Bearer token.

  • Premium and cracked UUID support, including offline UUID generation.

  • 15-minute in-memory caching with asynchronous requests.

  • Local overrides persisted in config/tiertag.json.

  • All ten tiers with distinct Minecraft formatting colors.

  • Client commands:

    • /tiertag set <username> <gamemode> <tier>

    • /tiertag remove <username>

    • /tiertag info <username>

    • /tiertag refresh

  • Tier prefixes integrated into player nametags, tab entries, and chat messages.

  • Client-only mixin configuration and split client source set structure.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
14 659
src/client/java/com/kodari/tiertag/TierTagClient.java
package com.kodari.tiertag;

import com.mojang.brigadier.CommandDispatcher;
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.FabricClientCommandSource;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.PlayerListEntry;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal;

public final class TierTagClient implements ClientModInitializer {
    private static TierTagConfig config;
    private static TierDatabase database;
    private static final Map<String, PlayerIdentity> identities = new HashMap<>();
    private static int scanTicks;

    @Override
    public void onInitializeClient() {
        config = TierTagConfig.load();
        database = new TierDatabase(config);
        ClientTickEvents.END_CLIENT_TICK.register(TierTagClient::onClientTick);
        ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> registerCommands(dispatcher));
    }

    private static void onClientTick(MinecraftClient client) {
        if (client.world == null || client.player == null || ++scanTicks % 20 != 0) {
            return;
        }
        scanOnlinePlayers(client);
    }

    private static void scanOnlinePlayers(MinecraftClient client) {
        for (PlayerEntity player : client.world.getPlayers()) {
            remember(player.getName().getString(), player.getUuid());
        }
        if (client.getNetworkHandler() != null) {
            for (PlayerListEntry entry : client.getNetworkHandler().getPlayerList()) {
                remember(entry.getProfile().getName(), entry.getProfile().getId());
            }
        }
        for (PlayerIdentity identity : identities.values()) {
            database.request(identity.username(), identity.actualUuid());
        }
    }

    private static void remember(String username, UUID actualUuid) {
        if (username != null && !username.isBlank()) {
            identities.put(username.toLowerCase(), new PlayerIdentity(username, actualUuid));
        }
    }

    public static Text decoratePlayerName(String username, UUID actualUuid, Text original) {
        Text tag = tagFor(username, actualUuid);
        return tag == null ? original : Text.empty().append(tag).append(original);
    }

    public static Text decorateChatMessage(Text message) {
        String plain = message.getString();
        for (PlayerIdentity identity : identities.values()) {
            if (isChatFrom(plain, identity.username())) {
                Text tag = tagFor(identity.username(), identity.actualUuid());
                if (tag != null && !plain.startsWith("[")) {
                    return Text.empty().append(tag).append(message);
                }
                break;
            }
        }
        return message;
    }

    private static boolean isChatFrom(String message, String username) {
        return message.startsWith(username + ":")
            || message.startsWith("<" + username + ">")
            || message.contains(" " + username + ":");
    }

    private static Text tagFor(String username, UUID actualUuid) {
        if (database == null || config == null) {
            return null;
        }
        PlayerTiers tiers = database.cached(actualUuid, username);
        if (tiers == null) {
            tiers = PlayerTiers.empty(username);
        }
        return tiers.withOverrides(config.override(OfflineUuid.forUsername(username))).tagText();
    }

    private static void registerCommands(CommandDispatcher<FabricClientCommandSource> dispatcher) {
        dispatcher.register(literal("tiertag")
            .then(literal("set")
                .then(argument("username", StringArgumentType.word())
                    .then(argument("gamemode", StringArgumentType.word())
                        .then(argument("tier", StringArgumentType.word())
                            .executes(context -> {
                                String username = StringArgumentType.getString(context, "username");
                                String gamemode = StringArgumentType.getString(context, "gamemode");
                                String tier = StringArgumentType.getString(context, "tier").toUpperCase();
                                if (!TierColors.isTier(tier)) {
                                    feedback(Text.literal("Unknown tier. Use HT1, LT1, HT2, LT2, HT3, LT3, HT4, LT4, HT5, or LT5.").formatted(Formatting.RED));
                                    return 0;
                                }
                                config.setOverride(OfflineUuid.forUsername(username), gamemode, tier);
                                feedback(Text.literal("Saved local override for " + username + ": " + gamemode + " " + tier).formatted(Formatting.GREEN));
                                return 1;
                            })))))
            .then(literal("remove")
                .then(argument("username", StringArgumentType.word())
                    .executes(context -> {
                        String username = StringArgumentType.getString(context, "username");
                        boolean removed = config.removeOverride(OfflineUuid.forUsername(username));
                        feedback(Text.literal(removed ? "Removed local override for " + username + "." : "No local override found for " + username + "."));
                        return removed ? 1 : 0;
                    })))
            .then(literal("info")
                .then(argument("username", StringArgumentType.word())
                    .executes(context -> {
                        printInfo(StringArgumentType.getString(context, "username"));
                        return 1;
                    })))
            .then(literal("refresh")
                .executes(context -> {
                    database.clear();
                    identities.clear();
                    scanTicks = 19;
                    feedback(Text.literal("Tier cache flushed; refreshing online players.").formatted(Formatting.GREEN));
                    return 1;
                })));
    }

    private static void printInfo(String username) {
        PlayerIdentity identity = identities.get(username.toLowerCase());
        UUID actualUuid = identity == null ? null : identity.actualUuid();
        PlayerTiers cached = database.cached(actualUuid, username);
        if (cached != null) {
            feedback(Text.literal(cached.withOverrides(config.override(OfflineUuid.forUsername(username))).summary()));
            return;
        }
        feedback(Text.literal("Fetching tiers for " + username + "..."));
        database.request(username, actualUuid, tiers -> feedback(Text.literal(
            tiers.withOverrides(config.override(OfflineUuid.forUsername(username))).summary())));
    }

    private static void feedback(Text message) {
        MinecraftClient client = MinecraftClient.getInstance();
        if (client.player != null) {
            client.player.sendMessage(message, false);
        }
    }

    private record PlayerIdentity(String username, UUID actualUuid) {
    }
}