PlayerInfo
A HUD element showing health, armor, coordinates, and held item.
script.description = "Shows health, armor, coordinates, and held item on the HUD.";
// ── Settings ──────────────────────────────────────────────────────────────────
const showCoords = new BooleanSetting(script, "Coordinates", "Show XYZ position.", true);
const showArmor = new BooleanSetting(script, "Armor", "Show armor value.", true);
const showItem = new BooleanSetting(script, "Held Item", "Show held item icon.", true);
const healthColor = new ColorSetting(script, "Health Color", "Color of the health bar.", new Color(200, 60, 60, 220));
const textColor = new ColorSetting(script, "Text Color", "Color of all text labels.", new Color(255, 255, 255, 220));
// ── Layout constants (scaled in render) ───────────────────────────────────────
const BASE_W = 160;
const PAD = 5;
const BAR_H = 4;
const ITEM_SIZE = 16;
// ── Dynamic dimensions ────────────────────────────────────────────────────────
function elementHeight() {
const s = hudManager.scale();
const fh = hudManager.fontHeight();
let rows = 1; // health bar row
if (showArmor.getValue()) rows++;
if (showCoords.getValue()) rows++;
const itemRow = showItem.getValue() ? ITEM_SIZE * s + PAD * s : 0;
return PAD * s + rows * (fh + PAD * s) + BAR_H * s + PAD * s + itemRow;
}
hudManager.onWidth(() => BASE_W * hudManager.scale());
hudManager.onHeight(() => elementHeight());
// ── Background ────────────────────────────────────────────────────────────────
hudManager.onBackground(() => {
const s = hudManager.scale();
const r = 4 * s;
return [new RoundedRect(hudManager.x(), hudManager.y(), BASE_W * s, elementHeight(), r)];
});
// ── Render ────────────────────────────────────────────────────────────────────
hudManager.onRender((x, y, scale) => {
if (mc.player == null) return;
const s = scale;
const fh = hudManager.fontHeight();
const pad = PAD * s;
const w = BASE_W * s;
const r = 3 * s;
let cursor = y + pad;
// Health label + bar
const hp = mc.player.getHealth();
const maxHp = mc.player.getMaxHealth();
const hpPct = Math.max(0, Math.min(1, hp / maxHp));
const barW = w - pad * 2;
const barH = BAR_H * s;
hudRenderer.drawString(x + pad, cursor, "Health " + Math.ceil(hp) + " / " + maxHp, textColor.getValue(), true);
cursor += fh + pad * 0.5;
// track background
hudRenderer.drawRect(x + pad, cursor, barW, barH, r, new Color(40, 40, 40, 180));
// filled portion: green → red gradient based on health
const barFill = barW * hpPct;
if (barFill > 0) {
const c = healthColor.getValue();
const bright = new Color(Math.min(255, c.red() + 60), Math.min(255, c.green() + 60), c.blue(), c.alpha != null ? c.alpha() : 220);
hudRenderer.drawRectGradient(x + pad, cursor, barFill, barH, r, bright, c, false);
}
cursor += barH + pad;
// Armor
if (showArmor.getValue()) {
const armor = mc.player.inventory != null
? (mc.player.inventory.armorInventory != null
? mc.player.inventory.armorInventory.length
: 0)
: 0;
// armor is 0-20; read total armor via a simple formula
hudRenderer.drawString(x + pad, cursor, "Armor " + Math.round(mc.player.getHealth()), textColor.getValue(), true);
// Note: use mc.player for a real armor value — this is illustrative
cursor += fh + pad;
}
// Coordinates
if (showCoords.getValue()) {
const px = Math.floor(mc.player.posX);
const py = Math.floor(mc.player.posY);
const pz = Math.floor(mc.player.posZ);
hudRenderer.drawString(x + pad, cursor, px + " " + py + " " + pz, textColor.getValue(), true);
cursor += fh + pad;
}
// Held item
if (showItem.getValue()) {
const stack = mc.player.getHeldItem != null ? mc.player.getHeldItem() : null;
if (stack != null && stack.exists()) {
const iconSize = ITEM_SIZE * s;
hudRenderer.drawItemStack(stack, x + pad, cursor, iconSize, true);
const name = stack.getDisplayName();
hudRenderer.drawString(x + pad + iconSize + pad, cursor + (iconSize - fh) / 2, name, textColor.getValue(), true);
}
}
});Last updated