Class IngredientStorageHelpers

java.lang.Object
org.cyclops.cyclopscore.ingredient.storage.IngredientStorageHelpers

public final class IngredientStorageHelpers extends Object
Helper methods for moving ingredients between IIngredientComponentStorage's.
  • Constructor Details

    • IngredientStorageHelpers

      public IngredientStorageHelpers()
  • Method Details

    • moveIngredientsIterative

      public static <T, M> T moveIngredientsIterative(IIngredientComponentStorage<T,M> source, IIngredientComponentStorage<T,M> destination, long maxQuantity, boolean simulate) throws InconsistentIngredientInsertionException
      Iteratively move the given maximum quantity of instances from source to destination. This is useful in cases that the internal transfer rate of certain storages have to be overridden. Note: When simulating, only a single iteration will be done. This is because the iterations don't actually take effect, which could cause infinite loops.
      Type Parameters:
      T - The instance type.
      M - The matching condition parameter.
      Parameters:
      source - A source storage to extract from.
      destination - A destination storage to insert to.
      maxQuantity - The maximum instance quantity to move.
      simulate - If the movement should be simulated.
      Returns:
      The moved ingredient.
      Throws:
      InconsistentIngredientInsertionException - When ingredients are lost due to inconsistent simulation.
    • moveIngredients

      public static <T, M> T moveIngredients(IIngredientComponentStorage<T,M> source, IIngredientComponentStorage<T,M> destination, long maxQuantity, boolean simulate) throws InconsistentIngredientInsertionException
      Move the given maximum quantity of instances from source to destination.
      Type Parameters:
      T - The instance type.
      M - The matching condition parameter.
      Parameters:
      source - A source storage to extract from.
      destination - A destination storage to insert to.
      maxQuantity - The maximum instance quantity to move.
      simulate - If the movement should be simulated.
      Returns:
      The moved ingredient.
      Throws:
      InconsistentIngredientInsertionException - When ingredients are lost due to inconsistent simulation.
    • moveIngredientsIterative

      public static <T, M> T moveIngredientsIterative(IIngredientComponentStorage<T,M> source, IIngredientComponentStorage<T,M> destination, T instance, M matchCondition, boolean simulate) throws InconsistentIngredientInsertionException
      Iteratively move the instance that matches the given match condition from source to destination. The quantity of the given instance indicates the maximum amount that can be moved. This is useful in cases that the internal transfer rate of certain storages have to be overridden. Note: When simulating, only a single iteration will be done. This is because the iterations don't actually take effect, which could cause infinite loops.
      Type Parameters:
      T - The instance type.
      M - The matching condition parameter.
      Parameters:
      source - A source storage to extract from.
      destination - A destination storage to insert to.
      instance - The prototype instance.
      matchCondition - The match condition.
      simulate - If the movement should be simulated.
      Returns:
      The moved ingredient.
      Throws:
      InconsistentIngredientInsertionException - When ingredients are lost due to inconsistent simulation.
    • moveIngredients

      public static <T, M> T moveIngredients(IIngredientComponentStorage<T,M> source, IIngredientComponentStorage<T,M> destination, T instance, M matchCondition, boolean simulate) throws InconsistentIngredientInsertionException
      Move the first instance that matches the given match condition from source to destination.
      Type Parameters:
      T - The instance type.
      M - The matching condition parameter.
      Parameters:
      source - A source storage to extract from.
      destination - A destination storage to insert to.
      instance - The prototype instance.
      matchCondition - The match condition.
      simulate - If the movement should be simulated.
      Returns:
      The moved ingredient.
      Throws:
      InconsistentIngredientInsertionException - When ingredients are lost due to inconsistent simulation.
    • moveIngredients

      public static <T, M> T moveIngredients(IIngredientComponentStorage<T,M> source, IIngredientComponentStorage<T,M> destination, Predicate<T> predicate, long maxQuantity, boolean exactQuantity, boolean simulate) throws InconsistentIngredientInsertionException
      Move the first instance that matches the given predicate from source to destination.
      Type Parameters:
      T - The instance type.
      M - The matching condition parameter.
      Parameters:
      source - A source storage to extract from.
      destination - A destination storage to insert to.
      predicate - The predicate to match instances by.
      maxQuantity - The max quantity that can be moved.
      exactQuantity - If the max quantity should be interpreted as an exact quantity.
      simulate - If the movement should be simulated.
      Returns:
      The moved ingredient.
      Throws:
      InconsistentIngredientInsertionException - When ingredients are lost due to inconsistent simulation.
    • moveIngredientsSlotted

      public static <T, M> T moveIngredientsSlotted(IIngredientComponentStorage<T,M> source, int sourceSlot, IIngredientComponentStorage<T,M> destination, int destinationSlot, T instance, M matchCondition, boolean simulate) throws InconsistentIngredientInsertionException
      Move ingredients from source to target with optional source and target slots, based on an ingredient prototype and match condition. If the algorithm should iterate over all source/destination slot, then the respective slot should be -1. If a slot is defined, and the storage is not an instance of IIngredientComponentStorageSlotted, then nothing will be moved.
      Type Parameters:
      T - The instance type.
      M - The matching condition parameter.
      Parameters:
      source - A source storage to extract from.
      sourceSlot - The source slot or -1 for any.
      destination - A destination storage to insert to.
      destinationSlot - The destination slot or -1 for any.
      instance - The prototype instance.
      matchCondition - The match condition.
      simulate - If the movement should be simulated.
      Returns:
      The moved ingredient.
      Throws:
      InconsistentIngredientInsertionException - When ingredients are lost due to inconsistent simulation.
    • moveIngredientsSlotted

      public static <T, M> T moveIngredientsSlotted(IIngredientComponentStorage<T,M> source, int sourceSlot, IIngredientComponentStorage<T,M> destination, int destinationSlot, Predicate<T> predicate, long maxQuantity, boolean exactQuantity, boolean simulate) throws InconsistentIngredientInsertionException
      Move ingredients from source to target with optional source and target slots, based on an ingredient predicate. If the algorithm should iterate over all source/destination slot, then the respective slot should be -1. If a slot is defined, and the storage is not an instance of IIngredientComponentStorageSlotted, then nothing will be moved.
      Type Parameters:
      T - The instance type.
      M - The matching condition parameter.
      Parameters:
      source - A source storage to extract from.
      sourceSlot - The source slot or -1 for any.
      destination - A destination storage to insert to.
      destinationSlot - The destination slot or -1 for any.
      predicate - The instance predicate.
      maxQuantity - The max quantity that can be moved.
      exactQuantity - If the max quantity should be interpreted as an exact quantity.
      simulate - If the movement should be simulated.
      Returns:
      The moved ingredient.
      Throws:
      InconsistentIngredientInsertionException - When ingredients are lost due to inconsistent simulation.
    • moveEffectiveSourceExactDestinationExact

      protected static <T, M> T moveEffectiveSourceExactDestinationExact(IIngredientComponentStorageSlotted<T,M> sourceSlotted, int sourceSlot, IIngredientComponentStorageSlotted<T,M> destinationSlotted, int destinationSlot, T extractedSimulated, T remaining, boolean simulate) throws InconsistentIngredientInsertionException
      Throws:
      InconsistentIngredientInsertionException
    • moveEffectiveSourceExactDestinationNonSlotted

      protected static <T, M> T moveEffectiveSourceExactDestinationNonSlotted(IIngredientComponentStorageSlotted<T,M> sourceSlotted, int sourceSlot, IIngredientComponentStorage<T,M> destination, T inserted, boolean simulate) throws InconsistentIngredientInsertionException
      Throws:
      InconsistentIngredientInsertionException
    • moveEffectiveSourceNonSlottedDestinationExact

      @Nullable protected static <T, M> T moveEffectiveSourceNonSlottedDestinationExact(IIngredientComponentStorage<T,M> source, IIngredientComponentStorageSlotted<T,M> destinationSlotted, int destinationSlot, M extractMatchCondition, T extractedSimulated, T remaining, boolean simulate) throws InconsistentIngredientInsertionException
      Throws:
      InconsistentIngredientInsertionException
    • handleRemainder

      public static <T, M> T handleRemainder(IIngredientComponentStorage<T,M> source, IIngredientComponentStorage<T,M> destination, T movedEffective, T remainingEffective) throws InconsistentIngredientInsertionException
      Throws:
      InconsistentIngredientInsertionException
    • moveIngredient

      public static <T, M> T moveIngredient(IIngredientComponentStorage<T,M> source, IIngredientComponentStorage<T,M> destination, T instance, M matchCondition, boolean simulate) throws InconsistentIngredientInsertionException
      Move the first instance that matches the given match condition from source to destination. The main difference of this method to moveIngredients(IIngredientComponentStorage, IIngredientComponentStorage, Object, Object, boolean) is that the latter method will try checking *multiple* ingredients from the source, while this method will only check the *first matching* ingredient. This makes this method potentially more efficient than the latter.
      Type Parameters:
      T - The instance type.
      M - The matching condition parameter.
      Parameters:
      source - A source storage to extract from.
      destination - A destination storage to insert to.
      instance - The prototype instance.
      matchCondition - The match condition.
      simulate - If the movement should be simulated.
      Returns:
      The moved ingredient.
      Throws:
      InconsistentIngredientInsertionException - When ingredients are lost due to inconsistent simulation.
    • insertIngredientQuantity

      public static <T, M> long insertIngredientQuantity(IIngredientComponentStorage<T,M> destination, T instance, boolean simulate)
      Insert an ingredient in a destination storage. The difference of this method compared to IIngredientComponentStorage.insert(Object, boolean) is that this method returns the actually inserted ingredient quantity instead of the remaining ingredient that was not inserted.
      Type Parameters:
      T - The instance type.
      M - The matching condition parameter.
      Parameters:
      destination - A storage.
      instance - An instance to insert.
      simulate - If the insertion should be simulated.
      Returns:
      The actual inserted ingredient quantity, or would-be inserted ingredient quantity if simulated.
    • insertIngredient

      public static <T, M> T insertIngredient(IIngredientComponentStorage<T,M> destination, T instance, boolean simulate)
      Insert an ingredient in a destination storage. The difference of this method compared to IIngredientComponentStorage.insert(Object, boolean) is that this method returns the actually inserted ingredient instead of the remaining ingredient that was not inserted.
      Type Parameters:
      T - The instance type.
      M - The matching condition parameter.
      Parameters:
      destination - A storage.
      instance - An instance to insert.
      simulate - If the insertion should be simulated.
      Returns:
      The actual inserted ingredient, or would-be inserted ingredient if simulated.
    • insertIngredientRemainderFixup

      public static <T, M> T insertIngredientRemainderFixup(IIngredientComponentStorage<T,M> source, IIngredientComponentStorage<T,M> destination, T instance, boolean simulate) throws InconsistentIngredientInsertionException
      Insert an ingredient in a destination storage. If not in simulation mode, and the instance does not completely fit into the destination, the remainder will be inserted into the source. If not everything can be re-inserted into the source, a warning will be emitted. The difference of this method compared to IIngredientComponentStorage.insert(Object, boolean) is that this method returns the actually inserted ingredient instead of the remaining ingredient that was not inserted.
      Type Parameters:
      T - The instance type.
      M - The matching condition parameter.
      Parameters:
      source - A source storage to insert fixup instances.
      destination - A storage.
      instance - An instance to insert.
      simulate - If the insertion should be simulated.
      Returns:
      The actual inserted ingredient, or would-be inserted ingredient if simulated.
      Throws:
      InconsistentIngredientInsertionException - When ingredients are lost due to inconsistent simulation.
    • wrapStorage

      public static <T, M> IIngredientComponentStorage<T,M> wrapStorage(IIngredientComponentStorage<T,M> ingredientComponentStorage, boolean read, boolean insert, boolean extract)
      Wrap the given ingredient storage, where you can configure if the storage is allowed to be read, inserted to, or extracted from. This will distinguish between slotted and slotless storages.
      Type Parameters:
      T - The instance type.
      M - The matching condition parameter.
      Parameters:
      ingredientComponentStorage - The storage to wrap.
      read - If the storage can be read.
      insert - If the storage can be inserted to.
      extract - If the storage can be extracted from.
      Returns:
      The wrapped storage.
    • serialize

      public static <T, M> net.minecraft.nbt.CompoundTag serialize(IIngredientComponentStorage<T,M> storage)
      Serialize the given storage to NBT. All ingredients, the max quantity, and whether or not it is slotted will be stored.
      Type Parameters:
      T - The instance type.
      M - The matching condition parameter.
      Parameters:
      storage - An ingredient storage.
      Returns:
      An NBT tag.
    • deserialize

      public static IIngredientComponentStorage<?,?> deserialize(net.minecraft.nbt.CompoundTag tag, long rateLimit)
      Deserialize the storage from the given NBT tag. All ingredients, the max quantity, and whether or not it is slotted will be restored.
      Parameters:
      tag - An NBT tag.
      rateLimit - The rate limit per insertion/extraction.
      Returns:
      The deserialized storage.
      Throws:
      IllegalArgumentException - If the tag was invalid.