Merge pull request #1848 from kacpherek/fix/1501-shulker-nesting

🤖 Agent: thank you! Merged after the contribution gate (checked against vanilla 26.3; one integration branch of main + all 14 of your PRs: fmt, clippy -D warnings, horizon-client/world tests, horizon-ctl scenarios on the server thread and in-frame, lan_e2e in both host modes, and the 30 harnesses covering these changes: 29 pass, the 30th is /help listing /pregen from #1805) and this head's verify.
This commit is contained in:
Ludovic Marie
2026-10-25 20:32:18 +01:00
committed by GitHub
3 changed files with 78 additions and 19 deletions
@@ -350,6 +350,14 @@ impl Hub {
return Vec::new();
}
let source = self.guests[i].menu.as_ref().map(|m| m.slots);
let shulker_box = self
.sync
.builtin
.get("minecraft:menu")
.and_then(|m| m.id("minecraft:shulker_box"));
let shulker = in_menu
&& shulker_box.is_some()
&& self.guests[i].menu.as_ref().map(|m| m.kind) == shulker_box;
let mut drag = if in_menu {
self.guests[i].menu.as_mut().and_then(|m| m.drag.take())
} else {
@@ -363,6 +371,7 @@ impl Hub {
let mut view = View {
guest,
container: items.as_deref_mut(),
shulker,
};
let (drops, changed) = menu::click(
&mut view,
+62 -13
View File
@@ -124,6 +124,8 @@ pub struct View<'a> {
pub guest: &'a mut Guest,
/// The container's slots, or None for the inventory menu.
pub container: Option<&'a mut [Option<ItemStack>]>,
/// The container is a shulker box (`ShulkerBoxSlot`).
pub shulker: bool,
}
impl View<'_> {
@@ -166,6 +168,12 @@ impl View<'_> {
if self.container.is_none() && (5..=8).contains(&i) {
return rules.armor_index(&stack.item) == Some(i - 5);
}
if self.shulker
&& self.container.as_ref().is_some_and(|c| i < c.len())
&& !crate::menu::fits_inside_container_items(stack)
{
return false;
}
self.enabled(i)
}
fn slot_max(&self, i: usize, stack: &ItemStack, rules: &dyn Rules) -> u32 {
@@ -678,6 +686,24 @@ mod tests {
)
}
/// The prepared resources' items, or None (the test is skipped) without them.
fn rules() -> Option<ItemRules> {
let root = std::env::var_os("HORIZON_RESOURCES")
.map(std::path::PathBuf::from)
.unwrap_or_else(|| {
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../../.local/horizon-client")
.join(horizon_core::VANILLA_VERSION)
});
if !root.join("items.json").exists() {
eprintln!("skipping resource-backed menu check: {}", root.display());
return None;
}
let registry = horizon_world::level::data::load_registry(&root).unwrap();
let facts = crate::blocks::BlockFacts::load(&root, &registry).unwrap();
Some(ItemRules(Items::load(&root, &registry, &facts).unwrap()))
}
fn contents(stack: &Option<ItemStack>) -> Vec<(String, u32)> {
let inside = stack.as_ref().and_then(|s| s.data());
inside.map_or(Vec::new(), |d| {
@@ -693,20 +719,9 @@ mod tests {
/// slot; a stack clicked onto a bundle goes in, as far as it fits.
#[test]
fn guest_chest_clicks_fill_and_empty_bundles() {
let root = std::env::var_os("HORIZON_RESOURCES")
.map(std::path::PathBuf::from)
.unwrap_or_else(|| {
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../../.local/horizon-client")
.join(horizon_core::VANILLA_VERSION)
});
if !root.join("items.json").exists() {
eprintln!("skipping resource-backed bundle check: {}", root.display());
let Some(rules) = rules() else {
return;
}
let registry = horizon_world::level::data::load_registry(&root).unwrap();
let facts = crate::blocks::BlockFacts::load(&root, &registry).unwrap();
let rules = ItemRules(Items::load(&root, &registry, &facts).unwrap());
};
let mut g = guest();
let mut chest = vec![
Some(ItemStack::new("minecraft:stone", 48)),
@@ -717,6 +732,7 @@ mod tests {
let mut view = View {
guest: &mut g,
container: Some(&mut chest),
shulker: false,
};
view.guest.carried = Some(ItemStack::new("minecraft:bundle", 1));
// Left click with the bundle: the stone goes in.
@@ -749,4 +765,37 @@ mod tests {
);
assert_eq!(contents(&view.get(2)), []);
}
#[test]
fn guest_shulker_box_menu_refuses_shulker_boxes() {
let Some(rules) = rules() else {
return;
};
for shulker in [true, false] {
let mut g = guest();
let mut container = vec![None; 27];
let mut drag = None;
let mut view = View {
guest: &mut g,
container: Some(&mut container),
shulker,
};
// Carried onto an empty slot, shift-clicked from the hotbar
// (menu slot 27 + 27) and swapped in with the number key 1.
view.guest.carried = Some(ItemStack::new("minecraft:red_shulker_box", 1));
click(&mut view, &mut drag, 0, 0, input::PICKUP, &rules);
assert_eq!(view.get(0).is_none(), shulker);
view.guest.carried = None;
view.guest.inventory[0] = Some(ItemStack::new("minecraft:shulker_box", 1));
click(&mut view, &mut drag, 54, 0, input::QUICK_MOVE, &rules);
assert_eq!(view.guest.inventory[0].is_some(), shulker);
view.guest.inventory[0] = Some(ItemStack::new("minecraft:blue_shulker_box", 1));
click(&mut view, &mut drag, 5, 0, input::SWAP, &rules);
assert_eq!(view.get(5).is_none(), shulker);
// Anything else goes in.
view.guest.carried = Some(ItemStack::new("minecraft:diamond", 1));
click(&mut view, &mut drag, 9, 0, input::PICKUP, &rules);
assert!(view.get(9).is_some());
}
}
}
+7 -6
View File
@@ -247,6 +247,12 @@ pub fn beacon_required_levels(effect: Option<&str>) -> i32 {
.map_or(i32::MAX, |i| i as i32 + 1)
}
/// `Item.canFitInsideContainerItems` (`ShulkerBoxSlot.mayPlace`): shulker
/// boxes cannot nest.
pub fn fits_inside_container_items(stack: &ItemStack) -> bool {
!stack.item.ends_with("shulker_box")
}
/// The containers a menu spans.
const PLAYER: usize = 0;
const MAIN: usize = 1;
@@ -1210,11 +1216,6 @@ impl Menu {
env.items.get(&stack.item).is_some_and(|i| i.fuel.is_some())
}
/// `Item.canFitInsideContainerItems`: shulker boxes cannot nest.
fn fits_inside(stack: &ItemStack) -> bool {
!(stack.item.ends_with("shulker_box"))
}
// ---- Slot ----
fn may_place(&self, env: &Env, s: usize, stack: &ItemStack) -> bool {
@@ -1247,7 +1248,7 @@ impl Menu {
.as_deref()
.is_some_and(|m| i.equip_allowed.as_ref().is_none_or(|a| a.contains(m)))
}),
Role::Shulker => Self::fits_inside(stack),
Role::Shulker => fits_inside_container_items(stack),
Role::Fuel => Self::is_fuel(env, stack) || Self::is_bucket(stack),
Role::Output | Role::CraftResult | Role::Inert => false,
Role::CrafterIn => self.data.get(self.slots[s].index) != Some(&1),