tutorial:transfer-api_storage
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:transfer-api_storage [2021/10/30 09:31] – technici4n | tutorial:transfer-api_storage [2021/10/30 09:57] – [What you can do with a Storage<FluidVariant>] technici4n | ||
---|---|---|---|
Line 2: | Line 2: | ||
//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: | ||
+ | **'' | ||
+ | |||
+ | ==== You already used Storage< | ||
[[tutorial: | [[tutorial: | ||
<code java> | <code java> | ||
Line 8: | Line 11: | ||
</ | </ | ||
- | The takeaway here is that '' | + | The takeaway here is that '' |
- | + | ||
- | **'' | + | |
+ | ==== Retrieving a Storage< | ||
Let's see how we can retrieve one from the world: | Let's see how we can retrieve one from the world: | ||
<code java> | <code java> | ||
Line 24: | Line 26: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ==== What you can do with a Storage< | ||
+ | === A look at Storage< | ||
+ | Let's have a look at '' | ||
+ | <code java> | ||
+ | public interface Storage< | ||
+ | // Try to insert a resource in the storage, return how much was inserted. | ||
+ | long insert(T resource, long maxAmount, TransactionContext transaction); | ||
+ | // Try to extract a resource from the storage, return how much was extracted. | ||
+ | long extract(T resource, long maxAmount, TransactionContext transaction); | ||
+ | // Iterate over the contents of this storage. | ||
+ | default Iterable< | ||
+ | |||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | 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 === | ||
+ | <code java> | ||
+ | Storage< | ||
+ | FluidVariant water = FluidVariant.of(Fluids.WATER); | ||
+ | |||
+ | // Open a transaction: | ||
+ | try (Transaction transaction = Transaction.openOuter()) { | ||
+ | // Try to insert, will return how much was actually inserted. | ||
+ | long amountInserted = storage.insert(water, | ||
+ | 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(); | ||
+ | } else { | ||
+ | // 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. |
tutorial/transfer-api_storage.txt · Last modified: 2021/10/30 10:25 by 127.0.0.1