Merge remote-tracking branch 'pr/762' into integ/mp-sh

This commit is contained in:
Ludovic Marie
2026-10-18 06:34:04 +02:00
3 changed files with 159 additions and 1 deletions
+38
View File
@@ -25,6 +25,9 @@ pub struct Report {
pub unreadable: BTreeMap<String, String>,
pub recipes_upgraded: usize,
pub loot_tables_upgraded: usize,
/// Blocks whose jar has no loot table but drop themselves in the oracle:
/// the mod registers that table in code, and carry-over writes it.
pub loot_tables_added: usize,
/// Worldgen and structure files (`data/<id>/worldgen/`, `structure/`).
pub worldgen: usize,
/// Recipes left as they were: custom types, NeoForge conditions.
@@ -351,6 +354,7 @@ pub fn carry(
let mut blockstates: BTreeMap<String, Value> = BTreeMap::new();
let mut item_models = BTreeSet::new();
let mut convention = BTreeSet::new();
let mut block_loot = BTreeSet::new();
for i in 0..zip.len() {
let mut entry = zip.by_index(i).map_err(|e| e.to_string())?;
let name = entry.name().to_owned();
@@ -401,6 +405,12 @@ pub fn carry(
}
}
} else if rel.starts_with("loot_table/") && name.starts_with("data/") {
if let Some(b) = rel
.strip_prefix("loot_table/blocks/")
.and_then(|b| b.strip_suffix(".json"))
{
block_loot.insert(b.to_owned());
}
if let Ok(mut table) = serde_json::from_slice::<Value>(&bytes) {
let before = table.clone();
upgrade_loot(&mut table);
@@ -470,6 +480,34 @@ pub fn carry(
.push(format!("its item is {}", block.item));
} else {
declared.insert(full.clone());
let drop = (!block_loot.contains(b))
.then(|| block.drops_itself(full))
.flatten();
if let Some(drop) = drop {
let mut entry = json!({"type": "minecraft:item", "name": full});
if let crate::oracle::SelfDrop::When(property, value) = drop {
entry["condition"] = json!({
"type": "minecraft:match_block",
"blocks": full,
"state": {property: value}
});
}
let table = json!({
"type": "minecraft:block",
"pools": [{
"condition": {"type": "minecraft:survives_explosion"},
"entries": [entry],
"rolls": 1
}],
"random_sequence": format!("{id}:blocks/{b}")
});
let file = inside.join(format!("data/{id}/loot_table/blocks/{b}.json"));
write(
&file,
&serde_json::to_vec_pretty(&table).unwrap_or_default(),
)?;
report.loot_tables_added += 1;
}
}
write_decl("block", b, &v)?;
report.blocks.insert(b.to_owned(), choice.template);
+119
View File
@@ -60,6 +60,10 @@ pub struct State {
pub sound: Value,
pub outline: Value,
pub collision: Value,
/// What breaking the state drops, per tool (`hand`, `diamond_pickaxe`,
/// `shears`, `silk_touch_pickaxe`...): item id → count.
#[serde(default)]
pub drops: BTreeMap<String, BTreeMap<String, u32>>,
}
#[derive(Debug, Deserialize)]
@@ -76,6 +80,23 @@ pub struct Item {
const VANILLA_PACKAGE: &str = "net.minecraft.";
/// A state key with its properties sorted, so keys compare across blocks.
/// Which states of a block drop the block itself.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SelfDrop {
/// Every state: `dropSelf`.
Always,
/// Only the states where this property has this value.
When(String, String),
}
/// A state key's `property=value` pairs.
fn pairs(key: &str) -> BTreeSet<(String, String)> {
key.split(',')
.filter_map(|p| p.split_once('='))
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.collect()
}
pub fn state_key(key: &str) -> String {
let mut parts: Vec<&str> = key.split(',').filter(|p| !p.is_empty()).collect();
parts.sort_unstable();
@@ -96,6 +117,45 @@ impl Block {
.first()
.is_some_and(|c| !c.starts_with(VANILLA_PACKAGE))
}
/// Every state drops one `item` (the block's own) with every tool, except
/// where the state needs a tool and gets none: vanilla's `dropSelf`. Or
/// only the states where one property has one value do, and the others
/// drop nothing with any tool: a door's lower half (`createDoorTable`).
/// Mods often register these tables in code, so their jar has no file.
pub fn drops_itself(&self, item: &str) -> Option<SelfDrop> {
let one = BTreeMap::from([(item.to_owned(), 1)]);
let (mut dropping, mut nothing) = (Vec::new(), Vec::new());
for (key, state) in &self.states {
if state.drops.is_empty() {
continue;
}
let mut drops = false;
let mut short = false;
for got in state.drops.values() {
if *got == one {
drops = true;
} else if got.is_empty() {
short = true;
} else {
return None;
}
}
match (drops, short) {
(true, true) if !state.requires_tool => return None,
(true, _) => dropping.push(pairs(key)),
(false, _) => nothing.push(pairs(key)),
}
}
let (first, rest) = dropping.split_first()?;
if nothing.is_empty() {
return Some(SelfDrop::Always);
}
first
.iter()
.filter(|pair| rest.iter().all(|s| s.contains(*pair)))
.find(|pair| nothing.iter().all(|s| !s.contains(*pair)))
.map(|(p, v)| SelfDrop::When(p.clone(), v.clone()))
}
fn default_state(&self) -> Option<&State> {
self.states
.get(&self.default)
@@ -373,6 +433,65 @@ mod tests {
assert_eq!(tile.differences[0], "sounds: no vanilla block has them");
}
#[test]
fn drops_itself_is_vanillas_drop_self() {
let cube = json!([[0, 0, 0, 1, 1, 1]]);
let with = |requires_tool: bool, drops: Value| {
let mut s = state(1.5, "stone", cube.clone());
s["requires_tool"] = json!(requires_tool);
s["drops"] = drops;
let b: Block = serde_json::from_value(block(&[], json!({}), json!({"": s}))).unwrap();
b.drops_itself("m:tile")
};
let me = json!({"m:tile": 1});
let always = Some(SelfDrop::Always);
assert_eq!(with(false, json!({"hand": me, "shears": me})), always);
// A state that needs a tool drops nothing by hand, as dropSelf does.
assert_eq!(
with(true, json!({"hand": {}, "diamond_pickaxe": me})),
always
);
assert_eq!(
with(false, json!({"hand": {}, "diamond_pickaxe": me})),
None
);
// Two of itself (a double slab), or something else, is not dropSelf.
assert_eq!(with(false, json!({"hand": {"m:tile": 2}})), None);
assert_eq!(with(false, json!({"hand": {"m:other": 1}})), None);
assert_eq!(with(false, json!({})), None);
}
#[test]
fn a_door_drops_from_its_lower_half() {
let cube = json!([[0, 0, 0, 1, 1, 1]]);
let me = json!({"hand": {"m:door": 1}, "shears": {"m:door": 1}});
let none = json!({"hand": {}, "shears": {}});
let door = |upper: &Value| {
let mut states = serde_json::Map::new();
for (key, drops) in [
("half=lower,open=false", &me),
("half=lower,open=true", &me),
("half=upper,open=false", upper),
("half=upper,open=true", upper),
] {
let mut s = state(3.0, "wood", cube.clone());
s["drops"] = drops.clone();
states.insert(key.to_owned(), s);
}
let props = json!({"half": ["upper", "lower"], "open": ["true", "false"]});
let b: Block =
serde_json::from_value(block(&[], props, Value::Object(states))).unwrap();
b.drops_itself("m:door")
};
assert_eq!(
door(&none),
Some(SelfDrop::When("half".to_owned(), "lower".to_owned()))
);
assert_eq!(door(&me), Some(SelfDrop::Always));
// Something else from the upper half: not a door table.
assert_eq!(door(&json!({"hand": {"m:other": 1}})), None);
}
#[test]
fn state_keys_sort() {
assert_eq!(
+2 -1
View File
@@ -158,12 +158,13 @@ pub fn carry(
Ok(report) => {
done += 1;
log(format!(
"{}: {} files, {} recipes upgraded ({} kept), {} loot tables upgraded, {} item definitions added, {} blocks ({} left out, {} with differences), {} items ({} left out), {} convention tags, {} outside",
"{}: {} files, {} recipes upgraded ({} kept), {} loot tables upgraded ({} added), {} item definitions added, {} blocks ({} left out, {} with differences), {} items ({} left out), {} convention tags, {} outside",
r.key,
report.copied,
report.recipes_upgraded,
report.recipes_kept.len(),
report.loot_tables_upgraded,
report.loot_tables_added,
report.item_definitions_added,
report.blocks.len(),
report.blocks_unknown.len(),