package dev.crucifixbeta.mod.modules.impl.client.hud;

import dev.crucifixbeta.CrucifixBeta;
import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.DeathEvent;
import dev.crucifixbeta.api.utils.Wrapper;
import dev.crucifixbeta.api.utils.render.Render2DUtil;
import dev.crucifixbeta.api.utils.render.TextUtil;
import dev.crucifixbeta.core.impl.FontManager;
import dev.crucifixbeta.mod.modules.HudModule;
import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.impl.client.ColorsModule;
import dev.crucifixbeta.mod.modules.settings.impl.BooleanSetting;
import dev.crucifixbeta.mod.modules.settings.impl.EnumSetting;
import dev.crucifixbeta.mod.modules.settings.impl.SliderSetting;
import java.awt.Color;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Deque;
import java.util.Locale;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.math.MathHelper;
import org.lwjgl.glfw.GLFW;

@Environment(EnvType.CLIENT)
public final class LavenderHudModules {
   private static final Color DEFAULT_ACCENT;
   private static final Color DEFAULT_ACCENT_2;
   private static final Color DEFAULT_BG;
   private static final Color DEFAULT_STROKE;
   private static final DecimalFormat ONE_DECIMAL;

   private LavenderHudModules() {
   }

   private static void drawPanel(DrawContext ctx, float x, float y, float width, float height, String title, boolean showTitle, boolean panel) {
      if (panel) {
         Render2DUtil.drawRoundedRect(ctx.getMatrices(), x, y, width, height, 6.0F, background());
         Render2DUtil.drawRoundedStroke(ctx.getMatrices(), x, y, width, height, 6.0F, DEFAULT_STROKE, 12);
      }

      if (showTitle) {
         drawText(ctx, title, (double)(x + 6.0F), (double)(y + 4.0F), themeColor(0.0).getRGB());
         Render2DUtil.horizontalGradient(
            ctx.getMatrices(), x + 6.0F, y + 16.0F, x + width - 6.0F, y + 17.0F, withAlpha(themeColor(0.0), 210), withAlpha(themeColor(90.0), 90)
         );
      }
   }

   private static void drawKey(DrawContext ctx, String label, int x, int y, int w, int h, boolean pressed) {
      Color var7 = pressed ? withAlpha(themeColor(0.0), 205) : background();
      Render2DUtil.drawRoundedRect(ctx.getMatrices(), (float)x, (float)y, (float)w, (float)h, 4.0F, var7);
      Render2DUtil.drawRoundedStroke(ctx.getMatrices(), (float)x, (float)y, (float)w, (float)h, 4.0F, DEFAULT_STROKE, 10);
      int var8 = x + (w - textWidth(label)) / 2;
      int var9 = y + centerTextOffset(h);
      drawText(ctx, label, (double)var8, (double)var9, Color.WHITE.getRGB());
   }

   private static Color background() {
      ColorsModule var0 = ColorsModule.INSTANCE;
      if (var0 == null) {
         return DEFAULT_BG;
      } else {
         Color var1 = var0.getHudBackgroundColor(1.0);
         return var1.getAlpha() <= 0 ? DEFAULT_BG : var1;
      }
   }

   private static Color themeColor(double delay) {
      ColorsModule var2 = ColorsModule.INSTANCE;
      return var2 == null ? DEFAULT_ACCENT : var2.getHudColor(delay);
   }

   private static Color secondThemeColor(double delay) {
      ColorsModule var2 = ColorsModule.INSTANCE;
      return var2 == null ? DEFAULT_ACCENT_2 : var2.getHudColor(delay + 45.0);
   }

   private static void drawText(DrawContext ctx, String text, double x, double y, int color) {
      TextUtil.drawString(ctx, text, x, y, color, useCustomFont(), false);
   }

   private static void drawScaledText(DrawContext ctx, String text, double x, double y, int color, float scale) {
      ctx.getMatrices().push();
      ctx.getMatrices().translate((float)x, (float)y, 0.0F);
      ctx.getMatrices().scale(scale, scale, 1.0F);
      drawText(ctx, text, 0.0, 0.0, color);
      ctx.getMatrices().pop();
   }

   private static boolean useCustomFont() {
      return HudSetting.useFont() && FontManager.ui != null;
   }

   private static int textWidth(String text) {
      if (text == null || text.isEmpty()) {
         return 0;
      } else {
         return useCustomFont() ? (int)Math.ceil((double)FontManager.ui.getWidth(text)) : Wrapper.mc.textRenderer.getWidth(text);
      }
   }

   private static int textHeight() {
      return useCustomFont() ? (int)Math.ceil((double)FontManager.ui.getFontHeight()) : 9;
   }

   private static int centerTextOffset(int height) {
      return Math.max(0, (height - textHeight()) / 2);
   }

   private static void drawPlayerHead(DrawContext ctx, AbstractClientPlayerEntity player, int x, int y, int size) {
      Identifier var5 = player.getSkinTextures().texture();
      ctx.fill(x, y, x + size, y + size, -13619146);
      ctx.drawTexture(var5, x, y, size, size, 8.0F, 8.0F, 8, 8, 64, 64);

      try {
         ctx.drawTexture(var5, x, y, size, size, 40.0F, 8.0F, 8, 8, 64, 64);
      } catch (Exception var7) {
      }
   }

   private static Identifier effectIcon(StatusEffectInstance effect) {
      Identifier var1 = Registries.STATUS_EFFECT.getId(effect.getEffectType().value());
      String var2 = var1 == null ? "speed" : var1.getPath();
      return Identifier.of("minecraft", "textures/mob_effect/" + var2 + ".png");
   }

   private static String formatDuration(StatusEffectInstance effect) {
      if (effect.isInfinite()) {
         return "Inf";
      } else {
         int var1 = Math.max(0, effect.getDuration() / 20);
         return String.format(Locale.ROOT, "%d:%02d", var1 / 60, var1 % 60);
      }
   }

   private static String romanLevel(int amplifier) {
      int var1 = amplifier + 1;
      if (var1 <= 1) {
         return "";
      } else {
         return " " + switch (var1) {
            case 2 -> "II";
            case 3 -> "III";
            case 4 -> "IV";
            case 5 -> "V";
            case 6 -> "VI";
            case 7 -> "VII";
            case 8 -> "VIII";
            case 9 -> "IX";
            case 10 -> "X";
            default -> String.valueOf(var1);
         };
      }
   }

