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,33 @@
// The grid: the box's colour, a square a slot, and each stack with its count and wear.
fn incari_shulkertooltips_draw(
game: &crate::game::Game<'_>,
stack: &horizon_client::items::ItemStack,
batch: &mut horizon_client::hud::HudBatch,
origin: [f32; 2],
size: [f32; 2],
) {
use crate::game::{incari_client as c, incari_shulkertooltips as m};
let Some(tinted) = c::settings(m::ID, |s| s.bool("tinted")) else {
return;
};
if size != m::SIZE || !m::is_shulker_box(&stack.item) {
return;
}
let [x, y] = origin;
if tinted {
game.native_fill(batch, [x, y, size[0], size[1]], m::box_color(&stack.item));
}
for slot in 0..(m::COLUMNS * m::ROWS) as u8 {
let [cx, cy] = m::cell(slot);
game.native_fill(batch, [x + cx, y + cy, 16.0, 16.0], m::SLOT);
}
let Some(data) = stack.data() else {
return;
};
for (slot, item) in &data.container {
if usize::from(*slot) < m::COLUMNS * m::ROWS {
let [cx, cy] = m::cell(*slot);
game.native_stack(batch, item, [x + cx, y + cy], true);
}
}
}
@@ -0,0 +1,62 @@
# Incari's Shulker Tooltips (features/shulkertooltips) for the Rust build (ADR 0068, 0069). Hooks by name where Horizon
# has stamped the hook point (native-hooks.json), by function where it's still pending. Its
# settings are the client's (rust/mods/incari): the module menu shows them.
[mod]
id = "incari_shulkertooltips"
version = "0.1.0"
priority = 100
[[module]]
id = "runtime"
file = "crates/horizon-client/src/game.rs"
name = "incari_shulkertooltips"
source = "runtime.rs"
[[injection]]
id = "size"
hook = "tooltip_component"
hook_version = 1
kind = "HEAD"
helper = "size.rs"
bindings = [
{ name = "game", ty = "&crate::game::Game<'_>", value = "self" },
{ name = "stack", ty = "&horizon_client::items::ItemStack", value = "stack" },
{ name = "size", ty = "&mut Option<[f32; 2]>", value = "&mut *size" },
]
[[injection]]
id = "draw"
hook = "tooltip_component_draw"
hook_version = 1
kind = "HEAD"
helper = "draw.rs"
bindings = [
{ name = "game", ty = "&crate::game::Game<'_>", value = "self" },
{ name = "stack", ty = "&horizon_client::items::ItemStack", value = "stack" },
{ name = "batch", ty = "&mut horizon_client::hud::HudBatch", value = "&mut *batch" },
{ name = "origin", ty = "[f32; 2]", value = "origin" },
{ name = "size", ty = "[f32; 2]", value = "size" },
]
[[injection]]
id = "key"
hook = "key"
hook_version = 1
kind = "HEAD"
helper = "key.rs"
bindings = [
{ name = "key", ty = "&str", value = "key" },
{ name = "pressed", ty = "bool", value = "pressed" },
]
[[injection]]
id = "lines"
hook = "tooltip"
hook_version = 1
kind = "HEAD"
helper = "lines.rs"
bindings = [
{ name = "game", ty = "&crate::game::Game<'_>", value = "self" },
{ name = "stack", ty = "&horizon_client::items::ItemStack", value = "stack" },
{ name = "lines", ty = "&mut Vec<(horizon_client::text::Text, u32)>", value = "&mut *lines" },
]
@@ -0,0 +1,4 @@
// Shift, for Only with Shift.
fn incari_shulkertooltips_key(key: &str, pressed: bool) {
crate::game::incari_shulkertooltips::shift_key(key, pressed);
}
@@ -0,0 +1,29 @@
// The game's "3 x Diamond" lines taken out while the grid shows them, as the Java module does.
fn incari_shulkertooltips_lines(
game: &crate::game::Game<'_>,
stack: &horizon_client::items::ItemStack,
lines: &mut Vec<(horizon_client::text::Text, u32)>,
) {
use crate::game::{incari_client as c, incari_shulkertooltips as m};
let Some(data) = stack.data() else {
return;
};
let shown = c::settings(m::ID, |s| m::grid(&stack.item, data.container.len(), s.bool("only-with-shift")));
if shown != Some(true) {
return;
}
// The game's own words, or its fallbacks when the language has none.
let format = |key: &str, fallback: &'static str| {
let text = game.native_translate(key);
if text == key { fallback.to_owned() } else { text }
};
let count = format("item.container.item_count", m::COUNT_FORMAT);
let more = format("item.container.more_items", m::MORE_FORMAT);
let mut slots: Vec<(u8, u32)> = data.container.iter().map(|(slot, s)| (*slot, s.count)).collect();
slots.sort_unstable_by_key(|s| s.0);
let counts: Vec<u32> = slots.into_iter().map(|s| s.1).collect();
let plain: Vec<String> = lines.iter().map(|l| l.0.plain()).collect();
if let Some(range) = m::contents_lines(&plain, &counts, &count, &more) {
let _ = lines.drain(range);
}
}
@@ -0,0 +1,231 @@
// Incari's Shulker Tooltips for the Rust build (ADR 0068, 0069), as features/shulkertooltips does
// it: a shulker box's tooltip shows its contents as a grid, nine across, on the box's colour, in
// place of the game's first five lines of names. Its settings are the module's in
// `incari_core::modules` (the Java ids). No game types: rust/lib.rs tests it.
/// The module's id, as the Java client's profiles name it.
pub const ID: &str = "shulkertooltips";
/// A shulker box's 27 slots, 9 a row, a slot 18 GUI pixels.
pub const COLUMNS: usize = 9;
pub const ROWS: usize = 3;
pub const CELL: f32 = 18.0;
pub const SIZE: [f32; 2] = [COLUMNS as f32 * CELL, ROWS as f32 * CELL];
/// An empty slot's square, as a container screen's.
pub const SLOT: u32 = 0x60_8B8B8B;
/// The game's own lines' formats when the language has none (`item.container.item_count`,
/// `item.container.more_items`).
pub const COUNT_FORMAT: &str = "%s x %s";
pub const MORE_FORMAT: &str = "and %s more...";
/// How many stacks the game names before "and 3 more...".
pub const NAMED: usize = 5;
/// Whether the grid shows for a box holding something: always, or with Shift held when
/// `only_with_shift`.
pub fn shows(only_with_shift: bool, shift: bool) -> bool {
!only_with_shift || shift
}
/// `format` split at its two `%s`: what goes before the first, between them and after. None for a
/// format this can't read (positional `%1$s`), whose lines are then left alone.
fn parts(format: &str) -> Option<(&str, &str, &str)> {
if format.contains("$s") {
return None;
}
let (a, rest) = format.split_once("%s")?;
match rest.split_once("%s") {
Some((b, c)) => Some((a, b, c)),
None => Some((a, rest, "")),
}
}
/// `line` starts with `count` where `format` has its first `%s`, followed by what comes next.
/// Doesn't allocate: it's asked for each line, each frame a tooltip shows.
fn starts_with_count(a: &str, b: &str, count: u32, line: &str) -> bool {
let Some(rest) = line.strip_prefix(a) else {
return false;
};
let digits = rest.bytes().take_while(u8::is_ascii_digit).count();
rest[..digits].parse::<u32>().ok() == Some(count) && rest[digits..].starts_with(b)
}
/// Whether `line` is the game's "3 x Diamond" for a stack of `count` (`format`: the language's
/// `item.container.item_count`).
pub fn is_count_line(format: &str, count: u32, line: &str) -> bool {
parts(format).is_some_and(|(a, b, _)| starts_with_count(a, b, count, line))
}
/// Whether `line` is the game's "and 3 more..." for `more` stacks.
pub fn is_more_line(format: &str, more: u32, line: &str) -> bool {
parts(format).is_some_and(|(a, b, _)| {
starts_with_count(a, b, more, line) && line.len() == a.len() + digits(more) + b.len()
})
}
/// How many digits `n` is written with.
fn digits(n: u32) -> usize {
n.checked_ilog10().map_or(1, |d| d as usize + 1)
}
/// Where the game's contents lines are in `lines` (the name first), for stacks of `counts` in slot
/// order: the range to take out, or none when they aren't there as expected.
pub fn contents_lines(
lines: &[impl AsRef<str>],
counts: &[u32],
count_format: &str,
more_format: &str,
) -> Option<std::ops::Range<usize>> {
let named = counts.len().min(NAMED);
let more = counts.len() - named;
let len = named + usize::from(more > 0);
if named == 0 {
return None;
}
(1..lines.len().saturating_sub(len - 1)).find_map(|start| {
let all_named =
(0..named).all(|j| is_count_line(count_format, counts[j], lines[start + j].as_ref()));
let ends =
more == 0 || is_more_line(more_format, more as u32, lines[start + named].as_ref());
(all_named && ends).then_some(start..start + len)
})
}
/// Whether the grid shows for `item` holding `stacks` stacks, Shift held or not, with the module's
/// Only with Shift.
pub fn grid(item: &str, stacks: usize, only_with_shift: bool) -> bool {
stacks > 0 && is_shulker_box(item) && shows(only_with_shift, shift())
}
pub fn is_shulker_box(item: &str) -> bool {
item.ends_with("shulker_box")
}
/// Where slot `slot`'s icon goes from the grid's top left.
pub fn cell(slot: u8) -> [f32; 2] {
let slot = usize::from(slot);
[
(slot % COLUMNS) as f32 * CELL + 1.0,
(slot / COLUMNS) as f32 * CELL + 1.0,
]
}
/// The box's colour behind the grid, translucent: its dye's, or the undyed box's purple.
pub fn box_color(item: &str) -> u32 {
const DYES: [(&str, u32); 16] = [
("white", 0xF9FFFE),
("orange", 0xF9801D),
("magenta", 0xC74EBD),
("light_blue", 0x3AB3DA),
("yellow", 0xFED83D),
("lime", 0x80C71F),
("pink", 0xF38BAA),
("gray", 0x474F52),
("light_gray", 0x9D9D97),
("cyan", 0x169C9C),
("purple", 0x8932B8),
("blue", 0x3C44AA),
("brown", 0x835432),
("green", 0x5E7C16),
("red", 0xB02E26),
("black", 0x1D1D21),
];
let name = item.rsplit(':').next().unwrap_or(item);
let dye = name.strip_suffix("_shulker_box");
let rgb = dye
.and_then(|d| DYES.iter().find(|e| e.0 == d))
.map_or(0x97_6997, |e| e.1);
0xA0_000000 | rgb
}
// Shift, as the key hook last saw it: the tooltip hooks can't ask.
thread_local! {
static SHIFT: std::cell::Cell<[bool; 2]> = const { std::cell::Cell::new([false; 2]) };
}
pub fn shift_key(key: &str, pressed: bool) {
let side = match key {
"key.keyboard.left.shift" => 0,
"key.keyboard.right.shift" => 1,
_ => return,
};
SHIFT.with(|s| {
let mut held = s.get();
held[side] = pressed;
s.set(held);
});
}
pub fn shift() -> bool {
SHIFT.with(|s| s.get().contains(&true))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lays_out_a_box() {
assert!(
is_shulker_box("minecraft:red_shulker_box") && is_shulker_box("minecraft:shulker_box")
);
assert!(!is_shulker_box("minecraft:chest"));
assert_eq!(cell(10), [19.0, 19.0]);
assert_eq!(SIZE, [162.0, 54.0]);
assert_eq!(box_color("minecraft:light_blue_shulker_box"), 0xA03A_B3DA);
assert_eq!(box_color("minecraft:shulker_box"), 0xA097_6997);
}
#[test]
fn finds_the_games_contents_lines() {
let lines = ["Shulker Box", "3 x Diamond", "64 x Stone", "Lore"];
assert_eq!(
contents_lines(&lines, &[3, 64], COUNT_FORMAT, MORE_FORMAT),
Some(1..3)
);
assert_eq!(
contents_lines(&lines, &[3, 63], COUNT_FORMAT, MORE_FORMAT),
None
);
let many = [
"Box",
"1 x A",
"2 x B",
"3 x C",
"4 x D",
"5 x E",
"and 2 more...",
"Lore",
];
assert_eq!(
contents_lines(&many, &[1, 2, 3, 4, 5, 6, 7], COUNT_FORMAT, MORE_FORMAT),
Some(1..7)
);
assert_eq!(
contents_lines(
&many[..6],
&[1, 2, 3, 4, 5, 6, 7],
COUNT_FORMAT,
MORE_FORMAT
),
None
);
assert!(!is_count_line(COUNT_FORMAT, 3, "33 x Dirt"));
assert!(
!is_count_line("%2$s ×%1$s", 3, "Dirt ×3"),
"positional: left alone"
);
assert!(is_more_line(MORE_FORMAT, 12, "and 12 more..."));
assert!(!is_more_line(MORE_FORMAT, 12, "and 12 more... and"));
assert!(shows(false, false) && shows(true, true) && !shows(true, false));
}
#[test]
fn hears_either_shift() {
shift_key("key.keyboard.right.shift", true);
assert!(shift());
shift_key("key.keyboard.right.shift", false);
shift_key("key.keyboard.a", true);
assert!(!shift());
}
}
@@ -0,0 +1,16 @@
// Room for the grid under a shulker box's tooltip, when it holds something.
fn incari_shulkertooltips_size(
game: &crate::game::Game<'_>,
stack: &horizon_client::items::ItemStack,
size: &mut Option<[f32; 2]>,
) {
use crate::game::{incari_client as c, incari_shulkertooltips as m};
let _ = game;
if size.is_some() {
return;
}
let stacks = stack.data().map_or(0, |d| d.container.len());
if c::settings(m::ID, |s| m::grid(&stack.item, stacks, s.bool("only-with-shift"))) == Some(true) {
*size = Some(m::SIZE);
}
}