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:43] – [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<FluidVariant> ==== | + | ==== A look at Storage<T> ==== |
| Let's have a look at '' | Let's have a look at '' | ||
| <code java> | <code java> | ||
| Line 43: | 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. | ||
| - | Let's look at an 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 54: | Line 54: | ||
| if (amountInserted == FluidConstants.BUCKET) { | if (amountInserted == FluidConstants.BUCKET) { | ||
| // " | // " | ||
| + | // You should call this if you are satisfied with the result of the operation, and want to keep it. | ||
| transaction.commit(); | transaction.commit(); | ||
| } else { | } else { | ||
| // Doing nothing " | // Doing nothing " | ||
| + | // You should call this if you are not satisfied with the result of the operation, and want to abort it. | ||
| } | } | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | ==== Second example: move exactly one bucket of lava from a storage to another storage ==== | ||
| + | <code java> | ||
| + | import static net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants.BUCKET; | ||
| + | |||
| + | Storage< | ||
| + | FluidVariant lava = FluidVariant.of(Fluids.LAVA); | ||
| + | |||
| + | try (Transaction transaction = Transaction.openOuter()) { | ||
| + | if (source.extract(lava, | ||
| + | transaction.commit(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | 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.1635587039.txt.gz · Last modified: 2021/10/30 09:43 by technici4n