Files
src
build.gradle
gradle.properties
settings.gradle
Code
64 3,130
src/client/java/com/kodari/operationdroploot/client/DropLootClient.java
package com.kodari.operationdroploot.client;

import com.kodari.operationdroploot.EnderiteTeleportPayload;
import com.kodari.operationdroploot.SnapshotPayload;
import java.util.Locale;
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.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.render.RenderTickCounter;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

public final class DropLootClient implements ClientModInitializer {
   private static final String[] RANKS = new String[]{"PEASANT", "BEGINNER", "INTERMEDIATE", "EXPLORER", "BOSS", "ENFORCER", "SUPREME", "GOD"};
   private static final int[] THRESHOLDS = new int[]{0, 50, 150, 400, 800, 1400, 2000, 2500};
   private static final int[] RANK_COLORS = new int[]{-4800823, -8857462, -10365488, -10114561, -15269, -36752, -3372033, -5990};
   private static volatile Snapshot snapshot = new Snapshot(0, 0, 0, 0, 0, 0, 0, "vanilla", true, 0, 0, 0, 0, true, 0, 0);
   private static KeyBinding hudToggle;
   private static KeyBinding enderiteTeleport;

   public void onInitializeClient() {
      KeyBinding.Category category = KeyBinding.Category.create(Identifier.of("operationdroploot", "controls"));
      hudToggle = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.operationdroploot.toggle_hud", 79, category));
      enderiteTeleport = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.operationdroploot.enderite_teleport", 86, category));
      ClientPlayNetworking.registerGlobalReceiver(SnapshotPayload.ID, (payload, context) -> snapshot = DropLootClient.Snapshot.parse(payload.data()));
      HudElementRegistry.addLast(Identifier.of("operationdroploot", "progress_hud"), DropLootClient::render);
      ClientTickEvents.END_CLIENT_TICK.register((ClientTickEvents.EndTick)(client) -> {
         if (hudToggle.wasPressed() && client.player != null) {
            client.player.networkHandler.sendChatCommand("hud toggle");
         }

         if (enderiteTeleport.wasPressed() && client.player != null) {
            ClientPlayNetworking.send(new EnderiteTeleportPayload());
         }

      });
   }

   private static void render(DrawContext context, RenderTickCounter tickCounter) {
      MinecraftClient client = MinecraftClient.getInstance();
      Snapshot data = snapshot;
      if (client.player != null && !client.options.hudHidden && data.hudVisible) {
         int screenWidth = client.getWindow().getScaledWidth();
         int width = Math.min(210, screenWidth - 12);
         if (width >= 150) {
            int height = 148;
            int x = screenWidth - width - 6;
            int y = 6;
            TextRenderer font = client.textRenderer;
            int rank = rankForXp(data.xp);
            int rankColor = RANK_COLORS[rank];
            int currentFloor = THRESHOLDS[rank];
            int target = rank == 7 ? 2500 : THRESHOLDS[rank + 1];
            float progress = target <= currentFloor ? 1.0F : Math.max(0.0F, Math.min(1.0F, (float)(data.xp - currentFloor) / (float)(target - currentFloor)));
            context.fill(x, y, x + width, y + height, -401138650);
            context.fill(x + 1, y + 1, x + width - 1, y + height - 1, -451075024);
            context.fill(x, y, x + width, y + 2, -15269);
            context.fill(x + 1, y + 2, x + 3, y + height - 1, rankColor);
            context.fill(x + 6, y + 3, x + width - 6, y + 4, 1429947989);
            context.drawTextWithShadow(font, "OPERATION · DROPLOOT", x + 8, y + 7, -986120);
            context.drawTextWithShadow(font, "V2", x + width - font.getWidth("V2") - 8, y + 7, -7760475);
            context.drawTextWithShadow(font, Text.literal(RANKS[rank]), x + 8, y + 21, rankColor);
            String xpText = format(data.xp) + " XP";
            context.drawTextWithShadow(font, xpText, x + width - font.getWidth(xpText) - 8, y + 21, -1643792);
            int pipWidth = 14;
            int pipGap = Math.max(2, (width - 16 - pipWidth * 8) / 7);

            for(int index = 0; index < 8; ++index) {
               int pipX = x + 8 + index * (pipWidth + pipGap);
               int color = index <= rank ? RANK_COLORS[index] : -13353911;
               context.fill(pipX, y + 34, pipX + pipWidth, y + 38, color);
               if (index == rank) {
                  context.fill(pipX, y + 33, pipX + pipWidth, y + 34, -1);
               }
            }

            int barX = x + 8;
            int barY = y + 45;
            int barWidth = width - 16;
            int gap = 2;
            int cellWidth = (barWidth - gap * 19) / 20;
            float cells = progress * 20.0F;

            for(int index = 0; index < 20; ++index) {
               int cellX = barX + index * (cellWidth + gap);
               context.fill(cellX, barY, cellX + cellWidth, barY + 6, -13617083);
               float amount = Math.max(0.0F, Math.min(1.0F, cells - (float)index));
               if (amount > 0.0F) {
                  int fillWidth = Math.max(1, Math.round((float)cellWidth * amount));
                  context.fill(cellX, barY, cellX + fillWidth, barY + 6, progressColor(((float)index + 0.5F) / 20.0F));
               }
            }

            String var10000 = format(data.xp);
            String xpProgress = var10000 + " / " + format(target) + " XP";
            String percent = Math.round(progress * 100.0F) + "%";
            context.drawTextWithShadow(font, xpProgress, x + 8, y + 55, -2630170);
            context.drawTextWithShadow(font, percent, x + width - font.getWidth(percent) - 8, y + 55, progressColor(progress));
            String next = rank == 7 ? "MAX RANK REACHED" : format(target - data.xp) + " XP TO " + RANKS[rank + 1] + " · TARGET " + format(target);
            context.drawTextWithShadow(font, fit(font, next, width - 16), x + 8, y + 66, -6313287);
            context.fill(x + 8, y + 79, x + width - 8, y + 80, -13024947);
            drawStatPair(context, font, x, y + 84, width, "KILLS", format(data.kills), "CHESTS", format(data.chests));
            drawStatPair(context, font, x, y + 96, width, "BOSSES", format(data.bosses), "STRUCTURES", format(data.structures));
            drawStatPair(context, font, x, y + 108, width, "MOBS", format(data.mobs) + " L+" + data.luck + "%", "MINED", format(data.blocksMined));
            switch (data.mode) {
               case "progression" -> var10000 = "MODE · PROGRESSION (UPGRADED DROPS)";
               case "hybrid" -> var10000 = "MODE · HYBRID (RAW + UPGRADED)";
               default -> var10000 = "MODE · VANILLA (RAW DROPS)";
            }

            String mode = var10000;
            int modeWidth = data.ascensions > 0 ? width - 16 - font.getWidth("FAVOUR " + data.ascensions) - 8 : width - 16;
            context.drawTextWithShadow(font, fit(font, mode, modeWidth), x + 8, y + 120, modeColor(data.mode));
            if (data.ascensions > 0) {
               String favour = "FAVOUR " + data.ascensions;
               context.drawTextWithShadow(font, favour, x + width - font.getWidth(favour) - 8, y + 120, -2509057);
            }

            String challenge = data.challengesEnabled ? challengeName(data.challenge) + "  " + Math.min(data.challengeProgress, challengeGoal(data.challenge)) + "/" + challengeGoal(data.challenge) : "CHALLENGES OFF";
            int challengeWidth = data.pendingRewards > 0 ? width - 16 - font.getWidth("REWARD READY") - 6 : width - 16;
            context.drawTextWithShadow(font, fit(font, challenge, challengeWidth), x + 8, y + 132, data.challengesEnabled ? -4798503 : -8945781);
            if (data.pendingRewards > 0) {
               String reward = "REWARD READY";
               context.drawTextWithShadow(font, reward, x + width - font.getWidth(reward) - 8, y + 132, -11412);
            }

         }
      }
   }

