Class BlockCapabilityCache<T,C extends @Nullable Object>
java.lang.Object
net.neoforged.neoforge.capabilities.BlockCapabilityCache<T,C>
A cache for block capabilities, to be used to track capabilities at a specific position, with a specific context.
The cache is invalidated when the level is notified of a change via ILevelExtension.invalidateCapabilities(BlockPos)
.
Instances are automatically cleared by the garbage collector when they are no longer in use.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate T
private boolean
true
if notifications received by the cache will be forwarded tolistener
.private boolean
private final BlockCapability
<T, C> private final C
private final net.minecraft.server.level.ServerLevel
private final ICapabilityInvalidationListener
private final net.minecraft.core.BlockPos
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
BlockCapabilityCache
(BlockCapability<T, C> capability, net.minecraft.server.level.ServerLevel level, net.minecraft.core.BlockPos pos, C context, BooleanSupplier isValid, Runnable invalidationListener) -
Method Summary
Modifier and TypeMethodDescriptioncontext()
static <T,
C extends @Nullable Object>
BlockCapabilityCache<T, C> create
(BlockCapability<T, C> capability, net.minecraft.server.level.ServerLevel level, net.minecraft.core.BlockPos pos, C context) Creates a new cache instance and registers it to the level.static <T,
C extends @Nullable Object>
BlockCapabilityCache<T, C> create
(BlockCapability<T, C> capability, net.minecraft.server.level.ServerLevel level, net.minecraft.core.BlockPos pos, C context, BooleanSupplier isValid, Runnable invalidationListener) Creates a new cache instance with an invalidation listener, and registers it to the level.Gets the capability instance, ornull
if the capability is not present.net.minecraft.server.level.ServerLevel
level()
net.minecraft.core.BlockPos
pos()
-
Field Details
-
capability
-
level
private final net.minecraft.server.level.ServerLevel level -
pos
private final net.minecraft.core.BlockPos pos -
context
-
cacheValid
private boolean cacheValidtrue
if notifications received by the cache will be forwarded tolistener
. By default and after each invalidation, this is set tofalse
. CallinggetCapability()
sets it totrue
. -
cachedCap
-
canQuery
private boolean canQuery -
listener
-
-
Constructor Details
-
BlockCapabilityCache
private BlockCapabilityCache(BlockCapability<T, C> capability, net.minecraft.server.level.ServerLevel level, net.minecraft.core.BlockPos pos, C context, BooleanSupplier isValid, Runnable invalidationListener)
-
-
Method Details
-
create
public static <T,C extends @Nullable Object> BlockCapabilityCache<T,C> create(BlockCapability<T, C> capability, net.minecraft.server.level.ServerLevel level, net.minecraft.core.BlockPos pos, C context) Creates a new cache instance and registers it to the level.- Parameters:
capability
- the capabilitylevel
- the levelpos
- the positioncontext
- extra context for the query
-
create
public static <T,C extends @Nullable Object> BlockCapabilityCache<T,C> create(BlockCapability<T, C> capability, net.minecraft.server.level.ServerLevel level, net.minecraft.core.BlockPos pos, C context, BooleanSupplier isValid, Runnable invalidationListener) Creates a new cache instance with an invalidation listener, and registers it to the level.A few details regarding the system:
- Calling
getCapability()
from the invalidation listener is not supported, as the block being invalidated might not be ready to be queried again yet. If you receive an invalidation notification, you should wait for some time (e.g. until your own tick) before checkinggetCapability()
again. - In general, do not perform any level access for the listener. The listener itself might be in a chunk that is being unloaded, for example.
- The listener does not receive notifications before
getCapability()
is called. After each invalidation,getCapability()
must be called again to enable further notifications.
- Parameters:
capability
- the capabilitylevel
- the levelpos
- the positioncontext
- extra context for the queryisValid
- a function to check if the listener still wants to receive notifications. A typical example would be() -> !this.isRemoved()
for a block entity that should not receive invalidation notifications anymore once it is removed.invalidationListener
- the invalidation listener. Will be called whenever the capability of the cache might have changed.
- Calling
-
level
public net.minecraft.server.level.ServerLevel level() -
pos
public net.minecraft.core.BlockPos pos() -
context
-
getCapability
Gets the capability instance, ornull
if the capability is not present.If the target position is not loaded, this method will return
null
.
-