diff --git a/crates/horizon-client/src/containers.rs b/crates/horizon-client/src/containers.rs index 7b8eb7a27..4281385df 100644 --- a/crates/horizon-client/src/containers.rs +++ b/crates/horizon-client/src/containers.rs @@ -79,6 +79,7 @@ impl BlockEntity { "minecraft:chest" | "minecraft:trapped_chest" | "minecraft:barrel" => 27, "minecraft:hopper" => 5, "minecraft:dispenser" | "minecraft:dropper" => 9, + n if n.ends_with("shulker_box") => 27, _ => return None, }; Some(Self::Chest(vec![None; size])) @@ -95,6 +96,7 @@ impl BlockEntity { "minecraft:furnace" => "minecraft:furnace", "minecraft:blast_furnace" => "minecraft:blast_furnace", "minecraft:smoker" => "minecraft:smoker", + n if n.ends_with("shulker_box") => "minecraft:shulker_box", _ => return None, }) } diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index f19b1f751..992e178c6 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -29,6 +29,7 @@ mod shield; mod sign_edit; mod station_control; mod stations; +mod storage; mod titles; mod tnt; mod totem; @@ -532,6 +533,11 @@ struct Game<'a> { boat_models: boats::BoatModels, /// The boat the player rides. riding: Option, + /// The ender inventory (27 slots) and the ender chest showing it. + ender_items: Vec>, + ender_open: Option<(i32, i32, i32)>, + /// Contents of broken shulker boxes waiting for their dropped item. + pending_shulker: HashMap<[i32; 3], Vec<(u8, ItemStack)>>, tnts: Vec, hoppers: HashMap<(i32, i32, i32), machines::HopperState>, creative_screen: Creative, @@ -822,7 +828,8 @@ impl Game<'_> { }); } /// Block.popResource - fn pop_resource(&mut self, pos: [i32; 3], stack: ItemStack) { + fn pop_resource(&mut self, pos: [i32; 3], mut stack: ItemStack) { + self.attach_shulker_contents(pos, &mut stack); let r = &mut self.world.random; let mut offset = || f64::from(r.next_float()) * 0.5 - 0.25; let p = [ @@ -1101,6 +1108,10 @@ impl Game<'_> { self.audio.play(&name, Some(c), volume, pitch); } LevelEvent::DropResources { pos, state } => { + if let Some(entity) = self.world.block_entities.remove(&(pos.x, pos.y, pos.z)) { + let name = self.res.facts.get(state).name.clone(); + self.release_contents([pos.x, pos.y, pos.z], entity, &name); + } let drops = self.res.items.block_drops( self.res.assets.registry, state, @@ -1112,11 +1123,6 @@ impl Game<'_> { self.pop_resource([pos.x, pos.y, pos.z], d); } } - if let Some(entity) = self.world.block_entities.remove(&(pos.x, pos.y, pos.z)) { - for stack in entity.contents() { - self.pop_resource([pos.x, pos.y, pos.z], stack); - } - } } LevelEvent::FallingBlock { pos, state } => { let p = Vec3::new( @@ -1204,10 +1210,22 @@ impl Game<'_> { let key = (pos[0], pos[1], pos[2]); if &*name == "minecraft:crafting_table" { self.craft_size = 3; + } else if &*name == "minecraft:ender_chest" { + self.open_ender_chest(key); + let at = [ + f64::from(key.0) + 0.5, + f64::from(key.1) + 0.5, + f64::from(key.2) + 0.5, + ]; + let pitch = self.world.random.next_float() * 0.1 + 0.9; + self.audio + .play("minecraft:block.ender_chest.open", Some(at), 0.5, pitch); } else if let Some(entity) = BlockEntity::for_block(&name) { self.world.block_entities.entry(key).or_insert(entity); self.container = Some(key); - let sound = if name.contains("chest") { + let sound = if storage::is_shulker_box(&name) { + Some("minecraft:block.shulker_box.open") + } else if name.contains("chest") { Some("minecraft:block.chest.open") } else if &*name == "minecraft:barrel" { Some("minecraft:block.barrel.open") @@ -1375,10 +1393,9 @@ impl Game<'_> { } .unwrap_or(self.world.air); if let Some(entity) = self.world.block_entities.remove(&(pos[0], pos[1], pos[2])) { - // Containers.dropContents - for stack in entity.contents() { - self.pop_resource(pos, stack); - } + // Containers.dropContents (shulker boxes keep theirs). + let name = facts.name.clone(); + self.release_contents(pos, entity, &name); if self.container == Some((pos[0], pos[1], pos[2])) { self.container = None; self.screen = Screen::Playing; @@ -1538,6 +1555,7 @@ impl Game<'_> { self.world .block_entities .insert((target[0], target[1], target[2]), entity); + self.fill_placed_box(target, &stack); if sign { self.open_sign_editor((target[0], target[1], target[2]), true); } @@ -2972,7 +2990,11 @@ impl Game<'_> { .get(self.world.block(key.0, key.1, key.2)) .name .clone(); - let sound = if name.contains("chest") { + let sound = if storage::is_shulker_box(&name) { + Some("minecraft:block.shulker_box.close") + } else if &*name == "minecraft:ender_chest" { + Some("minecraft:block.ender_chest.close") + } else if name.contains("chest") { Some("minecraft:block.chest.close") } else if &*name == "minecraft:barrel" { Some("minecraft:block.barrel.close") @@ -2993,6 +3015,7 @@ impl Game<'_> { } self.world.modified.insert((key.0 >> 4, key.2 >> 4)); } + self.close_ender_chest(); } fn click_inventory(&mut self, right: bool) { if self.station_click(right) { @@ -3012,6 +3035,11 @@ impl Game<'_> { let Some(slot) = self.slot_at(&batch, self.cursor.0, self.cursor.1) else { return; }; + if let Some(c) = &self.carried + && self.refuses(slot, c) + { + return; + } let max = |items: &Items, s: &ItemStack| items.get(&s.item).map_or(64, |i| i.max_stack); let shift = self.keys.contains(&KeyCode::ShiftLeft) || self.keys.contains(&KeyCode::ShiftRight); @@ -4137,6 +4165,11 @@ impl Game<'_> { let mut inventory = vec![None; 36]; self.armor = [None, None, None, None]; self.offhand = None; + self.ender_items = if level.ender_items.len() == 27 { + level.ender_items.clone() + } else { + vec![None; 27] + }; for (slot, id, count, damage) in &level.inventory { let stack = ItemStack { item: Arc::from(id.as_str()), @@ -6086,6 +6119,13 @@ impl Game<'_> { } } level.dimension = self.dim.id().to_owned(); + // An open ender chest holds the live copy. + if let Some(key) = self.ender_open + && let Some(BlockEntity::Chest(items)) = self.world.block_entities.get(&key) + { + self.ender_items = items.clone(); + } + level.ender_items = self.ender_items.clone(); level.active_effects = self .potions .effects @@ -7076,6 +7116,9 @@ pub fn run() -> Result<()> { boats: Vec::new(), boat_models: boats::BoatModels::load(&root), riding: None, + ender_items: vec![None; 27], + ender_open: None, + pending_shulker: HashMap::new(), tnts: Vec::new(), hoppers: HashMap::new(), creative_screen: Creative::load(&game_root)?, diff --git a/crates/horizon-client/src/game/explosion.rs b/crates/horizon-client/src/game/explosion.rs index ca7abaa98..943d8319e 100644 --- a/crates/horizon-client/src/game/explosion.rs +++ b/crates/horizon-client/src/game/explosion.rs @@ -407,11 +407,15 @@ impl Game<'_> { } } let contents = self.world.block_entities.remove(&(pos[0], pos[1], pos[2])); + let name = self + .res + .facts + .get(self.world.block(pos[0], pos[1], pos[2])) + .name + .clone(); self.level_set(pos, self.world.air)?; if let Some(entity) = contents { - for stack in entity.contents() { - self.pop_resource(pos, stack); - } + self.release_contents(pos, entity, &name); } if is_tnt { self.spawn_primed_tnt(BlockPos::new(pos[0], pos[1], pos[2]), true); diff --git a/crates/horizon-client/src/game/storage.rs b/crates/horizon-client/src/game/storage.rs new file mode 100644 index 000000000..4d161514f --- /dev/null +++ b/crates/horizon-client/src/game/storage.rs @@ -0,0 +1,81 @@ +//! Shulker boxes (`ShulkerBoxBlockEntity`: 27 slots that refuse other boxes, +//! kept in the dropped item's `container` component) and ender chests (the +//! player's `PlayerEnderChestContainer`, saved as `EnderItems`). +use super::Game; +use horizon_client::{containers::BlockEntity, items::ItemStack}; + +pub(super) fn is_shulker_box(name: &str) -> bool { + name.ends_with("shulker_box") +} +impl Game<'_> { + /// Block-entity contents on removal: shulker boxes keep theirs for the + /// dropped box (`copy_components`), other containers spill them. + pub(super) fn release_contents(&mut self, pos: [i32; 3], entity: BlockEntity, block: &str) { + if is_shulker_box(block) { + if let BlockEntity::Chest(items) = entity { + let kept = items + .into_iter() + .enumerate() + .filter_map(|(i, s)| Some((i as u8, s?))) + .collect(); + self.pending_shulker.insert(pos, kept); + } + return; + } + for stack in entity.contents() { + self.pop_resource(pos, stack); + } + } + /// The contents a shulker box dropped at `pos` carries. + pub(super) fn attach_shulker_contents(&mut self, pos: [i32; 3], stack: &mut ItemStack) { + if !is_shulker_box(&stack.item) { + return; + } + if let Some(contents) = self.pending_shulker.remove(&pos) + && !contents.is_empty() + { + stack.data_mut().container = contents; + } + } + /// A placed shulker box takes the item's contents. + pub(super) fn fill_placed_box(&mut self, pos: [i32; 3], stack: &ItemStack) { + let Some(data) = stack.data() else { return }; + if !is_shulker_box(&stack.item) || data.container.is_empty() { + return; + } + if let Some(BlockEntity::Chest(items)) = + self.world.block_entities.get_mut(&(pos[0], pos[1], pos[2])) + { + for (slot, s) in &data.container { + if let Some(cell) = items.get_mut(usize::from(*slot)) { + *cell = Some(s.clone()); + } + } + } + } + /// ShulkerBoxSlot.mayPlace: no shulker box inside a shulker box. + pub(super) fn refuses(&self, slot: usize, stack: &ItemStack) -> bool { + (300..400).contains(&slot) + && is_shulker_box(&stack.item) + && self.container.is_some_and(|k| { + is_shulker_box(&self.res.facts.get(self.world.block(k.0, k.1, k.2)).name) + }) + } + /// Opening an ender chest shows the player's ender inventory in its place. + pub(super) fn open_ender_chest(&mut self, key: (i32, i32, i32)) { + self.world + .block_entities + .insert(key, BlockEntity::Chest(self.ender_items.clone())); + self.container = Some(key); + self.ender_open = Some(key); + } + /// Closing it copies the items back and leaves no block-entity contents. + pub(super) fn close_ender_chest(&mut self) { + let Some(key) = self.ender_open.take() else { + return; + }; + if let Some(BlockEntity::Chest(items)) = self.world.block_entities.remove(&key) { + self.ender_items = items; + } + } +} diff --git a/crates/horizon-client/src/item_data.rs b/crates/horizon-client/src/item_data.rs index c95b33eac..8600b8481 100644 --- a/crates/horizon-client/src/item_data.rs +++ b/crates/horizon-client/src/item_data.rs @@ -22,6 +22,8 @@ pub struct StackData { pub potion: Option>, /// `minecraft:trim`: (material, pattern). pub trim: Option<(Arc, Arc)>, + /// `minecraft:container` (shulker boxes): the non-empty slots. + pub container: Vec<(u8, ItemStack)>, } impl StackData { pub fn is_empty(&self) -> bool { @@ -236,6 +238,19 @@ pub fn components_tag(damage: u32, data: Option<&StackData>) -> Option { ]), )); } + if !d.container.is_empty() { + let slots = d + .container + .iter() + .map(|(slot, stack)| { + Tag::compound([ + ("slot", Tag::Int(i32::from(*slot))), + ("item", crate::save::item_to_nbt(stack)), + ]) + }) + .collect(); + entries.push(("minecraft:container", Tag::List(slots))); + } } (!entries.is_empty()).then(|| Tag::compound(entries)) } @@ -263,6 +278,18 @@ pub fn decode_components(components: Option<&Tag>) -> (u32, Option list + .iter() + .filter_map(|e| { + Some(( + e.get("slot").and_then(int)?.clamp(0, 255) as u8, + crate::save::item_from_nbt(e.get("item")?)?, + )) + }) + .collect(), + _ => Vec::new(), + }, trim: c.get("minecraft:trim").and_then(|t| { Some(( Arc::from(t.get("material").and_then(string)?.as_str()), diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index 0b628f165..e135f7e6e 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -343,6 +343,9 @@ fn block_entity_id(e: &crate::containers::BlockEntity, block: &str) -> String { crate::containers::BlockEntity::Chest(_), "minecraft:trapped_chest" | "minecraft:barrel" | "minecraft:dropper", ) => block.to_owned(), + (crate::containers::BlockEntity::Chest(_), b) if b.ends_with("shulker_box") => { + "minecraft:shulker_box".to_owned() + } _ => e.id().to_owned(), } } @@ -1222,6 +1225,8 @@ pub struct Level { pub xp_seed: i32, /// Player.active_effects: (id, duration, amplifier, ambient, show_icon). pub active_effects: Vec<(String, i32, i32, bool, bool)>, + /// Player.EnderItems: the ender chest inventory (27 slots). + pub ender_items: Vec>, pub xp_level: i32, pub xp_progress: f32, pub xp_total: i32, @@ -1423,6 +1428,7 @@ impl Level { } } self.inventory = inventory; + self.ender_items = decode_items(p.get("EnderItems"), 27); self.inventory_components = inventory_components; self.xp_seed = n("XpSeed").unwrap_or(0) as i32; self.active_effects = match p.get("active_effects") { @@ -1448,7 +1454,7 @@ impl Level { let mut p = compound_or_empty(self.player_extra.clone()); // Keys rewritten from scratch. if let Tag::Compound(map) = &mut p { - for key in ["Inventory", "equipment", "Pos", "respawn"] { + for key in ["Inventory", "EnderItems", "equipment", "Pos", "respawn"] { map.remove(&JavaString::from(key)); } } @@ -1537,6 +1543,7 @@ impl Level { ), ("playerGameType", Tag::Int(i32::from(self.creative))), ("Inventory", Tag::List(inventory)), + ("EnderItems", encode_items(&self.ender_items)), ("abilities", abilities), ( "Dimension",