Class ReloadableCache<T>

java.lang.Object
net.darkhax.bookshelf.common.api.function.ReloadableCache<T>
Type Parameters:
T - The type of the cached value.
All Implemented Interfaces:
Function<net.minecraft.world.level.Level,T>

public class ReloadableCache<T> extends Object implements Function<net.minecraft.world.level.Level,T>
A cached value that is lazily loaded and will be invalidated automatically after the game has been reloaded.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    ReloadableCache(Function<net.minecraft.world.level.Level,T> delegate)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    apply(net.minecraft.world.level.Level level)
     
    void
    apply(net.minecraft.world.level.Level level, Consumer<T> consumer)
    Invokes the consumer with the cached value.
    boolean
    hasGameReloaded(net.minecraft.world.level.Level level)
    Checks if the game has reloaded since the last time the cache was updated.
    void
    Manually invalidates the cache.
    boolean
    Checks if the cache has cached a value.
    static <T> ReloadableCache<T>
    of(Function<net.minecraft.world.level.Level,T> delegate)
    Creates a cache of a value that will be recalculated when the game reloads.
    static <T> ReloadableCache<T>
    of(Supplier<T> supplier)
    Creates a cache of a value that will be recalculated when the game reloads.
    static <T> ReloadableCache<T>
    of(net.minecraft.resources.ResourceKey<? extends net.minecraft.core.Registry<T>> registry, net.minecraft.resources.ResourceLocation id)
    Creates a cache of a registry value that will be reaquired when the game reloads.
    static <T extends net.minecraft.world.item.crafting.Recipe<?>>
    ReloadableCache<Map<net.minecraft.resources.ResourceLocation,net.minecraft.world.item.crafting.RecipeHolder<T>>>
    of(net.minecraft.world.item.crafting.RecipeType<T> type)
    Creates a cache of recipe entries for a recipe type.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.util.function.Function

    andThen, compose
  • Constructor Details

    • ReloadableCache

      protected ReloadableCache(Function<net.minecraft.world.level.Level,T> delegate)
  • Method Details

    • apply

      @Nullable public T apply(net.minecraft.world.level.Level level)
      Specified by:
      apply in interface Function<net.minecraft.world.level.Level,T>
    • invalidate

      public void invalidate()
      Manually invalidates the cache. This will result in a new value being cached the next time apply(Level) is invoked.
    • isCached

      public boolean isCached()
      Checks if the cache has cached a value. This is not a substitute for null checking.
      Returns:
      Has the supplier cached a value.
    • hasGameReloaded

      public boolean hasGameReloaded(net.minecraft.world.level.Level level)
      Checks if the game has reloaded since the last time the cache was updated.
      Parameters:
      level - The current game level. This is used to provide context about the current state of the game.
      Returns:
      If the game has reloaded since the last time the cache was updated.
    • apply

      public void apply(net.minecraft.world.level.Level level, Consumer<T> consumer)
      Invokes the consumer with the cached value. This will cause a value to be cached if one has not been cached alread.
      Parameters:
      level - The current game level. This is used to provide context about the current state of the game.
      consumer - The consumer to invoke.
    • of

      public static <T> ReloadableCache<T> of(Supplier<T> supplier)
      Creates a cache of a value that will be recalculated when the game reloads.
      Type Parameters:
      T - The type of value held by the cache.
      Parameters:
      supplier - The supplier used to produce the cached value.
      Returns:
      A cache of a value that will be recalculated when the game reloads.
    • of

      public static <T> ReloadableCache<T> of(Function<net.minecraft.world.level.Level,T> delegate)
      Creates a cache of a value that will be recalculated when the game reloads.
      Type Parameters:
      T - The type of value held by the cache.
      Parameters:
      delegate - A function used to produce the value to cache.
      Returns:
      A cache of a value that will be recalculated when the game reloads.
    • of

      public static <T> ReloadableCache<T> of(net.minecraft.resources.ResourceKey<? extends net.minecraft.core.Registry<T>> registry, net.minecraft.resources.ResourceLocation id)
      Creates a cache of a registry value that will be reaquired when the game reloads.
      Type Parameters:
      T - The type of value held by the cache.
      Parameters:
      registry - The registry to look up the value in.
      id - The ID of the value to lookup.
      Returns:
      A cache of a registry value that will be reaquired when the game reloads.
    • of

      public static <T extends net.minecraft.world.item.crafting.Recipe<?>> ReloadableCache<Map<net.minecraft.resources.ResourceLocation,net.minecraft.world.item.crafting.RecipeHolder<T>>> of(net.minecraft.world.item.crafting.RecipeType<T> type)
      Creates a cache of recipe entries for a recipe type.
      Type Parameters:
      T - The type of the recipe.
      Parameters:
      type - The type of recipe.
      Returns:
      A map of recipes for the recipe type.