   private static Color durabilityColor(ItemStack stack) {
      if (stack.isDamageable() && stack.getMaxDamage() > 0) {
         float var1 = 1.0F - MathHelper.clamp((float)stack.getDamage() / (float)stack.getMaxDamage(), 0.0F, 1.0F);
         return healthColor(var1);
      } else {
         return Color.WHITE;
      }
   }

   private static Color healthColor(float pct) {
      pct = MathHelper.clamp(pct, 0.0F, 1.0F);
      if (pct > 0.75F) {
         return new Color(66, 246, 123);
      } else if (pct > 0.5F) {
         return new Color(228, 255, 105);
      } else {
         return pct > 0.35F ? new Color(236, 100, 64) : new Color(255, 65, 88);
      }
   }

   private static Color brighten(Color color, float factor) {
      int var2 = (int)MathHelper.clamp((float)color.getRed() * factor, 0.0F, 255.0F);
      int var3 = (int)MathHelper.clamp((float)color.getGreen() * factor, 0.0F, 255.0F);
      int var4 = (int)MathHelper.clamp((float)color.getBlue() * factor, 0.0F, 255.0F);
      return new Color(var2, var3, var4, color.getAlpha());
   }

   private static Color withAlpha(Color color, int alpha) {
      return new Color(color.getRed(), color.getGreen(), color.getBlue(), MathHelper.clamp(alpha, 0, 255));
   }

   private static String formatElapsed(long millis) {
      long var2 = Math.max(0L, millis / 1000L);
      long var4 = var2 % 60L;
      long var6 = var2 / 60L % 60L;
      long var8 = var2 / 3600L;
      return String.format(Locale.ROOT, "%02d:%02d:%02d", var8, var6, var4);
   }

   private static String serverName() {
      return !Wrapper.mc.isInSingleplayer() && Wrapper.mc.getCurrentServerEntry() != null
         ? Wrapper.mc.getCurrentServerEntry().address
         : "SinglePlayer";
   }

   private static String direction(int degree) {
      return switch ((degree % 360 + 360) % 360) {
         case 0 -> "N";
         case 45 -> "NE";
         case 90 -> "E";
         case 135 -> "SE";
         case 180 -> "S";
         case 225 -> "SW";
         case 270 -> "W";
         case 315 -> "NW";
         default -> String.valueOf(degree);
      };
   }

   static {

      DEFAULT_ACCENT = new Color(224, 80, 255, 255);
      DEFAULT_ACCENT_2 = new Color(255, 107, 214, 255);
      DEFAULT_BG = new Color(12, 12, 16, 150);
      DEFAULT_STROKE = new Color(255, 255, 255, 38);
      ONE_DECIMAL = new DecimalFormat("0.0");
   }
@Environment(EnvType.CLIENT)
   public static final class ArmorHud extends LavenderHudModules.PanelHudModule {
      private final EnumSetting<LavenderHudModules.ArmorHud.Alignment> alignment = this.add(
         new EnumSetting<>("Alignment", LavenderHudModules.ArmorHud.Alignment.Vertical)
      );
      private final BooleanSetting durability = this.add(new BooleanSetting("Durability", true));
      private final BooleanSetting showEmpty = this.add(new BooleanSetting("ShowEmpty", true));

      public ArmorHud() {
         super("Armor", "\u62a4\u7532", 14, 210, true);
      }

      @Override
      public void onRender2D(DrawContext ctx, float tickDelta) {
         if (mc.player == null) {
            this.clearHudBounds();
         } else {
            ArrayList<ItemStack> var3 = new ArrayList<>();

            for (int var4 = mc.player.getInventory().armor.size() - 1; var4 >= 0; var4--) {
               ItemStack var5 = mc.player.getInventory().armor.get(var4);
               if (!var5.isEmpty()) {
                  var3.add(var5);
               }
            }

            if (var3.isEmpty() && !this.showEmpty.getValue()) {
               this.clearHudBounds();
            } else {
               boolean var17 = this.alignment.getValue() == LavenderHudModules.ArmorHud.Alignment.Vertical;
               int var18 = this.headerHeight();
               int var6 = Math.max(1, var3.size());
               int var7 = var17 ? 78 : Math.max(72, 10 + var6 * 22);
               int var8 = var17 ? var18 + var6 * 18 + 7 : var18 + 27;
               int var9 = this.getHudRenderX(var7);
               int var10 = this.getHudRenderY(var8);
               this.drawPanel(ctx, (float)var9, (float)var10, (float)var7, (float)var8, "Armor");
               if (var3.isEmpty()) {
                  LavenderHudModules.drawText(ctx, "Empty..", (double)(var9 + 7), (double)(var10 + var18 + 2), Color.WHITE.getRGB());
                  this.setHudBounds(var9, var10, var7, var8);
               } else {
                  for (int var11 = 0; var11 < var3.size(); var11++) {
                     ItemStack var12 = (ItemStack)var3.get(var11);
                     int var13 = var17 ? var9 + 7 : var9 + 7 + var11 * 22;
                     int var14 = var17 ? var10 + var18 + var11 * 18 : var10 + var18 + 3;
                     ctx.drawItem(var12, var13, var14);
                     ctx.drawItemInSlot(mc.textRenderer, var12, var13, var14);
                     if (this.durability.getValue() && var12.isDamageable()) {
                        String var15 = String.valueOf(Math.max(0, var12.getMaxDamage() - var12.getDamage()));
                        int var16 = LavenderHudModules.durabilityColor(var12).getRGB();
                        if (var17) {
                           LavenderHudModules.drawText(ctx, var15, (double)(var13 + 20), (double)(var14 + 5), var16);
                        } else {
                           LavenderHudModules.drawScaledText(ctx, var15, (double)(var13 + 1), (double)(var14 + 16), var16, 0.62F);
                        }
                     }
                  }

                  this.setHudBounds(var9, var10, var7, var8);
               }
            }
         }
      }
@Environment(EnvType.CLIENT)
      public static enum Alignment {
         Vertical,
         Horizontal;

}
   }

   @Environment(EnvType.CLIENT)
   public static final class BpsCounterHud extends LavenderHudModules.MetricHudModule {

      public BpsCounterHud() {
         super("BPSCounter", "\u901f\u5ea6", 70, 390, false);
      }

      @Override
      protected String label() {
         return "BPS";
      }