   private static void drawStatPair(DrawContext context, TextRenderer font, int x, int y, int width, String leftLabel, String leftValue, String rightLabel, String rightValue) {
      int leftValueX = x + 8 + font.getWidth(leftLabel) + 4;
      context.drawTextWithShadow(font, leftLabel, x + 8, y, -7760475);
      context.drawTextWithShadow(font, leftValue, leftValueX, y, -1643792);
      int rightStart = x + width / 2 + 1;
      context.drawTextWithShadow(font, rightLabel, rightStart, y, -7760475);
      context.drawTextWithShadow(font, rightValue, x + width - font.getWidth(rightValue) - 8, y, -1643792);
   }

   private static int rankForXp(int xp) {
      int rank = 0;

      for(int index = 1; index < THRESHOLDS.length && xp >= THRESHOLDS[index]; rank = index++) {
      }

      return rank;
   }

   private static int progressColor(float progress) {
      return progress < 0.5F ? blend(-11171, -892073, progress * 2.0F) : blend(-892073, -10102384, (progress - 0.5F) * 2.0F);
   }

   private static int blend(int first, int second, float amount) {
      float value = Math.max(0.0F, Math.min(1.0F, amount));
      int red = (int)((float)(first >> 16 & 255) * (1.0F - value) + (float)(second >> 16 & 255) * value);
      int green = (int)((float)(first >> 8 & 255) * (1.0F - value) + (float)(second >> 8 & 255) * value);
      int blue = (int)((float)(first & 255) * (1.0F - value) + (float)(second & 255) * value);
      return -16777216 | red << 16 | green << 8 | blue;
   }

   private static int modeColor(String mode) {
      int var10000;
      switch (mode) {
         case "progression" -> var10000 = -15269;
         case "hybrid" -> var10000 = -9052988;
         default -> var10000 = -4669236;
      }

      return var10000;
   }

   private static String challengeName(int index) {
      String var10000;
      switch (index) {
         case 0 -> var10000 = "VARIED KILLS";
         case 1 -> var10000 = "CONTAINERS";
         case 2 -> var10000 = "ORE RUN";
         default -> var10000 = "EXPLORATION";
      }

      return var10000;
   }

   private static int challengeGoal(int index) {
      byte var10000;
      switch (index) {
         case 0 -> var10000 = 5;
         case 1 -> var10000 = 3;
         case 2 -> var10000 = 8;
         default -> var10000 = 2;
      }

      return var10000;
   }