Add double chests, shulker box and shift-click container menus

Double chests now open as one 54-slot Large Chest (right half first, as in vanilla) with the 222-pixel screen and the inventory moved down, and both halves update their comparators and saves. Shulker boxes use their own texture and "Shulker Box" title, ender chests and barrels show their names, and the hopper minecart is titled as such.

Shift-click now moves stacks between the player inventory and chests, barrels, shulker boxes (which refuse other shulker boxes), ender chests, hoppers, dispensers and droppers the way the vanilla menus do, and in furnaces it sends smeltable items to the input, fuel to the fuel slot and takes results to the inventory.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-09 13:27:13 +02:00
parent cdd5feb50c
commit 84dbcacd22
5 changed files with 350 additions and 55 deletions
+14 -49
View File
@@ -3050,7 +3050,7 @@ impl Game<'_> {
}
let mut v: Vec<usize> = (0..36).collect();
match self.container_kind() {
Some(BlockEntity::Chest(items)) => v.extend((0..items.len()).map(|i| 300 + i)),
Some(BlockEntity::Chest(_)) => v.extend((0..self.container_size()).map(|i| 300 + i)),
Some(BlockEntity::Furnace(_)) => v.extend(300..303),
Some(BlockEntity::Brewing(_)) => v.extend(300..305),
Some(BlockEntity::Crafter(_)) => v.extend(300..310),
@@ -3083,10 +3083,11 @@ impl Game<'_> {
}
let table = self.craft_size == 3;
// ContainerScreen (3 rows) shifts the player inventory down by one pixel.
let shift = if matches!(self.container_kind(), Some(BlockEntity::Chest(_))) {
1.0
} else {
0.0
// ChestMenu: the inventory sits `18 * (rows - 3) + 1` lower than the
// 3-row layout of the other menus.
let shift = match self.chest_rows() {
Some(rows) => 1.0 + (rows as f32 - 3.0) * 18.0,
None => 0.0,
};
match i {
0..9 => (8.0 + i as f32 * 18.0, 142.0 + shift),
@@ -3145,14 +3146,7 @@ impl Game<'_> {
return &mut self.offhand;
}
if i >= 300 {
let key = self.container;
return match key.and_then(|k| self.world.block_entities.get_mut(&k)) {
Some(e) => e
.slots_mut()
.get_mut(i - 300)
.unwrap_or(&mut self.scratch_slot),
None => &mut self.scratch_slot,
};
return self.container_slot_mut(i - 300);
}
if (100..109).contains(&i) {
&mut self.craft_grid[i - 100]
@@ -3211,42 +3205,7 @@ impl Game<'_> {
.and_then(|k| self.world.block_entities.get(&k))
{
Some(BlockEntity::Chest(_)) if self.draw_machine_screen(batch, ox, oy) => {}
Some(BlockEntity::Chest(_)) => {
// ContainerScreen with 3 rows: top 71px, then the player inventory from v=126.
part(
batch,
"gui/container/generic_54",
0.0,
0.0,
176.0,
71.0,
ox,
oy,
);
part(
batch,
"gui/container/generic_54",
0.0,
126.0,
176.0,
96.0,
ox,
oy + 71.0,
);
let name = self
.container
.map(|k| self.world.block(k.0, k.1, k.2))
.map(|s| self.res.facts.get(s).name.clone());
let title = if self.open_cart_kind().is_some() {
"Minecart with Chest"
} else if name.as_deref() == Some("minecraft:barrel") {
"Barrel"
} else {
"Chest"
};
batch.text(atlas, title, ox + 8.0, oy + 6.0, grey, false);
batch.text(atlas, "Inventory", ox + 8.0, oy + 73.0, grey, false);
}
Some(BlockEntity::Chest(_)) => self.draw_chest_screen(batch, ox, oy),
Some(BlockEntity::Furnace(f)) => {
let (tex, title) = match f.kind {
horizon_client::containers::FurnaceKind::Furnace => {
@@ -3453,6 +3412,12 @@ impl Game<'_> {
if self.crafter_click(slot, held_shift) {
return;
}
if held_shift && self.container.is_some() && self.container_quick_move(slot) {
if let Some(key) = self.container {
self.world.modified.insert((key.0 >> 4, key.2 >> 4));
}
return;
}
if let Some(c) = &self.carried
&& self.refuses(slot, c)
{
+23 -4
View File
@@ -322,10 +322,16 @@ impl Game<'_> {
if pos.1 == super::minecarts::CART_KEY_Y {
return;
}
self.world.modified.insert((pos.0 >> 4, pos.2 >> 4));
self.with_level(|b, level, ticks| {
b.container_changed(level, ticks, BlockPos::new(pos.0, pos.1, pos.2))
});
// A double chest's other half changes with it.
let halves: Vec<Pos> = std::iter::once(pos)
.chain(self.chest_other_half(pos))
.collect();
for pos in halves {
self.world.modified.insert((pos.0 >> 4, pos.2 >> 4));
self.with_level(|b, level, ticks| {
b.container_changed(level, ticks, BlockPos::new(pos.0, pos.1, pos.2))
});
}
}
/// `HopperBlockEntity.tryMoveInItem`.
fn try_move_in_item(
@@ -1056,6 +1062,13 @@ impl Game<'_> {
{
return super::beacon::HEIGHT;
}
if let Some(rows) = self.chest_rows() {
// ContainerScreen: 114 + 18 per row.
return 114.0 + rows as f32 * 18.0;
}
if self.container_block_is_shulker() {
return 167.0;
}
if self.machine_screen() == Some(Screen::Hopper) {
133.0
} else {
@@ -1091,7 +1104,13 @@ impl Game<'_> {
return false;
};
let atlas = &self.res.atlas;
let key_is_cart = self
.container
.is_some_and(|k| k.1 == super::minecarts::CART_KEY_Y);
let (texture, height, title) = match screen {
Screen::Hopper if key_is_cart => {
("gui/container/hopper", 133.0, "Minecart with Hopper")
}
Screen::Hopper => ("gui/container/hopper", 133.0, "Item Hopper"),
Screen::Dispenser => ("gui/container/dispenser", 166.0, "Dispenser"),
Screen::Dropper => ("gui/container/dispenser", 166.0, "Dropper"),
@@ -313,7 +313,7 @@ impl Game<'_> {
let c = self.container.and_then(|k| self.crafter_at(k))?;
let i = slot.checked_sub(300).filter(|i| *i < 9)?;
(self.carried.is_none() && c.items[i].is_none() && !c.disabled[i] && !self.spectator())
.then(|| self.translate("gui.togglable_slot", "Click to toggle"))
.then(|| self.translate("gui.togglable_slot", "Click to disable slot"))
}
/// A click in the crafter menu before the generic handling; true when
/// it was fully handled (the result, shift-clicks).
+5 -1
View File
@@ -492,7 +492,11 @@ impl Game<'_> {
// CrafterSlot.mayPlace: not a disabled slot.
return (!c.disabled[slot - 300], 64);
}
if (300..305).contains(&slot) {
if (300..400).contains(&slot) && !self.brewing_open() && self.refuses(slot, stack) {
// ShulkerBoxSlot.mayPlace.
return (false, 64);
}
if self.brewing_open() && (300..305).contains(&slot) {
return match slot - 300 {
0..=2 => (data.brewing.is_bottle(stack), 1),
3 => (data.brewing.is_reagent(stack), 64),
+307
View File
@@ -83,3 +83,310 @@ impl Game<'_> {
}
}
}
// ---- chest menus: double chests, titles and shift-click ----
/// `Direction.getClockWise` for horizontal facings.
fn clockwise(facing: &str) -> &'static str {
match facing {
"north" => "east",
"east" => "south",
"south" => "west",
_ => "north",
}
}
fn counter_clockwise(facing: &str) -> &'static str {
match clockwise(facing) {
"east" => "west",
"south" => "north",
"west" => "east",
_ => "south",
}
}
fn facing_step(facing: &str) -> (i32, i32) {
match facing {
"north" => (0, -1),
"south" => (0, 1),
"west" => (-1, 0),
_ => (1, 0),
}
}
impl Game<'_> {
/// The other half of the open double chest and whether the open one is
/// the right half (`ChestBlock.getConnectedDirection`).
fn chest_partner(&self, key: (i32, i32, i32)) -> Option<((i32, i32, i32), bool)> {
let registry = self.res.assets.registry;
let state = registry.state(self.world.block(key.0, key.1, key.2))?;
if !matches!(
state.name.as_str(),
"minecraft:chest" | "minecraft:trapped_chest"
) {
return None;
}
let kind = state.properties.get("type")?.as_str();
if kind == "single" {
return None;
}
let facing = state.properties.get("facing")?.as_str();
let dir = if kind == "left" {
clockwise(facing)
} else {
counter_clockwise(facing)
};
let (dx, dz) = facing_step(dir);
let other = (key.0 + dx, key.1, key.2 + dz);
let other_state = registry.state(self.world.block(other.0, other.1, other.2))?;
let other_kind = other_state.properties.get("type")?.as_str();
let paired = other_state.name == state.name
&& other_kind != "single"
&& other_kind != kind
&& matches!(
self.world.block_entities.get(&other),
Some(BlockEntity::Chest(items)) if items.len() == 27
);
paired.then_some((other, kind == "right"))
}
/// The other half of a double chest at `key`, for neighbour updates.
pub(super) fn chest_other_half(&self, key: (i32, i32, i32)) -> Option<(i32, i32, i32)> {
self.chest_partner(key).map(|p| p.0)
}
/// The block entities of the open container in menu order, with their
/// sizes: a double chest is its right half then its left half.
pub(super) fn container_parts(&self) -> Vec<((i32, i32, i32), usize)> {
let Some(key) = self.container else {
return Vec::new();
};
let Some(entity) = self.world.block_entities.get(&key) else {
return Vec::new();
};
let own = (key, entity.slots().len());
match self.chest_partner(key) {
Some((other, true)) => vec![own, (other, 27)],
Some((other, false)) => vec![(other, 27), own],
None => vec![own],
}
}
/// The number of slots of the open container.
pub(super) fn container_size(&self) -> usize {
self.container_parts().iter().map(|p| p.1).sum()
}
/// Container slot `n` (menu slot `300 + n`).
pub(super) fn container_slot_mut(&mut self, mut n: usize) -> &mut Option<ItemStack> {
let mut target = None;
for (pos, size) in self.container_parts() {
if n < size {
target = Some((pos, n));
break;
}
n -= size;
}
match target.and_then(|(pos, i)| {
self.world
.block_entities
.get_mut(&pos)
.and_then(|e| e.slots_mut().get_mut(i))
}) {
Some(slot) => slot,
None => &mut self.scratch_slot,
}
}
pub(super) fn container_block_is_shulker(&self) -> bool {
self.container_block()
.as_deref()
.is_some_and(is_shulker_box)
}
fn container_block(&self) -> Option<std::sync::Arc<str>> {
let k = self.container?;
Some(
self.res
.facts
.get(self.world.block(k.0, k.1, k.2))
.name
.clone(),
)
}
/// Rows of the open generic 9-wide container (`ChestMenu`), or None for
/// the other menus (hoppers, dispensers, shulker boxes, furnaces...).
pub(super) fn chest_rows(&self) -> Option<usize> {
if !matches!(self.container_kind()?, BlockEntity::Chest(_)) {
return None;
}
let generic = match self.container_block().as_deref() {
Some(
"minecraft:chest"
| "minecraft:trapped_chest"
| "minecraft:barrel"
| "minecraft:ender_chest",
) => true,
_ => self.open_cart_kind() == Some(super::minecarts::CartKind::Chest),
};
generic.then(|| self.container_size() / 9)
}
/// The title of the open chest-like menu.
fn chest_title(&self, rows: usize) -> String {
let (key, fallback) = match self.container_block().as_deref() {
Some("minecraft:barrel") => ("container.barrel", "Barrel"),
Some("minecraft:ender_chest") => ("container.enderchest", "Ender Chest"),
_ if self.open_cart_kind().is_some() => {
("entity.minecraft.chest_minecart", "Minecart with Chest")
}
_ if rows > 3 => ("container.chestDouble", "Large Chest"),
_ => ("container.chest", "Chest"),
};
self.translate(key, fallback)
}
/// `ContainerScreen` / `ShulkerBoxScreen`: the background with its
/// container rows, the title and the inventory label.
pub(super) fn draw_chest_screen(
&self,
batch: &mut horizon_client::hud::HudBatch,
ox: f32,
oy: f32,
) {
use horizon_client::hud::rgba;
let atlas = &self.res.atlas;
let grey = rgba(64, 64, 64, 255);
if self
.container_block()
.as_deref()
.is_some_and(is_shulker_box)
{
batch.sprite_region(
atlas,
"gui/container/shulker_box",
[ox, oy, 176.0, 167.0],
256.0,
256.0,
);
let title = self.translate("container.shulkerBox", "Shulker Box");
batch.text(atlas, &title, ox + 8.0, oy + 6.0, grey, false);
batch.text(atlas, "Inventory", ox + 8.0, oy + 73.0, grey, false);
return;
}
let rows = self.chest_rows().unwrap_or(3);
let top = rows as f32 * 18.0 + 17.0;
batch.sprite_region(
atlas,
"gui/container/generic_54",
[ox, oy, 176.0, top],
256.0,
256.0,
);
// The player half of the texture starts at v = 126.
if let Some(mut r) = atlas.sprites.get("gui/container/generic_54").copied() {
let (du, dv) = ((r.u1 - r.u0) / r.w as f32, (r.v1 - r.v0) / r.h as f32);
r.v0 += 126.0 * dv;
r.v1 = r.v0 + 96.0 * dv;
r.u1 = r.u0 + 176.0 * du;
batch.rect_uv(ox, oy + top, 176.0, 96.0, r, u32::MAX);
}
batch.text(
atlas,
&self.chest_title(rows),
ox + 8.0,
oy + 6.0,
grey,
false,
);
batch.text(
atlas,
"Inventory",
ox + 8.0,
oy + 114.0 + rows as f32 * 18.0 - 94.0,
grey,
false,
);
}
}
impl Game<'_> {
/// `quickMoveStack` of the open chest-like, hopper, dispenser or furnace
/// menu for a shift-click on `slot`; false when the menu has none.
pub(super) fn container_quick_move(&mut self, slot: usize) -> bool {
let size = self.container_size();
let furnace = match self.container_kind() {
Some(BlockEntity::Chest(_)) => None,
Some(BlockEntity::Furnace(f)) => Some(f.kind),
_ => return false,
};
if slot >= 300 + size.max(3) {
return false;
}
let Some(mut stack) = self.slot_mut(slot).take() else {
return true;
};
let player: Vec<usize> = (9..36).chain(0..9).collect();
let hotbar_or_main = |slot: usize| -> Vec<usize> {
if slot < 9 {
(9..36).collect()
} else {
(0..9).collect()
}
};
match furnace {
None => {
if slot >= 300 {
self.move_to(&mut stack, &player, true);
} else {
let targets: Vec<usize> = (300..300 + size).collect();
self.move_to(&mut stack, &targets, false);
}
}
Some(kind) => {
// AbstractFurnaceMenu.quickMoveStack.
if slot >= 300 {
self.move_to(&mut stack, &player, slot == 302);
} else {
let smeltable = self.res.recipes.cook(kind.recipe_type(), &stack).is_some();
let fuel = self
.res
.items
.get(&stack.item)
.is_some_and(|i| i.fuel.is_some());
let moved = if smeltable {
self.move_to(&mut stack, &[300], false)
} else if fuel {
self.move_to(&mut stack, &[301], false)
} else {
false
};
if !moved && !smeltable && !fuel {
let other = hotbar_or_main(slot);
self.move_to(&mut stack, &other, false);
}
if let Some(BlockEntity::Furnace(f)) = self
.container
.and_then(|k| self.world.block_entities.get_mut(&k))
{
f.cooking_timer = 0;
}
}
}
}
if stack.count > 0 {
*self.slot_mut(slot) = Some(stack);
}
true
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn double_chest_halves_connect_around_the_facing() {
// A left half facing north connects east, a right half west.
assert_eq!(facing_step(clockwise("north")), (1, 0));
assert_eq!(facing_step(counter_clockwise("north")), (-1, 0));
assert_eq!(facing_step(clockwise("south")), (-1, 0));
assert_eq!(facing_step(counter_clockwise("east")), (0, -1));
}
#[test]
fn shulker_boxes_are_recognised_by_name() {
assert!(is_shulker_box("minecraft:red_shulker_box"));
assert!(is_shulker_box("minecraft:shulker_box"));
assert!(!is_shulker_box("minecraft:chest"));
}
}