      @Override
      protected String value() {
         if (mc.player == null) {
            return "0.0";
         } else {
            double var1 = mc.player.getX() - mc.player.prevX;
            double var3 = mc.player.getZ() - mc.player.prevZ;
            double var5 = CrucifixBeta.TIMER == null ? 1.0 : (double)CrucifixBeta.TIMER.get();
            return LavenderHudModules.ONE_DECIMAL.format(Math.sqrt(var1 * var1 + var3 * var3) * 20.0 * var5);
         }
      }
}

   @Environment(EnvType.CLIENT)
   public static final class ClockHud extends LavenderHudModules.PanelHudModule {

      public ClockHud() {
         super("Clock", "\u65f6\u949f", 14, 430, false);
      }

      @Override
      public void onRender2D(DrawContext ctx, float tickDelta) {
         byte var3 = 72;
         byte var4 = 65;
         int var5 = this.getHudRenderX(var3);
         int var6 = this.getHudRenderY(var4);
         this.drawPanel(ctx, (float)var5, (float)var6, (float)var3, (float)var4, "Clock");
         LocalTime var7 = LocalTime.now();
         LocalDate var8 = LocalDate.now();
         int var9 = var7.getHour() % 12;
         int var10 = var7.getMinute();
         String var11 = String.format(Locale.ROOT, "%02d", var9 == 0 ? 12 : var9);
         String var12 = String.format(Locale.ROOT, "%02d", var10);
         String var13 = var8.getDayOfWeek().name().substring(0, 3).toLowerCase(Locale.ROOT);
         String var14 = var8.format(DateTimeFormatter.ofPattern("dd/MM", Locale.ROOT));
         float var15 = (float)var5 + (float)var3 / 2.0F;
         float var16 = (float)var6 + (float)var4 / 2.0F;

         for (byte var17 = 0; var17 < 360; var17 += 30) {
            double var18 = Math.toRadians((double)var17);
            float var20 = var15 + (float)Math.sin(var18) * 25.0F;
            float var21 = var16 - (float)Math.cos(var18) * 25.0F;
            float var22 = var15 + (float)Math.sin(var18) * 27.0F;
            float var23 = var16 - (float)Math.cos(var18) * 27.0F;
            Render2DUtil.drawLine(ctx.getMatrices(), var20, var21, var22, var23, new Color(160, 160, 160, 170).getRGB());
         }

         double var24 = Math.toRadians((double)var9 * 30.0 + (double)var10 / 2.0);
         Render2DUtil.drawLine(
            ctx.getMatrices(),
            var15,
            var16,
            var15 + (float)Math.sin(var24) * 19.0F,
            var16 - (float)Math.cos(var24) * 19.0F,
            LavenderHudModules.themeColor(0.0).getRGB()
         );
         LavenderHudModules.drawScaledText(ctx, var11, (double)(var5 + 10), (double)(var6 + 17), Color.WHITE.getRGB(), 1.45F);
         LavenderHudModules.drawScaledText(ctx, var12, (double)(var5 + 42), (double)(var6 + 17), Color.WHITE.getRGB(), 1.45F);
         LavenderHudModules.drawText(ctx, var13, (double)(var5 + 11), (double)(var6 + 44), Color.WHITE.getRGB());
         LavenderHudModules.drawText(ctx, var14, (double)(var5 + 42), (double)(var6 + 44), new Color(220, 220, 220).getRGB());
         this.setHudBounds(var5, var6, var3, var4);
      }
}

   @Environment(EnvType.CLIENT)
   public static final class CompassHud extends LavenderHudModules.PanelHudModule {

      public CompassHud() {
         super("Compass", "\u6307\u5357\u9488", 96, 430, false);
      }

      @Override
      public void onRender2D(DrawContext ctx, float tickDelta) {
         if (mc.player == null) {
            this.clearHudBounds();
         } else {
            short var3 = 260;
            byte var4 = 35;
            int var5 = this.getHudRenderX(var3);
            int var6 = this.getHudRenderY(var4);
            this.drawPanel(ctx, (float)var5, (float)var6, (float)var3, (float)var4, "Compass");
            float var7 = MathHelper.wrapDegrees(mc.player.getYaw()) + 180.0F;
            if (var7 < 0.0F) {
               var7 += 360.0F;
            }

            float var8 = (float)var5 + (float)var3 / 2.0F;

            for (byte var9 = 0; var9 < 360; var9 += 15) {
               float var10 = MathHelper.wrapDegrees((float)var9 - var7);
               float var11 = var8 + var10 * 2.35F;
               if (!(var11 < (float)(var5 + 8)) && !(var11 > (float)(var5 + var3 - 8))) {
                  boolean var12 = var9 % 45 == 0;
                  Render2DUtil.drawLine(
                     ctx.getMatrices(),
                     var11,
                     (float)(var6 + (var12 ? 8 : 12)),
                     var11,
                     (float)(var6 + 18),
                     new Color(var12 ? 180 : 110, var12 ? 180 : 110, var12 ? 180 : 110, 200).getRGB()
                  );
                  if (var12) {
                     String var13 = LavenderHudModules.direction(var9);
                     LavenderHudModules.drawText(
                        ctx, var13, (double)(var11 - (float)(LavenderHudModules.textWidth(var13) / 2)), (double)(var6 + 21), Color.WHITE.getRGB()
                     );
                  } else if (var9 % 30 == 0) {
                     String var15 = String.valueOf((int)var9);
                     LavenderHudModules.drawScaledText(
                        ctx,
                        var15,
                        (double)(var11 - (float)LavenderHudModules.textWidth(var15) * 0.3F),
                        (double)(var6 + 21),
                        new Color(150, 150, 150).getRGB(),
                        0.6F
                     );
                  }
               }
            }

            Render2DUtil.drawLine(ctx.getMatrices(), var8, (float)(var6 + 6), var8, (float)(var6 + 21), new Color(250, 50, 86).getRGB());
            String var14 = String.valueOf(Math.round(var7));
            LavenderHudModules.drawText(ctx, var14, (double)(var8 - (float)(LavenderHudModules.textWidth(var14) / 2)), (double)(var6 - 2), Color.WHITE.getRGB());
            this.setHudBounds(var5, var6, var3, var4);
         }
      }
}

   @Environment(EnvType.CLIENT)
   public static final class CpsCounterHud extends LavenderHudModules.MetricHudModule {
      private final Deque<Long> clicks = new ArrayDeque<>();
      private boolean lastPressed;

      public CpsCounterHud() {
         super("CPSCounter", "\u70b9\u51fb", 126, 390, false);
      }

      @Override
      protected String label() {
         return "CPS";
      }

      @Override
      protected String value() {
         long var1 = System.currentTimeMillis();
         if (mc.getWindow() != null) {
            boolean var3 = GLFW.glfwGetMouseButton(mc.getWindow().getHandle(), 0) == 1;
            if (var3 && !this.lastPressed) {
               this.clicks.addLast(var1);
            }

            this.lastPressed = var3;
         }

         while (!this.clicks.isEmpty() && this.clicks.peekFirst() + 1000L < var1) {
            this.clicks.pollFirst();
         }

         return String.valueOf(this.clicks.size());
      }
}

   @Environment(EnvType.CLIENT)
   public static final class FpsCounterHud extends LavenderHudModules.MetricHudModule {

      public FpsCounterHud() {
         super("FPSCounter", "\u5e27\u7387", 14, 390, false);
      }

      @Override
      protected String label() {
         return "FPS";
      }

      @Override
      protected String value() {
         return String.valueOf(CrucifixBeta.FPS == null ? 0 : CrucifixBeta.FPS.getFps());
      }
}

   @Environment(EnvType.CLIENT)
   public static final class HealthHud extends LavenderHudModules.PanelHudModule {

      public HealthHud() {
         super("Health", "\u8840\u91cf", 670, 390, false);
      }

      @Override
      public void onRender2D(DrawContext ctx, float tickDelta) {
         if (mc.player == null) {
            this.clearHudBounds();
         } else {
            float var3 = mc.player.getHealth() + mc.player.getAbsorptionAmount();
            float var4 = Math.max(1.0F, mc.player.getMaxHealth() + mc.player.getAbsorptionAmount());
            float var5 = MathHelper.clamp(var3 / var4, 0.0F, 1.0F);
            String var6 = LavenderHudModules.ONE_DECIMAL.format((double)var3) + " / " + LavenderHudModules.ONE_DECIMAL.format((double)var4);
            int var7 = Math.max(110, LavenderHudModules.textWidth("Health") + LavenderHudModules.textWidth(var6) + 18);
            byte var8 = 31;
            int var9 = this.getHudRenderX(var7);
            int var10 = this.getHudRenderY(var8);
            this.drawPanel(ctx, (float)var9, (float)var10, (float)var7, (float)var8, "Health");
            LavenderHudModules.drawText(ctx, "Health", (double)(var9 + 7), (double)(var10 + 6), LavenderHudModules.themeColor(0.0).getRGB());
            LavenderHudModules.drawText(ctx, var6, (double)(var9 + var7 - 7 - LavenderHudModules.textWidth(var6)), (double)(var10 + 6), Color.WHITE.getRGB());
            Render2DUtil.drawRoundedRect(ctx.getMatrices(), (float)(var9 + 7), (float)(var10 + 21), (float)(var7 - 14), 5.0F, 2.5F, new Color(0, 0, 0, 130));
            Render2DUtil.drawRoundedRect(
               ctx.getMatrices(),
               (float)(var9 + 7),
               (float)(var10 + 21),
               Math.max(2.0F, (float)(var7 - 14) * var5),
               5.0F,
               2.5F,
               LavenderHudModules.healthColor(var5)
            );
            this.setHudBounds(var9, var10, var7, var8);
         }
      }
}

   @Environment(EnvType.CLIENT)
   public static final class InventoryHud extends LavenderHudModules.PanelHudModule {

      public InventoryHud() {
         super("InventoryHud", "\u80cc\u5305HUD", 220, 210, true);
      }

      @Override
      public void onRender2D(DrawContext ctx, float tickDelta) {
         if (mc.player == null) {
            this.clearHudBounds();
         } else {
            int var3 = this.headerHeight();
            short var4 = 172;
            int var5 = var3 + 62;
            int var6 = this.getHudRenderX(var4);
            int var7 = this.getHudRenderY(var5);
            this.drawPanel(ctx, (float)var6, (float)var7, (float)var4, (float)var5, "InventoryHud");
            boolean var8 = false;

            for (int var9 = 9; var9 < Math.min(36, mc.player.getInventory().main.size()); var9++) {
               ItemStack var10 = mc.player.getInventory().main.get(var9);
               if (!var10.isEmpty()) {
                  var8 = true;
               }

               int var11 = var9 - 9;
               int var12 = var6 + 5 + var11 % 9 * 18;
               int var13 = var7 + var3 + 3 + var11 / 9 * 18;
               if (!var10.isEmpty()) {
                  ctx.drawItem(var10, var12, var13);
                  ctx.drawItemInSlot(mc.textRenderer, var10, var12, var13);
               }
            }

            if (!var8) {
               String var14 = "Empty...";
               LavenderHudModules.drawText(
                  ctx, var14, (double)(var6 + (var4 - LavenderHudModules.textWidth(var14)) / 2), (double)(var7 + var3 + 22), Color.WHITE.getRGB()
               );
            }

            this.setHudBounds(var6, var7, var4, var5);
         }
      }
}

   @Environment(EnvType.CLIENT)
   public static final class KeyBindsHud extends LavenderHudModules.PanelHudModule {
      private final BooleanSetting onlyState = this.add(new BooleanSetting("OnlyState", false));
      private final SliderSetting maxRows = this.add(new SliderSetting("MaxRows", 16, 1, 40));

      public KeyBindsHud() {
         super("KeyBinds", "\u6309\u952e\u7ed1\u5b9a", 370, 390, false);
      }

