Skip to main content

steel_core/inventory/menu/kinds/
ender_chest_menu.rs

1//! Ender chest menu.
2
3use std::sync::Arc;
4
5use steel_utils::{DowncastType, DowncastTypeKey};
6
7use crate::inventory::ender_chest::{ENDER_CHEST_SLOTS, SyncPlayerEnderChest};
8use crate::inventory::menu::kinds::chest_menu::chest_with_kind;
9use crate::inventory::prelude::*;
10use crate::player::player_inventory::PlayerInventory;
11
12/// Builds the menu over a player's ender chest container.
13#[must_use]
14pub fn ender_chest(
15    inventory: Shared<PlayerInventory>,
16    container_id: u8,
17    container: SyncPlayerEnderChest,
18) -> Menu {
19    chest_with_kind(
20        inventory,
21        container_id,
22        Arc::clone(&container),
23        ENDER_CHEST_SLOTS / 9,
24        EnderChestKind { container },
25    )
26}
27
28/// Per-menu ender chest state: the player's container, which knows which block
29/// entity the menu was opened from.
30pub struct EnderChestKind {
31    container: SyncPlayerEnderChest,
32}
33
34// SAFETY: This Steel-owned key uniquely identifies the concrete menu kind
35// within the process.
36unsafe impl DowncastType for EnderChestKind {
37    const TYPE_KEY: DowncastTypeKey = DowncastTypeKey::new("steel:menu/ender_chest");
38}
39
40impl MenuKind for EnderChestKind {
41    fn still_valid(&self, _behavior: &MenuBehavior, player: &Player) -> bool {
42        self.container.lock().still_valid(player)
43    }
44
45    /// `PlayerEnderChestContainer.stopOpen`
46    /// drops the active chest so a later validity check can't consult a stale one.
47    fn removed(&mut self, _behavior: &mut MenuBehavior, _player: &Player) {
48        self.container.lock().clear_active_chest();
49    }
50}