Class BlockCapabilityCache<T,C>
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
Modifier 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 ServerLevel
private final ICapabilityInvalidationListener
private final BlockPos
-
Constructor Summary
ModifierConstructorDescriptionprivate
BlockCapabilityCache
(BlockCapability<T, C> capability, ServerLevel level, BlockPos pos, C context, BooleanSupplier isValid, Runnable invalidationListener) -
Method Summary
Modifier and TypeMethodDescriptioncontext()
static <T,
C> BlockCapabilityCache<T, C> create
(BlockCapability<T, C> capability, ServerLevel level, BlockPos pos, C context) Creates a new cache instance and registers it to the level.static <T,
C> BlockCapabilityCache<T, C> create
(BlockCapability<T, C> capability, ServerLevel level, 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.level()
pos()
-
Field Details
-
capability
-
level
-
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, ServerLevel level, BlockPos pos, C context, BooleanSupplier isValid, Runnable invalidationListener)
-
-
Method Details
-
create
public static <T,C> BlockCapabilityCache<T,C> create(BlockCapability<T, C> capability, ServerLevel level, 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> BlockCapabilityCache<T,C> create(BlockCapability<T, C> capability, ServerLevel level, 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
-
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
.
-