Skip to main content

Container

Trait Container 

Source
pub trait Container:
    ErasedType
    + Send
    + Sync {
Show 22 methods // Required methods fn items(&self) -> &[ItemStack]; fn items_mut(&mut self) -> &mut [ItemStack]; fn set_changed(&mut self); // Provided methods fn get_container_size(&self) -> usize { ... } fn is_empty(&self) -> bool { ... } fn get_item(&self, slot: usize) -> &ItemStack { ... } fn contains_stack(&self, search_stack: &ItemStack) -> bool { ... } fn get_item_mut(&mut self, slot: usize) -> &mut ItemStack { ... } fn set_item(&mut self, slot: usize, stack: ItemStack) { ... } fn remove_item(&mut self, slot: usize, count: i32) -> ItemStack { ... } fn remove_item_no_update(&mut self, slot: usize) -> ItemStack { ... } fn get_max_stack_size(&self) -> i32 { ... } fn get_max_stack_size_for_item(&self, item: &ItemStack) -> i32 { ... } fn can_place_item(&self, _slot: usize, _stack: &ItemStack) -> bool { ... } fn can_take_item(&self, _slot: usize, _stack: &ItemStack) -> bool { ... } fn clear_content(&mut self) -> i32 { ... } fn clear_content_matching( &mut self, predicate: &mut dyn FnMut(&mut ItemStack) -> bool, ) -> i32 { ... } fn clear_or_count_matching_items( &mut self, predicate: &dyn Fn(&ItemStack) -> bool, amount_to_remove: i32, counting_only: bool, ) -> i32 { ... } fn with_indices<const N: usize>( &mut self, indices: [usize; N], ) -> [&mut ItemStack; N] where Self: Sized { ... } fn add(&mut self, stack: &mut ItemStack) -> bool { ... } fn iter(&self) -> Box<dyn Iterator<Item = &ItemStack> + '_> { ... } fn iter_mut(&mut self) -> Box<dyn Iterator<Item = &mut ItemStack> + '_> { ... }
}
Expand description

Something that contains items. I also use container interchangeably with inventory as they mean approximately the same thing. But inventory could also refer to the player’s inventory. Example: crate::player::player_inventory::PlayerInventory, crate::inventory::container::SimpleContainer

Concrete implementations must implement steel_utils::DowncastType with a unique, stable key so erased container references can recover their type.

§Locking contract

Implementations stored in a crate::inventory::lock::ContainerRef must keep their methods storage-local. In particular, set_item, remove_item, and set_changed must not call into the world or a block entity while the container mutex is held. Block-entity persistence and comparator callbacks belong to the owner supplied through crate::inventory::lock::ContainerRef::owned_by_block_entity, which runs only after every container lock has been released.

Required Methods§

Source

fn items(&self) -> &[ItemStack]

Returns the items in this container

Source

fn items_mut(&mut self) -> &mut [ItemStack]

Returns mutable references to the items in this container.

Source

fn set_changed(&mut self)

Marks this container as changed (dirty) for saving/syncing.

Provided Methods§

Source

fn get_container_size(&self) -> usize

Returns the number of slots in this container.

Source

fn is_empty(&self) -> bool

Returns true if all slots in this container are empty.

Source

fn get_item(&self, slot: usize) -> &ItemStack

Returns a reference to the item in the specified slot.

Source

fn contains_stack(&self, search_stack: &ItemStack) -> bool

Returns true if this container has a non-empty stack with the same item and components.

Mirrors vanilla Inventory.contains(ItemStack).

Source

fn get_item_mut(&mut self, slot: usize) -> &mut ItemStack

Returns a mutable reference to the item in the specified slot.

Source

fn set_item(&mut self, slot: usize, stack: ItemStack)

Sets the item in the specified slot.

Source

fn remove_item(&mut self, slot: usize, count: i32) -> ItemStack

Removes up to count items from the specified slot and returns them.

Source

fn remove_item_no_update(&mut self, slot: usize) -> ItemStack

Removes the item from the specified slot without triggering updates.

Source

fn get_max_stack_size(&self) -> i32

Returns the maximum stack size for this container.

Source

fn get_max_stack_size_for_item(&self, item: &ItemStack) -> i32

Returns the maximum stack size for a specific item in this container.

Takes the minimum of the container’s max stack size and the item’s max stack size. Based on Java’s Container.getMaxStackSize(ItemStack).

Source

fn can_place_item(&self, _slot: usize, _stack: &ItemStack) -> bool

Returns true if the specified item can be placed in the specified slot.

Source

fn can_take_item(&self, _slot: usize, _stack: &ItemStack) -> bool

Returns true if the specified item can be taken from the specified slot.

Source

fn clear_content(&mut self) -> i32

Clears all items from this container.

Source

fn clear_content_matching( &mut self, predicate: &mut dyn FnMut(&mut ItemStack) -> bool, ) -> i32

Clears all items from this container.

Source

fn clear_or_count_matching_items( &mut self, predicate: &dyn Fn(&ItemStack) -> bool, amount_to_remove: i32, counting_only: bool, ) -> i32

Removes or counts matching items using vanilla /clear semantics.

Source

fn with_indices<const N: usize>( &mut self, indices: [usize; N], ) -> [&mut ItemStack; N]
where Self: Sized,

Returns mutable references to N disjoint slots.

§Panics

Panics if any index is out of bounds or if any two indices are equal.

Source

fn add(&mut self, stack: &mut ItemStack) -> bool

Tries to add an item to the container.

First tries to stack with existing matching items, then tries empty slots. Returns true if the entire stack was added, false if some or all couldn’t fit. The passed stack is modified to contain any remaining items.

Based on Java’s Inventory.add(ItemStack).

Source

fn iter(&self) -> Box<dyn Iterator<Item = &ItemStack> + '_>

Returns a boxed iterator to the items in this container

Source

fn iter_mut(&mut self) -> Box<dyn Iterator<Item = &mut ItemStack> + '_>

Returns a boxed iterator to mutable references of the items in this container

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§