Inventory: lock a slot, deny a request, open a menu
This tutorial shows how a plugin locks a slot of the player inventory, denies an inventory request of a player and opens a menu of its own. Most of the code comes from the example crate examples/inventory_minigame, and we did not run it outside the tests of the repository. The example runs in one world, while a server keeps the inventory in each dimension world, so read the section about the example before you register a plugin.
Every inventory packet becomes one InventoryInput
Section titled “Every inventory packet becomes one InventoryInput”The packet edge of the inventory, InventoryServerPlugin, has one observer for each of the five inventory packets of a client: a click, a close, a hand action, a creative set and a change of the hotbar slot. Each observer writes one InventoryInput message.1
pub enum InventoryInput { Click { player: Entity, container_id: i32, state_id: i32, click: Click, changed: Vec<ClaimedSlot>, carried: SlotView, }, Close { player: Entity, }, Hand { player: Entity, action: HandAction, }, CreativeSet { player: Entity, slot: i16, stack: Option<ItemStack>, }, SelectSlot { player: Entity, slot: u16, },}crates/mcrs_minecraft_inventory_server/src/inventory_input.rs:12-37
The system apply_inventory_input of the same crate applies these messages in the client input phase of the tick, in the set InventorySet::Input of VanillaInventoryPlugin. A click becomes a Request, a list of slot operations with an id, which carries the request source of the vanilla plugin. A hand action goes to a command of the vanilla plugin and becomes a request there. A pick-up click on a button entry of a menu moves no stack and writes EntryPressed instead.2
A request meets two checks, and each is a place where your plugin can say no. First the engine raises RequestCheck on the player, and an observer can deny the request. Then it plans the operations against the SlotPolicy of each holder, the entity that owns the slots, and one refused take or place rejects the whole request. Either way no slot changes. At the end the engine writes and triggers RequestResult, whose outcome is Ok(()), Refusal::Denied or Refusal::Rejected.
The close request, which moves the stacks of the cursor and the crafting grid back when a menu closes, skips RequestCheck, and take gates do not apply to it.
Stacks at the packet boundary
Section titled “Stacks at the packet boundary”The requests use ItemStack from mcrs_minecraft_item_stack. Count in mcrs_minecraft_item_component holds 1 to 99; an empty slot is None. ItemTemplate holds the item, count and patch, and ItemStack::normalize(template, prototypes) removes patch entries that do not change the prototype. Read effective components through StackWithTable, which implements StackView over the stack and item table. A component absent from both patch and prototype stays absent for a presence predicate.3 The component crate also owns Kind, component values and component type ids. The old ProtoStack model still exists in mcrs_minecraft_item; RFD 8 records the remaining migration.
WireStack and WireStackWithLength find a stack’s byte bounds without registries; their read methods take names and return Result<Option<ItemTemplate>, WireError>. The creative-slot packet uses WireStackWithLength and checks each component’s declared length. A decode failure closes the connection, while a semantic read refusal, such as count 100, drops that creative request.4 Container clicks carry registry-free HashedStack values. An unknown item id there reaches stack matching and causes a slot resync instead of a packet decode rejection.5
Lock a slot with the slot policy
Section titled “Lock a slot with the slot policy”A SlotPolicy component on a holder gives each slot a SlotAccess: the largest count the slot holds (limit), a gate for a take and a gate for a place. A Gate permits, refuses or asks a function. PlayerHoldersPlugin, which VanillaInventoryPlugin adds, gives each new Player its holders with their policies. The armour slots ask the armour gates, and the crafting result refuses a place.6
The arena plugin of the example locks slot 8, the last slot of the hotbar, while the player is in its game. Both gates of the locked access refuse.
const LOCKED_SLOT: u16 = 8;
const LOCKED: SlotAccess = SlotAccess { limit: 99, take: Gate::Refuses, place: Gate::Refuses,};examples/inventory_minigame/src/arena_rules_plugin.rs:25-31
fn lock_slot(enter: On<EnterGame>, mut players: Query<&mut SlotPolicy>, mut commands: Commands) { let player = enter.entity; let Ok(mut policy) = players.get_mut(player) else { return; }; let unlocked = policy.access(LOCKED_SLOT); if unlocked == LOCKED { return; } policy.set(LOCKED_SLOT, LOCKED); commands.entity(player).insert(UnlockedAccess(unlocked));}examples/inventory_minigame/src/arena_rules_plugin.rs:68-79
lock_slot observes EnterGame, an event of the example. It keeps the old access in UnlockedAccess, and unlock_slot puts it back at LeaveGame.
To decide for each stack, register a function with register_slot_gate of ItemAppExt and put the returned id into Gate::Ask(id). The function is fn(&World, &GateQuery) -> Filter, with Filter from mcrs_minecraft_inventory_transaction. It reads the world without changing it, and GateQuery names the slot, the acting player and the stack. A take gate is asked only when the slot holds a stack, and an unknown id refuses. The armour take gate in armour_take.rs works this way: it refuses a stack with an enchantment that locks armour, unless the player is in creative mode.7
The policy guards requests only. give and give_or_drop of HolderCommandsExt make no request, so they ignore the policy and can put a stack into a locked slot.
Deny a request with RequestCheck
Section titled “Deny a request with RequestCheck”In Paper you cancel an InventoryClickEvent. Here you write an observer of RequestCheck, an entity event that the engine raises on the player before it plans a request. check.request() returns the request with its id, its kind and its operations, and check.deny(denial) denies it. The first denial counts, and a later deny changes nothing.6
fn veto_frozen( mut check: On<RequestCheck>, frozen: Query<(), With<Frozen>>, rules: Res<ArenaRules>,) { if frozen.contains(check.actor) { check.deny(Denial { source: rules.source, code: FROZEN_CODE, }); }}examples/inventory_minigame/src/arena_rules_plugin.rs:94-105
A Denial has a code that your plugin chooses and the source of your plugin. A request source is an opaque value that the world hands out to each plugin that asks, and app.allocate_request_source() of ItemAppExt is the only way to get one. It fails with RegisterError::SourcesExhausted when no source is left. Your plugin puts its source into each Denial and into the RequestId { source, serial } of each request it makes, so a reader of RequestResult can tell its results from the others. The vanilla plugin keeps its own source in VanillaInventoryRequests and launches the drops of a request only when the request carries that source.7
impl Plugin for ArenaRulesPlugin { fn build(&self, app: &mut App) { let Ok(source) = app.allocate_request_source() else { return; }; app.insert_resource(ArenaRules { source }) .add_observer(lock_slot) .add_observer(unlock_slot) .add_observer(veto_frozen) .add_observer(count_denials); }}examples/inventory_minigame/src/arena_rules_plugin.rs:44-55
The plugin allocates its source in build, keeps it in the resource ArenaRules and adds nothing when no source is left. Its observer of RequestResult counts the denials with its own source in Frozen::denied.
Open a menu of your own
Section titled “Open a menu of your own”A menu is data. MenuLayout in mcrs_minecraft_inventory_menu, a crate without Bevy, lists the entries of a menu, and each entry points to a slot of one part. MenuType comes from mcrs_minecraft_menu_key::menu_keys. MenuLayout::of(MenuType::Generic9x3) returns Some(layout) for a chest with three rows: 27 entries of part 0 (GENERIC_CONTAINER), then the 36 player slots as part 1 (GENERIC_PLAYER). MenuLayout::new(kind, entries, routes) builds any other layout, where a route says where a shift-click moves a stack. It returns None above 128 entries or for a route outside the menu.2
MenuCommandsExt opens a menu on the player with open_menu or open_titled_menu. Both take the layout, the holders of the parts in MenuParts and the button entries in MenuButtons. The shop of the example opens over a new holder with 27 slots and one offer.
fn open_shop(open: On<OpenShop>, shop: Res<Shop>, mut commands: Commands) { let player = open.entity; let holder = commands .spawn(( Slots::new(usize::from(SHOP_SIZE)), SlotPolicy::locked(), ShopHolder, )) .id(); let mut buttons = MenuButtons::default(); for offer in &shop.offers { commands .spawn_stack(offer.stack.clone()) .put_at(holder, offer.entry); buttons = buttons.with(offer.entry); } let Some(parts) = MenuParts::new([holder, player]) else { return; }; commands.entity(player).open_titled_menu( shop.layout.clone(), parts, buttons, MenuTitle(Text::text("Shop")), );}examples/inventory_minigame/src/shop_plugin.rs:83-108
The holder of the shop has SlotPolicy::locked(). A click on a button entry moves no stack, and a pick-up click writes the message EntryPressed { viewer, menu, entry, button }. Other clicks can still reach the shop slots, such as a shift-click from the player slots, and the locked policy refuses them. The shop system give_offers reads EntryPressed and gives the player the stack of the pressed offer. On a server, the packet edge sends the menu with its type and title to the client.
close_menu closes the open menu, and opening a menu first closes any menu above the inventory. Each open and close triggers MenuOpened or MenuClosed on the player and writes it as a message. A menu whose holder loses its Slots closes by itself, and the shop despawns its holder when its menu goes away.
The example runs in one world, a server in many
Section titled “The example runs in one world, a server in many”examples/inventory_minigame is one headless app. Its main.rs adds ItemPlugin, VanillaInventoryPlugin, KitPlugin, ArenaRulesPlugin and ShopPlugin to one App with add_plugins. The crate links no packet edge, so there is no client and no packet. The integration test stands in for them. It spawns a Player, triggers EnterGame and OpenShop, sends requests with HolderCommandsExt::request and writes EntryPressed by hand.
A take from the locked slot fails with Reason::TakeDenied, and a take from slot 10 commits. A frozen player gets Refusal::Denied with the source of the arena plugin, which differs from the vanilla source. Pressing the offer gives the stack, and the shop holder goes away with the shop menu. The README of the example lists the commands that run the app and the test.
A server has no such single world. Its plugin VanillaItemEntityPlugin adds a dimension builder that puts the item engine, VanillaInventoryPlugin and the packet edge into every dimension world.
fn build_dimension_items(host: &World, _: &DimensionBuild<'_>, sub_app: &mut SubApp) { let Some(table) = host.get_resource::<ItemTable>().copied() else { tracing::warn!("the host has no item table; the dimension world has no item entities"); return; }; add_entity_motion(sub_app); sub_app.insert_resource(table); sub_app.add_plugins(( ItemPlugin::default(), ItemEntityPlugin, VanillaInventoryPlugin, InventoryServerPlugin, ));crates/mcrs_minecraft_server/src/item_entities.rs:32-44
So the players, their slot policies, RequestCheck and EntryPressed live in the dimension world of each player. Register your plugin with app.add_dimension_plugin(ArenaRulesPlugin). Every dimension world builds a clone of it after the dimension builders, so its build allocates a source and adds the observers in that world. The plugin must be Clone, as both plugins of the example are. With add_plugins it lands in the main app, where its observers never see a player. Trigger your own events, such as EnterGame, in the world of the player too. Dimensions are separate worlds explains add_dimension_plugin. No test of the repository runs these plugins in a server yet.
Inventory on death
Section titled “Inventory on death”VanillaDeathDropsPlugin reads Died and drops the main inventory, then the off hand and armour from feet to head. Stacks with a vanishing enchantment effect are deleted. keep_inventory or spectator mode preserves these slots. The plugin reads the dimension’s current game rules; disable it to replace this policy.8
Cursor and crafting-grid stacks drop later, when the dead player is hidden after the death delay, leaves, or respawns, whichever happens first. This also happens with keep_inventory enabled. These drops do not go through an inventory click request, so RequestCheck is not their cancellation hook. Handle damage, death and respawn covers the death and respawn messages.
Hand drops and arm swings
Section titled “Hand drops and arm swings”A successful vanilla hand-drop request emits SwingPlayer for the main hand, with the default whack animation and a base duration of 6 ticks. Tracking observers receive the swing packet; late viewers receive no replay.9 The player guide explains the mcrs client’s current limitation. Item stacks also have typed AttackAnimation, InteractAnimation and PiercingWeapon components. Their codecs and predicates work without an engine; the swing cooldown kernel is mcrs_minecraft_swing.10
The client and what is left
Section titled “The client and what is left”The mcrs client takes clicks in its inventory screen.11 It draws the eight generic container screens with item tooltips, drag previews and double clicks, so a player sees a menu your plugin opens.12 In creative mode the inventory key opens the creative screen with tabs, scrolling, search and stack clicks.13 The remaining screen limits are on The client today. The player inventory issue still covers agreement with a vanilla client and persistence across a rejoin.14
Container and creative screens are entities on ScreenStack from mcrs_minecraft_gui_screen; ScreenEntry controls how a new screen replaces existing ones. The client today describes inventory screen changes when the creative ability changes.15
Next: Dimensions are separate worlds.
Footnotes
Section titled “Footnotes”-
Works#767 Adds the vanilla inventory plugin and the
InventoryInputmessage. ↩ -
Works#977 Describes a menu as data and opens it with button entries over any holder. ↩ ↩2
-
Works#1186 Adds prototype-aware stacks and correct component-presence predicates. ↩
-
Works#1137 Checks the creative-slot wire frame before semantic reading. ↩
-
Works#1135 Moves container-click hashes to the registry-free stack type. ↩
-
Works#846 Adds the slot policy for the player inventory and
RequestCheck, where an observer can deny an inventory request. ↩ ↩2 -
Works#988 Gives each plugin a request source of its own and adds the take rule of the armour slots. ↩ ↩2
-
Works#1084 Adds death drops, including cursor and crafting-grid stacks, and shares
EnchantmentEffectswith the armour take rule. ↩ -
Works#1064 Replicates player swings for hand drops, attacks and accepted placements. ↩
-
Works#1063 Adds typed item animation inputs and the pure swing kernel. ↩
-
Works#129 Tracks the clicks of the mcrs client, which Works#1004 finished for the inventory screen. ↩
-
Works#130 Tracks the container screens of the mcrs client. ↩
-
Works#131 Tracks the creative inventory screen of the mcrs client. ↩
-
Partial#521 Tracks the specification and the plugin of the player inventory. ↩
Sources and verification
Verified: 26.4-snapshot-7, 1cf84286c, 2026-11-30
- crates/mcrs_minecraft_inventory_server/src/inventory_input.rs
- crates/mcrs_minecraft_inventory_server/src/inventory_server_plugin.rs
- crates/mcrs_minecraft_inventory_server/src/apply_inventory_input.rs
- crates/mcrs_minecraft_inventory_server/src/apply_click.rs
- crates/mcrs_minecraft_inventory_server/src/open_menu_sync.rs
- crates/mcrs_minecraft_inventory_server/tests/it/menu.rs
- crates/mcrs_minecraft_vanilla_inventory/src/vanilla_inventory_plugin.rs
- crates/mcrs_minecraft_vanilla_inventory/src/apply_close.rs
- crates/mcrs_minecraft_vanilla_inventory/src/launch_drops.rs
- crates/mcrs_minecraft_engine_item/src/commit.rs
- crates/mcrs_minecraft_engine_item/src/give.rs
- crates/mcrs_minecraft_engine_item/src/slot_policy.rs
- crates/mcrs_minecraft_engine_item/src/slot_access.rs
- crates/mcrs_minecraft_engine_item/src/gate.rs
- crates/mcrs_minecraft_engine_item/src/item_app_ext.rs
- crates/mcrs_minecraft_engine_item/src/request_check.rs
- crates/mcrs_minecraft_engine_item/src/request_source.rs
- crates/mcrs_minecraft_engine_item/src/request_sources.rs
- crates/mcrs_minecraft_engine_item/src/request_result.rs
- crates/mcrs_minecraft_player_holders/src/player_holders_plugin.rs
- crates/mcrs_minecraft_player_holders/src/player_policy.rs
- crates/mcrs_minecraft_player_holders/src/restore_holders.rs
- crates/mcrs_minecraft_player_holders/src/armour_take.rs
- crates/mcrs_minecraft_inventory_menu/src/menu_layout.rs
- crates/mcrs_minecraft_engine_inventory_menu/src/menu_commands_ext.rs
- crates/mcrs_minecraft_engine_inventory_menu/src/entry_pressed.rs
- crates/mcrs_minecraft_engine_inventory_menu/src/close_on_holder_removal.rs
- crates/mcrs_minecraft_server/src/item_entities.rs
- crates/mcrs_minecraft_engine_host/src/sub_app_builder.rs
- examples/inventory_minigame/README.md
- examples/inventory_minigame/src/main.rs
- examples/inventory_minigame/src/arena_rules_plugin.rs
- examples/inventory_minigame/src/shop_plugin.rs
- examples/inventory_minigame/tests/it/arena_rules.rs
- examples/inventory_minigame/tests/it/shop.rs
- PR #767
- PR #846
- PR #977
- PR #988
- PR #1004
- issue #129
- issue #130
- issue #131
- issue #521
- crates/mcrs_minecraft_menu_key/src/menu_keys.rs
- crates/mcrs_minecraft_client/tests/it/screen_clicks.rs
- crates/mcrs_minecraft_client/tests/it/creative_screen_clicks.rs
- PR #1016
- PR #1025
- PR #1024
- PR #1046
- crates/mcrs_minecraft_item_component/src/attack_animation.rs
- crates/mcrs_minecraft_item_component/src/interact_animation.rs
- crates/mcrs_minecraft_item_component/src/piercing_weapon.rs
- crates/mcrs_minecraft_vanilla_inventory/src/drop_swing.rs
- crates/mcrs_minecraft_vanilla_death/src/drop_inventory.rs
- crates/mcrs_minecraft_vanilla_death/src/drop_held_stacks.rs
- crates/mcrs_minecraft_player_holders/src/enchantment_effects.rs
- crates/mcrs_minecraft_server/tests/it/death_drops.rs
- PR #1063
- PR #1064
- PR #1084
- crates/mcrs_minecraft_swing/src/lib.rs
- crates/mcrs_minecraft_predicate/src/component_holder.rs
- crates/mcrs_minecraft_replication/src/entity_codec.rs
- crates/mcrs_minecraft_engine_player/src/swing.rs
- crates/mcrs_minecraft_item_stack/src/saved.rs
- crates/mcrs_minecraft_item_stack/src/wire_value.rs
- crates/mcrs_minecraft_item_component/src/count.rs
- crates/mcrs_minecraft_item_component/src/kind.rs
- crates/mcrs_minecraft_item_component/src/lib.rs
- crates/mcrs_minecraft_item/src/stack.rs
- docs/rfd/0008-item-stack-model.md
- crates/mcrs_minecraft_item_component/src/item_template.rs
- crates/mcrs_minecraft_item_stack/src/item_stack.rs
- crates/mcrs_minecraft_item_stack/src/stack_with_table.rs
- crates/mcrs_minecraft_item_stack/src/wire_stack.rs
- crates/mcrs_minecraft_item_stack/src/wire_stack_with_length.rs
- crates/mcrs_minecraft_item_stack/src/hashed_stack.rs
- crates/mcrs_minecraft_inventory_server/src/set_creative_mode_slot.rs
- crates/mcrs_minecraft_protocol_game/src/serverbound.rs
- crates/mcrs_minecraft_protocol_game/tests/it/item/wire_frame.rs
- crates/mcrs_minecraft_gui_screen/src/screen_entry.rs
- crates/mcrs_minecraft_client_gui/src/creative_screen_state.rs
- crates/mcrs_minecraft_client_gui/src/menu_screen_kind.rs
- crates/mcrs_minecraft_client_gui/src/inventory_clicks.rs
- crates/mcrs_minecraft_client_gui/src/click_state.rs
- crates/mcrs_minecraft_inventory_client_view/src/slot_view.rs
- PR #1135
- PR #1137
- PR #1186
- PR #1187
Related open issues:
- #115 WIP Other players see held items and armour
- #521 Partial Player inventory: specification and plugin
- #660 WIP Edition-neutral inventory transactions
- #895 WIP Inventory specification: run the experiments and reach the status ready
- #896 WIP Inventory transactions specification: run the server experiments
- #981 WIP Keep the open menu of a player across a cell crossing
- #1007 WIP Creative hotbar row: pick up all gathers only from the hotbar entries
- #1041 Partial Creative screen: limit pick up all and the drag of the hotbar row to the hotbar row