diff --git a/crates/horizon-client/src/game/commands/terrain_cmds.rs b/crates/horizon-client/src/game/commands/terrain_cmds.rs index 678f4e2ab..970a28bb1 100644 --- a/crates/horizon-client/src/game/commands/terrain_cmds.rs +++ b/crates/horizon-client/src/game/commands/terrain_cmds.rs @@ -2,6 +2,7 @@ use super::{Arg, Context, Game, R, Target, err, items::Owner}; use horizon_client::{ chunk::{HEIGHT, MIN_Y}, + command::CommandError, items::ItemStack, scoreboard_cmd::{Holder, Tracker}, text::Text, @@ -433,11 +434,20 @@ impl Game<'_> { fn tool_stack(&mut self, ctx: &Context) -> R> { if let Some(Arg::Item(p)) = ctx.get("tool") { + let (damage, extra) = match p + .components + .as_deref() + .map(horizon_client::item_data::command_components) + { + Some(Ok(tag)) => horizon_client::item_data::decode_components(Some(&tag)), + Some(Err(e)) => return Err(CommandError::new(Text::literal(e))), + None => (0, None), + }; return Ok(Some(ItemStack { item: p.id.as_str().into(), count: 1, - damage: 0, - extra: None, + damage, + extra, })); } let entity = self @@ -461,19 +471,39 @@ impl Game<'_> { let Some(Arg::Resource(table)) = ctx.get("loot_table").cloned() else { return Ok(0); }; - if !self.res.items.has_loot_table(&table) { - return Err(err("argument.resource_or_id.failed_to_parse", &[&table])); - } - let held = if id == "loot.fish" { - self.tool_stack(ctx)? - } else { - None - }; - let drops = - self.res + // An inline table (ResourceOrIdArgument): no random sequence. + if table.starts_with('{') { + let (tag, _) = horizon_world::nbt::parse_snbt(&table) + .map_err(|e| err("argument.nbt.invalid", &[&e]))?; + let json = horizon_client::scoreboard::tag_to_json(&tag); + let drops = self + .res .items - .loot_table(&table, false, held.as_ref(), &mut self.world.random); - (drops, None) + .inline_loot_table(&json, &mut self.world.random); + (drops, None) + } else { + if !self.res.items.has_loot_table(&table) { + return Err(err("argument.resource_or_id.failed_to_parse", &[&table])); + } + let held = if id == "loot.fish" { + self.tool_stack(ctx)? + } else { + None + }; + let drops = match self.res.items.loot_random_sequence(&table) { + Some(seq) => { + let random = self.rand_sequences.get(&seq, self.level.seed); + self.res.items.loot_table(&table, false, held.as_ref(), random) + } + None => self.res.items.loot_table( + &table, + false, + held.as_ref(), + &mut self.world.random, + ), + }; + (drops, None) + } } "loot.kill" => { let targets = self.entities(ctx, "target", false)?; @@ -493,12 +523,20 @@ impl Game<'_> { None | Some(Some(Target::Player)) ); let held = self.slot_get(&super::items::Slot::Inv(self.selected)); - let drops = self.res.items.entity_drops( - &ty, - by_player, - held.as_ref(), - &mut self.world.random, - ); + let drops = match self.res.items.loot_random_sequence(&table) { + Some(seq) => { + let random = self.rand_sequences.get(&seq, self.level.seed); + self.res + .items + .entity_drops(&ty, by_player, held.as_ref(), random) + } + None => self.res.items.entity_drops( + &ty, + by_player, + held.as_ref(), + &mut self.world.random, + ), + }; (drops, Some(table)) } _ => { @@ -511,12 +549,23 @@ impl Game<'_> { return Err(err("commands.drop.no_loot_table.block", &[&name])); } let tool = self.tool_stack(ctx)?; - let drops = self.res.items.block_drops( - self.res.assets.registry, - state, - tool.as_ref(), - &mut self.world.random, - ); + let drops = match self.res.items.loot_random_sequence(&table) { + Some(seq) => { + let random = self.rand_sequences.get(&seq, self.level.seed); + self.res.items.block_drops( + self.res.assets.registry, + state, + tool.as_ref(), + random, + ) + } + None => self.res.items.block_drops( + self.res.assets.registry, + state, + tool.as_ref(), + &mut self.world.random, + ), + }; (drops, Some(table)) } }; diff --git a/crates/horizon-client/src/items.rs b/crates/horizon-client/src/items.rs index e0c5a0366..e5af5d995 100644 --- a/crates/horizon-client/src/items.rs +++ b/crates/horizon-client/src/items.rs @@ -526,13 +526,33 @@ impl Items { pub fn has_loot_table(&self, id: &str) -> bool { self.loot.contains_key(id) } + /// A loot table's `random_sequence`. + pub fn loot_random_sequence(&self, id: &str) -> Option { + self.loot + .get(id)? + .get("random_sequence")? + .as_str() + .map(str::to_owned) + } + /// `LootTable.getRandomItems` of an inline table. + pub fn inline_loot_table(&self, table: &Value, random: &mut R) -> Vec { + let empty = BTreeMap::new(); + let ctx = LootContext { + items: self, + block: "", + properties: &empty, + held: None, + killed_by_player: false, + }; + ctx.evaluate(table, random) + } /// Evaluate the block's loot table for a player break without explosions. - pub fn block_drops( + pub fn block_drops( &self, registry: &BlockRegistry, state: BlockStateId, held: Option<&ItemStack>, - random: &mut LegacyRandom, + random: &mut R, ) -> Vec { let Some(block) = registry.state(state) else { return vec![]; @@ -561,12 +581,12 @@ impl Items { } /// Evaluate an entity's loot table (`minecraft:entities/`). /// Evaluate any loot table by id (e.g. `minecraft:gameplay/chicken_lay`). - pub fn loot_table( + pub fn loot_table( &self, id: &str, killed_by_player: bool, held: Option<&ItemStack>, - random: &mut LegacyRandom, + random: &mut R, ) -> Vec { let Some(table) = self.loot.get(id) else { return vec![]; @@ -581,12 +601,12 @@ impl Items { }; ctx.evaluate(table, random) } - pub fn entity_drops( + pub fn entity_drops( &self, entity: &str, killed_by_player: bool, held: Option<&ItemStack>, - random: &mut LegacyRandom, + random: &mut R, ) -> Vec { let name = entity.trim_start_matches("minecraft:"); let Some(table) = self.loot.get(&format!("minecraft:entities/{name}")) else { @@ -604,7 +624,7 @@ impl Items { } } impl LootContext<'_> { - fn evaluate(&self, table: &Value, random: &mut LegacyRandom) -> Vec { + fn evaluate(&self, table: &Value, random: &mut R) -> Vec { let ctx = self; let mut out = Vec::new(); for pool in table["pools"].as_array().into_iter().flatten() { @@ -643,7 +663,7 @@ impl LootContext<'_> { fn enchantment_level(&self, id: &str) -> u32 { self.held.map_or(0, |h| h.enchantment_level(id)) } - fn number(&self, v: &Value, random: &mut LegacyRandom) -> f32 { + fn number(&self, v: &Value, random: &mut R) -> f32 { match v { Value::Number(n) => n.as_f64().unwrap_or(0.0) as f32, Value::Object(o) => match o @@ -674,7 +694,7 @@ impl LootContext<'_> { _ => 1.0, } } - fn int_number(&self, v: &Value, random: &mut LegacyRandom) -> i32 { + fn int_number(&self, v: &Value, random: &mut R) -> i32 { if let Value::Object(o) = v && o.get("type") .and_then(Value::as_str) @@ -691,7 +711,7 @@ impl LootContext<'_> { } self.number(v, random).round() as i32 } - fn condition(&self, c: &Value, random: &mut LegacyRandom) -> bool { + fn condition(&self, c: &Value, random: &mut R) -> bool { let (kind, c) = match c { Value::String(s) => (s.as_str(), &Value::Null), Value::Object(o) => (o.get("type").and_then(Value::as_str).unwrap_or(""), c), @@ -800,7 +820,7 @@ impl LootContext<'_> { _ => true, } } - fn conditions(&self, o: &Value, random: &mut LegacyRandom) -> bool { + fn conditions(&self, o: &Value, random: &mut R) -> bool { if let Some(c) = o.get("condition") && !self.condition(c, random) { @@ -813,10 +833,10 @@ impl LootContext<'_> { .all(|c| self.condition(c, random)) } /// Expand entries; returns whether an entry succeeded (for alternatives). - fn expand( + fn expand( &self, e: &Value, - random: &mut LegacyRandom, + random: &mut R, out: &mut Vec<(i32, ItemStack, Value)>, ) -> bool { if !self.conditions(e, random) { @@ -874,7 +894,7 @@ impl LootContext<'_> { _ => false, } } - fn choose(&self, entries: &[Value], random: &mut LegacyRandom, produced: &mut Vec) { + fn choose(&self, entries: &[Value], random: &mut R, produced: &mut Vec) { let mut candidates = Vec::new(); for e in entries { self.expand(e, random, &mut candidates); @@ -901,7 +921,7 @@ impl LootContext<'_> { self.modifiers(&modifiers, &mut stack, random); produced.push(stack); } - fn modifiers(&self, m: &Value, stack: &mut ItemStack, random: &mut LegacyRandom) { + fn modifiers(&self, m: &Value, stack: &mut ItemStack, random: &mut R) { let list: Vec<&Value> = match m { Value::Array(a) => a.iter().collect(), Value::Object(_) => vec![m], diff --git a/crates/horizon-client/src/snbt.rs b/crates/horizon-client/src/snbt.rs index 7ccb8f8e1..698fd0f2d 100644 --- a/crates/horizon-client/src/snbt.rs +++ b/crates/horizon-client/src/snbt.rs @@ -376,8 +376,8 @@ pub fn java_hash_order( // Keys sharing a bucket iterate in insertion order: the order vanilla's // savers write them, as far as it is known here. const WRITE_ORDER: &[&str] = &[ - // ItemStack.CODEC, block entities. - "id", "count", "components", "x", "y", "z", + // ItemStack.CODEC. + "id", "count", "components", // Entity.saveWithoutId. "Pos", "Motion", "Rotation", "fall_distance", "Fire", "Air", "OnGround", "Invulnerable", "PortalCooldown", "UUID", "CustomName", "CustomNameVisible", @@ -392,11 +392,19 @@ pub fn java_hash_order( "Invisible", "Small", "ShowArms", "DisabledSlots", "NoBasePlate", "Marker", "Pose", ]; + // BlockEntity.saveMetadata writes the position after the entity's data. + const AFTER: &[&str] = &["x", "y", "z"]; let rank = |k: &horizon_world::nbt::JavaString| { let k = string_of(k); WRITE_ORDER .iter() .position(|w| *w == k) + .or_else(|| { + AFTER + .iter() + .position(|w| *w == k) + .map(|i| WRITE_ORDER.len() + 1 + i) + }) .unwrap_or(WRITE_ORDER.len()) }; let mut entries: Vec<_> = c.iter().collect(); diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCommandExecOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCommandExecOracle.java index de6ee1fa1..ba41c44ca 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCommandExecOracle.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaCommandExecOracle.java @@ -244,6 +244,12 @@ public final class VanillaCommandExecOracle { } drain(); super.tickServer(() -> false); + // The player's network listener ticks it each server tick + // (ServerGamePacketListenerImpl.tickPlayer); this one has no + // listening connection, so tick the player here. + if (player != null) { + player.doTick(); + } drain(); }