Files
horizon-airdef/crates/horizon-client/src/mod_content_tests.rs
T
Ludovic Marie ac7b7f98c0 Content mods in the game: loot, recipes, models, names and icons from mod namespaces
The game builds the content root from the enabled content mods at startup.
Loaders that only read the minecraft namespace now also read the content
mods' namespaces (from the root's marker file): block loot tables, recipes,
blockstates, language files, and the HUD atlas's item sprites. /give and the
command registries know the new items and blocks. A block without a
blockstate of its own takes its template's, so every state has a model.

Adds examples/ruby-content, a folder content mod: a ruby block and a ruby
item with recipes, loot, tags, models, textures and names.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 19:23:35 +02:00

219 lines
8.2 KiB
Rust

use super::*;
use crate::{assets::Assets, blocks::BlockFacts, items::Items};
#[test]
fn renames_ids_keys_and_states() {
let mut r = Rename::new("minecraft:iron_block", "ruby:ruby_block");
r.states = 10..12;
r.first = 100;
assert_eq!(
r.string("minecraft:iron_block").as_deref(),
Some("ruby:ruby_block")
);
assert_eq!(
r.string("Block{minecraft:iron_block}").as_deref(),
Some("Block{ruby:ruby_block}")
);
assert_eq!(
r.string("block.minecraft.iron_block").as_deref(),
Some("block.ruby.ruby_block")
);
assert_eq!(r.string("minecraft:iron_blocks"), None);
assert_eq!(r.string("block.minecraft.iron_bars"), None);
assert_eq!(
(r.id(9), r.id(10), r.id(11), r.id(12)),
(None, Some(100), Some(101), None)
);
}
#[test]
fn json_keeps_key_order_and_numbers() {
let text = r#"{"z":1,"a":[0.6,5.0,-1,{"m":null,"b":true}],"k":"v"}"#;
let doc: J = serde_json::from_str(text).unwrap();
assert_eq!(serde_json::to_string(&doc).unwrap(), text);
}
#[test]
fn tags_merge_without_repeats() {
let vanilla = r#"{"values": ["minecraft:stone"]}"#;
let more = [
J::Str("ruby:ruby_block".into()),
J::Str("minecraft:stone".into()),
];
let merged: serde_json::Value =
serde_json::from_str(&merge_tag(Some(vanilla), &more, "t").unwrap()).unwrap();
assert_eq!(
merged["values"],
serde_json::json!(["minecraft:stone", "ruby:ruby_block"])
);
}
fn prepared() -> Option<PathBuf> {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3");
root.join("gameplay-facts.json").exists().then_some(root)
}
/// A content mod folder: a full block, a door and an item-less glass from
/// templates, an item, a vanilla tag addition and a language file.
fn ruby_mod(dir: &Path) -> ContentMod {
let files: [(&str, &str); 7] = [
(
"horizon-mod.toml",
"id = \"ruby\"\nname = \"Ruby\"\nversion = \"0.1.0\"\napi = \"0.2\"\nside = \"both\"\ncapabilities = [\"registries\"]\n",
),
(
"data/ruby/horizon/block/ruby_block.json",
r#"{"template": "minecraft:iron_block", "properties": {"hardness": 4.0, "light": 3,
"sounds": "minecraft:amethyst_block", "map_color": 28, "friction": 0.8}}"#,
),
(
"data/ruby/horizon/block/ruby_door.json",
r#"{"template": "minecraft:oak_door"}"#,
),
(
"data/ruby/horizon/block/ruby_glass.json",
r#"{"template": "minecraft:glass", "item": false}"#,
),
(
"data/ruby/horizon/item/ruby.json",
r#"{"template": "minecraft:diamond", "max_stack_size": 16, "rarity": "rare"}"#,
),
(
"data/minecraft/tags/block/mineable/pickaxe.json",
r#"{"values": ["ruby:ruby_block"]}"#,
),
(
"assets/ruby/lang/en_us.json",
r#"{"block.ruby.ruby_block": "Block of Ruby"}"#,
),
];
for (name, text) in files {
let path = dir.join(name);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, text).unwrap();
}
let package = horizon_mods::Package::read(dir).unwrap();
ContentMod {
id: "ruby".into(),
content: Content::read(dir, &package.manifest).unwrap(),
}
}
/// The overlay over the real prepared resources: every loader reads it, the
/// new blocks behave like their templates with the overrides, and vanilla's
/// states are where they were.
#[test]
fn content_root_loads_with_every_table() {
let Some(base) = prepared() else {
return;
};
let scratch = std::env::temp_dir().join(format!("horizon-mod-content-{}", std::process::id()));
let _ = fs::remove_dir_all(&scratch);
let m = ruby_mod(&scratch.join("ruby"));
let root = resource_root(&base, std::slice::from_ref(&m), &scratch.join("cache")).unwrap();
assert_ne!(root, base);
let vanilla = Assets::load(&base).unwrap();
let assets = Assets::load(&root).unwrap();
let (old, new) = (vanilla.registry, assets.registry);
let n = old.len() as u32;
let iron = old.default_state("minecraft:iron_block").unwrap();
let door = old.default_state("minecraft:oak_door").unwrap();
let door_first = (0..n)
.map(BlockStateId)
.find(|&s| old.state(s).unwrap().name == "minecraft:oak_door")
.unwrap();
// Vanilla keeps its ids; blocks come after, sorted: ruby_block, ruby_door, ruby_glass.
assert_eq!(new.len(), old.len() + 1 + 64 + 1);
assert_eq!(new.default_state("minecraft:iron_block"), Some(iron));
let ruby = new.default_state("ruby:ruby_block").unwrap();
assert_eq!(ruby, BlockStateId(n));
let ruby_door = new.default_state("ruby:ruby_door").unwrap();
assert_eq!(ruby_door, BlockStateId(n + 1 + (door.0 - door_first.0)));
assert_eq!(
new.default_state("ruby:ruby_glass"),
Some(BlockStateId(n + 65))
);
let old_facts = BlockFacts::load(&base, old).unwrap();
let facts = BlockFacts::load(&root, new).unwrap();
let (a, b) = (old_facts.get(iron), facts.get(ruby));
assert_eq!((b.hardness, b.emission, b.friction), (4.0, 3, 0.8));
assert_eq!(b.explosion_resistance, a.explosion_resistance);
assert_eq!(b.outline, a.outline);
assert_eq!(b.classes, a.classes);
assert_eq!(&*b.item, "ruby:ruby_block");
assert_eq!(b.map_color, 28);
let amethyst = old_facts.get(old.default_state("minecraft:amethyst_block").unwrap());
assert_eq!(b.sounds.break_sound, amethyst.sounds.break_sound);
for i in 0..64 {
let (v, m) = (
old_facts.get(BlockStateId(door_first.0 + i)),
facts.get(BlockStateId(n + 1 + i)),
);
assert_eq!(
(v.outline.clone(), v.hardness, v.classes.clone()),
(m.outline.clone(), m.hardness, m.classes.clone())
);
assert_eq!(&*m.name, "ruby:ruby_door");
}
let glass = facts.get(BlockStateId(n + 65));
assert_eq!(&*glass.item, "minecraft:air");
// Vanilla's own rows are untouched.
assert_eq!(facts.get(iron).hardness, a.hardness);
assert_eq!(&*facts.get(iron).name, "minecraft:iron_block");
let items = Items::load(&root, new, &facts).unwrap();
let ruby_block = &items.items["ruby:ruby_block"];
assert_eq!(ruby_block.block, Some(ruby));
let gem = &items.items["ruby:ruby"];
assert_eq!(gem.max_stack, 16);
assert!(!items.items.contains_key("ruby:ruby_glass"));
assert!(items.block_tags["minecraft:mineable/pickaxe"].contains("ruby:ruby_block"));
crate::collisions::load(&root, new).unwrap();
crate::models::load_visibility(&root, new).unwrap();
crate::light::LightTables::load(&root, new, &facts).unwrap();
crate::motion::MotionResources::load(&root, &assets).unwrap();
horizon_world::worldgen::generator::GeneratorData::load(&root.join("worldgen"), new).unwrap();
assert!(root.join("assets/ruby/lang/en_us.json").is_file());
// Blocks without a blockstate of their own take their template's.
assert_eq!(
fs::read(root.join("assets/ruby/blockstates/ruby_door.json")).unwrap(),
fs::read(base.join("assets/minecraft/blockstates/oak_door.json")).unwrap()
);
// The base root was only read.
assert!(
fs::symlink_metadata(base.join("facts.json"))
.unwrap()
.is_file()
);
fs::remove_dir_all(&scratch).unwrap();
}
#[test]
fn refuses_unknown_templates_and_vanilla_ids() {
let Some(base) = prepared() else {
return;
};
let scratch = std::env::temp_dir().join(format!("horizon-mod-refuse-{}", std::process::id()));
let _ = fs::remove_dir_all(&scratch);
let mut m = ruby_mod(&scratch.join("ruby"));
m.content.blocks[0].template = "minecraft:not_a_block".into();
let e = build(&base, std::slice::from_ref(&m), &scratch.join("out")).unwrap_err();
assert!(e.to_string().contains("is not a block"), "{e}");
fs::remove_dir_all(&scratch).unwrap();
}
#[test]
fn no_content_keeps_the_base_root() {
let base = Path::new("/nonexistent/base");
assert_eq!(
resource_root(base, &[], Path::new("/nonexistent/cache")).unwrap(),
base
);
}
use horizon_world::registry::BlockStateId;