Package codechicken.lib.capability
Class CapabilityCache
java.lang.Object
codechicken.lib.capability.CapabilityCache
A simple cache for capabilities as viewed from a specific world position.
Provides a cleaner api to the Neo provided BlockCapabilityCache. The Neo cache requires you know, up-front,
what capabilities you wish to receive, this is not always the case and not always easily computable.
For Support of API's that notify you when your host was moved, you should call setLevelPos(net.minecraft.server.level.ServerLevel, net.minecraft.core.BlockPos)
to ensure the capability cache
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.server.level.ServerLevel level, net.minecraft.core.BlockPos pos) -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all internal state.<T,C> T getCapability(net.neoforged.neoforge.capabilities.BlockCapability<T, C> capability, net.minecraft.core.Direction to, C ctx) Gets a capability from the block intodirection fromCapabilityCache's position.<T> TgetCapability(net.neoforged.neoforge.capabilities.BlockCapability<T, @Nullable net.minecraft.core.Direction> capability, net.minecraft.core.Direction to) Gets a capability from the block intodirection fromCapabilityCache's position.<T> TgetCapabilityOr(net.neoforged.neoforge.capabilities.BlockCapability<T, net.minecraft.core.Direction> capability, net.minecraft.core.Direction to, T default_) Overload ofgetCapability(net.neoforged.neoforge.capabilities.BlockCapability<T, net.minecraft.core.Direction>, net.minecraft.core.Direction)with support for returning another object when empty.voidsetLevelPos(net.minecraft.server.level.ServerLevel level, net.minecraft.core.BlockPos pos) Call this whenCapabilityCache's host has been moved.
Clears all internal state and sets a new world / pos.
-
Constructor Details
-
CapabilityCache
public CapabilityCache() -
CapabilityCache
public CapabilityCache(net.minecraft.server.level.ServerLevel level, net.minecraft.core.BlockPos pos)
-
-
Method Details
-
setLevelPos
public void setLevelPos(net.minecraft.server.level.ServerLevel level, net.minecraft.core.BlockPos pos) Call this whenCapabilityCache's host has been moved.
Clears all internal state and sets a new world / pos.- Parameters:
level- The new world.pos- The new pos.
-
clear
public void clear()Clears all internal state. Everything will be re-cached. -
getCapabilityOr
public <T> T getCapabilityOr(net.neoforged.neoforge.capabilities.BlockCapability<T, net.minecraft.core.Direction> capability, net.minecraft.core.Direction to, T default_) Overload ofgetCapability(net.neoforged.neoforge.capabilities.BlockCapability<T, net.minecraft.core.Direction>, 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
@Nullable public <T> T getCapability(net.neoforged.neoforge.capabilities.BlockCapability<T, @Nullable net.minecraft.core.Direction> 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:
- The capability instance or
null.
-
getCapability
@Nullable public <T,C> T getCapability(net.neoforged.neoforge.capabilities.BlockCapability<T, C> capability, net.minecraft.core.Direction to, C ctx) Gets a capability from the block intodirection fromCapabilityCache's position.- Parameters:
capability- The capability to get.to- The direction to ask.ctx- The context, usually the face.- Returns:
- The capability instance or
null.
-