Per-mod parity harness against a dev-only NeoForge oracle

The harness the modpack design calls for, for data-tier mods:

- tools/neoforge_oracle.py installs a NeoForge 21.1.259 server (ATM10's)
  under .local/neoforge-oracle, builds the exporter in
  mods/horizon/src/neoforge-oracle (our code, never shipped), runs it
  headless with the user's jars and writes every block (class chain,
  states, per-state hardness, light, tool need, map colour, sounds,
  outline and collision; drops for hand, diamond tools, shears and silk
  touch; placement; BlockSetType and WoodType), item, recipe and tag of
  the mods' namespaces, plus the vanilla blocks for templates. About 30 s.
- examples/mod_port_probe.rs builds the content root from the carried
  mods and dumps what Horizon's loaders make of the same things.
- tools/mod_port_harness.py compares them per mod and writes
  <work>/mods/<id>/harness/result.json. 1.21.1 sound events map to 26.3's
  through vanilla blocks both have, and vanilla items newer than 1.21.1
  are not counted against a recipe.

On ATM10's own jars (MIT): Macaw's Trapdoors passes everything; Doors,
Fences, Paths, Glassential, Factory Blocks and Cloud Glass pass every
drop, recipe, tag and placement and fail only on what API 0.2 cannot say
(shapes and sound events of their own, blocks of classes no vanilla block
shares). Jars, facts and results stay in the work folder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-16 23:27:20 +02:00
parent 886def4360
commit 32f5f01e22
6 changed files with 1128 additions and 2 deletions
@@ -0,0 +1,271 @@
//! The Horizon side of a mod port's harness (tools/mod_port_harness.py,
//! docs/modpacks.md): builds the content root for carried-over mods and
//! writes what Horizon makes of every block state, drop, recipe and tag the
//! NeoForge oracle exported.
//!
//! cargo run -p horizon-client --example mod_port_probe -- PREPARED WORK OUT.json [MOD...]
//!
//! WORK is a `horizon-port-pack` work folder: `oracle/facts.json` and
//! `mods/<id>/carry/`. The root is built in `WORK/horizon-root`.
use horizon_client::{
Result,
assets::Assets,
blocks::BlockFacts,
crafting::{Recipe, Recipes},
items::{ItemStack, Items},
mod_content::{self, ContentMod},
stations::StationRecipes,
};
use horizon_core::{geometry::Axis, random::LegacyRandom};
use horizon_world::chunk::BlockState;
use serde_json::{Map, Value, json};
use std::{collections::BTreeMap, fs, path::Path};
fn boxes(list: impl IntoIterator<Item = [f64; 6]>) -> Value {
let mut v: Vec<[f64; 6]> = list.into_iter().collect();
v.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
json!(v)
}
fn sound(s: &horizon_client::blocks::Sounds) -> Value {
json!({"volume": s.volume, "pitch": s.pitch, "break": &*s.break_sound, "step": &*s.step,
"place": &*s.place, "hit": &*s.hit, "fall": &*s.fall})
}
fn tool(name: &str) -> Option<ItemStack> {
match name {
"hand" => None,
"silk_touch_pickaxe" => {
let mut pick = ItemStack::new("minecraft:diamond_pickaxe", 1);
pick.enchant("minecraft:silk_touch", 1);
Some(pick)
}
other => Some(ItemStack::new(&format!("minecraft:{other}"), 1)),
}
}
fn main() -> Result<()> {
let args: Vec<String> = std::env::args().skip(1).collect();
let [base, work, out, wanted @ ..] = args.as_slice() else {
return Err("usage: mod_port_probe PREPARED WORK OUT.json [MOD...]".into());
};
let (base, work) = (Path::new(base), Path::new(work));
let oracle: Value = serde_json::from_slice(&fs::read(work.join("oracle/facts.json"))?)?;
let mut mods = Vec::new();
for entry in fs::read_dir(work.join("mods"))?.flatten() {
let id = entry.file_name().to_string_lossy().into_owned();
let dir = entry.path().join("carry");
if !(wanted.is_empty() || wanted.contains(&id)) || !dir.is_dir() {
continue;
}
let package = horizon_mods::Package::read(&dir)?;
let content = horizon_mods::Content::read(&dir, &package.manifest)?;
mods.push(ContentMod {
id: package.manifest.id.clone(),
content,
});
}
mods.sort_by(|a, b| a.id.cmp(&b.id));
let root = work.join("horizon-root");
let began = std::time::Instant::now();
let step = |what: &str| {
let what = what.to_owned();
move |e: Box<dyn std::error::Error>| format!("{what}: {e}")
};
mod_content::build(base, &mods, &root).map_err(step("content root"))?;
let built = began.elapsed().as_secs_f64();
let assets = Assets::load(&root).map_err(step("assets"))?;
let registry = assets.registry;
let facts = BlockFacts::load(&root, registry).map_err(step("block facts"))?;
let items = Items::load(&root, registry, &facts).map_err(step("items"))?;
let recipes = Recipes::load(&root, &items).map_err(step("recipes"))?;
let stations = StationRecipes::load(&root, &items);
let collisions =
horizon_client::collisions::load(&root, registry).map_err(step("collisions"))?;
let loaded = began.elapsed().as_secs_f64() - built;
let ours = |id: &str| {
id.split_once(':')
.is_some_and(|(ns, _)| mods.iter().any(|m| m.id == ns))
};
let tools: Vec<String> = serde_json::from_value(oracle["tools"].clone())?;
let mut blocks = Map::new();
for (name, block) in oracle["blocks"].as_object().ok_or("oracle: no blocks")? {
if !ours(name) {
continue;
}
let Some(default) = registry.default_state(name) else {
blocks.insert(name.clone(), Value::Null);
continue;
};
let mut states = Map::new();
for (key, o) in block["states"].as_object().into_iter().flatten() {
let properties: BTreeMap<String, String> = key
.split(',')
.filter(|p| !p.is_empty())
.filter_map(|p| p.split_once('='))
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.collect();
let descriptor = BlockState {
name: name.clone(),
properties,
};
let Some(id) = registry.id(&descriptor) else {
states.insert(key.clone(), Value::Null);
continue;
};
let f = facts.get(id);
let collision = collisions
.cached_shapes()
.cached_shape(id)
.ok()
.flatten()
.map(|s| {
boxes(s.boxes().iter().map(|b| {
[
b.min(Axis::X),
b.min(Axis::Y),
b.min(Axis::Z),
b.max(Axis::X),
b.max(Axis::Y),
b.max(Axis::Z),
]
}))
});
let mut row = json!({
"hardness": f.hardness,
"explosion_resistance": f.explosion_resistance,
"emission": f.emission,
"requires_tool": f.requires_tool,
"friction": f.friction,
"map_color_id": f.map_color,
"can_occlude": f.can_occlude,
"sound": sound(&f.sounds),
"outline": boxes(f.outline.iter().copied()),
"collision": collision,
"classes": f.classes.iter().map(|c| &**c).collect::<Vec<_>>(),
});
if o.get("drops").is_some() {
let mut drops = Map::new();
for t in &tools {
let mut random = LegacyRandom::new(0);
let held = tool(t);
let mut counts: BTreeMap<String, u32> = BTreeMap::new();
for s in items.block_drops(registry, id, held.as_ref(), &mut random) {
*counts.entry(s.item.to_string()).or_default() += s.count;
}
drops.insert(t.clone(), json!(counts));
}
row["drops"] = Value::Object(drops);
}
states.insert(key.clone(), row);
}
let item = facts.get(default).item.to_string();
blocks.insert(
name.clone(),
json!({"states": states, "item": item, "item_known": items.items.contains_key(&*item)}),
);
}
// Vanilla's sounds, by block: the harness maps the oracle's version's
// sound events to this one's through them.
let mut vanilla_sounds = Map::new();
for name in oracle["blocks"]
.as_object()
.into_iter()
.flatten()
.map(|(k, _)| k)
{
if let Some(id) = name
.starts_with("minecraft:")
.then(|| registry.default_state(name))
.flatten()
{
vanilla_sounds.insert(name.clone(), sound(&facts.get(id).sounds));
}
}
let mut item_rows = Map::new();
for name in oracle["items"]
.as_object()
.into_iter()
.flatten()
.map(|(k, _)| k)
{
item_rows.insert(
name.clone(),
match items.items.get(name.as_str()) {
Some(i) => json!({"max_stack_size": i.max_stack, "block": i.block.map(|b| b.0)}),
None => Value::Null,
},
);
}
let sorted = |list: &[String]| {
let mut v = list.to_vec();
v.sort();
v.dedup();
v
};
let mut recipe_rows = Map::new();
for (name, recipe) in &recipes.crafting {
if !ours(name) {
continue;
}
let row = match recipe {
Recipe::Shaped { cells, result, .. } => json!({
"result": result.item.as_deref(), "count": result.count,
"ingredients": cells.iter().flatten().map(|i| sorted(&i.1)).collect::<Vec<_>>()}),
Recipe::Shapeless {
ingredients,
result,
} => json!({
"result": result.item.as_deref(), "count": result.count,
"ingredients": ingredients.iter().map(|i| sorted(&i.1)).collect::<Vec<_>>()}),
Recipe::Special(_) => json!({"special": true}),
};
recipe_rows.insert(name.clone(), row);
}
for c in recipes.cooking.iter().filter(|c| ours(&c.id)) {
recipe_rows.insert(
c.id.clone(),
json!({"result": &*c.result.item, "count": c.result.count,
"ingredients": [sorted(&c.ingredient.1)]}),
);
}
for s in stations.stonecutting.iter().filter(|s| ours(&s.id)) {
recipe_rows.insert(
s.id.clone(),
json!({"result": &*s.result.item, "count": s.result.count,
"ingredients": [sorted(&s.input.1)]}),
);
}
let mut block_tags = Map::new();
for (tag, members) in &items.block_tags {
let mut ids: Vec<&String> = members.iter().filter(|m| ours(m)).collect();
if !ids.is_empty() {
ids.sort();
block_tags.insert(tag.clone(), json!(ids));
}
}
let report = json!({
"states": registry.len(),
"build_seconds": built,
"load_seconds": loaded,
"blocks": blocks,
"items": item_rows,
"recipes": recipe_rows,
"block_tags": block_tags,
"vanilla_sounds": vanilla_sounds,
});
fs::write(out, serde_json::to_vec(&report)?)?;
println!(
"{out}: {} blocks, {} items, {} recipes; root built in {built:.2} s, loaded in {loaded:.2} s",
blocks.len(),
item_rows.len(),
recipe_rows.len()
);
Ok(())
}
+45 -2
View File
@@ -399,8 +399,51 @@ A **harness per mod**, generated from the tier:
| deep | entity scenarios (spawn, tick N, positions and states, drops), and render captures where the look is the point, compared as images with a tolerance |
The comparison skips vanilla 1.21.1-versus-26.3 differences: it normalises ids and components
through the step-4 upgrader and compares only the mod's own ids and effects. The harness format
is `tools/parity.py`'s, so the same runner, local or CI, runs vanilla and mod harnesses.
through the step-4 upgrader and compares only the mod's own ids and effects.
**Status: the data-tier harness runs.**
```
python3 tools/neoforge_oracle.py export --jars <mods> --deps <their libraries> --out <work>/oracle/facts.json
cargo run -p horizon-port-pack -- carry <work> --resources .local/horizon-client/26.3
python3 tools/mod_port_harness.py --work <work> # or --jars/--deps to export first
```
- `tools/neoforge_oracle.py` installs a NeoForge server (21.1.259, ATM10's) under
`.local/neoforge-oracle/`, builds the exporter mod in `mods/horizon/src/neoforge-oracle/` (our code, dev
only), starts the server headless with the jars and writes, for every block of the mods'
namespaces and every vanilla block: the class chain, states, per-state hardness, light, tool
need, map colour, sounds, outline and collision shapes; drops for hand, diamond tools, shears
and silk touch; placement on a floor and a wall; BlockSetType and WoodType. Then items (class,
stack size, rarity, food), recipes (type, result, count, resolved ingredients) and tags. One run
with the seven mods below takes about 30 s.
- `crates/horizon-client/examples/mod_port_probe.rs` builds the content root from the carried
mods and dumps what Horizon's loaders make of the same blocks, drops, recipes and tags.
- `tools/mod_port_harness.py` compares them per mod (blocks, properties, sounds, shapes, drops,
placement against the template's in the oracle, vanilla tags, recipes, items) and writes
`<work>/mods/<id>/harness/result.json`. It maps 1.21.1 sound events to 26.3's through the
vanilla blocks both versions have (`block.metal.*` became `block.iron.*`) and ignores vanilla
items 1.21.1 did not have (pale oak in `#minecraft:logs`).
The oracle needs the mods' jars, so it is not a `tools/parity.py` harness: it runs where the
user's jars are, and its facts and results stay in the work folder. The first real mods
(ATM10's own jars from Modrinth, all MIT; checks passed / compared):
| mod | blocks | properties | sounds | shapes | drops | placement | tags | recipes | items |
|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|
| Macaw's Trapdoors 1.1.5 | 179/179 | 11456/11456 | 11456/11456 | 11456/11456 | 10024/10024 | 358/358 | 534/534 | 191/191 | 191/191 |
| Macaw's Doors 1.1.5 | 239/245 | 15296/15296 | 15296/15296 | 12352/15296 | 13384/13384 | 478/478 | 941/941 | 257/257 | 251/257 |
| Macaw's Fences and Walls 1.2.1 | 150/180 | 4800/4800 | 4800/4800 | 4032/4800 | 33600/33600 | 300/300 | 432/432 | 222/222 | 150/180 |
| Macaw's Paths and Pavings 1.1.1 | 234/315 | 4602/4602 | 4602/4602 | 4472/4602 | 6734/6734 | 468/468 | 416/416 | 315/315 | 234/315 |
| Glassential-renewed 3.4.7 | 105/114 | 5177/5177 | 5177/5177 | 4727/5177 | 6447/6447 | 210/210 | 105/105 | 143/143 | 106/115 |
| Factory Blocks 1.4.0 | 42/49 | 42/42 | 0/42 | 42/42 | 294/294 | 84/84 | 42/42 | 1/1 | 42/49 |
| Cloud Glass 1.1.0 | 16/16 | 16/16 | 16/16 | 0/16 | 112/112 | 32/32 | – | 31/31 | 16/16 |
What fails is what API 0.2 cannot express yet: shapes of their own (Japanese doors, hedges,
paths a sixteenth high, glass without collision), sound events of the mod's own
(`factory_blocks:metal_sound`), and blocks whose vanilla class has no vanilla block with the
same states (garage doors, double gates, engraved and facing paths, fans). Every drop, recipe
and tag of the declared blocks matches.
### 7. Coverage report
@@ -0,0 +1,426 @@
package dev.horizonlab.neoforge;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.EmptyBlockGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.storage.loot.LootParams;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.server.ServerStartedEvent;
/**
* Dev-only oracle for Horizon's mod ports (docs/modpacks.md): once the server has started,
* writes every block, item, recipe and tag of the namespaces named in {@code
* horizon.oracle.namespaces} (plus the vanilla blocks, so templates can be compared) to {@code
* horizon.oracle.out}, then stops the server. Never shipped to players.
*/
@Mod("horizon_oracle")
public final class FactsExporter {
private static final BlockPos FLOOR = new BlockPos(0, 99, 0);
public FactsExporter(IEventBus modBus) {
NeoForge.EVENT_BUS.addListener(this::started);
}
private void started(ServerStartedEvent event) {
MinecraftServer server = event.getServer();
Path out = Path.of(System.getProperty("horizon.oracle.out", "horizon-oracle.json"));
Set<String> namespaces =
new TreeSet<>(Arrays.asList(System.getProperty("horizon.oracle.namespaces", "").split(",")));
namespaces.remove("");
try (Writer w = Files.newBufferedWriter(out, StandardCharsets.UTF_8);
JsonWriter json = new JsonWriter(w)) {
new Export(server, namespaces, json).run();
} catch (Throwable e) {
e.printStackTrace();
System.exit(3);
}
System.out.println("horizon-oracle: wrote " + out);
server.halt(false);
}
private static final class Export {
final MinecraftServer server;
final ServerLevel level;
final Set<String> namespaces;
final JsonWriter json;
final List<ItemStack> tools = new ArrayList<>();
final List<String> toolNames = new ArrayList<>();
Export(MinecraftServer server, Set<String> namespaces, JsonWriter json) {
this.server = server;
this.level = server.overworld();
this.namespaces = namespaces;
this.json = json;
tool("hand", ItemStack.EMPTY);
tool("diamond_pickaxe", new ItemStack(Items.DIAMOND_PICKAXE));
tool("diamond_axe", new ItemStack(Items.DIAMOND_AXE));
tool("diamond_shovel", new ItemStack(Items.DIAMOND_SHOVEL));
tool("diamond_hoe", new ItemStack(Items.DIAMOND_HOE));
tool("shears", new ItemStack(Items.SHEARS));
ItemStack silk = new ItemStack(Items.DIAMOND_PICKAXE);
silk.enchant(
server.registryAccess()
.registryOrThrow(Registries.ENCHANTMENT)
.getHolderOrThrow(Enchantments.SILK_TOUCH),
1);
tool("silk_touch_pickaxe", silk);
}
void tool(String name, ItemStack stack) {
toolNames.add(name);
tools.add(stack);
}
boolean ours(ResourceLocation id) {
return namespaces.contains(id.getNamespace());
}
void run() throws IOException {
json.beginObject();
json.name("schema").value(1);
json.name("minecraft").value(net.minecraft.SharedConstants.getCurrentVersion().getName());
json.name("namespaces");
json.beginArray();
for (String n : namespaces) json.value(n);
json.endArray();
json.name("tools");
json.beginArray();
for (String n : toolNames) json.value(n);
json.endArray();
blocks();
// Vanilla's items, so harnesses can tell later versions' additions apart.
json.name("vanilla_items");
json.beginArray();
for (Item item : BuiltInRegistries.ITEM) {
ResourceLocation id = BuiltInRegistries.ITEM.getKey(item);
if (id.getNamespace().equals("minecraft")) json.value(id.toString());
}
json.endArray();
items();
recipes();
tags();
json.endObject();
}
void blocks() throws IOException {
level.setBlock(FLOOR, Blocks.STONE.defaultBlockState(), 3);
json.name("blocks");
json.beginObject();
for (Block block : BuiltInRegistries.BLOCK) {
ResourceLocation id = BuiltInRegistries.BLOCK.getKey(block);
boolean mod = ours(id);
if (!mod && !id.getNamespace().equals("minecraft")) continue;
json.name(id.toString());
json.beginObject();
json.name("class");
json.beginArray();
for (Class<?> c = block.getClass(); c != null && c != Object.class; c = c.getSuperclass()) {
json.value(c.getName());
}
json.endArray();
BlockState def = block.defaultBlockState();
json.name("properties");
json.beginObject();
for (Property<?> p : block.getStateDefinition().getProperties()) {
json.name(p.getName());
json.beginArray();
for (Object v : p.getPossibleValues()) json.value(name(p, v));
json.endArray();
}
json.endObject();
json.name("default").value(stateKey(def));
json.name("item").value(BuiltInRegistries.ITEM.getKey(block.asItem()).toString());
json.name("explosion_resistance").value(block.getExplosionResistance());
json.name("friction").value(block.getFriction());
json.name("speed_factor").value(block.getSpeedFactor());
json.name("jump_factor").value(block.getJumpFactor());
blockSetType(block);
json.name("states");
json.beginObject();
for (BlockState state : block.getStateDefinition().getPossibleStates()) {
json.name(stateKey(state));
json.beginObject();
json.name("hardness").value(state.getDestroySpeed(EmptyBlockGetter.INSTANCE, BlockPos.ZERO));
json.name("emission").value(state.getLightEmission());
json.name("requires_tool").value(state.requiresCorrectToolForDrops());
json.name("can_occlude").value(state.canOcclude());
json.name("replaceable").value(state.canBeReplaced());
json.name("random_ticks").value(state.isRandomlyTicking());
var color = state.getMapColor(EmptyBlockGetter.INSTANCE, BlockPos.ZERO);
json.name("map_color").value(color.col);
json.name("map_color_id").value(color.id);
sound(state.getSoundType(level, FLOOR.above(), null));
json.name("outline");
boxes(state.getShape(EmptyBlockGetter.INSTANCE, BlockPos.ZERO));
json.name("collision");
boxes(state.getCollisionShape(EmptyBlockGetter.INSTANCE, BlockPos.ZERO));
if (mod && lootSample(block, state)) drops(state);
json.endObject();
}
json.endObject();
if (block.asItem() instanceof BlockItem bi) placement(bi);
json.endObject();
}
json.endObject();
}
/** Every state of a small block; else the default and each one-property change from it. */
boolean lootSample(Block block, BlockState state) {
if (block.getStateDefinition().getPossibleStates().size() <= 32) return true;
BlockState def = block.defaultBlockState();
int differ = 0;
for (Property<?> p : block.getStateDefinition().getProperties()) {
if (!state.getValue(p).equals(def.getValue(p))) differ++;
}
return differ <= 1;
}
void drops(BlockState state) throws IOException {
json.name("drops");
json.beginObject();
for (int i = 0; i < tools.size(); i++) {
LootParams.Builder params =
new LootParams.Builder(level)
.withParameter(LootContextParams.ORIGIN, Vec3.atCenterOf(FLOOR.above()))
.withParameter(LootContextParams.TOOL, tools.get(i).copy());
Map<String, Integer> counts = new TreeMap<>();
for (ItemStack s : state.getDrops(params)) {
counts.merge(BuiltInRegistries.ITEM.getKey(s.getItem()).toString(), s.getCount(), Integer::sum);
}
json.name(toolNames.get(i));
json.beginObject();
for (Map.Entry<String, Integer> e : counts.entrySet()) json.name(e.getKey()).value(e.getValue());
json.endObject();
}
json.endObject();
}
void placement(BlockItem item) throws IOException {
BlockPos at = FLOOR.above();
level.setBlock(at, Blocks.AIR.defaultBlockState(), 3);
json.name("placement");
json.beginObject();
for (Direction face : new Direction[] {Direction.UP, Direction.NORTH}) {
BlockPos clicked = face == Direction.UP ? FLOOR : at.relative(face.getOpposite());
level.setBlock(clicked, Blocks.STONE.defaultBlockState(), 3);
Vec3 hit = Vec3.atCenterOf(clicked).add(Vec3.atLowerCornerOf(face.getNormal()).scale(0.5));
// A player-less context (the constructor is protected, hence the subclass).
BlockPlaceContext ctx =
new BlockPlaceContext(
level, null, InteractionHand.MAIN_HAND, new ItemStack(item),
new BlockHitResult(hit, face, clicked, false)) {};
BlockState placed = null;
try {
placed = item.getBlock().getStateForPlacement(ctx);
} catch (RuntimeException e) {
// Needs a player: recorded as no placement.
}
json.name(face.getName()).value(placed == null ? null : stateKey(placed));
if (face != Direction.UP) level.setBlock(clicked, Blocks.AIR.defaultBlockState(), 3);
}
json.endObject();
}
/** The BlockSetType (doors, trapdoors, buttons, plates) and WoodType (fence gates) a block keeps. */
void blockSetType(Block block) throws IOException {
String setType = null, woodType = null;
Boolean byHand = null;
for (Class<?> c = block.getClass(); c != null && c != Object.class; c = c.getSuperclass()) {
for (var f : c.getDeclaredFields()) {
try {
if (f.getType() == net.minecraft.world.level.block.state.properties.BlockSetType.class && setType == null) {
f.setAccessible(true);
var t = (net.minecraft.world.level.block.state.properties.BlockSetType) f.get(block);
if (t != null) {
setType = t.name();
byHand = t.canOpenByHand();
}
} else if (f.getType() == net.minecraft.world.level.block.state.properties.WoodType.class && woodType == null) {
f.setAccessible(true);
var t = (net.minecraft.world.level.block.state.properties.WoodType) f.get(block);
if (t != null) woodType = t.name();
}
} catch (ReflectiveOperationException | RuntimeException e) {
// Not readable: left out.
}
}
}
if (setType != null) {
json.name("block_set_type");
json.beginObject();
json.name("name").value(setType);
json.name("can_open_by_hand").value(byHand);
json.endObject();
}
if (woodType != null) json.name("wood_type").value(woodType);
}
void sound(SoundType s) throws IOException {
json.name("sound");
json.beginObject();
json.name("volume").value(s.getVolume());
json.name("pitch").value(s.getPitch());
event("break", s.getBreakSound());
event("step", s.getStepSound());
event("place", s.getPlaceSound());
event("hit", s.getHitSound());
event("fall", s.getFallSound());
json.endObject();
}
void event(String name, SoundEvent e) throws IOException {
json.name(name).value(e.getLocation().toString());
}
void boxes(VoxelShape shape) throws IOException {
json.beginArray();
for (AABB b : shape.toAabbs()) {
json.beginArray();
json.value(b.minX).value(b.minY).value(b.minZ).value(b.maxX).value(b.maxY).value(b.maxZ);
json.endArray();
}
json.endArray();
}
void items() throws IOException {
json.name("items");
json.beginObject();
for (Item item : BuiltInRegistries.ITEM) {
ResourceLocation id = BuiltInRegistries.ITEM.getKey(item);
if (!ours(id)) continue;
ItemStack stack = new ItemStack(item);
json.name(id.toString());
json.beginObject();
json.name("class").value(item.getClass().getName());
json.name("max_stack_size").value(stack.getMaxStackSize());
json.name("rarity").value(stack.getRarity().getSerializedName());
if (item instanceof BlockItem bi) {
json.name("block").value(BuiltInRegistries.BLOCK.getKey(bi.getBlock()).toString());
}
var food = stack.get(net.minecraft.core.component.DataComponents.FOOD);
if (food != null) {
json.name("food");
json.beginObject();
json.name("nutrition").value(food.nutrition());
json.name("saturation").value(food.saturation());
json.endObject();
}
json.endObject();
}
json.endObject();
}
void recipes() throws IOException {
json.name("recipes");
json.beginObject();
for (RecipeHolder<?> holder : server.getRecipeManager().getRecipes()) {
if (!ours(holder.id())) continue;
var recipe = holder.value();
json.name(holder.id().toString());
json.beginObject();
json.name("type").value(BuiltInRegistries.RECIPE_TYPE.getKey(recipe.getType()).toString());
json.name("serializer").value(BuiltInRegistries.RECIPE_SERIALIZER.getKey(recipe.getSerializer()).toString());
ItemStack result = recipe.getResultItem(server.registryAccess());
json.name("result").value(BuiltInRegistries.ITEM.getKey(result.getItem()).toString());
json.name("count").value(result.getCount());
json.name("ingredients");
json.beginArray();
for (Ingredient ing : recipe.getIngredients()) {
Set<String> ids = new TreeSet<>();
for (ItemStack s : ing.getItems()) ids.add(BuiltInRegistries.ITEM.getKey(s.getItem()).toString());
json.beginArray();
for (String i : ids) json.value(i);
json.endArray();
}
json.endArray();
json.endObject();
}
json.endObject();
}
/** Tags that hold one of our ids, with only those members; and every `c` tag, whole. */
void tags() throws IOException {
json.name("tags");
json.beginObject();
for (String kind : new String[] {"block", "item"}) {
json.name(kind);
json.beginObject();
Map<String, Set<String>> out = new TreeMap<>();
var registry = kind.equals("block") ? BuiltInRegistries.BLOCK : BuiltInRegistries.ITEM;
registry.getTags().forEach(pair -> {
String tag = pair.getFirst().location().toString();
boolean convention = pair.getFirst().location().getNamespace().equals("c");
Set<String> members = new LinkedHashSet<>();
for (var holder : pair.getSecond()) {
ResourceLocation id = holder.unwrapKey().orElseThrow().location();
if (convention || ours(id) || ours(pair.getFirst().location())) members.add(id.toString());
}
if (!members.isEmpty()) out.put(tag, new TreeSet<>(members));
});
for (var e : out.entrySet()) {
json.name(e.getKey());
json.beginArray();
for (String m : e.getValue()) json.value(m);
json.endArray();
}
json.endObject();
}
json.endObject();
}
static String stateKey(BlockState state) {
StringBuilder b = new StringBuilder();
for (Property<?> p : state.getProperties()) {
if (b.length() > 0) b.append(',');
b.append(p.getName()).append('=').append(name(p, state.getValue(p)));
}
return b.toString();
}
@SuppressWarnings({"unchecked", "rawtypes"})
static String name(Property p, Object v) {
return p.getName((Comparable) v);
}
}
}
@@ -0,0 +1,11 @@
# Dev-only: the NeoForge oracle for Horizon's mod-port harnesses (docs/modpacks.md).
# Built and run by tools/neoforge_oracle.py; never shipped.
modLoader = "javafml"
loaderVersion = "[4,)"
license = "Apache-2.0"
[[mods]]
modId = "horizon_oracle"
version = "1"
displayName = "Horizon oracle (dev only)"
description = "Exports block, item, recipe and tag facts for Horizon's parity harnesses."
+211
View File
@@ -0,0 +1,211 @@
#!/usr/bin/env python3
"""Per-mod parity harness for ported mods (docs/modpacks.md, "The Java oracle and per-mod harnesses").
mod_port_harness.py --work WORK [--resources PREPARED] [--jars DIR --deps DIR] [MOD ...]
WORK is a `horizon-port-pack` work folder whose mods were carried over. With --jars the NeoForge
oracle (tools/neoforge_oracle.py, dev-only) first exports WORK/oracle/facts.json. The Horizon side
is crates/horizon-client/examples/mod_port_probe.rs: the content root built from WORK/mods/*/carry
and what the game's loaders make of it. For every block and item the mod registers, compared:
- blocks: declared in Horizon at all; per state hardness, explosion resistance, light, tool need,
friction, map colour, sounds, outline and collision shapes;
- drops: the loot of each sampled state for hand, diamond tools, shears and silk touch;
- placement: the state the block takes when placed, against its template's in the same oracle
(Horizon places the template as vanilla does, which the vanilla harnesses check);
- tags: the vanilla block tags (tool and mining tags) holding the mod's blocks;
- recipes: each recipe's result, count and ingredients;
- items: present, and their stack size.
Writes WORK/mods/<id>/harness/result.json per mod and prints a table. Jars, facts and results stay
in WORK, outside the repository.
"""
import argparse
import json
import math
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
CHECKS = ['blocks', 'properties', 'sounds', 'shapes', 'drops', 'placement', 'tags', 'recipes', 'items']
EXAMPLES = 8
def key(k):
return ','.join(sorted(p for p in k.split(',') if p))
def cells(boxes, grid=32):
out = set()
for b in boxes:
lo = [math.floor(round(v * grid, 6)) for v in b[:3]]
hi = [math.ceil(round(v * grid, 6)) for v in b[3:]]
for x in range(lo[0], hi[0]):
for y in range(lo[1], hi[1]):
for z in range(lo[2], hi[2]):
out.add((x, y, z))
return out
def same_shape(a, b):
if a is None or b is None:
return a == b
sa, sb = sorted(map(tuple, a)), sorted(map(tuple, b))
if sa == sb:
return True
return cells(a) == cells(b)
class Check:
def __init__(self):
self.passed = 0
self.failed = 0
self.examples = []
def record(self, ok, what):
if ok:
self.passed += 1
else:
self.failed += 1
if len(self.examples) < EXAMPLES:
self.examples.append(what)
def json(self):
return {'passed': self.passed, 'failed': self.failed, 'examples': self.examples}
def sound_upgrades(oracle, horizon):
"""The oracle version's vanilla sound sets → this version's, through the vanilla blocks both have."""
out = {}
for name, hs in horizon.get('vanilla_sounds', {}).items():
ob = oracle['blocks'].get(name)
if ob:
os_ = ob['states'].get(ob['default']) or next(iter(ob['states'].values()))
out.setdefault(json.dumps(os_['sound'], sort_keys=True), []).append(hs)
return out
def compare_mod(mod, oracle, horizon, carry, upgrades, old_items):
checks = {c: Check() for c in CHECKS}
prefix = mod + ':'
templates = {prefix + b: t for b, t in carry.get('blocks', {}).items()}
vanilla_tags = {t: set(m) for t, m in oracle['tags']['block'].items() if t.startswith('minecraft:')}
for name, ob in oracle['blocks'].items():
if not name.startswith(prefix):
continue
hb = horizon['blocks'].get(name)
checks['blocks'].record(hb is not None, f'{name}: not in Horizon ({carry.get("blocks_unknown", {}).get(name[len(prefix):], "left out")})')
if hb is None:
continue
for k, os_ in ob['states'].items():
hs = hb['states'].get(k)
if hs is None:
checks['properties'].record(False, f'{name}[{k}]: no such state in Horizon')
continue
props = [('hardness', os_['hardness'], hs['hardness']),
('explosion_resistance', ob['explosion_resistance'], hs['explosion_resistance']),
('emission', os_['emission'], hs['emission']),
('requires_tool', os_['requires_tool'], hs['requires_tool']),
('friction', ob['friction'], hs['friction']),
('map_color', os_['map_color_id'], hs['map_color_id'])]
bad = [f'{p} {o} vs {h}' for p, o, h in props
if (abs(o - h) > 1e-6 if isinstance(o, float) else o != h)]
checks['properties'].record(not bad, f'{name}[{k}]: ' + '; '.join(bad))
upgraded = upgrades.get(json.dumps(os_['sound'], sort_keys=True), [])
checks['sounds'].record(os_['sound'] == hs['sound'] or hs['sound'] in upgraded,
f'{name}[{k}]: {os_["sound"]["break"]} vs {hs["sound"]["break"]}')
shapes_ok = same_shape(os_['outline'], hs['outline']) and same_shape(os_['collision'], hs['collision'])
checks['shapes'].record(shapes_ok, f'{name}[{k}]: outline {os_["outline"]} vs {hs["outline"]}')
for tool, odrops in os_.get('drops', {}).items():
hdrops = (hs.get('drops') or {}).get(tool)
checks['drops'].record(odrops == hdrops, f'{name}[{k}] {tool}: {odrops} vs {hdrops}')
# Placement: the mod block against its template, both in the oracle.
template = templates.get(name)
ot = oracle['blocks'].get(template) if template else None
if ot and 'placement' in ob:
for face, placed in ob['placement'].items():
theirs = ot.get('placement', {}).get(face)
checks['placement'].record(
placed is not None and theirs is not None and key(placed) == key(theirs),
f'{name} on {face}: {placed} vs {template} {theirs}')
for tag, members in vanilla_tags.items():
if name in members:
checks['tags'].record(name in horizon['block_tags'].get(tag, []), f'{name}: not in #{tag}')
for rid, orec in oracle['recipes'].items():
if not rid.startswith(prefix):
continue
hrec = horizon['recipes'].get(rid)
if hrec is None:
checks['recipes'].record(False, f'{rid} ({orec["type"]}): not loaded')
continue
# Vanilla items added after the oracle's version (pale oak in a log tag) are not the mod's.
newer = lambda i: i.startswith('minecraft:') and old_items and i not in old_items
oing = sorted(sorted(i) for i in orec['ingredients'] if i)
hing = sorted(sorted(x for x in i if not newer(x)) for i in hrec.get('ingredients', []) if i)
ok = (orec['result'], orec['count']) == (hrec.get('result'), hrec.get('count')) and oing == hing
why = f'{rid}: {orec["result"]} x{orec["count"]} vs {hrec.get("result")} x{hrec.get("count")}'
if oing != hing:
missing = [i for i in oing if i not in hing][:2]
why += f'; ingredients differ, e.g. oracle {missing}'
checks['recipes'].record(ok, why)
for iid, oi in oracle['items'].items():
if not iid.startswith(prefix):
continue
hi = horizon['items'].get(iid)
checks['items'].record(hi is not None and hi['max_stack_size'] == oi['max_stack_size'],
f'{iid}: {"missing" if hi is None else "stack " + str(hi["max_stack_size"])}')
result = {c: v.json() for c, v in checks.items()}
failed = sum(v.failed for v in checks.values())
total = sum(v.passed + v.failed for v in checks.values())
return {'mod': mod, 'passed': failed == 0 and total > 0, 'checks': result,
'blocks_with_differences': len(carry.get('differences', {}))}
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('--work', required=True)
parser.add_argument('--resources', default=str(ROOT / '.local/horizon-client/26.3'))
parser.add_argument('--jars', help='export the oracle facts from these jars first')
parser.add_argument('--deps')
parser.add_argument('mods', nargs='*')
args = parser.parse_args()
work = Path(args.work).resolve()
facts = work / 'oracle' / 'facts.json'
if args.jars:
facts.parent.mkdir(parents=True, exist_ok=True)
command = [sys.executable, str(ROOT / 'tools/neoforge_oracle.py'), 'export', '--jars', args.jars, '--out', str(facts)]
if args.deps:
command += ['--deps', args.deps]
subprocess.run(command, check=True)
if not facts.exists():
sys.exit(f'{facts}: no oracle facts (run with --jars, or tools/neoforge_oracle.py export)')
subprocess.run(['cargo', 'build', '--release', '-q', '-p', 'horizon-client', '--example', 'mod_port_probe'],
cwd=ROOT, check=True)
metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'],
cwd=ROOT, text=True))
probe = Path(metadata['target_directory']) / 'release/examples/mod_port_probe'
out = work / 'horizon.json'
subprocess.run([str(probe), args.resources, str(work), str(out), *args.mods], check=True)
oracle = json.loads(facts.read_text())
horizon = json.loads(out.read_text())
upgrades = sound_upgrades(oracle, horizon)
old_items = set(oracle.get('vanilla_items', []))
mods = args.mods or sorted(p.name for p in (work / 'mods').iterdir() if (p / 'carry').is_dir())
print(f'{"mod":<16} {"result":<7} ' + ' '.join(f'{c:>12}' for c in CHECKS))
for mod in mods:
carry_report = work / 'mods' / mod / 'carry-report.json'
carry = json.loads(carry_report.read_text()) if carry_report.exists() else {}
result = compare_mod(mod, oracle, horizon, carry, upgrades, old_items)
(work / 'mods' / mod / 'harness').mkdir(parents=True, exist_ok=True)
(work / 'mods' / mod / 'harness' / 'result.json').write_text(json.dumps(result, indent=1))
cols = []
for c in CHECKS:
r = result['checks'][c]
n = r['passed'] + r['failed']
cols.append(f'{r["passed"]}/{n}' if n else '-')
print(f'{mod:<16} {"pass" if result["passed"] else "FAIL":<7} ' + ' '.join(f'{c:>12}' for c in cols))
if __name__ == '__main__':
main()
+164
View File
@@ -0,0 +1,164 @@
#!/usr/bin/env python3
"""Dev-only NeoForge oracle for Horizon's mod-port harnesses (docs/modpacks.md).
neoforge_oracle.py export --jars DIR [--deps DIR] [--namespaces a,b] --out FACTS.json
installs a NeoForge server under .local/neoforge-oracle (once), builds the exporter mod in
mods/horizon/src/neoforge-oracle, starts the server headless with the given mod jars, and writes every
block, item, recipe and tag of the mods' namespaces (and the vanilla blocks, for templates) to
FACTS.json. The jars are the user's own; nothing here is shipped or committed.
"""
import argparse
import json
import os
import shutil
import subprocess
import sys
import time
import urllib.request
import zipfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
LOCAL = ROOT / '.local' / 'neoforge-oracle'
SOURCE = ROOT / 'mods' / 'horizon' / 'src' / 'neoforge-oracle'
# ATM10 8.x: Minecraft 1.21.1 on NeoForge 21.1.259 (docs/modpacks.md).
NEOFORGE = os.environ.get('HORIZON_NEOFORGE', '21.1.259')
MAVEN = 'https://maven.neoforged.net/releases/net/neoforged/neoforge'
def install():
"""The NeoForge server, installed once per version."""
home = LOCAL / NEOFORGE
args = home / f'libraries/net/neoforged/neoforge/{NEOFORGE}/unix_args.txt'
if args.exists():
return home
home.mkdir(parents=True, exist_ok=True)
installer = LOCAL / f'neoforge-{NEOFORGE}-installer.jar'
if not installer.exists():
url = f'{MAVEN}/{NEOFORGE}/neoforge-{NEOFORGE}-installer.jar'
with urllib.request.urlopen(url, timeout=120) as r:
installer.write_bytes(r.read())
with (LOCAL / f'install-{NEOFORGE}.log').open('w') as log:
subprocess.run(['java', '-jar', str(installer), '--installServer', str(home)],
cwd=LOCAL, stdout=log, stderr=subprocess.STDOUT, check=True)
return home
def classpath(home):
libs = home / 'libraries'
first = [libs / f'net/neoforged/neoforge/{NEOFORGE}/neoforge-{NEOFORGE}-server.jar',
libs / f'net/neoforged/neoforge/{NEOFORGE}/neoforge-{NEOFORGE}-universal.jar']
rest = sorted(p for p in libs.rglob('*.jar') if p not in first and 'slim' not in p.name
and 'extra' not in p.name and not p.name.endswith('-unpacked.jar'))
return os.pathsep.join(map(str, first + rest))
def build(home):
"""The exporter mod jar, rebuilt when its sources change."""
out = LOCAL / 'build'
jar = out / 'horizon-oracle.jar'
sources = sorted((SOURCE / 'java').rglob('*.java'))
resources = sorted(p for p in (SOURCE / 'resources').rglob('*') if p.is_file())
newest = max(p.stat().st_mtime for p in sources + resources)
if jar.exists() and jar.stat().st_mtime > newest:
return jar
classes = out / 'classes'
shutil.rmtree(classes, ignore_errors=True)
classes.mkdir(parents=True)
javac = subprocess.run(['javac', '--release', '21', '-proc:none', '-nowarn', '-cp', classpath(home),
'-d', str(classes), *map(str, sources)], capture_output=True, text=True)
if javac.returncode:
sys.exit(f'the exporter does not compile:\n{javac.stdout}{javac.stderr}')
with zipfile.ZipFile(jar, 'w', zipfile.ZIP_DEFLATED) as z:
for p in sorted(classes.rglob('*.class')):
z.write(p, p.relative_to(classes).as_posix())
for p in resources:
z.write(p, p.relative_to(SOURCE / 'resources').as_posix())
return jar
SERVER_PROPERTIES = """level-type=minecraft\\:flat
online-mode=false
server-port=0
spawn-protection=0
view-distance=2
simulation-distance=2
generate-structures=false
spawn-monsters=false
spawn-animals=false
"""
def instance(name, jars, extra=()):
"""A server folder with `jars` (and the exporter) in mods/."""
home = install()
run = LOCAL / 'runs' / name
shutil.rmtree(run, ignore_errors=True)
(run / 'mods').mkdir(parents=True)
(run / 'libraries').symlink_to(home / 'libraries')
(run / 'eula.txt').write_text('eula=true\n')
(run / 'server.properties').write_text(SERVER_PROPERTIES)
for jar in [*jars, *extra]:
(run / 'mods' / Path(jar).name).symlink_to(Path(jar).resolve())
return home, run
def start(home, run, props, log_name='server.log', timeout=900):
"""Runs the server to its end; returns (seconds, the log)."""
args = home / f'libraries/net/neoforged/neoforge/{NEOFORGE}/unix_args.txt'
command = ['java', '-Xmx4G', *[f'-D{k}={v}' for k, v in props.items()], f'@{args}', 'nogui']
began = time.monotonic()
with (run / log_name).open('w') as log:
subprocess.run(command, cwd=run, stdin=subprocess.DEVNULL, stdout=log,
stderr=subprocess.STDOUT, timeout=timeout)
return time.monotonic() - began, (run / log_name).read_text(errors='replace')
def mod_ids(jar):
"""The mod ids a jar declares (its neoforge.mods.toml)."""
import tomllib
with zipfile.ZipFile(jar) as z:
for name in ('META-INF/neoforge.mods.toml', 'META-INF/mods.toml'):
if name in z.namelist():
try:
toml = tomllib.loads(z.read(name).decode('utf-8', 'replace'))
except tomllib.TOMLDecodeError:
return []
return [m.get('modId', '') for m in toml.get('mods', [])]
return []
def export(args):
jars = sorted(Path(args.jars).glob('*.jar'))
deps = sorted(Path(args.deps).glob('*.jar')) if args.deps else []
namespaces = args.namespaces or ','.join(i for j in jars for i in mod_ids(j))
home = install()
exporter = build(home)
home, run = instance('export', jars, [*deps, exporter])
out = Path(args.out).resolve()
out.unlink(missing_ok=True)
seconds, log = start(home, run, {'horizon.oracle.out': out, 'horizon.oracle.namespaces': namespaces})
if not out.exists():
tail = '\n'.join(log.splitlines()[-40:])
sys.exit(f'the oracle wrote nothing; the end of {run / "server.log"}:\n{tail}')
facts = json.loads(out.read_text())
mods = sum(1 for b in facts['blocks'] if not b.startswith('minecraft:'))
print(f'{out}: {mods} mod blocks, {len(facts["items"])} items, {len(facts["recipes"])} recipes '
f'({namespaces}); server ran {seconds:.1f} s')
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
sub = parser.add_subparsers(dest='command', required=True)
e = sub.add_parser('export', help='write the facts of the mods in --jars')
e.add_argument('--jars', required=True, help='the mods to export')
e.add_argument('--deps', help='their required libraries (loaded, not exported)')
e.add_argument('--namespaces', help='comma-separated; default: the jars\' mod ids')
e.add_argument('--out', required=True)
args = parser.parse_args()
{'export': export}[args.command](args)
if __name__ == '__main__':
main()