tutorial:transfer-api_fluid-containing-items
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorial:transfer-api_fluid-containing-items [2022/02/11 17:17] – [The problem] technici4n | tutorial:transfer-api_fluid-containing-items [2022/02/11 18:40] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 7: | Line 7: | ||
| ===== The problem ===== | ===== The problem ===== | ||
| When we are dealing with fluid-containing items, we are interacting with fluid containers stored inside of an inventory. For example, this is the sequence of operations that must be executed to empty a water bucket: | When we are dealing with fluid-containing items, we are interacting with fluid containers stored inside of an inventory. For example, this is the sequence of operations that must be executed to empty a water bucket: | ||
| - | * Check that the inventory still contains | + | * The first step is to remove 1 water bucket item from the current slot, that is the slot that contains |
| - | * Ensure at least 1 bucket | + | * The second step is to try to add one empty bucket item to the current slot, at the same position. |
| - | * Try to replace 1 bucket of water by 1 empty bucket in the inventory, abort if it fails. | + | * If that fails, the third step is to add the empty bucket |
| - | * Commit these operations, and return that 1 bucket was extracted. | + | * The water extraction can only proceed if both step 1, and step 2 or 3, succeed. |
| You do not need to understand this in detail, but this should give an idea of where we are headed. In code, this is what this looks like: (taken from '' | You do not need to understand this in detail, but this should give an idea of where we are headed. In code, this is what this looks like: (taken from '' | ||
| Line 43: | Line 43: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | ===== ContainerItemContext ===== | ||
| + | [[https:// | ||
| + | * The specific slot in the inventory that the fluid container is queried from. 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' | ||
| + | * The context may also contain additional slots. | ||
| + | |||
| + | You usually don't interact with these methods directly, since '' | ||
| + | |||
| + | ==== Obtaining instances ==== | ||
| + | Fabric provides various static methods to create a '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | A word of caution: don't use '' | ||
| + | |||
| + | ==== 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, | ||
| + | // Use it to query a fluid storage. | ||
| + | Storage< | ||
| + | if (handStorage != null) { | ||
| + | // Use the storage: any usual Storage< | ||
| + | try (Transaction transaction = Transaction.openOuter()) { | ||
| + | handStorage.insert(FluidVariant.of(Fluids.WATER), | ||
| + | transaction.commit(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | TODO: | ||
| + | - filling an item, example from TR | ||
| + | - using the existing base implementations for items | ||
tutorial/transfer-api_fluid-containing-items.1644599859.txt.gz · Last modified: 2022/02/11 17:17 by technici4n