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
FieldsModifier and TypeFieldDescriptionstatic final ReloadableCacheA reloadable cache that will always return null. -
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.static ReloadableCache<net.minecraft.world.entity.Entity> entity(net.minecraft.nbt.CompoundTag entityData) Creates a cache of an entity instance.voidApplies a function to the cached value if the value is not null.voidManually invalidates the cache.booleanisCached()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> RMaps non null cache values to a new 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.static <T extends net.minecraft.world.item.crafting.Recipe<?>>
ReloadableCache<Map<net.minecraft.resources.ResourceLocation, net.minecraft.world.item.crafting.RecipeHolder<T>>> Creates a cache of recipe entries for a recipe type.
-
Field Details
-
EMPTY
A reloadable cache that will always return null. Not all empty instances will match this instance.
-
-
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.
-
apply
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
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
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
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.
-
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.
-