Merge pull request #2390 from kacpherek/fix/block-loot-copy-components

🤖 Agent: Proven in union R1 (branch union/cg19-r1, main + 83 of kacpherek's PRs): the 98 harnesses it can affect, clippy -D warnings, tests, lan_e2e in-frame and on the server thread.
This commit is contained in:
Ludovic Marie
2026-10-27 14:52:41 +01:00
committed by GitHub
5 changed files with 115 additions and 3 deletions
+1
View File
@@ -4240,6 +4240,7 @@ impl Game<'_> {
return Ok(true);
};
let target = [pos.x, pos.y, pos.z];
let state = self.apply_item_block_state(target, state, &stack)?;
self.mod_block_event(target, state, true);
if let Some(entity) = BlockEntity::for_block(&self.res.facts().get(state).name) {
let key = (target[0], target[1], target[2]);
+42 -2
View File
@@ -4,9 +4,13 @@
//! placed item gives its block entity.
use super::Game;
use horizon_client::{
block_entity_item, block_entity_nbt, containers::BlockEntity, item_data, items::ItemStack, save,
Result, block_entity_item, block_entity_nbt, containers::BlockEntity, item_data,
items::ItemStack, save,
};
use horizon_world::{
nbt::{Compound, Tag},
registry::BlockStateId,
};
use horizon_world::nbt::{Compound, Tag};
/// The component patch of `stack`.
fn components(stack: &ItemStack) -> Compound {
@@ -77,6 +81,42 @@ impl Game<'_> {
Some(stack)
}
/// `BlockItem.updateBlockStateFromTag`: the properties of the item's
/// `block_state` component (a hive's honey level, a statue's pose) set on
/// the block just placed at `pos`. Returns the state it has now.
pub(super) fn apply_item_block_state(
&mut self,
pos: [i32; 3],
state: BlockStateId,
stack: &ItemStack,
) -> Result<BlockStateId> {
let Some(Tag::Compound(saved)) =
stack.data().and_then(|d| d.other("minecraft:block_state"))
else {
return Ok(state);
};
let Some(current) = self.pack.assets.registry.state(state) else {
return Ok(state);
};
// Only the properties the block has.
let props: Vec<(String, String)> = saved
.iter()
.filter_map(|(k, v)| match v {
Tag::String(v) => Some((k.to_string_lossy(), v.to_string_lossy())),
_ => None,
})
.filter(|(k, v)| current.properties.get(k).is_some_and(|old| old != v))
.collect();
let props: Vec<(&str, &str)> = props.iter().map(|(k, v)| (&**k, &**v)).collect();
match self.with_props(state, &props) {
Some(new) if !props.is_empty() && new != state => {
self.level_set(pos, new)?;
Ok(new)
}
_ => Ok(state),
}
}
/// `BlockItem.updateCustomBlockEntityTag` and `updateBlockEntityComponents`
/// for the block entity just placed at `pos` from `stack`; `game_master`:
/// the placer can use game master blocks (a dispenser can't).
@@ -1548,6 +1548,7 @@ impl Game<'_> {
return Ok((Some(stack), FAIL));
};
let target = (placed.x, placed.y, placed.z);
let state = self.apply_item_block_state([target.0, target.1, target.2], state, &stack)?;
if let Some(entity) = BlockEntity::for_block(&self.res.facts().get(state).name) {
self.world.block_entities.insert(target, entity);
self.apply_placed_item([target.0, target.1, target.2], &stack, false);
+45 -1
View File
@@ -5,9 +5,11 @@
//! world as soon as the behaviour returns, within the same tick.
use super::{Game, GameLevel, Screen, dispenser::new_item_motion, storage::is_shulker_box};
use horizon_client::{
block_entity_item, block_entity_nbt,
containers::{Banner, BlockEntity},
item_data,
items::ItemStack,
save,
};
use horizon_core::position::BlockPos;
use horizon_core::random::RandomSource;
@@ -16,7 +18,7 @@ use horizon_world::{
LevelEvent,
removal::{self, PreRemove},
},
nbt::{JavaString, Tag},
nbt::{Compound, JavaString, Tag},
registry::BlockStateId,
};
use std::collections::HashMap;
@@ -32,6 +34,9 @@ pub(super) struct Pending {
pub(super) names: HashMap<[i32; 3], (String, String)>,
/// A decorated pot's sherds (back, left, right, front).
pub(super) pot: HashMap<[i32; 3], [Option<ItemStack>; 4]>,
/// A head's profile, note block sound and name, or a hive's bees: the
/// dropped item and the component patch it takes.
pub(super) copied: HashMap<[i32; 3], (String, Compound)>,
}
impl Pending {
/// Remember what the block entity at `pos` copies into the drops of `block`.
@@ -49,6 +54,31 @@ impl Pending {
if let Some(name) = name {
self.names.insert(pos, (block.to_owned(), name));
}
// `copy_components` from the block entity: the head tables copy the
// profile (player heads), note block sound and name, the hive tables
// the bees.
let include: &[&str] = match entity {
BlockEntity::Skull(_) if block.starts_with("minecraft:player_") => &[
"minecraft:profile",
"minecraft:note_block_sound",
"minecraft:custom_name",
],
BlockEntity::Skull(_) => &["minecraft:custom_name"],
BlockEntity::Beehive(_) => &["minecraft:bees"],
_ => &[],
};
if !include.is_empty() {
let saved = save::encode_block_entity((pos[0], pos[1], pos[2]), entity, block);
let mut patch = block_entity_nbt::canonical(&saved)
.map(|saved| block_entity_item::pick(&saved))
.unwrap_or_default();
patch.retain(|k, _| include.contains(&k.to_string_lossy().as_str()));
if !patch.is_empty() {
// `Blocks.wallVariant`: a wall head drops the standing item.
let item = block.replacen("_wall_", "_", 1);
self.copied.insert(pos, (item, patch));
}
}
if is_shulker_box(block)
&& let BlockEntity::Chest(items) = entity
{
@@ -76,6 +106,19 @@ impl Pending {
{
stack.data_mut().custom_name = Some(name);
}
if self
.copied
.get(&pos)
.is_some_and(|(item, _)| **item == *stack.item)
&& let Some((_, copied)) = self.copied.remove(&pos)
{
let mut patch = match item_data::encode_components(stack) {
Some(Tag::Compound(c)) => c,
_ => Default::default(),
};
patch.extend(copied);
(stack.damage, stack.extra) = item_data::decode_components(Some(&Tag::Compound(patch)));
}
if &*stack.item == "minecraft:decorated_pot" {
// The pot's block entity always copies its decorations, empty or not.
let sides = self.pot.remove(&pos).unwrap_or_default();
@@ -155,6 +198,7 @@ impl Pending {
self.shulker.remove(&pos);
self.banner.remove(&pos);
self.names.remove(&pos);
self.copied.remove(&pos);
}
}
+26
View File
@@ -1154,6 +1154,8 @@ impl Items {
ctx.evaluate(table, random)
}
}
/// `DataComponents.BLOCK_STATE`.
const BLOCK_STATE: &str = "minecraft:block_state";
impl LootContext<'_> {
fn evaluate<R: RandomSource>(&self, table: &Value, random: &mut R) -> Vec<ItemStack> {
let ctx = self;
@@ -1561,6 +1563,30 @@ impl LootContext<'_> {
}
}
}
// CopyBlockState: the listed properties of the broken block
// become the item's `block_state` component.
"minecraft:copy_state" => {
use horizon_world::nbt::Tag;
let mut copied = match stack.data().and_then(|d| d.other(BLOCK_STATE)) {
Some(Tag::Compound(c)) => c.clone(),
_ => Default::default(),
};
for name in f["properties"]
.as_array()
.into_iter()
.flatten()
.filter_map(Value::as_str)
{
if let Some(value) = self.properties.get(name) {
copied.insert(name.into(), Tag::String(value.as_str().into()));
}
}
if !copied.is_empty() {
stack
.data_mut()
.set_other(BLOCK_STATE, Some(Tag::Compound(copied)));
}
}
// ApplyExplosionDecay: each item survives with 1 / radius.
"minecraft:explosion_decay" => {
if let Some(radius) = self.explosion_radius {