Interface ContainerItemContext


public interface ContainerItemContext
A context that allows an item-queried Storage implementation to interact with its containing inventory, such as a player inventory or an emptying or filling machine. For example, it is what allows the Storage<FluidVariant> of a water bucket to replace the full bucket by an empty bucket on extraction. Such items that contain resources are often referred to as "container items".

When an item API requires a ContainerItemContext as context, it will generally be suitable to obtain a context instance with ofPlayerHand(net.minecraft.entity.player.PlayerEntity, net.minecraft.util.Hand) or ofPlayerCursor(net.minecraft.entity.player.PlayerEntity, net.minecraft.screen.ScreenHandler), and then use find(net.fabricmc.fabric.api.lookup.v1.item.ItemApiLookup<A, net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext>) to query an API instance.

When water is extracted from the Storage of a water bucket, this is how it interacts with the context:

  • The first step is to remove one water bucket item from the current slot, that is the slot that contains the water bucket.
  • The second step is to try to add one empty bucket item to the current slot, at the same position.
  • If that fails, the third step is to add the empty bucket item somewhere else in the inventory.
  • The water extraction can only proceed if both step 1, and step 2 or 3, succeed.
Before attempting to change the current item, the Storage implementation must of course check that the item in the current slot is still a water bucket.

A ContainerItemContext allows these operations to be performed, thanks to the following parts:

  • The main slot or current slot of the context, containing the item the API was queried for initially. In the example above, this is the slot containing the water bucket, used for steps 1 and 2.
  • An overflow insertion function, that can be used to insert items into the context's inventory when insertion into a specific slot fails. In our example above, this is the function used for step 3.
  • The context may also contain additional slots, accessible through getAdditionalSlots().

Implementors of item APIs can freely use these methods, but most will generally want to use the following convenience methods instead: