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.

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 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 when CapabilityCache'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)
      Notifies CapabilityCache of a IForgeBlock.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 by from block, 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 of getCapability(net.minecraftforge.common.capabilities.Capability<T>, net.minecraft.core.Direction) with support for evaluating a NonNullSupplier when 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 to or what default_ supplies.
    • getCapabilityOr

      public <T> T getCapabilityOr(net.minecraftforge.common.capabilities.Capability<T> capability, net.minecraft.core.Direction to, T default_)
      Overload of getCapability(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 to or default_
    • 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 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:
      A LazyOptional of the capability, may be empty.