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
ConstructorsModifierConstructorDescriptionprotectedReloadableCache(Function<net.minecraft.world.level.Level, T> delegate) -
Method Summary
Modifier and TypeMethodDescriptionapply(net.minecraft.world.level.Level level) voidInvokes the consumer with the cached value.booleanhasGameReloaded(net.minecraft.world.level.Level level) Checks if the game has reloaded since the last time the cache was updated.voidManually invalidates the cache.booleanisCached()Checks if the cache has cached a value.static <T> ReloadableCache<T> Creates a cache of a value that will be recalculated when the game reloads.static <T> ReloadableCache<T> 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.
-
Constructor Details
-
ReloadableCache
-
-
Method Details
-
apply
-
invalidate
public void invalidate()Manually invalidates the cache. This will result in a new value being cached the next timeapply(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
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
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
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.
-