From 0c9bddecda77f837232ff424c9924af62bc42421 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Tue, 27 Oct 2026 06:11:01 +0100 Subject: [PATCH] Let block hoppers move items into and out of minecart containers --- crates/horizon-client/src/game/machines.rs | 197 ++++++++++++++++----- 1 file changed, 153 insertions(+), 44 deletions(-) diff --git a/crates/horizon-client/src/game/machines.rs b/crates/horizon-client/src/game/machines.rs index 0562af8cd..01e9e3acf 100644 --- a/crates/horizon-client/src/game/machines.rs +++ b/crates/horizon-client/src/game/machines.rs @@ -593,27 +593,41 @@ impl Game<'_> { self.container_changed(pos); } } - /// `HopperBlockEntity.ejectItems`. + /// `HopperBlockEntity.ejectItems`: into the block container in front, else + /// a container entity there (`getContainerAt`). fn hopper_eject(&mut self, pos: Pos, hopper: &View) -> bool { let state = self.world.block(pos.0, pos.1, pos.2); let c = self.containers(); let Some(facing) = c.prop(state, "facing").and_then(dir_from_name) else { return false; }; - let Some(target) = c.view(offset(pos, facing), false) else { + let front = offset(pos, facing); + let face = facing.opposite(); + if let Some(target) = c.view(front, false) { + return self.eject_into(pos, hopper, &target, face); + } + let Some(id) = self.entity_container_at([ + f64::from(front.0) + 0.5, + f64::from(front.1) + 0.5, + f64::from(front.2) + 0.5, + ]) else { return false; }; - let face = facing.opposite(); - if c.is_full(&target, face) { + self.with_entity_container(id, |g, target| g.eject_into(pos, hopper, target, face)) + .unwrap_or(false) + } + /// The rest of `HopperBlockEntity.ejectItems`: one item into `target`. + fn eject_into(&mut self, pos: Pos, hopper: &View, target: &View, face: Direction) -> bool { + if self.containers().is_full(target, face) { return false; } for slot in 0..hopper.len() { let Some(one) = self.take_one(hopper, slot) else { continue; }; - match self.add_item(Some(pos), &target, one, Some(face)) { + match self.add_item(Some(pos), target, one, Some(face)) { None => { - self.view_changed(&target); + self.view_changed(target); return true; } Some(rest) => self.put_back(hopper, slot, rest), @@ -624,7 +638,12 @@ impl Game<'_> { /// `HopperBlockEntity.suckInItems`: from the container above, or item entities. fn hopper_suck(&mut self, pos: Pos, hopper: &View) -> bool { let above = (pos.0, pos.1 + 1, pos.2); - if let Some(took) = self.suck_from_container(above, hopper) { + let at = [ + f64::from(pos.0) + 0.5, + f64::from(pos.1) + 1.5, + f64::from(pos.2) + 0.5, + ]; + if let Some(took) = self.suck_from_container(above, at, hopper) { return took; } let above_state = self.world.block(above.0, above.1, above.2); @@ -658,11 +677,16 @@ impl Game<'_> { self.suck_item_entities(hopper, min, max) } /// The container half of `HopperBlockEntity.suckInItems`: one item from the - /// container at `above`, through its bottom face. `None` when there is no - /// container there. - fn suck_from_container(&mut self, above: Pos, hopper: &View) -> Option { + /// container at `above`, through its bottom face; with no block container + /// there, from a container entity whose box meets the one around `at` + /// (`getSourceContainerOrHandler`). `None` when there is no container. + fn suck_from_container(&mut self, above: Pos, at: [f64; 3], hopper: &View) -> Option { let c = self.containers(); - let source = c.view(above, false)?; + let Some(source) = c.view(above, false) else { + let id = self.entity_container_at(at)?; + return self + .with_entity_container(id, |g, source| g.suck_from_slots(None, source, hopper)); + }; if let Kind::Composter(level) = source.kind { if level != 8 { return Some(false); @@ -681,26 +705,133 @@ impl Game<'_> { return Some(moved); } let source_hopper = (source.kind == Kind::Hopper).then_some(above); + Some(self.suck_from_slots(source_hopper, &source, hopper)) + } + /// `HopperBlockEntity.tryTakeInItemFromSlot` over the `DOWN` face of `source`. + fn suck_from_slots( + &mut self, + source_hopper: Option, + source: &View, + hopper: &View, + ) -> bool { for slot in source.slots_for_face(Some(Direction::Down)) { let c = self.containers(); - let Some(stack) = c.get(&source, slot) else { + let Some(stack) = c.get(source, slot) else { continue; }; - if !c.can_take(&source, slot, stack, Direction::Down) { + if !c.can_take(source, slot, stack, Direction::Down) { continue; } - let Some(one) = self.take_one(&source, slot) else { + let Some(one) = self.take_one(source, slot) else { continue; }; match self.add_item(source_hopper, hopper, one, None) { None => { - self.view_changed(&source); - return Some(true); + self.view_changed(source); + return true; } - Some(rest) => self.put_back(&source, slot, rest), + Some(rest) => self.put_back(source, slot, rest), } } - Some(false) + false + } + /// `HopperBlockEntity.getEntityContainer`: one of the alive container + /// entities (`EntitySelector.CONTAINER_ENTITY_SELECTOR`: chest and hopper + /// minecarts, chest boats) whose box meets the 1x1x1 box around `at`, + /// picked at random. + fn entity_container_at(&mut self, at: [f64; 3]) -> Option { + let meets = |b: horizon_core::geometry::Aabb| { + let (lo, hi) = (b.min_position(), b.max_position()); + lo.x < at[0] + 0.5 + && hi.x > at[0] - 0.5 + && lo.y < at[1] + 0.5 + && hi.y > at[1] - 0.5 + && lo.z < at[2] + 0.5 + && hi.z > at[2] - 0.5 + }; + let mut found: Vec = self + .carts + .iter() + .filter(|c| { + matches!( + c.kind, + super::minecarts::CartKind::Chest | super::minecarts::CartKind::Hopper + ) && meets(c.bounds()) + }) + .map(|c| c.id) + .collect(); + found.extend( + self.boats + .iter() + .filter(|b| b.chest && meets(b.bounds())) + .map(|b| b.id), + ); + if found.is_empty() { + return None; + } + let pick = self + .world + .random + .next_int_bound(found.len() as i32) + .unwrap_or(0); + found.get(pick as usize).copied() + } + /// Runs `f` on a container entity's items as a container, under the + /// placeholder key its menu uses (the open menu's copy when it is open), + /// then hands the items back to the entity. + fn with_entity_container( + &mut self, + id: u32, + f: impl FnOnce(&mut Self, &View) -> R, + ) -> Option { + let mut items = if let Some(c) = self.carts.iter_mut().find(|c| c.id == id) { + std::mem::take(&mut c.items) + } else { + std::mem::take(&mut self.boats.iter_mut().find(|b| b.id == id)?.items) + }; + let key = (i32::MAX - 15, super::minecarts::CART_KEY_Y, id as i32); + let (view, open) = self.lend_items(key, &mut items); + let result = f(self, &view); + self.reclaim_items(key, open, &mut items); + if let Some(c) = self.carts.iter_mut().find(|c| c.id == id) { + c.items = items; + } else if let Some(b) = self.boats.iter_mut().find(|b| b.id == id) { + b.items = items; + } + Some(result) + } + /// A vehicle's slots as a container under its placeholder `key`. When its + /// menu is open the placeholder is the live copy and stays as it is. + /// Returns the view and whether the menu was open. + fn lend_items(&mut self, key: Pos, items: &mut Vec>) -> (View, bool) { + let open = self.world.block_entities.contains_key(&key); + if !open { + let slots = BlockEntity::Chest(std::mem::take(items)); + self.world.block_entities.insert(key, slots); + } + let size = self + .world + .block_entities + .get(&key) + .map_or(0, |e| e.slots().len()); + let view = View { + parts: vec![(key, size)], + kind: Kind::Generic, + }; + (view, open) + } + /// Gives `lend_items`' slots back to the vehicle, so its own items stay + /// the one source of truth: a menu copy that is still around (open, or + /// left by a guest's close) gets the changed items copied to `items`. + fn reclaim_items(&mut self, key: Pos, open: bool, items: &mut Vec>) { + let slots = if open { + self.world.block_entities.get(&key).cloned() + } else { + self.world.block_entities.remove(&key) + }; + if let Some(BlockEntity::Chest(slots)) = slots { + *items = slots; + } } /// The item half of `HopperBlockEntity.suckInItems` (`getItemsAtAndAbove`): /// adds the item entities that meet the box, until one is taken whole. @@ -736,20 +867,7 @@ impl Game<'_> { // The cart's slots are edited as a container under the placeholder key // its menu uses; when that menu is open the placeholder is the live copy. let key = (i32::MAX - 15, super::minecarts::CART_KEY_Y, cart.id as i32); - let open = self.world.block_entities.contains_key(&key); - if !open { - let items = BlockEntity::Chest(std::mem::take(&mut cart.items)); - self.world.block_entities.insert(key, items); - } - let size = self - .world - .block_entities - .get(&key) - .map_or(0, |e| e.slots().len()); - let view = View { - parts: vec![(key, size)], - kind: Kind::Generic, - }; + let (view, open) = self.lend_items(key, &mut cart.items); let p = cart.position; // getLevelY is the cart's y + 0.5, so the container is the block at // y + 1.5, and Hopper.SUCK_AABB (moved by getLevel - 0.5) starts 11/16 @@ -759,7 +877,8 @@ impl Game<'_> { (p.y + 1.5).floor() as i32, p.z.floor() as i32, ); - let took = match self.suck_from_container(above, &view) { + let at = [p.x, p.y + 1.5, p.z]; + let took = match self.suck_from_container(above, at, &view) { Some(took) => took, None => self.suck_item_entities( &view, @@ -777,17 +896,7 @@ impl Game<'_> { [hi.x + 0.25, hi.y, hi.z + 0.25], ); } - // The cart's own items stay the one source of truth: a menu copy that - // is still around (open, or left by a guest's close) gets the pulled - // items back into `cart.items`, so reopening never restores a stale copy. - let slots = if open { - self.world.block_entities.get(&key).cloned() - } else { - self.world.block_entities.remove(&key) - }; - if let Some(BlockEntity::Chest(items)) = slots { - cart.items = items; - } + self.reclaim_items(key, open, &mut cart.items); } // ---- dispensers and droppers ----