User Tools

Site Tools


tutorial:transfer-api_fluid-containing-items

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
tutorial:transfer-api_fluid-containing-items [2022/02/11 18:16] technici4ntutorial:transfer-api_fluid-containing-items [2022/02/11 18:21] technici4n
Line 58: Line 58:
  
 A word of caution: don't use ''ContainerItemContext.withInitial(stack)'' unless you know what you're doing. It **does not** mutate the stack. A word of caution: don't use ''ContainerItemContext.withInitial(stack)'' unless you know what you're doing. It **does not** mutate the stack.
 +
 +==== The API in action ====
 +An example to understand what is going on: how to query a storage for the main hand of a player, and insert 1 bucket of water into it:
 +<code java>
 +PlayerEntity player;
 +
 +// Build the ContainerItemContext.
 +ContainerItemContext handContext = ContainerItemContext.ofPlayerHand(player, Hand.MAIN_HAND);
 +// Use it to query a fluid storage.
 +Storage<FluidVariant> handStorage = handContext.find(FluidStorage.ITEM);
 +if (handStorage != null) {
 + // Use the storage: any usual Storage<FluidVariant> can be attempted.
 + try (Transaction transaction = Transaction.openOuter()) {
 + handStorage.insert(FluidVariant.of(Fluids.WATER), FluidConstants.BUCKET, transaction);
 + transaction.commit();
 + }
 +}
 +</code>
tutorial/transfer-api_fluid-containing-items.txt · Last modified: 2022/02/11 18:40 by 127.0.0.1