Files
src
Code
6 348
src/main/java/com/lumix/client/ArmorHudRenderer.java
package com.lumix.client;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.network.PlayerListEntry;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.StatusEffectInstance;
import net.minecraft.text.Text;
import net.minecraft.util.hit.EntityHitResult;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public final class ArmorHudRenderer {
private static final int PANEL_WIDTH = 104;
private static final int PANEL_HEIGHT = 34;
private static final DateTimeFormatter CLOCK_FORMAT = DateTimeFormatter.ofPattern("HH:mm:ss");
private ArmorHudRenderer() {
}
public static void render(DrawContext context) {
MinecraftClient client = MinecraftClient.getInstance();
PlayerEntity player = client.player;
if (player == null || client.options.hudHidden) {
return;
}
int height = context.getScaledWindowHeight();
LivingEntity target = getTarget(client);
int targetY = height - 112;
renderCoordinates(context, client, player);
renderClock(context, client, player);
if (target != null) {
renderTarget(context, client, player, target, targetY);
}
renderPotions(context, client, player, target != null ? targetY - 20 : height - 66);
int x = 10;
int panelY = height - PANEL_HEIGHT - 10;
int labelY = panelY - 16;
context.fill(x, labelY, x + 67, labelY + 13, 0xB8202A3B);
context.fill(x, labelY, x + 2, labelY + 13, 0xFF43D6C5);
context.drawText(client.textRenderer, Text.literal("ARMOR HUD"), x + 6, labelY + 3, 0xFFE4EDF5, false);
context.fill(x, panelY, x + PANEL_WIDTH, panelY + PANEL_HEIGHT, 0xD90D1420);
context.fill(x, panelY, x + PANEL_WIDTH, panelY + 1, 0xFF43D6C5);
context.fill(x, panelY + PANEL_HEIGHT - 1, x + PANEL_WIDTH, panelY + PANEL_HEIGHT, 0xFF263A4B);
for (int index = 0; index < 4; index++) {
int slotX = x + 5 + index * 24;
ItemStack armor = player.getInventory().getArmorStack(3 - index);
context.fill(slotX, panelY + 4, slotX + 20, panelY + 24, 0xAA202B39);
if (armor.isEmpty()) {
continue;
}
context.drawItem(armor, slotX + 2, panelY + 5);
int barX = slotX + 1;
int barY = panelY + 27;
int barWidth = 18;
context.fill(barX, barY, barX + barWidth, barY + 3, 0xFF26313D);
int maxDamage = armor.getMaxDamage();
if (maxDamage > 0) {
float durability = (float) (maxDamage - armor.getDamage()) / maxDamage;
int durabilityWidth = Math.round(barWidth * durability);
int color = durability > 0.5F ? 0xFF43D6C5 : durability > 0.25F ? 0xFFFFC857 : 0xFFFF5B6E;
context.fill(barX, barY, barX + durabilityWidth, barY + 3, color);
}
}
}
private static void renderCoordinates(DrawContext context, MinecraftClient client, PlayerEntity player) {
int x = 10;
int y = 23;
context.fill(x, 7, x + 80, 20, 0xB8202A3B);
context.drawText(client.textRenderer, Text.literal("COORDINATES"), x + 6, 10, 0xFFE4EDF5, false);
context.fill(x, y, x + 176, y + 31, 0xD90D1420);
context.fill(x, y, x + 2, y + 31, 0xFF43D6C5);
context.drawText(client.textRenderer,
"X " + player.getBlockX() + " Y " + player.getBlockY() + " Z " + player.getBlockZ(),
x + 7, y + 4, 0xFFFFFFFF, false);
String dimension = player.getWorld().getRegistryKey().getValue().getPath().toUpperCase(Locale.ROOT);
context.drawText(client.textRenderer, dimension, x + 7, y + 17, 0xFF9EABB9, false);
}
private static void renderClock(DrawContext context, MinecraftClient client, PlayerEntity player) {
int width = 210;
int x = (context.getScaledWindowWidth() - width) / 2;
int y = 42;
context.fill(x, y, x + width, y + 25, 0xD90D1420);
context.fill(x, y, x + width, y + 1, 0xFF43D6C5);
PlayerListEntry entry = client.getNetworkHandler() == null
? null
: client.getNetworkHandler().getPlayerListEntry(player.getUuid());
String latency = entry == null ? "--" : Integer.toString(entry.getLatency());
String text = LocalTime.now().format(CLOCK_FORMAT) + " | "
+ MinecraftClient.getInstance().getCurrentFps() + " fps | " + latency + " ms";
int textWidth = client.textRenderer.getWidth(text);
context.drawText(client.textRenderer, text, x + (width - textWidth) / 2, y + 8, 0xFFE4EDF5, false);
}
private static LivingEntity getTarget(MinecraftClient client) {
if (client.crosshairTarget instanceof EntityHitResult hitResult
&& hitResult.getEntity() instanceof LivingEntity livingEntity
&& livingEntity.isAlive()) {
return livingEntity;
}
return null;
}
private static void renderTarget(DrawContext context, MinecraftClient client, PlayerEntity player,
LivingEntity target, int y) {
int x = 10;
int width = 190;
int height = 46;
context.fill(x, y - 16, x + 78, y - 3, 0xB8202A3B);
context.drawText(client.textRenderer, Text.literal("TARGET HUD"), x + 6, y - 13, 0xFFE4EDF5, false);
context.fill(x, y, x + width, y + height, 0xD90D1420);
context.fill(x, y, x + 2, y + height, 0xFF43D6C5);
context.drawText(client.textRenderer, target.getDisplayName(), x + 7, y + 5, 0xFFFFFFFF, false);
float maxHealth = Math.max(1.0F, target.getMaxHealth());
float health = Math.max(0.0F, target.getHealth());
String healthText = Math.round(health * 10.0F) / 10.0F + "/" + Math.round(maxHealth) + " HP";
String distanceText = Math.round(player.distanceTo(target) * 10.0F) / 10.0F + "m";
context.drawText(client.textRenderer, healthText, x + 7, y + 21, 0xFFB8C4D2, false);
int distanceWidth = client.textRenderer.getWidth(distanceText);
context.drawText(client.textRenderer, distanceText, x + width - distanceWidth - 7, y + 21, 0xFFB8C4D2, false);
int barX = x + 7;
int barY = y + 35;
int barWidth = width - 14;
context.fill(barX, barY, barX + barWidth, barY + 4, 0xFF26313D);
context.fill(barX, barY, barX + Math.round(barWidth * Math.min(1.0F, health / maxHealth)),
barY + 4, 0xFF43D6C5);
}
private static void renderPotions(DrawContext context, MinecraftClient client, PlayerEntity player, int bottomY) {
List<StatusEffectInstance> effects = new ArrayList<>(player.getStatusEffects());
if (effects.isEmpty()) {
return;
}
int x = 10;
int rowHeight = 20;
int visibleRows = Math.min(effects.size(), 6);
int panelHeight = visibleRows * rowHeight;
int y = Math.max(44, bottomY - panelHeight);
context.fill(x, y - 16, x + 65, y - 3, 0xB8202A3B);
context.drawText(client.textRenderer, Text.literal("POTIONS"), x + 6, y - 13, 0xFFE4EDF5, false);
for (int index = 0; index < visibleRows; index++) {
int rowY = y + index * rowHeight;
context.fill(x, rowY, x + 180, rowY + 18, 0xD90D1420);
context.fill(x, rowY, x + 2, rowY + 18, 0xFF43D6C5);
if (index == visibleRows - 1 && effects.size() > visibleRows) {
context.drawText(client.textRenderer, "+" + (effects.size() - visibleRows) + " more effects",
x + 7, rowY + 5, 0xFFE4EDF5, false);
continue;
}
StatusEffectInstance effect = effects.get(index);
String name = effect.getEffectType().getName().getString();
if (effect.getAmplifier() > 0) {
name += " " + (effect.getAmplifier() + 1);
}
String duration = formatDuration(effect.getDuration());
context.drawText(client.textRenderer, name, x + 7, rowY + 5, 0xFFE4EDF5, false);
int durationWidth = client.textRenderer.getWidth(duration);
context.drawText(client.textRenderer, duration, x + 173 - durationWidth, rowY + 5,
0xFFB8C4D2, false);
}
}
private static String formatDuration(int ticks) {
if (ticks < 0) {
return "∞";
}
int seconds = ticks / 20;
return (seconds / 60) + ":" + (seconds % 60 < 10 ? "0" : "") + (seconds % 60);
}
}