      @Override
      public void onRender2D(DrawContext ctx, float tickDelta) {
         if (CrucifixBeta.MODULE == null) {
            this.clearHudBounds();
         } else {
            ArrayList<Module> var3 = new ArrayList<>();

            for (Module var5 : CrucifixBeta.MODULE.getModules()) {
               if (var5.getBindSetting().getValue() != -1 && (!this.onlyState.getValue() || !var5.isOff())) {
                  var3.add(var5);
               }
            }

            var3.sort(Comparator.comparing(Module::getDisplayName));
            if (var3.size() > this.maxRows.getValueInt()) {
               var3 = new ArrayList<>(var3.subList(0, this.maxRows.getValueInt()));
            }

            int var14 = this.headerHeight();
            int var15 = LavenderHudModules.textHeight() + 3;
            int var6 = 95;

            for (Module var8 : var3) {
               String var9 = var8.getBindSetting().getKeyString() + " " + (var8.isOn() ? "[True]" : "[False]");
               var6 = Math.max(var6, 15 + LavenderHudModules.textWidth(var8.getDisplayName()) + LavenderHudModules.textWidth(var9) + 10);
            }

            int var16 = var14 + Math.max(1, var3.size()) * var15 + 7;
            int var17 = this.getHudRenderX(var6);
            int var18 = this.getHudRenderY(var16);
            this.drawPanel(ctx, (float)var17, (float)var18, (float)var6, (float)var16, "KeyBinds");
            if (var3.isEmpty()) {
               LavenderHudModules.drawText(ctx, "Empty...", (double)(var17 + 7), (double)(var18 + var14 + 3), Color.WHITE.getRGB());
               this.setHudBounds(var17, var18, var6, var16);
            } else {
               int var10 = var18 + var14 + 3;

               for (Module var12 : var3) {
                  String var13 = var12.getBindSetting().getKeyString() + " " + (var12.isOn() ? "[True]" : "[False]");
                  LavenderHudModules.drawText(ctx, var12.getDisplayName(), (double)(var17 + 7), (double)var10, Color.WHITE.getRGB());
                  LavenderHudModules.drawText(
                     ctx,
                     var13,
                     (double)(var17 + var6 - 7 - LavenderHudModules.textWidth(var13)),
                     (double)var10,
                     var12.isOn() ? LavenderHudModules.themeColor(0.0).getRGB() : new Color(130, 130, 130).getRGB()
                  );
                  var10 += var15;
               }

               this.setHudBounds(var17, var18, var6, var16);
            }
         }
      }
}

   @Environment(EnvType.CLIENT)
   public static final class KeyStrokesHud extends HudModule {
      private final BooleanSetting space = this.add(new BooleanSetting("Space", true));

      public KeyStrokesHud() {
         super("KeyStrokes", "\u6309\u952e\u663e\u793a", 480, 390);
      }

      @Override
      public void onRender2D(DrawContext ctx, float tickDelta) {
         if (mc.options == null) {
            this.clearHudBounds();
         } else {
            byte var3 = 22;
            byte var4 = 3;
            int var5 = var3 * 3 + var4 * 2;
            int var6 = var3 * 2 + var4 + (this.space.getValue() ? var4 + 18 : 0);
            int var7 = this.getHudRenderX(var5);
            int var8 = this.getHudRenderY(var6);
            LavenderHudModules.drawKey(ctx, "W", var7 + var3 + var4, var8, var3, var3, mc.options.forwardKey.isPressed());
            LavenderHudModules.drawKey(ctx, "A", var7, var8 + var3 + var4, var3, var3, mc.options.leftKey.isPressed());
            LavenderHudModules.drawKey(ctx, "S", var7 + var3 + var4, var8 + var3 + var4, var3, var3, mc.options.backKey.isPressed());
            LavenderHudModules.drawKey(
               ctx, "D", var7 + (var3 + var4) * 2, var8 + var3 + var4, var3, var3, mc.options.rightKey.isPressed()
            );
            if (this.space.getValue()) {
               LavenderHudModules.drawKey(ctx, "Space", var7, var8 + (var3 + var4) * 2, var5, 18, mc.options.jumpKey.isPressed());
            }

            this.setHudBounds(var7, var8, var5, var6);
         }
      }
}

   @Environment(EnvType.CLIENT)
   public static final class LevelCounterHud extends LavenderHudModules.MetricHudModule {

      public LevelCounterHud() {
         super("LevelCounter", "\u7b49\u7ea7", 270, 390, false);
      }

      @Override
      protected String label() {
         return "Level";
      }

      @Override
      protected String value() {
         return mc.player == null ? "0" : String.valueOf(mc.player.experienceLevel);
      }
}

   @Environment(EnvType.CLIENT)
   public static final class MemoryHud extends LavenderHudModules.MetricHudModule {

      public MemoryHud() {
         super("Memory", "\u5185\u5b58", 186, 390, true);
      }

      @Override
      protected String label() {
         return "Mem:";
      }

      @Override
      protected String value() {
         Runtime var1 = Runtime.getRuntime();
         long var2 = var1.totalMemory() - var1.freeMemory();
         long var4 = Math.max(1L, var1.maxMemory());
         return var2 * 100L / var4 + "%";
      }
}

   @Environment(EnvType.CLIENT)
   private abstract static class MetricHudModule extends HudModule {
      protected final BooleanSetting showTitle;
      protected final BooleanSetting panel;

      protected MetricHudModule(String name, String chinese, int defaultX, int defaultY, boolean defaultTitle) {
         super(name, chinese, defaultX, defaultY);
         this.showTitle = this.add(new BooleanSetting("Title", defaultTitle));
         this.panel = this.add(new BooleanSetting("Panel", true));
      }

      protected abstract String label();

      protected abstract String value();

      @Override
      public void onRender2D(DrawContext ctx, float tickDelta) {
         if (nullCheck()) {
            this.clearHudBounds();
         } else {
            String var3 = this.showTitle.getValue() ? this.label() + " " : "";
            String var4 = this.value();
            String var5 = var3 + var4;
            int var6 = Math.max(22, LavenderHudModules.textWidth(var5) + 8);
            byte var7 = 17;
            int var8 = this.getHudRenderX(var6);
            int var9 = this.getHudRenderY(var7);
            if (this.panel.getValue()) {
               Render2DUtil.drawRoundedRect(ctx.getMatrices(), (float)var8, (float)var9, (float)var6, (float)var7, 5.0F, LavenderHudModules.background());
               Render2DUtil.drawRoundedStroke(
                  ctx.getMatrices(), (float)var8, (float)var9, (float)var6, (float)var7, 5.0F, LavenderHudModules.DEFAULT_STROKE, 12
               );
            }

            int var10 = var8 + 4;
            int var11 = var9 + LavenderHudModules.centerTextOffset(var7);
            if (!var3.isEmpty()) {
               LavenderHudModules.drawText(ctx, var3, (double)var10, (double)var11, LavenderHudModules.themeColor(0.0).getRGB());
               var10 += LavenderHudModules.textWidth(var3);
            }

            LavenderHudModules.drawText(ctx, var4, (double)var10, (double)var11, Color.WHITE.getRGB());
            this.setHudBounds(var8, var9, var6, var7);
         }
      }
   }

   @Environment(EnvType.CLIENT)
   private abstract static class PanelHudModule extends HudModule {
      protected final BooleanSetting showTitle;
      protected final BooleanSetting panel;

      protected PanelHudModule(String name, String chinese, int defaultX, int defaultY, boolean defaultTitle) {
         super(name, chinese, defaultX, defaultY);
         this.showTitle = this.add(new BooleanSetting("Title", defaultTitle));
         this.panel = this.add(new BooleanSetting("Panel", true));
      }

