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:15] – [A more complicated example: Extracting the contents of a storage] 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 92: 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 104: Line 105:
  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 111: Line 112:
 } }
 </code> </code>
 +
 +==== 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