tutorial:transfer-api_storage
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorial:transfer-api_storage [2021/10/30 09:57] – [What you can do with a Storage<FluidVariant>] technici4n | tutorial:transfer-api_storage [2021/10/30 10:25] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ===== Fabric Transfer API: Understanding Storage<T> ===== | + | ===== Fabric Transfer API: Understanding Storage<FluidVariant> ===== |
| //This article is part of a series on the Fabric Transfer API. [[tutorial: | //This article is part of a series on the Fabric Transfer API. [[tutorial: | ||
| Line 27: | Line 27: | ||
| </ | </ | ||
| - | ==== What you can do with a Storage< | + | ==== A look at Storage< |
| - | === A look at Storage< | + | |
| Let's have a look at '' | Let's have a look at '' | ||
| <code java> | <code java> | ||
| Line 44: | Line 43: | ||
| This interface allows us to insert into a storage, extract from it, and read its contents. | This interface allows us to insert into a storage, extract from it, and read its contents. | ||
| - | === First example: how to insert exactly one bucket of water into a storage === | + | ==== First example: how to insert exactly one bucket of water into a storage |
| <code java> | <code java> | ||
| Storage< | Storage< | ||
| Line 64: | Line 63: | ||
| </ | </ | ||
| - | === Second example: move exactly one bucket of lava from a storage to another storage === | + | ==== Second example: move exactly one bucket of lava from a storage to another storage |
| <code java> | <code java> | ||
| import static net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants.BUCKET; | import static net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants.BUCKET; | ||
| Line 78: | Line 77: | ||
| </ | </ | ||
| Hopefully you understand why this works, and why this will never duplicate or void fluid. | Hopefully you understand why this works, and why this will never duplicate or void fluid. | ||
| + | |||
| + | ==== A more complicated example: Extracting the contents of a storage ==== | ||
| + | In the previous example, we knew that we wanted to move water or lava. But what if we don't know what to extract? The answer is: iterate over the contents! | ||
| + | This example also introduces the concept of nested transactions. | ||
| + | <code java> | ||
| + | Storage< | ||
| + | Predicate< | ||
| + | long totalExtracted = 0; | ||
| + | |||
| + | // Open a transaction, | ||
| + | try (Transaction transaction = Transaction.openOuter()) { | ||
| + | // Loop over the contents! Each StorageView< | ||
| + | for (StorageView< | ||
| + | if (view.isResourceBlank()) continue; // This means that the view contains no resource, represented by FluidVariant.blank(). | ||
| + | FluidVariant storedResource = view.getResource(); | ||
| + | if (!filter.test(storedResource)) continue; // The filter rejected this resource, skip it. | ||
| + | |||
| + | // If you want to extract any amount <= view.getAmount(), | ||
| + | totalExtracted += view.extract(storedResource, | ||
| + | |||
| + | // If you want to extract either the exact amount or nothing, you can use a nested transaction! | ||
| + | try (Transaction nestedTransaction = transaction.openNested()) { | ||
| + | long amount = view.getAmount(); | ||
| + | long extracted = view.extract(storedResource, | ||
| + | if (extracted == amount) { | ||
| + | totalExtracted += amount; | ||
| + | nestedTransaction.commit(); | ||
| + | } else { | ||
| + | // If we do nothing, the extraction is cancelled immediately when nestedTransaction is closed at the end of the try { ... } block. | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | transaction.commit(); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Conclusion ==== | ||
| + | You should now be able to use '' | ||
| + | |||
| + | You should also have a look at '' | ||
| + | |||
tutorial/transfer-api_storage.1635587876.txt.gz · Last modified: 2021/10/30 09:57 by technici4n