      protected int headerHeight() {
         return this.showTitle.getValue() ? 19 : 6;
      }

      protected void drawPanel(DrawContext ctx, float x, float y, float width, float height, String title) {
         LavenderHudModules.drawPanel(ctx, x, y, width, height, title, this.showTitle.getValue(), this.panel.getValue());
      }
   }

   @Environment(EnvType.CLIENT)
   public static final class PlayerListHud extends LavenderHudModules.PanelHudModule {
      private final BooleanSetting showSelf = this.add(new BooleanSetting("Self", true));
      private final BooleanSetting showFriends = this.add(new BooleanSetting("Friends", true));
      private final BooleanSetting avatars = this.add(new BooleanSetting("Avatars", true));
      private final SliderSetting maxRows = this.add(new SliderSetting("MaxRows", 12, 1, 40));

      public PlayerListHud() {
         super("PlayerListHud", "\u73a9\u5bb6\u5217\u8868", 14, 310, false);
      }

      @Override
      public void onRender2D(DrawContext ctx, float tickDelta) {
         if (mc.player != null && mc.world != null) {
            ArrayList<LavenderHudModules.PlayerListHud.PlayerRow> var3 = new ArrayList<>();

            for (AbstractClientPlayerEntity var5 : mc.world.getPlayers()) {
               if (var5 != null && var5.isAlive() && (var5 != mc.player || this.showSelf.getValue())) {
                  boolean var6 = CrucifixBeta.FRIEND != null && CrucifixBeta.FRIEND.isFriend(var5);
                  if (!var6 || this.showFriends.getValue()) {
                     var3.add(LavenderHudModules.PlayerListHud.PlayerRow.from(var5, var6));
                  }
               }
            }

            var3.sort(Comparator.comparing(row -> row.name.toLowerCase(Locale.ROOT)));
            if (var3.size() > this.maxRows.getValueInt()) {
               var3 = new ArrayList<>(var3.subList(0, this.maxRows.getValueInt()));
            }

            int var15 = this.avatars.getValue() ? 18 : 0;
            int var16 = this.headerHeight();
            int var17 = Math.max(16, LavenderHudModules.textHeight() + 5);
            int var7 = LavenderHudModules.textWidth("Empty...");

            for (LavenderHudModules.PlayerListHud.PlayerRow var9 : var3) {
               var7 = Math.max(var7, LavenderHudModules.textWidth(var9.text));
            }

            int var18 = Math.max(130, 12 + var15 + var7);
            int var19 = var16 + Math.max(1, var3.size()) * var17 + 7;
            int var10 = this.getHudRenderX(var18);
            int var11 = this.getHudRenderY(var19);
            this.drawPanel(ctx, (float)var10, (float)var11, (float)var18, (float)var19, "PlayerListHud");
            if (var3.isEmpty()) {
               LavenderHudModules.drawText(ctx, "Empty...", (double)(var10 + 7), (double)(var11 + var16 + 4), Color.WHITE.getRGB());
               this.setHudBounds(var10, var11, var18, var19);
            } else {
               int var12 = var11 + var16 + 3;

               for (LavenderHudModules.PlayerListHud.PlayerRow var14 : var3) {
                  if (this.avatars.getValue()) {
                     LavenderHudModules.drawPlayerHead(ctx, var14.player, var10 + 6, var12, 13);
                  }

                  LavenderHudModules.drawText(ctx, var14.text, (double)(var10 + 7 + var15), (double)(var12 + 3), var14.color.getRGB());
                  var12 += var17;
               }

               this.setHudBounds(var10, var11, var18, var19);
            }
         } else {
            this.clearHudBounds();
         }
      }
@Environment(EnvType.CLIENT)
      private static final class PlayerRow {
         private final AbstractClientPlayerEntity player;
         private final String name;
         private final String text;
         private final Color color;

         private PlayerRow(AbstractClientPlayerEntity player, String name, String text, Color color) {
            this.player = player;
            this.name = name;
            this.text = text;
            this.color = color;
         }

         private static LavenderHudModules.PlayerListHud.PlayerRow from(AbstractClientPlayerEntity player, boolean friend) {
            float var2 = player.getHealth() + player.getAbsorptionAmount();
            float var3 = Math.max(1.0F, player.getMaxHealth() + player.getAbsorptionAmount());
            int var4 = Math.round(MathHelper.clamp(var2 / var3, 0.0F, 1.0F) * 100.0F);
            String var5 = player == Wrapper.mc.player
               ? "[Me] "
               : (player.isTeammate(Wrapper.mc.player) ? "[Team] " : (friend ? "[Friend] " : ""));
            String var6 = player.getName().getString();
            Color var7 = friend ? new Color(96, 216, 255) : LavenderHudModules.healthColor((float)var4 / 100.0F);
            return new LavenderHudModules.PlayerListHud.PlayerRow(player, var6, var5 + var6 + " " + var4 + "%", var7);
         }
}
   }

   @Environment(EnvType.CLIENT)
   public static final class PotionHud extends LavenderHudModules.PanelHudModule {
      private final BooleanSetting icons = this.add(new BooleanSetting("Icons", true));

      public PotionHud() {
         super("PotionHud", "\u836f\u6c34HUD", 120, 210, true);
      }

      @Override
      public void onRender2D(DrawContext ctx, float tickDelta) {
         if (mc.player == null) {
            this.clearHudBounds();
         } else {
            ArrayList<StatusEffectInstance> var3 = new ArrayList<>(mc.player.getStatusEffects());
            var3.sort(Comparator.comparing(e -> e.getEffectType().value().getName().getString()));
            int var4 = this.headerHeight();
            byte var5 = 21;
            int var6 = Math.max(1, var3.size());
            short var7 = 132;
            int var8 = var4 + var6 * var5 + 6;
            int var9 = this.getHudRenderX(var7);
            int var10 = this.getHudRenderY(var8);
            this.drawPanel(ctx, (float)var9, (float)var10, (float)var7, (float)var8, "PotionHud");
            if (var3.isEmpty()) {
               LavenderHudModules.drawText(ctx, "Empty...", (double)(var9 + 7), (double)(var10 + var4 + 4), Color.WHITE.getRGB());
               this.setHudBounds(var9, var10, var7, var8);
            } else {
               int var11 = var10 + var4 + 2;

               for (StatusEffectInstance var13 : var3) {
                  Color var14 = new Color(var13.getEffectType().value().getColor() & 16777215);
                  int var15 = this.icons.getValue() ? 22 : 0;
                  if (this.icons.getValue()) {
                     Identifier var16 = LavenderHudModules.effectIcon(var13);

                     try {
                        ctx.drawTexture(var16, var9 + 6, var11 + 2, 0.0F, 0.0F, 16, 16, 16, 16);
                     } catch (Exception var18) {
                     }
                  }

                  String var19 = var13.getEffectType().value().getName().getString() + LavenderHudModules.romanLevel(var13.getAmplifier());
                  LavenderHudModules.drawText(ctx, var19, (double)(var9 + 7 + var15), (double)(var11 + 1), LavenderHudModules.brighten(var14, 1.25F).getRGB());
                  LavenderHudModules.drawText(
                     ctx, LavenderHudModules.formatDuration(var13), (double)(var9 + 7 + var15), (double)(var11 + 11), new Color(210, 210, 210).getRGB()
                  );
                  var11 += var5;
               }

               this.setHudBounds(var9, var10, var7, var8);
            }
         }
      }
}

