Class CapabilityCache
Several things need to be implemented depending on your usage, if you wish to have a time-out
based cache, meaning if a capability for a block is 'empty', it will wait setWaitTicks(int)
number of ticks to check again, if you are using this class in that mode, you must call
tick() each game tick otherwise nothing will ever re-cache.
You can also call onNeighborChanged(net.minecraft.core.BlockPos), doing so will force a re-cache of all empty capabilities
from the block that notified us of a change, on the next query.
For Support of API's that notify you when your host was moved, you should probably call setWorldPos(net.minecraft.world.level.Level, net.minecraft.core.BlockPos)
to clear the entire cache and notify of world and/or position change.
It is also possible to create CapabilityCache without a world and position. Doing so, will
cause all getters to return 'empty' until one is assigned.
Created by covers1624 on 4/19/20.
-
Constructor Summary
ConstructorsConstructorDescriptionCapabilityCache(net.minecraft.world.level.Level world, net.minecraft.core.BlockPos pos) -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all internal state.<T> net.minecraftforge.common.util.LazyOptional<T>getCapability(net.minecraftforge.common.capabilities.Capability<T> capability, net.minecraft.core.Direction to) Gets a capability from the block intodirection fromCapabilityCache's position.<T> TgetCapabilityOr(net.minecraftforge.common.capabilities.Capability<T> capability, net.minecraft.core.Direction to, net.minecraftforge.common.util.NonNullSupplier<T> default_) Overload ofgetCapability(net.minecraftforge.common.capabilities.Capability<T>, net.minecraft.core.Direction)with support for evaluating aNonNullSupplierwhen empty.<T> TgetCapabilityOr(net.minecraftforge.common.capabilities.Capability<T> capability, net.minecraft.core.Direction to, T default_) Overload ofgetCapability(net.minecraftforge.common.capabilities.Capability<T>, net.minecraft.core.Direction)with support for returning another object when empty.voidonNeighborChanged(net.minecraft.core.BlockPos from) NotifiesCapabilityCacheof aIForgeBlock.onNeighborChange(net.minecraft.world.level.block.state.BlockState, net.minecraft.world.level.LevelReader, net.minecraft.core.BlockPos, net.minecraft.core.BlockPos)event.
Marks all empty capabilities provided byfromblock, to be re-cached next query.voidsetWaitTicks(int ticks) Sets the number of ticks to wait between re-caching an 'empty' capability.
The default is to wait 5 seconds.voidsetWorldPos(net.minecraft.world.level.Level world, net.minecraft.core.BlockPos pos) Call this whenCapabilityCache's host has been moved.
Clears all internal state and sets a new world / pos.voidtick()Call this to notify of a new game tick.
-
Constructor Details
-
CapabilityCache
public CapabilityCache() -
CapabilityCache
public CapabilityCache(net.minecraft.world.level.Level world, net.minecraft.core.BlockPos pos)
-
-
Method Details
-
setWaitTicks
public void setWaitTicks(int ticks) Sets the number of ticks to wait between re-caching an 'empty' capability.
The default is to wait 5 seconds.- Parameters:
ticks- The number of game ticks to wait.
-
tick
public void tick()Call this to notify of a new game tick. -
setWorldPos
public void setWorldPos(net.minecraft.world.level.Level world, net.minecraft.core.BlockPos pos) Call this whenCapabilityCache's host has been moved.
Clears all internal state and sets a new world / pos.- Parameters:
world- The new world.pos- The new pos.
-
clear
public void clear()Clears all internal state. Everything will be re-cached. -
onNeighborChanged
public void onNeighborChanged(net.minecraft.core.BlockPos from) NotifiesCapabilityCacheof aIForgeBlock.onNeighborChange(net.minecraft.world.level.block.state.BlockState, net.minecraft.world.level.LevelReader, net.minecraft.core.BlockPos, net.minecraft.core.BlockPos)event.
Marks all empty capabilities provided byfromblock, to be re-cached next query.- Parameters:
from- The from position.
-
getCapabilityOr
public <T> T getCapabilityOr(net.minecraftforge.common.capabilities.Capability<T> capability, net.minecraft.core.Direction to, net.minecraftforge.common.util.NonNullSupplier<T> default_) Overload ofgetCapability(net.minecraftforge.common.capabilities.Capability<T>, net.minecraft.core.Direction)with support for evaluating aNonNullSupplierwhen empty.- Parameters:
capability- The capability to get.to- The direction to ask.default_- The supplier to evaluate when empty.- Returns:
- The instance, either the capability at
toor whatdefault_supplies.
-
getCapabilityOr
public <T> T getCapabilityOr(net.minecraftforge.common.capabilities.Capability<T> capability, net.minecraft.core.Direction to, T default_) Overload ofgetCapability(net.minecraftforge.common.capabilities.Capability<T>, net.minecraft.core.Direction)with support for returning another object when empty.- Parameters:
capability- The capability to get.to- The direction to ask.default_- The object to return when empty.- Returns:
- The instance, either the capability at
toordefault_
-
getCapability
public <T> net.minecraftforge.common.util.LazyOptional<T> getCapability(net.minecraftforge.common.capabilities.Capability<T> capability, net.minecraft.core.Direction to) Gets a capability from the block intodirection fromCapabilityCache's position. For example, calling this withNORTH, will get a capability from the block INNORTHdirection on ITSSOUTHface.- Parameters:
capability- The capability to get.to- The direction to ask.- Returns:
- A
LazyOptionalof the capability, may be empty.
-