Files
horizon-airdef/crates/horizon-port-pack/src/processors.rs
T
Ludovic Marie 8e095b88fa Carry: write Lithostitched's processor references in
lithostitched:reference runs the processors of other lists in turn.
Carry now writes their processors in its place, in order and references
in them too, from the mod's own processor lists. A reference to a tag or
to a list the mod lacks stays, and is dropped and counted as before.
ChoiceTheorem's Overhauled Village builds most of its lists this way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 04:17:42 +02:00

1116 lines
46 KiB
Rust

//! Structure processors the structure libraries write in Java, carried over
//! as data the game runs: the ones that give spawners their mob.
//!
//! - Moog's Structure Lib's and Repurposed Structures' spawner randomizing
//! processors pick a mob from a weighted list for each spawner. They become
//! `horizon:spawner_mobs` (`horizon_world::worldgen::structure::processor`),
//! with Repurposed's `rs_spawners` list written in.
//! - YUNG's Better Dungeons and Better Desert Temples turn a spawner or a
//! placeholder block into a spawner of a fixed mob. That is vanilla's `rule`
//! processor with an `append_static` block entity.
//! - Moog's vanilla loot swap only acts when its mod's "replace vanilla"
//! preset is on; one that ships off is a no-op and is taken out.
//! - Lithostitched's `reference` runs other processor lists in turn: their
//! processors are written in its place (Choice Theorem's Overhauled
//! Villages builds most of its lists so).
//!
//! What is not here is taken out of its list later and counted
//! (`worldgen::upgrade`). The entity NBT is written in 26.3's shape (an
//! `equipment` map, not 1.21.1's `HandItems` and `ArmorItems`).
use serde_json::{Value, json};
use std::{
collections::{BTreeMap, BTreeSet},
io::{Read, Seek},
};
/// What the processors read from the mod's own data files.
#[derive(Debug, Default)]
pub struct ProcessorData {
/// Repurposed Structures' `rs_spawners/<path>.json` by id: each mob and
/// its weight, without the weight-0 entries its loader removes.
pub spawners: BTreeMap<String, Vec<(String, f64)>>,
/// Moog's "replace vanilla" presets that are on by default:
/// `(mod id, vanilla_key)`.
pub replace_vanilla: BTreeSet<(String, String)>,
/// The mod's processor lists by id, for Lithostitched's references.
pub lists: BTreeMap<String, Value>,
}
impl ProcessorData {
pub fn read<R: Read + Seek>(zip: &mut zip::ZipArchive<R>) -> Self {
let mut out = Self::default();
for i in 0..zip.len() {
let Ok(mut entry) = zip.by_index(i) else {
continue;
};
let name = entry.name().to_owned();
let Some((ns, rest)) = name
.strip_prefix("data/")
.and_then(|r| r.strip_suffix(".json"))
.and_then(|r| r.split_once('/'))
else {
continue;
};
let spawners = rest.strip_prefix("rs_spawners/");
let list = rest.strip_prefix("worldgen/processor_list/");
if spawners.is_none() && list.is_none() && rest != "moogs_structures/replace_vanilla" {
continue;
}
let mut text = String::new();
let Ok(v) = entry
.read_to_string(&mut text)
.map_err(|_| ())
.and_then(|_| serde_json::from_str::<Value>(&text).map_err(|_| ()))
else {
continue;
};
if let Some(path) = list {
out.lists.insert(format!("{ns}:{path}"), v);
continue;
}
if let Some(path) = spawners {
let mobs = v["mobs"]
.as_array()
.into_iter()
.flatten()
.filter_map(|m| Some((m["name"].as_str()?.to_owned(), m["weight"].as_f64()?)))
.filter(|(_, w)| *w != 0.)
.collect();
out.spawners.insert(format!("{ns}:{path}"), mobs);
continue;
}
for preset in v["presets"].as_array().into_iter().flatten() {
if preset["default_enabled"].as_bool() != Some(true) {
continue;
}
for r in preset["replacements"].as_array().into_iter().flatten() {
if let Some(key) = r["vanilla_key"].as_str() {
out.replace_vanilla.insert((ns.to_owned(), key.to_owned()));
}
}
}
}
out
}
}
/// Calls `f` on each processor list in a worldgen file: a `processor_list`
/// file, and the inline lists of a template pool's elements (list elements
/// included). Whether any call changed something.
pub fn each_list(kind: &str, v: &mut Value, f: &mut dyn FnMut(&mut Value) -> bool) -> bool {
match kind {
"processor_list" => f(v),
"template_pool" => {
let mut changed = false;
for e in v["elements"].as_array_mut().into_iter().flatten() {
let element = &mut e["element"];
if let Some(list) = element.get_mut("elements").and_then(Value::as_array_mut) {
for inner in list {
if let Some(p) = inner.get_mut("processors") {
changed |= f(p);
}
}
} else if let Some(p) = element.get_mut("processors") {
changed |= f(p);
}
}
changed
}
_ => false,
}
}
/// The processors of a list: `{"processors": [...]}` or a bare list (an id
/// names another file and is left alone).
pub fn processors(list: &mut Value) -> Option<&mut Vec<Value>> {
match list {
Value::Array(a) => Some(a),
Value::Object(o) => o.get_mut("processors").and_then(Value::as_array_mut),
_ => None,
}
}
/// Rewrites the processors of a worldgen file that have a data form; each is
/// counted by type in `carried`. Whether anything changed.
pub fn carry(
kind: &str,
v: &mut Value,
data: &ProcessorData,
carried: &mut BTreeMap<String, usize>,
) -> bool {
each_list(kind, v, &mut |list| {
let Some(list) = processors(list) else {
return false;
};
let mut changed = false;
let expanded = references(std::mem::take(list), data, carried, 0);
changed |= expanded.1;
let mut out = Vec::with_capacity(expanded.0.len());
for p in expanded.0 {
let kind = p["processor_type"].as_str().unwrap_or_default().to_owned();
match as_data(&kind, &p, data) {
Some(replacement) => {
*carried.entry(kind).or_default() += 1;
out.extend(replacement);
changed = true;
}
None => out.push(p),
}
}
*list = out;
changed
})
}
/// Repurposed Structures' rule tests and the game's types that run them
/// (`horizon_world::worldgen`'s `rule_test` and `structure::processor`).
const RULE_TESTS: [(&str, &str); 3] = [
(
"repurposed_structures:matter_phase_rule_test",
"horizon:matter_phase",
),
(
"repurposed_structures:piece_origin_axis_aligned_linear_pos_rule_test",
"horizon:piece_origin_axis_aligned_linear_pos",
),
(
"repurposed_structures:y_value_pos_rule_test",
"horizon:y_value",
),
];
/// Renames the rule tests written in Java that the game runs under its own
/// type ([`RULE_TESTS`]) in a worldgen file's processor lists; each is
/// counted by type in `carried`. Their fields are the mod's. Whether
/// anything changed. Without this the game, like vanilla, refuses the list
/// and every pool naming it: Repurposed's mineshafts and Nether monument.
pub fn rule_tests(kind: &str, v: &mut Value, carried: &mut BTreeMap<String, usize>) -> bool {
fn rename(v: &mut Value, carried: &mut BTreeMap<String, usize>) -> bool {
match v {
Value::Object(o) => {
let mut changed = false;
if let Some(Value::String(t)) = o.get_mut("predicate_type")
&& let Some((old, new)) = RULE_TESTS.iter().find(|(old, _)| t == old)
{
*carried.entry((*old).to_owned()).or_default() += 1;
*t = (*new).to_owned();
changed = true;
}
o.values_mut().fold(changed, |c, x| rename(x, carried) | c)
}
Value::Array(a) => a.iter_mut().fold(false, |c, x| rename(x, carried) | c),
_ => false,
}
}
each_list(kind, v, &mut |list| rename(list, carried))
}
/// Lithostitched's references in a list, each replaced by the processors of
/// the lists it names, in order (references in them too, to a depth of 8).
/// One naming a tag or a list the mod does not have stays, to be dropped.
/// Whether any was replaced.
fn references(
list: Vec<Value>,
data: &ProcessorData,
carried: &mut BTreeMap<String, usize>,
depth: usize,
) -> (Vec<Value>, bool) {
let mut out = Vec::with_capacity(list.len());
let mut changed = false;
for p in list {
let named = (p["processor_type"] == "lithostitched:reference" && depth < 8)
.then(|| referenced(&p["processor_lists"], data))
.flatten();
match named {
Some(inner) => {
*carried.entry("lithostitched:reference".into()).or_default() += 1;
out.extend(references(inner, data, carried, depth + 1).0);
changed = true;
}
None => out.push(p),
}
}
(out, changed)
}
/// The processors of the lists a reference names: an id, an inline list, or
/// a list of either.
fn referenced(lists: &Value, data: &ProcessorData) -> Option<Vec<Value>> {
let one = |l: &Value| -> Option<Vec<Value>> {
let mut list = match l {
Value::String(id) if !id.starts_with('#') => {
let id = if id.contains(':') {
id.clone()
} else {
format!("minecraft:{id}")
};
data.lists.get(&id)?.clone()
}
Value::Object(_) | Value::Array(_) => l.clone(),
_ => return None,
};
processors(&mut list).map(std::mem::take)
};
match lists {
// A bare list of processors is one inline list; else a list of lists.
Value::Array(a) if a.iter().any(|x| x.get("processor_type").is_some()) => Some(a.clone()),
Value::Array(a) => Some(a.iter().map(one).collect::<Option<Vec<_>>>()?.concat()),
other => one(other),
}
}
/// The data form of a Java processor: the processors to put in its place
/// (none for a no-op), or `None` when it has none.
fn as_data(kind: &str, p: &Value, data: &ProcessorData) -> Option<Vec<Value>> {
let (ns, _) = kind.split_once(':')?;
Some(match kind {
"moogs_structures:spawner_randomizing_processor" => {
let mobs = p["weighted_entities"]
.as_array()?
.iter()
.map(|e| {
// The entry's NBT, then its id over it.
let mut entity = match &e["nbt"] {
Value::Object(o) => o.clone(),
_ => serde_json::Map::new(),
};
entity.insert("id".into(), e["entity"].as_str()?.into());
Some(json!({"entity": entity, "weight": e["weight"].as_f64()?}))
})
.collect::<Option<Vec<_>>>()?;
vec![spawner_mobs(p, mobs, None)]
}
"repurposed_structures:spawner_randomizing_processor" => {
// A list the mod does not ship falls back to a vanilla dungeon
// mob in the mod; left out here.
let list = data
.spawners
.get(p["rs_spawner_resourcelocation"].as_str()?)?;
let mobs = list
.iter()
.map(|(id, weight)| json!({"entity": {"id": id}, "weight": weight}))
.collect();
vec![spawner_mobs(p, mobs, repurposed_light(p)?)]
}
"moogs_structures:vanilla_loot_swap_processor" => {
let key = (
p["modid"].as_str()?.to_owned(),
p["vanilla_key"].as_str()?.to_owned(),
);
if data.replace_vanilla.contains(&key) {
return None;
}
Vec::new()
}
"moogs_structures:trial_spawner_randomizing_processor" => {
// The configs by id: 26.3 reads them from `trial_spawner/`, the
// folder Moog's loads them from.
let mut nbt = json!({"normal_config": p["normal_config"].as_str()?});
if let Some(ominous) = p["ominous_config"].as_str() {
nbt["ominous_config"] = ominous.into();
}
// The state the template was saved with goes.
let saved = [
"server_data",
"shared_data",
"spawn_data",
"cooldown_end_at_tick",
"next_mob_spawns_at",
];
vec![same_state_rule(&TRIAL_SPAWNER_STATES, &saved, |_| {
nbt.clone()
})]
}
"moogs_structures:vault_randomizing_processor" => {
let normal = p["loot_table"].as_str()?;
let config = |ominous: bool| {
let (table, key) = match ominous {
true => (
p["ominous_loot_table"].as_str().unwrap_or(normal),
p["ominous_key_item"]
.as_str()
.unwrap_or("minecraft:ominous_trial_key"),
),
false => (
normal,
p["key_item"].as_str().unwrap_or("minecraft:trial_key"),
),
};
// Merged into the vault's config, as the mod puts both in.
json!({"config": {"loot_table": table, "key_item": {"id": key, "count": 1}}})
};
vec![same_state_rule(
&VAULT_STATES,
&["server_data", "shared_data"],
|s| config(s.get("ominous") == Some(&"true")),
)]
}
_ if ns == "betterdeserttemples"
&& let Some(processor) = desert_temple(kind) =>
{
vec![processor]
}
"lithostitched:block_swap" => {
// The same map; the game's swap keeps the properties both have.
p["blocks"].as_object()?;
vec![json!({"processor_type": "horizon:block_swap", "blocks": p["blocks"]})]
}
"betterdungeons:small_dungeon_chest_processor" => {
// Its config's defaults: one chest at least, two at most, a
// fifth of the others kept until then; ChestBlock covers both.
vec![json!({"processor_type": "horizon:count_cap",
"blocks": ["minecraft:chest", "minecraft:trapped_chest"],
"min": 1, "max": 2, "chance": 0.2f32 as f64,
"replacement": block("minecraft:cave_air")})]
}
_ if ns == "betterdungeons" || ns == "betterdeserttemples" => vec![yung_spawner(kind, p)?],
_ => return None,
})
}
/// A block's states, as (block, [(property, values)]).
type States = (
&'static str,
&'static [(&'static str, &'static [&'static str])],
);
const TRIAL_SPAWNER_STATES: States = (
"minecraft:trial_spawner",
&[
(
"trial_spawner_state",
&[
"inactive",
"waiting_for_players",
"active",
"waiting_for_reward_ejection",
"ejecting_reward",
"cooldown",
],
),
("ominous", &["false", "true"]),
],
);
const VAULT_STATES: States = (
"minecraft:vault",
&[
("facing", &["north", "south", "west", "east"]),
(
"vault_state",
&["inactive", "active", "unlocking", "ejecting"],
),
("ominous", &["false", "true"]),
],
);
/// Moog's trial spawner and vault processors keep the block, take keys out
/// of its block entity and set others. As vanilla's `rule`: one rule per
/// state of the block, matching it and writing it back, with the game's
/// `horizon:edit` modifier taking out `remove` and merging `nbt(state)` in.
fn same_state_rule(
states: &States,
remove: &[&str],
nbt: impl Fn(&BTreeMap<&str, &str>) -> Value,
) -> Value {
let (block, properties) = states;
let mut all = vec![BTreeMap::new()];
for (key, values) in properties.iter() {
all = all
.into_iter()
.flat_map(|s| {
values.iter().map(move |v| {
let mut s = s.clone();
s.insert(*key, *v);
s
})
})
.collect();
}
let rules: Vec<Value> = all
.iter()
.map(|s| {
let state = json!({"id": block, "properties": s});
json!({
"input_predicate": {"predicate_type": "minecraft:blockstate_match", "block_state": state},
"location_predicate": {"predicate_type": "minecraft:always_true"},
"output_state": state,
"block_entity_modifier": {"type": "horizon:edit", "remove": remove, "data": nbt(s)},
})
})
.collect();
json!({"processor_type": "minecraft:rule", "rules": rules})
}
/// An entity's NBT in 26.3's shape, in place: 1.21.1's `ArmorItems` (feet
/// to head), `HandItems` (main hand, off hand) and their drop chances become
/// `equipment` and `drop_chances` by slot; empty items and the default drop
/// chance (0.085) are left out, as 26.3 saves them. Whether it changed.
pub fn upgrade_entity(entity: &mut Value) -> bool {
let Some(o) = entity.as_object_mut() else {
return false;
};
const ARMOR: [&str; 4] = ["feet", "legs", "chest", "head"];
const HANDS: [&str; 2] = ["mainhand", "offhand"];
let mut changed = false;
for (items, chances, slots) in [
("ArmorItems", "ArmorDropChances", &ARMOR[..]),
("HandItems", "HandDropChances", &HANDS[..]),
] {
if let Some(Value::Array(list)) = o.remove(items) {
changed = true;
for (slot, item) in slots.iter().zip(list) {
if item.get("id").is_some() {
let equipment = o.entry("equipment").or_insert_with(|| json!({}));
equipment[*slot] = item;
}
}
}
if let Some(Value::Array(list)) = o.remove(chances) {
changed = true;
for (slot, chance) in slots.iter().zip(list) {
if chance.as_f64().is_some_and(|c| (c - 0.085).abs() > 1e-6) {
let drop = o.entry("drop_chances").or_insert_with(|| json!({}));
drop[*slot] = chance;
}
}
}
}
changed
}
/// A 1.21.1 trial spawner config (`trial_spawner/`) in 26.3's shape, in
/// place: its mobs' NBT ([`upgrade_entity`]). Whether it changed.
pub fn upgrade_trial_spawner(config: &mut Value) -> bool {
let mut changed = false;
for p in config["spawn_potentials"]
.as_array_mut()
.into_iter()
.flatten()
{
changed |= upgrade_entity(&mut p["data"]["entity"]);
}
changed
}
/// A block state as 26.3 writes it.
fn block(id: &str) -> Value {
json!({"id": id})
}
/// Better Desert Temples' placeholder blocks (glass, concrete and wool in
/// the templates) as the mod's processors turn them: the fixed ones as
/// vanilla's `rule`, the random ones as the game's `horizon:pick`, with the
/// mod's bounds on one `nextFloat` (YUNG's BlockStateRandomizer adds its
/// chances up in floats). The ones that read or change the world around
/// (pillars, Diorite's waterlogging) stay dropped.
fn desert_temple(kind: &str) -> Option<Value> {
let swap = |from: &str, to: &str| {
json!({"processor_type": "minecraft:rule", "rules": [{
"input_predicate": {"predicate_type": "minecraft:block_match", "block": from},
"location_predicate": {"predicate_type": "minecraft:always_true"},
"output_state": block(to)}]})
};
let pick = |from: &str, picks: Vec<Value>, default: Value| json!({"processor_type": "horizon:pick", "block": from, "picks": picks, "default": default});
// YUNG's BlockStateRandomizer: an entry wins below the sum of the
// chances so far, summed as floats.
let randomizer = |entries: &[(f32, &str)]| {
let mut bottom = 0f32;
entries
.iter()
.map(|(chance, id)| {
bottom += chance;
json!({"below": bottom as f64, "state": block(id)})
})
.collect::<Vec<_>>()
};
let at = |below: f32, id: &str| json!({"below": below as f64, "state": block(id)});
// Sandstone stairs, upside down, facing Plane.HORIZONTAL.getRandomDirection.
let stairs = || {
let states: Vec<Value> = ["north", "east", "south", "west"]
.iter()
.map(|facing| {
json!({"id": "minecraft:sandstone_stairs", "properties": {
"facing": facing, "half": "top", "shape": "straight", "waterlogged": "false"}})
})
.collect();
json!({"states": states})
};
Some(match kind.strip_prefix("betterdeserttemples:")? {
"blue_concrete_processor" => swap("minecraft:blue_concrete", "minecraft:blue_terracotta"),
"yellow_concrete_processor" => {
swap("minecraft:yellow_concrete", "minecraft:yellow_terracotta")
}
"red_wool_processor" => swap("minecraft:red_wool", "minecraft:tnt"),
"white_stained_glass_processor" => pick(
"minecraft:white_stained_glass",
randomizer(&[(0.4, "minecraft:cobweb")]),
json!({"state": block("minecraft:air")}),
),
"polished_diorite_processor" => pick(
"minecraft:polished_diorite",
randomizer(&[(0.6, "minecraft:cut_sandstone")]),
json!({"state": block("minecraft:sandstone")}),
),
"orange_stained_glass_processor" => {
let mut sus = at(0.01, "minecraft:suspicious_sand");
sus["nbt"] = json!({"LootTable": "minecraft:archaeology/desert_pyramid"});
let mut stairs = stairs();
stairs["below"] = (0.45f32 as f64).into();
pick(
"minecraft:orange_stained_glass",
vec![
sus,
at(0.1, "minecraft:sand"),
at(0.2, "minecraft:sandstone"),
stairs,
],
json!({"state": block("minecraft:cut_sandstone")}),
)
}
"red_stained_glass_processor" => pick(
"minecraft:red_stained_glass",
vec![at(0.3, "minecraft:sand"), at(0.75, "minecraft:sandstone")],
stairs(),
),
"pot_processor" => {
// Four sherds, one float each; the pot back to its default state.
let values: Vec<Value> = [
(0.05f32, "archer"),
(0.1, "miner"),
(0.15, "prize"),
(0.2, "skull"),
]
.iter()
.map(|(below, sherd)| {
json!({"below": *below as f64, "value": format!("minecraft:{sherd}_pottery_sherd")})
})
.collect();
pick(
"minecraft:decorated_pot",
Vec::new(),
json!({"state": block("minecraft:decorated_pot"), "lists": [{
"key": "sherds", "count": 4, "values": values, "default": "minecraft:brick"}]}),
)
}
_ => return None,
})
}
/// [`as_data`]'s `horizon:spawner_mobs` for the mods' spawner randomizing
/// processors, whose fields have the same names and defaults.
fn spawner_mobs(p: &Value, mobs: Vec<Value>, rules: Option<Value>) -> Value {
let mut out = json!({"processor_type": "horizon:spawner_mobs", "mobs": mobs});
for key in [
"delay",
"min_spawn_delay",
"max_spawn_delay",
"spawn_count",
"max_nearby_entities",
"required_player_range",
"spawn_range",
] {
if let Some(n) = p.get(key).filter(|n| n.is_i64()) {
out[key] = n.clone();
}
}
if let Some(r) = rules {
out["custom_spawn_rules"] = r;
}
if let Some(b) = p.get("spawner_replacement_block") {
out["replacement"] = b.clone();
}
out
}
/// Repurposed Structures' `custom_spawn_rules` from its light level ranges:
/// `Some(None)` without either, `None` when one does not read. The sky
/// light's upper bound is written as `max_exclusive`, as the mod does.
fn repurposed_light(p: &Value) -> Option<Option<Value>> {
let range = |key: &str| -> Option<Option<(i64, i64)>> {
match p.get(key) {
None => Some(None),
Some(Value::Number(n)) => n.as_i64().map(|n| Some((n, n))),
Some(v) => Some(Some((
v["min_inclusive"].as_i64()?,
v["max_inclusive"].as_i64()?,
))),
}
};
let (block, sky) = (
range("valid_block_light_level")?,
range("valid_sky_light_level")?,
);
if block.is_none() && sky.is_none() {
return Some(None);
}
let mut rules = json!({});
if let Some((min, max)) = block {
rules["block_light_limit"] = json!({"min_inclusive": min, "max_inclusive": max});
}
if let Some((min, max)) = sky {
rules["sky_light_limit"] = json!({"min_inclusive": min, "max_exclusive": max});
}
Some(Some(rules))
}
/// An item as 26.3 saves it.
fn item(id: &str) -> Value {
json!({"id": format!("minecraft:{id}"), "count": 1})
}
/// YUNG's processors that make a spawner of a fixed mob (YUNG's API's
/// `MobSpawnerData`), as vanilla's `rule`: every `input` block becomes a
/// spawner with that NBT.
fn yung_spawner(kind: &str, p: &Value) -> Option<Value> {
const SPAWNER: &str = "minecraft:spawner";
// (input block, mob, fields other than the defaults, whether the mob is
// also the only potential, its equipment)
let (input, mob, fields, potential, equipment): (_, _, &[(&str, i64)], _, _) = match kind {
"betterdungeons:mob_spawner_processor" => {
(SPAWNER, p["spawner_mob"].as_str()?, &[], true, None)
}
"betterdungeons:zombie_mob_spawner_processor" => (
SPAWNER,
"minecraft:zombie",
&[("MaxNearbyEntities", 8)],
true,
None,
),
"betterdungeons:skeleton_mob_spawner_processor" => (
SPAWNER,
"minecraft:skeleton",
&[
("RequiredPlayerRange", 18),
("MaxNearbyEntities", 8),
("MaxSpawnDelay", 650),
],
true,
None,
),
"betterdungeons:small_nether_dungeon_mob_spawner_processor" => {
let mob = p["spawner_mob"].as_str()?;
// Wither skeletons get netherite armor that never drops and a
// stone sword (with the mod's default config, they and blazes
// keep their loot).
let armor = (mob == "minecraft:wither_skeleton").then(|| {
json!({
"equipment": {
"feet": item("netherite_boots"),
"legs": item("netherite_leggings"),
"chest": item("netherite_chestplate"),
"head": item("netherite_helmet"),
"mainhand": item("stone_sword"),
},
"drop_chances": {"feet": 0.0, "legs": 0.0, "chest": 0.0, "head": 0.0},
})
});
(SPAWNER, mob, &[], true, armor)
}
"betterdungeons:zombie_tombstone_spawner_processor" => (
"minecraft:black_stained_glass",
"minecraft:skeleton",
&[],
false,
Some(json!({"equipment": {"mainhand": item("iron_sword")}})),
),
"betterdeserttemples:gravel_processor" => (
"minecraft:gravel",
"minecraft:husk",
&[("MaxNearbyEntities", 8), ("RequiredPlayerRange", 24)],
false,
None,
),
// An empty hand list in 1.21.1: the skeletons spawn without a bow,
// since a spawn entry with more than its id is not finalized.
"betterdeserttemples:bone_block_processor" => (
"minecraft:bone_block",
"minecraft:skeleton",
&[("RequiredPlayerRange", 32)],
false,
Some(json!({"equipment": {}})),
),
"betterdeserttemples:infested_cracked_stone_bricks_processor" => (
"minecraft:infested_cracked_stone_bricks",
"minecraft:silverfish",
&[("RequiredPlayerRange", 24)],
false,
None,
),
// 1.21.1 ignores the items' old `Count` and `tag` (the shield's
// pattern, the boots' dye): plain items.
"betterdeserttemples:yellow_wool_processor" => (
"minecraft:yellow_wool",
"minecraft:husk",
&[("MaxNearbyEntities", 8), ("RequiredPlayerRange", 24)],
false,
Some(json!({"equipment": {
"mainhand": item("golden_sword"),
"offhand": item("shield"),
"feet": item("leather_boots"),
"legs": item("leather_leggings"),
"chest": item("golden_chestplate"),
"head": item("chainmail_helmet"),
}})),
),
_ => return None,
};
let mut entity = json!({"id": mob});
if let Some(Value::Object(extra)) = equipment {
entity.as_object_mut()?.extend(extra);
}
let potentials = match potential {
true => json!([{"data": {"entity": entity.clone()}, "weight": 1}]),
false => json!([]),
};
let mut nbt = json!({
"Delay": 20, "MinSpawnDelay": 200, "MaxSpawnDelay": 800, "SpawnCount": 4,
"MaxNearbyEntities": 6, "RequiredPlayerRange": 16, "SpawnRange": 4,
"SpawnData": {"entity": entity},
"SpawnPotentials": potentials,
});
for (key, value) in fields {
nbt[*key] = (*value).into();
}
Some(json!({
"processor_type": "minecraft:rule",
"rules": [{
"input_predicate": {"predicate_type": "minecraft:block_match", "block": input},
"location_predicate": {"predicate_type": "minecraft:always_true"},
"output_state": {"Name": SPAWNER},
"block_entity_modifier": {"type": "minecraft:append_static", "data": nbt},
}],
}))
}
#[cfg(test)]
mod tests {
use super::*;
fn data() -> ProcessorData {
let mut d = ProcessorData::default();
d.spawners.insert(
"repurposed_structures:dungeons/badlands".into(),
vec![
("minecraft:husk".into(), 10.),
("minecraft:cave_spider".into(), 3.),
],
);
d.replace_vanilla.insert(("mvs".into(), "village".into()));
d
}
#[test]
fn spawner_processors_become_data() {
let mut list = json!({"processors": [
{"processor_type": "moogs_structures:spawner_randomizing_processor",
"weighted_entities": [{"entity": "minecraft:blaze", "weight": 2, "nbt": {"Health": 5}}],
"spawn_count": 2, "spawner_replacement_block": {"Name": "minecraft:stone"}},
{"processor_type": "repurposed_structures:spawner_randomizing_processor",
"rs_spawner_resourcelocation": "repurposed_structures:dungeons/badlands",
"valid_block_light_level": {"min_inclusive": 0, "max_inclusive": 7}},
{"processor_type": "repurposed_structures:spawner_randomizing_processor",
"rs_spawner_resourcelocation": "repurposed_structures:missing"},
{"processor_type": "minecraft:gravity", "heightmap": "WORLD_SURFACE_WG", "offset": 0},
{"processor_type": "moogs_structures:vanilla_loot_swap_processor",
"modid": "mns", "vanilla_key": "fortress", "loot_table_mapping": {}},
{"processor_type": "moogs_structures:vanilla_loot_swap_processor",
"modid": "mvs", "vanilla_key": "village", "loot_table_mapping": {}}]});
let mut carried = BTreeMap::new();
assert!(carry("processor_list", &mut list, &data(), &mut carried));
let p = list["processors"].as_array().unwrap();
assert_eq!(p.len(), 5, "{p:#?}");
assert_eq!(
p[0],
json!({"processor_type": "horizon:spawner_mobs",
"mobs": [{"entity": {"Health": 5, "id": "minecraft:blaze"}, "weight": 2.0}],
"spawn_count": 2, "replacement": {"Name": "minecraft:stone"}})
);
assert_eq!(
p[1],
json!({"processor_type": "horizon:spawner_mobs",
"mobs": [{"entity": {"id": "minecraft:husk"}, "weight": 10.0},
{"entity": {"id": "minecraft:cave_spider"}, "weight": 3.0}],
"custom_spawn_rules": {"block_light_limit": {"min_inclusive": 0, "max_inclusive": 7}}})
);
// Unknown list, vanilla processor, and a swap whose preset is on: kept
// (the Java ones are dropped later); the swap that is off is gone.
assert_eq!(
p[2]["rs_spawner_resourcelocation"],
"repurposed_structures:missing"
);
assert_eq!(p[3]["processor_type"], "minecraft:gravity");
assert_eq!(p[4]["modid"], "mvs");
assert_eq!(
carried,
BTreeMap::from([
("moogs_structures:spawner_randomizing_processor".into(), 1),
("moogs_structures:vanilla_loot_swap_processor".into(), 1),
(
"repurposed_structures:spawner_randomizing_processor".into(),
1
),
])
);
}
#[test]
fn yungs_spawners_become_rules() {
let mut pool = json!({"elements": [{"element": {"processors": {"processors": [
{"processor_type": "betterdungeons:skeleton_mob_spawner_processor"},
{"processor_type": "betterdeserttemples:bone_block_processor"},
{"processor_type": "betterdungeons:small_nether_dungeon_mob_spawner_processor",
"spawner_mob": "minecraft:wither_skeleton"},
{"processor_type": "betterdungeons:candle_processor"}]}}}]});
let mut carried = BTreeMap::new();
assert!(carry("template_pool", &mut pool, &data(), &mut carried));
let p = &pool["elements"][0]["element"]["processors"]["processors"];
let rule = |i: usize| &p[i]["rules"][0];
assert_eq!(rule(0)["input_predicate"]["block"], "minecraft:spawner");
assert_eq!(
rule(0)["output_state"],
json!({"Name": "minecraft:spawner"})
);
let nbt = &rule(0)["block_entity_modifier"]["data"];
assert_eq!(
(
nbt["RequiredPlayerRange"].clone(),
nbt["MaxNearbyEntities"].clone()
),
(json!(18), json!(8))
);
assert_eq!(nbt["MaxSpawnDelay"], 650);
assert_eq!(
nbt["SpawnPotentials"],
json!([{"data": {"entity": {"id": "minecraft:skeleton"}}, "weight": 1}])
);
// Bone blocks become bowless skeleton spawners with no potentials.
let bone = &rule(1)["block_entity_modifier"]["data"];
assert_eq!(rule(1)["input_predicate"]["block"], "minecraft:bone_block");
assert_eq!(
bone["SpawnData"],
json!({"entity": {"id": "minecraft:skeleton", "equipment": {}}})
);
assert_eq!(bone["SpawnPotentials"], json!([]));
let wither = &rule(2)["block_entity_modifier"]["data"]["SpawnData"]["entity"];
assert_eq!(
wither["equipment"]["head"]["id"],
"minecraft:netherite_helmet"
);
assert_eq!(wither["drop_chances"]["feet"], 0.0);
assert_eq!(p[3]["processor_type"], "betterdungeons:candle_processor");
assert_eq!(carried.values().sum::<usize>(), 3);
}
#[test]
fn trial_spawners_and_vaults_drop_their_saved_state() {
let mut list = json!({"processors": [
{"processor_type": "moogs_structures:trial_spawner_randomizing_processor",
"normal_config": "mns:arena/normal", "ominous_config": "mns:arena/ominous"},
{"processor_type": "moogs_structures:vault_randomizing_processor",
"loot_table": "mns:vaults/main", "ominous_loot_table": "mns:vaults/ominous"}]});
let mut carried = BTreeMap::new();
assert!(carry("processor_list", &mut list, &data(), &mut carried));
let p = list["processors"].as_array().unwrap();
let rules = |i: usize| p[i]["rules"].as_array().unwrap().clone();
// Every state of the block: 6 x 2 trial spawners, 4 x 4 x 2 vaults.
assert_eq!((rules(0).len(), rules(1).len()), (12, 32));
let spawner = &rules(0)[0];
assert_eq!(
spawner["input_predicate"]["block_state"],
spawner["output_state"]
);
assert_eq!(
spawner["block_entity_modifier"],
json!({"type": "horizon:edit",
"remove": ["server_data", "shared_data", "spawn_data",
"cooldown_end_at_tick", "next_mob_spawns_at"],
"data": {"normal_config": "mns:arena/normal",
"ominous_config": "mns:arena/ominous"}})
);
for rule in rules(1) {
let ominous = rule["output_state"]["properties"]["ominous"] == "true";
let (table, key) = match ominous {
true => ("mns:vaults/ominous", "minecraft:ominous_trial_key"),
false => ("mns:vaults/main", "minecraft:trial_key"),
};
assert_eq!(
rule["block_entity_modifier"],
json!({"type": "horizon:edit", "remove": ["server_data", "shared_data"],
"data": {"config": {"loot_table": table,
"key_item": {"id": key, "count": 1}}}})
);
}
assert_eq!(carried.len(), 2);
}
#[test]
fn trial_spawner_mobs_take_26_3s_equipment() {
let mut config = json!({"spawn_potentials": [{"weight": 1, "data": {"entity": {
"id": "minecraft:skeleton",
"ArmorItems": [{}, {}, {}, {"id": "minecraft:iron_helmet", "count": 1}],
"ArmorDropChances": [0.085, 0.085, 0.085, 0.0],
"HandItems": [{"id": "minecraft:bow", "count": 1}, {}],
"HandDropChances": [0.085, 0.085]}}}]});
assert!(upgrade_trial_spawner(&mut config));
assert_eq!(
config["spawn_potentials"][0]["data"]["entity"],
json!({"id": "minecraft:skeleton",
"equipment": {"head": {"id": "minecraft:iron_helmet", "count": 1},
"mainhand": {"id": "minecraft:bow", "count": 1}},
"drop_chances": {"head": 0.0}})
);
assert!(!upgrade_trial_spawner(&mut config));
}
#[test]
fn desert_temple_placeholders_become_rules_and_picks() {
let mut list = json!({"processors": [
{"processor_type": "betterdeserttemples:red_wool_processor"},
{"processor_type": "betterdeserttemples:white_stained_glass_processor"},
{"processor_type": "betterdeserttemples:orange_stained_glass_processor"},
{"processor_type": "betterdeserttemples:pot_processor"},
{"processor_type": "betterdeserttemples:quartz_pillar_processor"}]});
let mut carried = BTreeMap::new();
assert!(carry("processor_list", &mut list, &data(), &mut carried));
let p = list["processors"].as_array().unwrap();
assert_eq!(
p[0]["rules"][0]["output_state"],
json!({"id": "minecraft:tnt"})
);
assert_eq!(p[1]["processor_type"], "horizon:pick");
assert_eq!(p[1]["picks"][0]["below"], 0.4f32 as f64);
assert_eq!(p[1]["default"]["state"]["id"], "minecraft:air");
let glass = &p[2]["picks"];
assert_eq!(glass[0]["state"]["id"], "minecraft:suspicious_sand");
assert_eq!(
glass[0]["nbt"]["LootTable"],
"minecraft:archaeology/desert_pyramid"
);
assert_eq!(glass[3]["states"].as_array().unwrap().len(), 4);
assert_eq!(glass[3]["below"], 0.45f32 as f64);
assert_eq!(p[3]["default"]["lists"][0]["count"], 4);
// The pillar reads the world below: still Java, still dropped later.
assert_eq!(
p[4]["processor_type"],
"betterdeserttemples:quartz_pillar_processor"
);
assert_eq!(carried.values().sum::<usize>(), 4);
}
#[test]
fn small_dungeon_chests_are_capped() {
let mut list = json!({"processors": [
{"processor_type": "betterdungeons:small_dungeon_chest_processor"}]});
let mut carried = BTreeMap::new();
assert!(carry("processor_list", &mut list, &data(), &mut carried));
let cap = &list["processors"][0];
assert_eq!(cap["processor_type"], "horizon:count_cap");
assert_eq!(
(cap["min"].as_u64(), cap["max"].as_u64()),
(Some(1), Some(2))
);
assert_eq!(cap["replacement"], json!({"id": "minecraft:cave_air"}));
}
#[test]
fn lithostitched_block_swaps_become_the_games() {
let mut list = json!({"processors": [
{"processor_type": "lithostitched:block_swap",
"blocks": {"minecraft:cobblestone": "minecraft:mossy_cobblestone"}},
{"processor_type": "lithostitched:block_swap", "blocks": []}]});
let mut carried = BTreeMap::new();
assert!(carry("processor_list", &mut list, &data(), &mut carried));
let p = list["processors"].as_array().unwrap();
assert_eq!(
p[0],
json!({"processor_type": "horizon:block_swap",
"blocks": {"minecraft:cobblestone": "minecraft:mossy_cobblestone"}})
);
// A map that is not one stays Java (dropped later).
assert_eq!(p[1]["processor_type"], "lithostitched:block_swap");
}
#[test]
fn lithostitched_references_are_written_in() {
let mut data = data();
let gravity = json!({"processor_type": "minecraft:gravity", "heightmap": "WORLD_SURFACE_WG", "offset": 0});
data.lists
.insert("v:road".into(), json!({"processors": [gravity.clone()]}));
data.lists.insert(
"v:house".into(),
json!({"processors": [
{"processor_type": "lithostitched:reference", "processor_lists": "v:road"},
{"processor_type": "lithostitched:block_swap", "blocks": {"minecraft:dirt": "minecraft:grass_block"}}]}),
);
let mut list = json!({"processors": [
{"processor_type": "lithostitched:reference", "processor_lists": ["v:house", "v:road"]},
{"processor_type": "lithostitched:reference", "processor_lists": "#v:roads"},
{"processor_type": "lithostitched:reference", "processor_lists": ["v:nowhere"]}]});
let mut carried = BTreeMap::new();
assert!(carry("processor_list", &mut list, &data, &mut carried));
let p = list["processors"].as_array().unwrap();
// House (its own reference to road first, then its swap), then road;
// the swap inside is carried too.
assert_eq!(p[0], gravity);
assert_eq!(p[1]["processor_type"], "horizon:block_swap");
assert_eq!(p[2], gravity);
// A tag or a list the mod lacks stays, to be dropped.
assert_eq!(p[3]["processor_lists"], "#v:roads");
assert_eq!(p[4]["processor_lists"], json!(["v:nowhere"]));
assert_eq!(carried["lithostitched:reference"], 2);
}
#[test]
fn repurposed_rule_tests_become_the_games() {
let rule = |input: Value, position: Value| {
json!({"input_predicate": input, "location_predicate": {"predicate_type":
"repurposed_structures:matter_phase_rule_test", "phase_to_test_for": "AIR"},
"position_predicate": position, "output_state": {"id": "minecraft:lava"}})
};
let mut pool = json!({"elements": [{"weight": 1, "element": {
"element_type": "minecraft:single_pool_element", "processors": {"processors": [
{"processor_type": "minecraft:rule", "rules": [rule(
json!({"predicate_type": "minecraft:always_true"}),
json!({"predicate_type": "repurposed_structures:y_value_pos_rule_test",
"min_y_value": 0, "max_y_value": 31}))]},
{"processor_type": "minecraft:capped", "limit": 2, "delegate": {
"processor_type": "minecraft:rule", "rules": [rule(
json!({"predicate_type": "minecraft:block_match", "block": "minecraft:rail"}),
json!({"predicate_type":
"repurposed_structures:piece_origin_axis_aligned_linear_pos_rule_test",
"axis": "y", "min_dist": 0, "max_dist": 1}))]}}]}}}]});
let mut carried = BTreeMap::new();
assert!(rule_tests("template_pool", &mut pool, &mut carried));
let list = &pool["elements"][0]["element"]["processors"]["processors"];
let first = &list[0]["rules"][0];
assert_eq!(
first["location_predicate"]["predicate_type"],
"horizon:matter_phase"
);
assert_eq!(first["location_predicate"]["phase_to_test_for"], "AIR");
assert_eq!(
first["position_predicate"]["predicate_type"],
"horizon:y_value"
);
assert_eq!(first["position_predicate"]["max_y_value"], 31);
let delegated = &list[1]["delegate"]["rules"][0];
assert_eq!(
delegated["position_predicate"]["predicate_type"],
"horizon:piece_origin_axis_aligned_linear_pos"
);
assert_eq!(
delegated["input_predicate"]["predicate_type"],
"minecraft:block_match"
);
assert_eq!(carried.values().sum::<usize>(), 4);
assert_eq!(carried["repurposed_structures:matter_phase_rule_test"], 2);
// A file holding none is left as it is.
assert!(!rule_tests("template_pool", &mut pool, &mut carried));
assert!(!rule_tests(
"structure",
&mut json!({"predicate_type":
"repurposed_structures:y_value_pos_rule_test"}),
&mut carried
));
}
}