   @Environment(EnvType.CLIENT)
   public static final class RadarHud extends LavenderHudModules.PanelHudModule {
      private final SliderSetting range = this.add(new SliderSetting("Range", 30.0, 8.0, 128.0, 1.0));
      private final BooleanSetting scanner = this.add(new BooleanSetting("Scanner", true));

      public RadarHud() {
         super("Radar", "\u96f7\u8fbe", 560, 390, false);
      }

      @Override
      public void onRender2D(DrawContext ctx, float tickDelta) {
         if (mc.player != null && mc.world != null) {
            byte var3 = 100;
            int var4 = this.getHudRenderX(var3);
            int var5 = this.getHudRenderY(var3);
            this.drawPanel(ctx, (float)var4, (float)var5, (float)var3, (float)var3, "Radar");
            float var6 = (float)var4 + (float)var3 / 2.0F;
            float var7 = (float)var5 + (float)var3 / 2.0F;
            Render2DUtil.drawLine(ctx.getMatrices(), (float)(var4 + 8), var7, (float)(var4 + var3 - 8), var7, new Color(255, 255, 255, 35).getRGB());
            Render2DUtil.drawLine(ctx.getMatrices(), var6, (float)(var5 + 8), var6, (float)(var5 + var3 - 8), new Color(255, 255, 255, 35).getRGB());
            if (this.scanner.getValue()) {
               double var8 = (double)System.currentTimeMillis() / 9.0 % 360.0;
               double var10 = Math.toRadians(var8);
               Render2DUtil.drawLine(
                  ctx.getMatrices(),
                  var6,
                  var7,
                  var6 + (float)Math.cos(var10) * 42.0F,
                  var7 + (float)Math.sin(var10) * 42.0F,
                  LavenderHudModules.withAlpha(LavenderHudModules.themeColor(0.0), 150).getRGB()
               );
            }

            double var28 = Math.toRadians((double)mc.player.getYaw());
            double var29 = Math.sin(var28);
            double var12 = Math.cos(var28);
            float var14 = 42.0F / this.range.getValueFloat();

            for (AbstractClientPlayerEntity var16 : mc.world.getPlayers()) {
               if (var16 != mc.player && var16.isAlive()) {
                  double var17 = var16.getX() - mc.player.getX();
                  double var19 = var16.getZ() - mc.player.getZ();
                  double var21 = Math.sqrt(var17 * var17 + var19 * var19);
                  if (!(var21 > this.range.getValue())) {
                     float var23 = (float)((var17 * var12 - var19 * var29) * (double)var14);
                     float var24 = (float)((var19 * var12 + var17 * var29) * (double)var14);
                     float var25 = MathHelper.clamp(var6 + var23, (float)(var4 + 8), (float)(var4 + var3 - 8));
                     float var26 = MathHelper.clamp(var7 + var24, (float)(var5 + 8), (float)(var5 + var3 - 8));
                     boolean var27 = CrucifixBeta.FRIEND != null && CrucifixBeta.FRIEND.isFriend(var16);
                     Render2DUtil.drawRoundedRect(
                        ctx.getMatrices(), var25 - 2.0F, var26 - 2.0F, 4.0F, 4.0F, 2.0F, var27 ? new Color(96, 216, 255) : LavenderHudModules.themeColor(var21)
                     );
                  }
               }
            }

            this.setHudBounds(var4, var5, var3, var3);
         } else {
            this.clearHudBounds();
         }
      }
}

   @Environment(EnvType.CLIENT)
   public static final class SessionInfoHud extends LavenderHudModules.PanelHudModule {
      private final BooleanSetting user = this.add(new BooleanSetting("User", true));
      private final BooleanSetting server = this.add(new BooleanSetting("Server", true));
      private final long startTime = System.currentTimeMillis();
      private int kills;
      private int deaths;

      public SessionInfoHud() {
         super("SessionInfo", "\u4f1a\u8bdd\u4fe1\u606f", 160, 310, false);
      }

      @EventListener
      public void onDeath(DeathEvent event) {
         if (event != null && event.getPlayer() != null && mc.player != null) {
            PlayerEntity var2 = event.getPlayer();
            if (var2 == mc.player) {
               this.deaths++;
            } else {
               boolean var3 = CrucifixBeta.FRIEND != null && CrucifixBeta.FRIEND.isFriend(var2);
               if (!var3 && mc.player.distanceTo(var2) <= 10.0F) {
                  this.kills++;
               }
            }
         }
      }

