Archive incari-client (client, 26.3, clean)

This commit is contained in:
2026-11-28 00:54:38 -06:00
parent 7a6174687f
commit 12838e838c
2383 changed files with 280266 additions and 0 deletions
@@ -0,0 +1,192 @@
package incari.features.armorpotionview;
import incari.features.armorpotionview.ArmorPotionView.Durability;
import incari.features.armorpotionview.ArmorPotionView.Layout;
import incari.framework.game.EquipmentSlot;
import incari.framework.game.ItemView;
import incari.framework.game.PlayerView;
import incari.framework.hud.Anchor;
import incari.framework.hud.HudCanvas;
import incari.framework.hud.HudElement;
import incari.framework.hud.Tone;
import org.jspecify.annotations.Nullable;
/**
* The armor the player wears, helmet first, then what they hold: each item's icon with the game's
* durability bar, and its durability written beside it in the module's format.
*/
final class ArmorElement extends HudElement {
private static final EquipmentSlot[] ARMOR = {
EquipmentSlot.HEAD, EquipmentSlot.CHEST, EquipmentSlot.LEGS, EquipmentSlot.FEET
};
private static final int ICON = 16;
/** Between an icon and its text. */
private static final int TEXT_GAP = 2;
/** Between rows (vertical) or cells (horizontal). */
private static final int ROW_GAP = 1;
private static final int CELL_GAP = 4;
private final ArmorPotionView module;
// This draw's rows, refilled without allocating each time the HUD asks.
private final ItemView[] items = new ItemView[ARMOR.length + 1];
private final @Nullable String[] texts = new String[ARMOR.length + 1];
// Each row's text, made again only when its numbers change.
private final int[] cachedDamage = new int[ARMOR.length + 1];
private final int[] cachedMax = new int[ARMOR.length + 1];
private final @Nullable Durability[] cachedFormat = new Durability[ARMOR.length + 1];
private final @Nullable String[] cachedText = new String[ARMOR.length + 1];
ArmorElement(ArmorPotionView module) {
super("armor", "Armor", Anchor.BOTTOM_RIGHT, 2, 2);
this.module = module;
}
@Override
public boolean isVisible() {
return module.showArmor.get() && collect(false) > 0;
}
@Override
public boolean isPlaceable() {
return module.showArmor.get();
}
@Override
public int width(HudCanvas canvas) {
int count = collect(canvas.editing());
if (module.layout.get() == Layout.HORIZONTAL) {
int width = 0;
for (int i = 0; i < count; i++) {
width += (i > 0 ? CELL_GAP : 0) + cellWidth(canvas, i);
}
return width;
}
int width = 0;
for (int i = 0; i < count; i++) {
width = Math.max(width, cellWidth(canvas, i));
}
return width;
}
@Override
public int height(HudCanvas canvas) {
int count = collect(canvas.editing());
if (count == 0) {
return 0;
}
return module.layout.get() == Layout.HORIZONTAL ? ICON : count * (ICON + ROW_GAP) - ROW_GAP;
}
@Override
public void draw(HudCanvas canvas) {
int count = collect(canvas.editing());
boolean horizontal = module.layout.get() == Layout.HORIZONTAL;
int textY = (ICON - canvas.lineHeight()) / 2 + 1;
int x = 0;
int y = 0;
for (int i = 0; i < count; i++) {
ItemView item = items[i];
canvas.item(item, x, y, true);
String text = texts[i];
if (text != null) {
canvas.text(text, x + ICON + TEXT_GAP, y + textY, tone(item));
}
if (horizontal) {
x += cellWidth(canvas, i) + CELL_GAP;
} else {
y += ICON + ROW_GAP;
}
}
}
private int cellWidth(HudCanvas canvas, int row) {
String text = texts[row];
return text == null ? ICON : ICON + TEXT_GAP + canvas.textWidth(text);
}
private Tone tone(ItemView item) {
if (!module.wearColors.get() || item.maxDamage() <= 0) {
return Tone.TEXT;
}
// Thresholds in whole numbers: remaining / max below a half, below a fifth.
long remaining = item.remaining();
long max = item.maxDamage();
if (remaining * 5 < max) {
return Tone.NEGATIVE;
}
return remaining * 2 < max ? Tone.WARNING : Tone.POSITIVE;
}
/**
* Fills this draw's rows from the player (the sample in the editor when the real player has
* nothing to show) and returns how many.
*/
private int collect(boolean editing) {
PlayerView player = module.player();
int rows = player == null ? 0 : fill(player);
return rows == 0 && editing ? fill(module.samplePlayer()) : rows;
}
private int fill(PlayerView player) {
int count = 0;
for (EquipmentSlot slot : ARMOR) {
ItemView item = player.item(slot);
if (!item.isEmpty()) {
count = add(count, item);
}
}
if (module.showHeld.get()) {
ItemView held = player.item(EquipmentSlot.MAINHAND);
if (!held.isEmpty()) {
count = add(count, held);
}
}
return count;
}
private int add(int row, ItemView item) {
items[row] = item;
// The bar alone: the icon carries it, nothing is written beside it.
texts[row] = item.isDamageable() && module.durability.get() != Durability.BAR ? text(row, item) : null;
return row + 1;
}
/** {@code item}'s durability in the module's format, made again only when it changed. */
private String text(int row, ItemView item) {
int damage = item.damage();
int max = item.maxDamage();
Durability format = module.durability.get();
String cached = cachedText[row];
if (cached != null && cachedDamage[row] == damage && cachedMax[row] == max && cachedFormat[row] == format) {
return cached;
}
String text = format(format, max - damage, max);
cachedDamage[row] = damage;
cachedMax[row] = max;
cachedFormat[row] = format;
cachedText[row] = text;
return text;
}
/** "86%", "312", "312/363", or nothing for the bar alone. */
static String format(Durability format, int remaining, int max) {
return switch (format) {
case PERCENT -> percent(remaining, max) + "%";
case REMAINING -> Integer.toString(remaining);
case REMAINING_OF_MAX -> remaining + "/" + max;
case BAR -> "";
};
}
/** Whole percent left, rounded down, but 1 while anything's left: 0% is broken. */
static int percent(int remaining, int max) {
if (max <= 0 || remaining <= 0) {
return 0;
}
return Math.max(1, (int) ((long) remaining * 100 / max));
}
}
@@ -0,0 +1,132 @@
package incari.features.armorpotionview;
import incari.framework.Category;
import incari.framework.Module;
import incari.framework.game.PlayerView;
import incari.framework.setting.BooleanSetting;
import incari.framework.setting.ChoiceSetting;
import org.jspecify.annotations.Nullable;
/**
* Armor &amp; Potion View: two HUD elements, placed apart. One shows the armor the player wears (and
* what they hold) with how worn each piece is, so a breaking helmet is seen before it breaks; the
* other lists the active effects with their levels and the time left.
*/
public final class ArmorPotionView extends Module {
/** How an item's durability is written beside it. */
public enum Durability {
/** "86%". */
PERCENT("Percent"),
/** "312": uses left. */
REMAINING("Uses left"),
/** "312/363". */
REMAINING_OF_MAX("Uses left of max"),
/** Nothing beside it: the game's bar on the icon only. */
BAR("Bar only");
private final String label;
Durability(String label) {
this.label = label;
}
/** What the settings screen calls it. */
public String label() {
return label;
}
}
/** Which way the armor runs. */
public enum Layout {
/** Helmet at the top, boots at the bottom. */
VERTICAL("Vertical"),
/** Helmet on the left, boots on the right. */
HORIZONTAL("Horizontal");
private final String label;
Layout(String label) {
this.label = label;
}
/** What the settings screen calls it. */
public String label() {
return label;
}
}
/** Which side of the effects' text their icons go. */
public enum Side {
/** Icons on the left, text after. */
LEFT("Left"),
/** Icons on the right, text before: for the screen's right side. */
RIGHT("Right");
private final String label;
Side(String label) {
this.label = label;
}
/** What the settings screen calls it. */
public String label() {
return label;
}
}
final BooleanSetting showArmor = settings()
.bool("show-armor", "Show armor", true)
.describedAs("Shows the armor you wear and its durability.");
final BooleanSetting showHeld = settings()
.bool("show-held", "Show held item", true)
.describedAs("Adds the item in your main hand below the armor.")
.visibleWhen(showArmor::get);
final ChoiceSetting<Durability> durability = settings()
.choice("durability", "Durability", Durability.values(), Durability.PERCENT, Durability::label)
.describedAs("How durability is shown beside each piece.")
.visibleWhen(showArmor::get);
final BooleanSetting wearColors = settings()
.bool("wear-colors", "Color by wear", true)
.describedAs("Durability turns amber below half and red below a fifth.")
.visibleWhen(() -> showArmor.get() && durability.get() != Durability.BAR);
final ChoiceSetting<Layout> layout = settings()
.choice("layout", "Armor layout", Layout.values(), Layout.VERTICAL, Layout::label)
.visibleWhen(showArmor::get);
final BooleanSetting showEffects = settings()
.bool("show-effects", "Show effects", true)
.describedAs("Lists your active effects with the time they have left.");
final BooleanSetting effectNames = settings()
.bool("effect-names", "Effect names", true)
.describedAs("Off: icons and times only.")
.visibleWhen(showEffects::get);
final BooleanSetting kindColors = settings()
.bool("kind-colors", "Color by kind", false)
.describedAs("Helpful effects' names in green, harmful ones' in red.")
.visibleWhen(() -> showEffects.get() && effectNames.get());
final ChoiceSetting<Side> side = settings()
.choice("side", "Effect icons on the", Side.values(), Side.LEFT, Side::label)
.visibleWhen(showEffects::get);
/** Armor &amp; Potion View, off until the player turns it on. */
public ArmorPotionView() {
super(
"armorpotionview",
"Armor & Potion View",
"Shows your armor's durability and your active effects on the HUD.",
Category.HUD);
hud(new ArmorElement(this));
hud(new EffectsElement(this));
}
/** The player, or null outside a world. */
@Nullable
PlayerView player() {
return system().game().player();
}
/** The HUD editor's stand-in player. */
PlayerView samplePlayer() {
return system().game().samplePlayer();
}
}
@@ -0,0 +1,183 @@
package incari.features.armorpotionview;
import incari.features.armorpotionview.ArmorPotionView.Side;
import incari.framework.game.EffectCategory;
import incari.framework.game.EffectView;
import incari.framework.game.PlayerView;
import incari.framework.hud.Anchor;
import incari.framework.hud.HudCanvas;
import incari.framework.hud.HudElement;
import incari.framework.hud.Tone;
import java.util.Arrays;
import org.jspecify.annotations.Nullable;
/**
* The player's active effects, one row each in the game's order: the effect's icon, its name and
* level, and the time it has left (amber in its last ten seconds).
*/
final class EffectsElement extends HudElement {
private static final int ICON = 18;
private static final int TEXT_GAP = 3;
private static final int ROW_GAP = 2;
/** Ticks left from which the time turns amber: ten seconds. */
private static final int ENDING = 200;
/** Rows with their text kept; more effects than this make their text each draw. */
private static final int CACHED = 32;
private static final String[] NUMERALS = {"", " II", " III", " IV", " V", " VI", " VII", " VIII", " IX", " X"};
private final ArmorPotionView module;
// Each row's text, made again only when what it says changes.
private final @Nullable String[] cachedNameOf = new String[CACHED];
private final int[] cachedAmplifier = new int[CACHED];
private final @Nullable String[] cachedName = new String[CACHED];
private final int[] cachedSeconds = new int[CACHED];
private final @Nullable String[] cachedTime = new String[CACHED];
EffectsElement(ArmorPotionView module) {
// Top left, below Toggle Sprint's line and the FPS, CPS and reach readouts, so a long list
// grows away from them.
super("effects", "Effects", Anchor.TOP_LEFT, 2, 62);
this.module = module;
Arrays.fill(cachedSeconds, Integer.MIN_VALUE);
}
@Override
public boolean isVisible() {
PlayerView player = module.player();
return module.showEffects.get() && player != null && player.effectCount() > 0;
}
@Override
public boolean isPlaceable() {
return module.showEffects.get();
}
@Override
public int width(HudCanvas canvas) {
PlayerView player = player(canvas);
int count = player.effectCount();
int text = 0;
boolean names = module.effectNames.get();
for (int i = 0; i < count; i++) {
EffectView effect = player.effect(i);
if (names) {
text = Math.max(text, canvas.textWidth(name(i, effect)));
}
text = Math.max(text, canvas.textWidth(time(i, effect)));
}
return count == 0 ? 0 : ICON + TEXT_GAP + text;
}
@Override
public int height(HudCanvas canvas) {
int count = player(canvas).effectCount();
return count == 0 ? 0 : count * (ICON + ROW_GAP) - ROW_GAP;
}
@Override
public void draw(HudCanvas canvas) {
PlayerView player = player(canvas);
int count = player.effectCount();
boolean right = module.side.get() == Side.RIGHT;
boolean names = module.effectNames.get();
int width = right ? width(canvas) : 0;
int line = canvas.lineHeight();
for (int i = 0; i < count; i++) {
EffectView effect = player.effect(i);
int y = i * (ICON + ROW_GAP);
canvas.effect(effect, right ? width - ICON : 0, y);
String time = time(i, effect);
Tone timeTone = !effect.isInfinite() && effect.duration() < ENDING ? Tone.WARNING : Tone.MUTED;
if (names) {
String name = name(i, effect);
text(canvas, name, right, width, y, nameTone(effect));
text(canvas, time, right, width, y + line, timeTone);
} else {
text(canvas, time, right, width, y + (ICON - line) / 2 + 1, timeTone);
}
}
}
private static void text(HudCanvas canvas, String text, boolean right, int width, int y, Tone tone) {
int x = right ? width - ICON - TEXT_GAP - canvas.textWidth(text) : ICON + TEXT_GAP;
canvas.text(text, x, y, tone);
}
private Tone nameTone(EffectView effect) {
if (!module.kindColors.get()) {
return Tone.TEXT;
}
return effect.category() == EffectCategory.BENEFICIAL
? Tone.POSITIVE
: effect.category() == EffectCategory.HARMFUL ? Tone.NEGATIVE : Tone.TEXT;
}
/** The real player, or in the HUD editor the sample when the real one has no effects. */
private PlayerView player(HudCanvas canvas) {
PlayerView player = module.player();
if (player != null && player.effectCount() > 0) {
return player;
}
return canvas.editing() ? module.samplePlayer() : PlayerView.EMPTY;
}
/** "Speed II", made again only when the effect in this row changes. */
@SuppressWarnings("ReferenceEquality") // The client hands out the same name string each time.
private String name(int row, EffectView effect) {
String name = effect.name();
int amplifier = effect.amplifier();
if (row < CACHED && cachedNameOf[row] == name && cachedAmplifier[row] == amplifier) {
String cached = cachedName[row];
if (cached != null) {
return cached;
}
}
String text = name + level(amplifier);
if (row < CACHED) {
cachedNameOf[row] = name;
cachedAmplifier[row] = amplifier;
cachedName[row] = text;
}
return text;
}
/** "1:23", made again only when the second changes. */
private String time(int row, EffectView effect) {
int seconds = effect.isInfinite() ? -1 : effect.duration() / 20;
if (row < CACHED && cachedSeconds[row] == seconds) {
String cached = cachedTime[row];
if (cached != null) {
return cached;
}
}
String text = seconds < 0 ? "∞" : clock(seconds);
if (row < CACHED) {
cachedSeconds[row] = seconds;
cachedTime[row] = text;
}
return text;
}
/** " II" for amplifier 1; nothing for level I; numbers past X. */
static String level(int amplifier) {
if (amplifier >= 0 && amplifier < NUMERALS.length) {
return NUMERALS[amplifier];
}
return " " + (amplifier + 1);
}
/** "0:08", "1:23", "1:02:03". */
static String clock(int seconds) {
int hours = seconds / 3600;
int minutes = seconds / 60 % 60;
int secs = seconds % 60;
String tail = (secs < 10 ? ":0" : ":") + secs;
if (hours > 0) {
return hours + (minutes < 10 ? ":0" : ":") + minutes + tail;
}
return minutes + tail;
}
}
@@ -0,0 +1,5 @@
/** Armor & Potion View (ADR 0050): armor, its durability and the active effects on the HUD. */
@NullMarked
package incari.features.armorpotionview;
import org.jspecify.annotations.NullMarked;
@@ -0,0 +1,164 @@
package incari.features.armorpotionview;
import static org.assertj.core.api.Assertions.assertThat;
import incari.features.armorpotionview.ArmorPotionView.Durability;
import incari.framework.game.EffectCategory;
import incari.framework.game.EquipmentSlot;
import incari.framework.hud.Anchor;
import incari.framework.hud.HudElement;
import incari.framework.testing.FakePlayer;
import incari.framework.testing.TestGame;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class ArmorPotionViewTest {
private final ArmorPotionView module = new ArmorPotionView();
private final TestGame game = new TestGame(module);
private final HudElement armor = module.hudElements().get(0);
private final HudElement effects = module.hudElements().get(1);
@BeforeEach
void atTheTopLeft() {
module.setEnabled(true);
for (HudElement element : module.hudElements()) {
element.anchor().set(Anchor.TOP_LEFT);
element.offsetX().set(0);
element.offsetY().set(0);
}
}
@Test
void nothingOutsideAWorldOrWithNothingOn() {
assertThat(game.drawHud().drawn).isEmpty();
game.join();
assertThat(game.drawHud().drawn).isEmpty();
}
@Test
void eachPieceWithItsDurabilityColoredByWear() {
game.join()
.wear(EquipmentSlot.HEAD, "helmet", 0, 363)
.wear(EquipmentSlot.CHEST, "chestplate", 300, 528)
.wear(EquipmentSlot.FEET, "boots", 400, 429)
.hold(EquipmentSlot.MAINHAND, "dirt", 64);
effects.anchor().set(Anchor.BOTTOM_RIGHT);
assertThat(game.drawHud().drawn)
.containsExactly(
"item \"helmet\" at 0,0 +decorations",
"text \"100%\" at 18,4 POSITIVE",
"item \"chestplate\" at 0,17 +decorations",
"text \"43%\" at 18,21 WARNING",
"item \"boots\" at 0,34 +decorations",
"text \"6%\" at 18,38 NEGATIVE",
// Held, but nothing to wear out: no text.
"item \"dirt\" at 0,51 +decorations");
}
@Test
void durabilityInEachFormat() {
assertThat(ArmorElement.format(Durability.PERCENT, 312, 363)).isEqualTo("85%");
assertThat(ArmorElement.format(Durability.REMAINING, 312, 363)).isEqualTo("312");
assertThat(ArmorElement.format(Durability.REMAINING_OF_MAX, 312, 363)).isEqualTo("312/363");
assertThat(ArmorElement.percent(1, 1000)).isEqualTo(1);
assertThat(ArmorElement.percent(0, 1000)).isZero();
}
@Test
void theBarAloneWritesNothingAndPlainColorsAreTheText() {
game.join().wear(EquipmentSlot.HEAD, "helmet", 350, 363);
module.durability.set(Durability.BAR);
assertThat(game.drawHud().drawn).containsExactly("item \"helmet\" at 0,0 +decorations");
module.durability.set(Durability.REMAINING);
module.wearColors.set(false);
assertThat(game.drawHud().drawn)
.containsExactly("item \"helmet\" at 0,0 +decorations", "text \"13\" at 18,4 TEXT");
}
@Test
void horizontalArmorRunsAcross() {
game.join().wear(EquipmentSlot.HEAD, "helmet", 0, 100).wear(EquipmentSlot.LEGS, "leggings", 50, 100);
module.showHeld.set(false);
module.layout.set(ArmorPotionView.Layout.HORIZONTAL);
// "100%" is 24 wide: the next cell starts at 16 + 2 + 24 + 4.
assertThat(game.drawHud().drawn)
.containsExactly(
"item \"helmet\" at 0,0 +decorations",
"text \"100%\" at 18,4 POSITIVE",
"item \"leggings\" at 46,0 +decorations",
"text \"50%\" at 64,4 POSITIVE");
}
@Test
void effectsWithLevelsAndTimeLeft() {
game.join()
.effect("Speed", 1, 1660, EffectCategory.BENEFICIAL)
.effect("Poison", 0, 150, EffectCategory.HARMFUL)
.effect("Night Vision", 0, -1, EffectCategory.BENEFICIAL);
assertThat(game.drawHud().drawn)
.containsExactly(
"effect \"Speed\" at 0,0",
"text \"Speed II\" at 21,0 TEXT",
"text \"1:23\" at 21,9 MUTED",
"effect \"Poison\" at 0,20",
"text \"Poison\" at 21,20 TEXT",
// The last ten seconds stand out.
"text \"0:07\" at 21,29 WARNING",
"effect \"Night Vision\" at 0,40",
"text \"Night Vision\" at 21,40 TEXT",
"text \"∞\" at 21,49 MUTED");
}
@Test
void effectsColoredByKindIconsOnTheRightTimesOnly() {
game.join()
.effect("Speed", 0, 400, EffectCategory.BENEFICIAL)
.effect("Weakness", 2, 9000, EffectCategory.HARMFUL);
module.kindColors.set(true);
assertThat(game.drawHud().drawn)
.contains("text \"Speed\" at 21,0 POSITIVE", "text \"Weakness III\" at 21,20 NEGATIVE");
module.side.set(ArmorPotionView.Side.RIGHT);
module.effectNames.set(false);
// Width: 18 + 3 + "7:30" (24) = 45; the times end 3 left of the icons.
assertThat(game.drawHud().drawn)
.containsExactly(
"effect \"Speed\" at 27,0",
"text \"0:20\" at 0,5 MUTED",
"effect \"Weakness\" at 27,20",
"text \"7:30\" at 0,25 MUTED");
}
@Test
void levelsAndClocks() {
assertThat(EffectsElement.level(0)).isEmpty();
assertThat(EffectsElement.level(3)).isEqualTo(" IV");
assertThat(EffectsElement.level(254)).isEqualTo(" 255");
assertThat(EffectsElement.clock(8)).isEqualTo("0:08");
assertThat(EffectsElement.clock(3723)).isEqualTo("1:02:03");
}
@Test
void theHudEditorShowsTheSampleWhenThePlayerHasNothing() {
game.join();
FakePlayer sample = game.sample;
sample.wear(EquipmentSlot.HEAD, "sample helmet", 0, 363).effect("Speed", 0, 600, EffectCategory.BENEFICIAL);
assertThat(game.drawHud(true).drawn)
.contains("item \"sample helmet\" at 0,0 +decorations", "effect \"Speed\" at 0,0");
assertThat(game.drawHud().drawn).isEmpty();
}
@Test
void eachHalfTurnsOffOnItsOwn() {
game.join().wear(EquipmentSlot.HEAD, "helmet", 0, 100).effect("Speed", 0, 400, EffectCategory.BENEFICIAL);
module.showArmor.set(false);
assertThat(armor.isPlaceable()).isFalse();
assertThat(game.drawHud().drawn).noneMatch(line -> line.startsWith("item"));
module.showEffects.set(false);
assertThat(game.drawHud().drawn).isEmpty();
}
}
@@ -0,0 +1,24 @@
package incari.features.armorpotionview;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchRule;
/** A feature sees the module framework and the JDK's basics: no game, platform or other feature. */
@AnalyzeClasses(packages = "incari.features.armorpotionview", importOptions = ImportOption.DoNotIncludeTests.class)
class BoundaryTest {
@ArchTest
static final ArchRule onlyTheFramework = classes()
.should()
.onlyDependOnClassesThat()
.resideInAnyPackage(
"java.lang..",
"java.util..",
"org.jspecify..",
"incari.framework..",
"incari.features.armorpotionview..");
}