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.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final ReloadableCache
    A reloadable cache that will always return null.
  • 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.
    static ReloadableCache<net.minecraft.world.entity.Entity>
    entity(net.minecraft.nbt.CompoundTag entityData)
    Creates a cache of an entity instance.
    void
    ifPresent(net.minecraft.world.level.Level level, Consumer<T> consumer)
    Applies a function to the cached value if the value is not null.
    void
    Manually invalidates the cache.
    boolean
    Checks if the cache has cached a value.
    static ReloadableCache<net.minecraft.world.entity.LivingEntity>
    living(net.minecraft.nbt.CompoundTag entityData)
    Creates a cache of a living entity instance.
    <R> R
    map(net.minecraft.world.level.Level level, Function<T,R> mapper)
    Maps non null cache values to a new 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.
    static <T extends net.minecraft.world.item.crafting.Recipe<?>>
    ReloadableCache<Map<net.minecraft.resources.ResourceLocation,net.minecraft.world.item.crafting.RecipeHolder<T>>>
    recipes(Supplier<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
  • Field Details

    • EMPTY

      public static final ReloadableCache EMPTY
      A reloadable cache that will always return null. Not all empty instances will match this instance.
  • 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.
    • 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 already.
      Parameters:
      level - The current game level. This is used to provide context about the current state of the game.
      consumer - The consumer to invoke.
    • ifPresent

      public void ifPresent(net.minecraft.world.level.Level level, Consumer<T> consumer)
      Applies a function to the cached value if the value is not null.
      Parameters:
      level - The current game level. This is used to provide context about the current state of the game.
      consumer - The consumer to invoke.
    • map

      @Nullable public <R> R map(net.minecraft.world.level.Level level, Function<T,R> mapper)
      Maps non null cache values to a new value.
      Type Parameters:
      R - The return type.
      Parameters:
      level - The current game level. This is used to provide context about the current state of the game.
      mapper - A mapper function to map the cached value to something new. This is only used if the value is not null.
      Returns:
      The mapped value or null.
    • 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.
    • recipes

      public static <T extends net.minecraft.world.item.crafting.Recipe<?>> ReloadableCache<Map<net.minecraft.resources.ResourceLocation,net.minecraft.world.item.crafting.RecipeHolder<T>>> recipes(Supplier<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.
    • entity

      public static ReloadableCache<net.minecraft.world.entity.Entity> entity(net.minecraft.nbt.CompoundTag entityData)
      Creates a cache of an entity instance.
      Parameters:
      entityData - The data used to create the entity.
      Returns:
      A reloadable entity instance.
    • living

      public static ReloadableCache<net.minecraft.world.entity.LivingEntity> living(net.minecraft.nbt.CompoundTag entityData)
      Creates a cache of a living entity instance.
      Parameters:
      entityData - The data used to create the entity.
      Returns:
      A reloadable living entity instance.