0a116d9523
A pure move, the first slice of splitting the client lib into crates: - horizon-command: the command/ module (Brigadier port, vanilla tree, argument types, selectors), as is. - horizon-client-core: what command needs from the lib, with no other dependency inside it: text, lang, snbt, nbt_path, component, scoreboard, bossbar, gamerules, block_entity_nbt, fastutil_order; plus items moved out of lib modules that sit in the cycle, unchanged: ParticleType (particles.rs), the key display name (keybinds.rs), SOUND_SOURCES (settings.rs), uuid_text/uuid_from_text (save.rs), Registries/RegistryIds and parse_identifier (command/args.rs). horizon-client re-exports every module and item under its old path (`horizon_client::text`, `crate::command::Registries`, `particles::ParticleType`...), so no caller changes. Inside the moved files only paths changed (`crate::snbt` -> `horizon_client_core::snbt` in command, `crate::command::args::parse_identifier` -> `crate::identifier::parse_identifier`...), `sign_side` became pub, lang.rs includes the lang files from horizon-client's assets, and gamerules' test that saves a Level moved to save.rs (it needs the client's save). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
514 lines
16 KiB
Rust
514 lines
16 KiB
Rust
//! Tag text output: `StringTagVisitor` (compact SNBT) and
|
|
//! `TextComponentTagVisitor` (the coloured form commands print), plus Java's
|
|
//! number formatting.
|
|
use crate::text::{self, Style, Text};
|
|
use horizon_world::nbt::Tag;
|
|
|
|
/// `Double.toString`.
|
|
pub fn java_double(v: f64) -> String {
|
|
java_float_text(v, &format!("{v}"), &format!("{v:e}"))
|
|
}
|
|
/// `Float.toString`.
|
|
pub fn java_float(v: f32) -> String {
|
|
java_float_text(f64::from(v), &format!("{v}"), &format!("{v:e}"))
|
|
}
|
|
fn java_float_text(v: f64, plain: &str, sci: &str) -> String {
|
|
if v.is_nan() {
|
|
return "NaN".into();
|
|
}
|
|
if v.is_infinite() {
|
|
return if v > 0.0 { "Infinity" } else { "-Infinity" }.into();
|
|
}
|
|
if v == 0.0 {
|
|
return if v.is_sign_negative() { "-0.0" } else { "0.0" }.into();
|
|
}
|
|
let a = v.abs();
|
|
if (1.0e-3..1.0e7).contains(&a) {
|
|
if plain.contains('.') {
|
|
plain.to_owned()
|
|
} else {
|
|
format!("{plain}.0")
|
|
}
|
|
} else {
|
|
let (mantissa, exponent) = sci.split_once('e').unwrap_or((sci, "0"));
|
|
let mantissa = if mantissa.contains('.') {
|
|
mantissa.to_owned()
|
|
} else {
|
|
format!("{mantissa}.0")
|
|
};
|
|
format!("{mantissa}E{exponent}")
|
|
}
|
|
}
|
|
|
|
/// `SnbtGrammar.escapeControlCharacters`.
|
|
fn escape_control(c: char) -> Option<&'static str> {
|
|
Some(match c {
|
|
'\u{8}' => "b",
|
|
'\u{c}' => "f",
|
|
'\n' => "n",
|
|
'\r' => "r",
|
|
'\t' => "t",
|
|
_ => return None,
|
|
})
|
|
}
|
|
/// `StringTag.quoteAndEscape`.
|
|
pub fn quote_and_escape(input: &str) -> String {
|
|
let mut out = String::from(" ");
|
|
let mut quote = '\0';
|
|
for c in input.chars() {
|
|
if c == '\\' {
|
|
out.push_str("\\\\");
|
|
} else if c != '"' && c != '\'' {
|
|
match escape_control(c) {
|
|
Some(e) => {
|
|
out.push('\\');
|
|
out.push_str(e);
|
|
}
|
|
None => out.push(c),
|
|
}
|
|
} else {
|
|
if quote == '\0' {
|
|
quote = if c == '"' { '\'' } else { '"' };
|
|
}
|
|
if quote == c {
|
|
out.push('\\');
|
|
}
|
|
out.push(c);
|
|
}
|
|
}
|
|
if quote == '\0' {
|
|
quote = '"';
|
|
}
|
|
out.replace_range(0..1, "e.to_string());
|
|
out.push(quote);
|
|
out
|
|
}
|
|
fn unquoted_key(key: &str) -> bool {
|
|
if key.eq_ignore_ascii_case("true") || key.eq_ignore_ascii_case("false") {
|
|
return false;
|
|
}
|
|
let mut chars = key.chars();
|
|
let first_ok = chars
|
|
.next()
|
|
.is_some_and(|c| c.is_ascii_alphabetic() || c == '.' || c == '_');
|
|
// `[A-Za-z._]+[A-Za-z0-9._+-]*`
|
|
first_ok && chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '+' | '-'))
|
|
}
|
|
fn string_of(t: &horizon_world::nbt::JavaString) -> String {
|
|
t.to_string_lossy()
|
|
}
|
|
|
|
/// `Tag.toString` (StringTagVisitor).
|
|
pub fn to_snbt(tag: &Tag) -> String {
|
|
let mut out = String::new();
|
|
visit(tag, &mut out);
|
|
out
|
|
}
|
|
fn visit(tag: &Tag, out: &mut String) {
|
|
match tag {
|
|
Tag::End => out.push_str("END"),
|
|
Tag::Byte(v) => out.push_str(&format!("{v}b")),
|
|
Tag::Short(v) => out.push_str(&format!("{v}s")),
|
|
Tag::Int(v) => out.push_str(&v.to_string()),
|
|
Tag::Long(v) => out.push_str(&format!("{v}L")),
|
|
Tag::Float(v) => {
|
|
out.push_str(&java_float(f32::from_bits(*v)));
|
|
out.push('f');
|
|
}
|
|
Tag::Double(v) => {
|
|
out.push_str(&java_double(f64::from_bits(*v)));
|
|
out.push('d');
|
|
}
|
|
Tag::String(s) => out.push_str("e_and_escape(&string_of(s))),
|
|
Tag::ByteArray(a) => {
|
|
out.push_str("[B;");
|
|
out.push_str(
|
|
&a.iter()
|
|
.map(|v| format!("{}B", *v as i8))
|
|
.collect::<Vec<_>>()
|
|
.join(","),
|
|
);
|
|
out.push(']');
|
|
}
|
|
Tag::IntArray(a) => {
|
|
out.push_str("[I;");
|
|
out.push_str(&a.iter().map(i32::to_string).collect::<Vec<_>>().join(","));
|
|
out.push(']');
|
|
}
|
|
Tag::LongArray(a) => {
|
|
out.push_str("[L;");
|
|
out.push_str(
|
|
&a.iter()
|
|
.map(|v| format!("{v}L"))
|
|
.collect::<Vec<_>>()
|
|
.join(","),
|
|
);
|
|
out.push(']');
|
|
}
|
|
Tag::List(l) => {
|
|
out.push('[');
|
|
for (i, t) in l.iter().enumerate() {
|
|
if i > 0 {
|
|
out.push(',');
|
|
}
|
|
visit(t, out);
|
|
}
|
|
out.push(']');
|
|
}
|
|
Tag::Compound(c) => {
|
|
out.push('{');
|
|
let mut entries: Vec<(String, &Tag)> =
|
|
c.iter().map(|(k, v)| (string_of(k), v)).collect();
|
|
entries.sort_by(|a, b| a.0.cmp(&b.0));
|
|
for (i, (k, v)) in entries.iter().enumerate() {
|
|
if i > 0 {
|
|
out.push(',');
|
|
}
|
|
if unquoted_key(k) {
|
|
out.push_str(k);
|
|
} else {
|
|
out.push_str("e_and_escape(k));
|
|
}
|
|
out.push(':');
|
|
visit(v, out);
|
|
}
|
|
out.push('}');
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---- the coloured form -----------------------------------------------------
|
|
|
|
struct Pretty {
|
|
indentation: String,
|
|
result: Text,
|
|
indent_depth: usize,
|
|
depth: usize,
|
|
}
|
|
fn is_numeric(t: &Tag) -> bool {
|
|
matches!(
|
|
t,
|
|
Tag::Byte(_) | Tag::Short(_) | Tag::Int(_) | Tag::Long(_) | Tag::Float(_) | Tag::Double(_)
|
|
)
|
|
}
|
|
impl Pretty {
|
|
fn lit(&mut self, s: &str, color: Option<u32>) {
|
|
let t = match color {
|
|
Some(c) => Text::styled(s, Style::color(c)),
|
|
None => Text::literal(s),
|
|
};
|
|
self.result.push(t);
|
|
}
|
|
fn number(&mut self, s: &str, suffix: &str) {
|
|
self.lit(s, Some(text::GOLD));
|
|
if !suffix.is_empty() {
|
|
self.lit(suffix, Some(text::RED));
|
|
}
|
|
}
|
|
fn quoted(&mut self, input: &str, color: u32) {
|
|
let q = quote_and_escape(input);
|
|
let (open, inner) = (&q[..1], &q[1..q.len() - 1]);
|
|
self.lit(open, None);
|
|
self.lit(inner, Some(color));
|
|
self.lit(open, None);
|
|
}
|
|
fn array<T: std::fmt::Display>(&mut self, prefix: &str, items: &[T], suffix: &str) {
|
|
self.lit("[", None);
|
|
self.lit(prefix, Some(text::RED));
|
|
self.lit(";", None);
|
|
for (i, v) in items.iter().enumerate().take(128) {
|
|
self.lit(" ", None);
|
|
self.number(&v.to_string(), suffix);
|
|
if i != items.len() - 1 {
|
|
self.lit(",", None);
|
|
}
|
|
}
|
|
if items.len() > 128 {
|
|
self.lit("<...>", Some(text::GRAY));
|
|
}
|
|
self.lit("]", None);
|
|
}
|
|
fn sub(&mut self, tag: &Tag, indent: bool) {
|
|
if indent {
|
|
self.indent_depth += 1;
|
|
}
|
|
self.depth += 1;
|
|
self.visit(tag);
|
|
if indent {
|
|
self.indent_depth -= 1;
|
|
}
|
|
self.depth -= 1;
|
|
}
|
|
fn visit(&mut self, tag: &Tag) {
|
|
match tag {
|
|
Tag::End => {}
|
|
Tag::Byte(v) => self.number(&v.to_string(), "b"),
|
|
Tag::Short(v) => self.number(&v.to_string(), "s"),
|
|
Tag::Int(v) => self.number(&v.to_string(), ""),
|
|
Tag::Long(v) => self.number(&v.to_string(), "L"),
|
|
Tag::Float(v) => self.number(&java_float(f32::from_bits(*v)), "f"),
|
|
Tag::Double(v) => self.number(&java_double(f64::from_bits(*v)), "d"),
|
|
Tag::String(s) => self.quoted(&string_of(s), text::GREEN),
|
|
Tag::ByteArray(a) => {
|
|
let v: Vec<i8> = a.iter().map(|b| *b as i8).collect();
|
|
self.array("B", &v, "b");
|
|
}
|
|
Tag::IntArray(a) => self.array("I", a, ""),
|
|
Tag::LongArray(a) => self.array("L", a, "L"),
|
|
Tag::List(l) => self.list(l),
|
|
Tag::Compound(c) => self.compound(c),
|
|
}
|
|
}
|
|
fn list(&mut self, l: &[Tag]) {
|
|
if l.is_empty() {
|
|
self.lit("[", None);
|
|
self.lit("]", None);
|
|
} else if self.depth >= 64 {
|
|
self.lit("[", None);
|
|
self.lit("<...>", Some(text::GRAY));
|
|
self.lit("]", None);
|
|
} else if l.len() >= 8 || l.iter().all(is_numeric) {
|
|
self.lit("[", None);
|
|
for (i, t) in l.iter().enumerate() {
|
|
if i != 0 {
|
|
self.lit(",", None);
|
|
self.lit(" ", None);
|
|
}
|
|
self.sub(t, false);
|
|
}
|
|
self.lit("]", None);
|
|
} else {
|
|
self.lit("[", None);
|
|
if !self.indentation.is_empty() {
|
|
self.lit("\n", None);
|
|
}
|
|
let entry = self.indentation.repeat(self.indent_depth + 1);
|
|
let spacing = if self.indentation.is_empty() {
|
|
" "
|
|
} else {
|
|
"\n"
|
|
};
|
|
for (i, t) in l.iter().enumerate().take(128) {
|
|
self.lit(&entry, None);
|
|
self.sub(t, true);
|
|
if i != l.len() - 1 {
|
|
self.lit(",", None);
|
|
self.lit(spacing, None);
|
|
}
|
|
}
|
|
if l.len() > 128 {
|
|
self.lit(&entry, None);
|
|
self.lit("<...>", Some(text::GRAY));
|
|
}
|
|
if !self.indentation.is_empty() {
|
|
self.lit("\n", None);
|
|
let close = self.indentation.repeat(self.indent_depth);
|
|
self.lit(&close, None);
|
|
}
|
|
self.lit("]", None);
|
|
}
|
|
}
|
|
fn compound(&mut self, c: &horizon_world::nbt::Compound) {
|
|
if c.is_empty() {
|
|
self.lit("{", None);
|
|
self.lit("}", None);
|
|
} else if self.depth >= 64 {
|
|
self.lit("{", None);
|
|
self.lit("<...>", Some(text::GRAY));
|
|
self.lit("}", None);
|
|
} else {
|
|
self.lit("{", None);
|
|
if !self.indentation.is_empty() {
|
|
self.lit("\n", None);
|
|
}
|
|
let entry = self.indentation.repeat(self.indent_depth + 1);
|
|
let spacing = if self.indentation.is_empty() {
|
|
" "
|
|
} else {
|
|
"\n"
|
|
};
|
|
let n = c.len();
|
|
for (i, (k, v)) in java_hash_order(c).into_iter().enumerate() {
|
|
self.lit(&entry, None);
|
|
let key = string_of(k);
|
|
if !key.is_empty()
|
|
&& key
|
|
.chars()
|
|
.all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '.' | '_' | '+' | '-'))
|
|
{
|
|
self.lit(&key, Some(text::AQUA));
|
|
} else {
|
|
self.quoted(&key, text::AQUA);
|
|
}
|
|
self.lit(":", None);
|
|
self.lit(" ", None);
|
|
self.sub(v, true);
|
|
if i + 1 < n {
|
|
self.lit(",", None);
|
|
self.lit(spacing, None);
|
|
}
|
|
}
|
|
if !self.indentation.is_empty() {
|
|
self.lit("\n", None);
|
|
let close = self.indentation.repeat(self.indent_depth);
|
|
self.lit(&close, None);
|
|
}
|
|
self.lit("}", None);
|
|
}
|
|
}
|
|
}
|
|
/// The iteration order of the `HashMap<String, Tag>` a `CompoundTag` holds,
|
|
/// for a map that grew to its size by insertion: buckets by
|
|
/// `HashMap.hash(String.hashCode())`, keys sharing a bucket in key order.
|
|
pub fn java_hash_order(
|
|
c: &horizon_world::nbt::Compound,
|
|
) -> Vec<(&horizon_world::nbt::JavaString, &Tag)> {
|
|
let mut capacity = 16usize;
|
|
while c.len() > capacity * 3 / 4 {
|
|
capacity *= 2;
|
|
}
|
|
let bucket = |k: &horizon_world::nbt::JavaString| {
|
|
let h = string_of(k)
|
|
.encode_utf16()
|
|
.fold(0u32, |h, ch| h.wrapping_mul(31).wrapping_add(u32::from(ch)));
|
|
((h ^ (h >> 16)) as usize) & (capacity - 1)
|
|
};
|
|
// Keys sharing a bucket iterate in insertion order: the order vanilla's
|
|
// savers write them, as far as it is known here.
|
|
const WRITE_ORDER: &[&str] = &[
|
|
// ItemStack.CODEC.
|
|
"id",
|
|
"count",
|
|
"components",
|
|
// Entity.saveWithoutId.
|
|
"Pos",
|
|
"Motion",
|
|
"Rotation",
|
|
"fall_distance",
|
|
"Fire",
|
|
"Air",
|
|
"OnGround",
|
|
"Invulnerable",
|
|
"PortalCooldown",
|
|
"UUID",
|
|
"CustomName",
|
|
"CustomNameVisible",
|
|
"Silent",
|
|
"NoGravity",
|
|
"Glowing",
|
|
"TicksFrozen",
|
|
"HasVisualFire",
|
|
"Tags",
|
|
"data",
|
|
"Passengers",
|
|
// LivingEntity.addAdditionalSaveData.
|
|
"Health",
|
|
"HurtTime",
|
|
"DeathTime",
|
|
"AbsorptionAmount",
|
|
"current_impulse_context_reset_grace_time",
|
|
"current_explosion_impact_pos",
|
|
"attributes",
|
|
"active_effects",
|
|
"FallFlying",
|
|
"sleeping_pos",
|
|
"Brain",
|
|
"equipment",
|
|
// ArmorStand.
|
|
"Invisible",
|
|
"Small",
|
|
"ShowArms",
|
|
"DisabledSlots",
|
|
"NoBasePlate",
|
|
"Marker",
|
|
"Pose",
|
|
];
|
|
// BlockEntity.saveMetadata writes the position after the entity's data.
|
|
const AFTER: &[&str] = &["x", "y", "z"];
|
|
let rank = |k: &horizon_world::nbt::JavaString| {
|
|
let k = string_of(k);
|
|
WRITE_ORDER
|
|
.iter()
|
|
.position(|w| *w == k)
|
|
.or_else(|| {
|
|
AFTER
|
|
.iter()
|
|
.position(|w| *w == k)
|
|
.map(|i| WRITE_ORDER.len() + 1 + i)
|
|
})
|
|
.unwrap_or(WRITE_ORDER.len())
|
|
};
|
|
let mut entries: Vec<_> = c.iter().collect();
|
|
entries.sort_by_key(|(k, _)| (bucket(k), rank(k)));
|
|
entries
|
|
}
|
|
/// `NbtUtils.toPrettyComponent`: the coloured, unindented form.
|
|
pub fn pretty(tag: &Tag) -> Text {
|
|
pretty_indented(tag, "")
|
|
}
|
|
pub fn pretty_indented(tag: &Tag, indentation: &str) -> Text {
|
|
let mut p = Pretty {
|
|
indentation: indentation.to_owned(),
|
|
result: Text::empty(),
|
|
indent_depth: 0,
|
|
depth: 0,
|
|
};
|
|
p.visit(tag);
|
|
p.result
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use horizon_world::nbt::parse_snbt;
|
|
|
|
fn rt(s: &str) -> String {
|
|
to_snbt(&parse_snbt(s).unwrap().0)
|
|
}
|
|
#[test]
|
|
fn compact_form_matches_vanilla() {
|
|
assert_eq!(
|
|
rt("{b:1b,a:\"x\",c:[1,2],d:1.5f,e:2d,f:3L,g:4s}"),
|
|
"{a:\"x\",b:1b,c:[1,2],d:1.5f,e:2.0d,f:3L,g:4s}"
|
|
);
|
|
assert_eq!(rt("{\"a b\":1}"), "{\"a b\":1}");
|
|
assert_eq!(rt("{true:1}"), "{\"true\":1}");
|
|
assert_eq!(rt("[B;1b,2b]"), "[B;1B,2B]");
|
|
assert_eq!(rt("[I;1,2]"), "[I;1,2]");
|
|
assert_eq!(rt("[L;1L]"), "[L;1L]");
|
|
assert_eq!(quote_and_escape("say \"hi\""), "'say \"hi\"'");
|
|
assert_eq!(quote_and_escape("it's \"x\""), "\"it's \\\"x\\\"\"");
|
|
assert_eq!(quote_and_escape("a\nb\\"), "\"a\\nb\\\\\"");
|
|
}
|
|
#[test]
|
|
fn java_number_text() {
|
|
assert_eq!(java_double(1.0), "1.0");
|
|
assert_eq!(java_double(0.1), "0.1");
|
|
assert_eq!(java_double(1.0e7), "1.0E7");
|
|
assert_eq!(java_double(12345678.9), "1.23456789E7");
|
|
assert_eq!(java_double(0.0001), "1.0E-4");
|
|
assert_eq!(java_double(-3.5), "-3.5");
|
|
assert_eq!(java_float(0.3), "0.3");
|
|
assert_eq!(java_float(100.0), "100.0");
|
|
assert_eq!(java_double(f64::NAN), "NaN");
|
|
}
|
|
#[test]
|
|
fn pretty_form_is_coloured_without_changing_text() {
|
|
let tag = parse_snbt("{Pos:[1.5d,2.0d],id:\"minecraft:pig\",n:3b}")
|
|
.unwrap()
|
|
.0;
|
|
let t = pretty(&tag);
|
|
assert_eq!(
|
|
t.plain(),
|
|
"{Pos: [1.5d, 2.0d], id: \"minecraft:pig\", n: 3b}"
|
|
);
|
|
// keys aqua, strings green, numbers gold.
|
|
let colors: Vec<_> = t.spans.iter().filter_map(|s| s.style.color).collect();
|
|
assert!(colors.contains(&text::AQUA));
|
|
assert!(colors.contains(&text::GREEN));
|
|
assert!(colors.contains(&text::GOLD));
|
|
let long = parse_snbt("{l:[{a:1},{b:2}]}").unwrap().0;
|
|
assert_eq!(pretty(&long).plain(), "{l: [{a: 1}, {b: 2}]}");
|
|
}
|
|
}
|