Class CapabilityCache

java.lang.Object
codechicken.lib.capability.CapabilityCache

public class CapabilityCache extends Object
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 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 when CapabilityCache'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_)
      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 to or default_
    • 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 in to direction from CapabilityCache's position. For example, calling this with NORTH, will get a capability from the block IN NORTH direction on ITS SOUTH face.
      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 in to direction from CapabilityCache'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.