      @Override
      public void onRender2D(DrawContext ctx, float tickDelta) {
         if (mc.player == null) {
            this.clearHudBounds();
         } else {
            ArrayList<String[]> var3 = new ArrayList<>();
            var3.add(new String[]{"Elap", LavenderHudModules.formatElapsed(System.currentTimeMillis() - this.startTime)});
            var3.add(new String[]{"Kill", String.valueOf(this.kills)});
            var3.add(new String[]{"Death", String.valueOf(this.deaths)});
            if (this.user.getValue()) {
               var3.add(new String[]{"User", mc.player.getName().getString()});
            }

            if (this.server.getValue()) {
               var3.add(new String[]{"Server", LavenderHudModules.serverName()});
            }

            int var4 = this.headerHeight();
            int var5 = LavenderHudModules.textHeight() + 3;
            int var6 = 160;

            for (String[] var8 : var3) {
               var6 = Math.max(var6, 18 + LavenderHudModules.textWidth(var8[0]) + LavenderHudModules.textWidth(var8[1]) + 16);
            }

            int var14 = var4 + var3.size() * var5 + 7;
            int var15 = this.getHudRenderX(var6);
            int var9 = this.getHudRenderY(var14);
            this.drawPanel(ctx, (float)var15, (float)var9, (float)var6, (float)var14, "SessionInfo");
            int var10 = var9 + var4 + 3;

            for (int var11 = 0; var11 < var3.size(); var11++) {
               String[] var12 = (String[])var3.get(var11);
               Color var13 = LavenderHudModules.themeColor((double)var11 * 18.0);
               LavenderHudModules.drawText(ctx, var12[0], (double)(var15 + 7), (double)var10, var13.getRGB());
               LavenderHudModules.drawText(
                  ctx, var12[1], (double)(var15 + var6 - 7 - LavenderHudModules.textWidth(var12[1])), (double)var10, Color.WHITE.getRGB()
               );
               var10 += var5;
            }

            this.setHudBounds(var15, var9, var6, var14);
         }
      }
}

   @Environment(EnvType.CLIENT)
   public static final class TargetInfoHud extends LavenderHudModules.PanelHudModule {
      private final BooleanSetting nearest = this.add(new BooleanSetting("Nearest", true));
      private final SliderSetting nearestRange = this.add(new SliderSetting("Range", 8.0, 2.0, 32.0, 0.5));
      private final BooleanSetting distance = this.add(new BooleanSetting("Distance", true));
      private LivingEntity target;
      private long targetTime;

      public TargetInfoHud() {
         super("TargetInfo", "\u76ee\u6807\u4fe1\u606f", 330, 310, false);
      }

      @Override
      public void onRender2D(DrawContext ctx, float tickDelta) {
         if (mc.player != null && mc.world != null) {
            LivingEntity var3 = this.resolveTarget();
            if (var3 != null && var3.isAlive()) {
               String var4 = var3.getName().getString();
               float var5 = Math.max(0.0F, var3.getHealth() + var3.getAbsorptionAmount());
               float var6 = Math.max(1.0F, var3.getMaxHealth() + var3.getAbsorptionAmount());
               float var7 = MathHelper.clamp(var5 / var6, 0.0F, 1.0F);
               String var8 = LavenderHudModules.ONE_DECIMAL.format((double)var5);
               int var9 = Math.max(154, 58 + LavenderHudModules.textWidth(var4) + LavenderHudModules.textWidth(var8));
               byte var10 = 50;
               int var11 = this.getHudRenderX(var9);
               int var12 = this.getHudRenderY(var10);
               this.drawPanel(ctx, (float)var11, (float)var12, (float)var9, (float)var10, "TargetInfo");
               if (var3 instanceof AbstractClientPlayerEntity var13) {
                  LavenderHudModules.drawPlayerHead(ctx, var13, var11 + 8, var12 + 9, 32);
               } else {
                  Render2DUtil.drawRoundedRect(ctx.getMatrices(), (float)(var11 + 8), (float)(var12 + 9), 32.0F, 32.0F, 5.0F, new Color(50, 50, 55, 190));
                  String var14 = var4.isEmpty() ? "?" : var4.substring(0, 1).toUpperCase(Locale.ROOT);
                  LavenderHudModules.drawText(
                     ctx,
                     var14,
                     (double)(var11 + 24 - LavenderHudModules.textWidth(var14) / 2),
                     (double)(var12 + 20),
                     LavenderHudModules.themeColor(0.0).getRGB()
                  );
               }

               int var17 = var11 + 48;
               LavenderHudModules.drawText(ctx, "Name:", (double)var17, (double)(var12 + 10), Color.WHITE.getRGB());
               LavenderHudModules.drawText(
                  ctx,
                  var4,
                  (double)(var17 + LavenderHudModules.textWidth("Name:") + 3),
                  (double)(var12 + 10),
                  LavenderHudModules.themeColor(0.0).getRGB()
               );
               if (this.distance.getValue()) {
                  String var18 = LavenderHudModules.ONE_DECIMAL.format((double)mc.player.distanceTo(var3)) + "m";
                  LavenderHudModules.drawText(
                     ctx, var18, (double)(var11 + var9 - 7 - LavenderHudModules.textWidth(var18)), (double)(var12 + 10), new Color(210, 210, 210).getRGB()
                  );
               }

               float var19 = (float)var17;
               float var15 = (float)(var12 + 32);
               float var16 = (float)(var9 - 58);
               Render2DUtil.drawRoundedRect(ctx.getMatrices(), var19, var15, var16, 6.0F, 3.0F, new Color(0, 0, 0, 120));
               Render2DUtil.drawRoundedRect(ctx.getMatrices(), var19, var15, Math.max(2.0F, var16 * var7), 6.0F, 3.0F, LavenderHudModules.healthColor(var7));
               LavenderHudModules.drawText(
                  ctx, var8, (double)(var11 + var9 - 7 - LavenderHudModules.textWidth(var8)), (double)(var12 + 27), Color.WHITE.getRGB()
               );
               this.setHudBounds(var11, var12, var9, var10);
            } else {
               this.clearHudBounds();
            }
         } else {
            this.clearHudBounds();
         }
      }

      private LivingEntity resolveTarget() {
         long var1 = System.currentTimeMillis();
         if (mc.crosshairTarget instanceof EntityHitResult var4 && var4.getEntity() instanceof LivingEntity var6 && var6 != mc.player) {
            this.target = var6;
            this.targetTime = var1;
            return var6;
         }

         if (this.nearest.getValue()) {
            LivingEntity var7 = nearestPlayer(this.nearestRange.getValueFloat());
            if (var7 != null) {
               this.target = var7;
               this.targetTime = var1;
               return var7;
            }
         }

         if (this.target != null && this.target.isAlive() && var1 - this.targetTime < 3000L) {
            return this.target;
         } else {
            return mc.currentScreen != null ? mc.player : null;
         }
      }

      private static LivingEntity nearestPlayer(float range) {
         AbstractClientPlayerEntity var1 = null;
         double var2 = Double.MAX_VALUE;

         for (AbstractClientPlayerEntity var5 : mc.world.getPlayers()) {
            if (var5 != mc.player && var5.isAlive()) {
               double var6 = (double)mc.player.distanceTo(var5);
               if (var6 <= (double)range && var6 < var2) {
                  var1 = var5;
                  var2 = var6;
               }
            }
         }

         return var1;
      }
}
}
