User Tools

Site Tools


tutorial:transfer-api_storage

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
tutorial:transfer-api_storage [2021/10/30 10:13] technici4ntutorial:transfer-api_storage [2021/10/30 10:25] technici4n
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:transfer-api|Link to the home page of the series]].// //This article is part of a series on the Fabric Transfer API. [[tutorial:transfer-api|Link to the home page of the series]].//
  
Line 78: Line 78:
 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.
  
-==== Extracting the contents of a storage ====+==== 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! 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> <code java>
 Storage<FluidVariant> storage; Storage<FluidVariant> storage;
Line 91: Line 92:
  if (view.isResourceBlank()) continue; // This means that the view contains no resource, represented by FluidVariant.blank().  if (view.isResourceBlank()) continue; // This means that the view contains no resource, represented by FluidVariant.blank().
  FluidVariant storedResource = view.getResource(); // Current resource  FluidVariant storedResource = view.getResource(); // Current resource
 + if (!filter.test(storedResource)) continue; // The filter rejected this resource, skip it.
  
  // If you want to extract any amount <= view.getAmount(), do this.  // If you want to extract any amount <= view.getAmount(), do this.
Line 100: Line 102:
  long extracted = view.extract(storedResource, amount, nestedTransaction);  long extracted = view.extract(storedResource, amount, nestedTransaction);
  if (extracted == amount) {  if (extracted == amount) {
 + totalExtracted += amount;
  nestedTransaction.commit(); // Validate the nestedTransaction: the outer one will have to committed as well to validate this change.  nestedTransaction.commit(); // Validate the nestedTransaction: the outer one will have to committed as well to validate this change.
  } else {  } else {
- // If we do nothing, the extraction is cancelled immediately when nestedTransaction is closed.+ // If we do nothing, the extraction is cancelled immediately when nestedTransaction is closed at the end of the try { ... } block.
  }  }
  }  }
Line 109: Line 112:
 } }
 </code> </code>
-If we are not satisfied with one of the operations, do we have to cancel the full transaction? The answer is no: we can have nested transactions!+ 
 +==== Conclusion ==== 
 +You should now be able to use ''Storage''s and ''StorageView''s. They can be a bit challenging in the beginning, but once you're used to them they are very easy to work with
 + 
 +You should also have a look at ''StorageUtil'': it already contains functions that perform common transfer operations. In particular[[https://maven.fabricmc.net/docs/fabric-api-0.41.0+1.17/net/fabricmc/fabric/api/transfer/v1/storage/StorageUtil.html#move(net.fabricmc.fabric.api.transfer.v1.storage.Storage,net.fabricmc.fabric.api.transfer.v1.storage.Storage,java.util.function.Predicate,long,net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext)|move]] is very useful if you just want to move resources between two storages.
  
tutorial/transfer-api_storage.txt · Last modified: 2021/10/30 10:25 by 127.0.0.1