Class HandlerType<T>

java.lang.Object
net.ixdarklord.coolcatcore.api.handler.HandlerType<T>
Type Parameters:
T - the handler's type

public final class HandlerType<T> extends Object
A kind of handler blocks, entities and items can hand out: something other code works with without knowing the holder (an inventory, an energy buffer, a fluid tank...).

 public static final HandlerType<EnergyStorage> ENERGY = HandlerType.create(id("energy"), EnergyStorage.class);

 // Offer it: implement HandlerProvider (ExtendedBlockEntity does, through its HandlerMap), or register a provider,
 // also for blocks, entities and items from other mods.
 ENERGY.registerBlockEntity(MY_GENERATOR.get(), (generator, side) -> generator.energy());

 // Use it:
 EnergyStorage storage = ENERGY.find(level, pos, Direction.UP);
 BlockHandlerCache<EnergyStorage> cache = ENERGY.createCache(level, pos, Direction.UP); // for repeated lookups
 
Lookups ask, in order: the holder itself (HandlerProvider, ItemHandlerProvider), the providers registered for its block, block entity type, entity type or item, then the fallbacks. The first non-null wins.
  • Method Details

    • create

      public static <T> HandlerType<T> create(net.minecraft.resources.ResourceLocation id, Class<T> handlerClass)
      Creates a handler type; each id may be used once.
    • byId

      @Nullable public static @Nullable HandlerType<?> byId(net.minecraft.resources.ResourceLocation id)
    • id

      public net.minecraft.resources.ResourceLocation id()
    • handlerClass

      public Class<T> handlerClass()
    • registerBlock

      public HandlerType<T> registerBlock(HandlerType.BlockProvider<T> provider, net.minecraft.world.level.block.Block... blocks)
    • registerBlockEntity

      public <BE extends net.minecraft.world.level.block.entity.BlockEntity> HandlerType<T> registerBlockEntity(net.minecraft.world.level.block.entity.BlockEntityType<BE> type, HandlerType.BlockEntityProvider<? super BE,T> provider)
    • registerBlockFallback

      public HandlerType<T> registerBlockFallback(HandlerType.BlockProvider<T> provider)
      Asked for every block the registered providers had nothing for.
    • registerEntity

      public <E extends net.minecraft.world.entity.Entity> HandlerType<T> registerEntity(net.minecraft.world.entity.EntityType<E> type, HandlerType.EntityProvider<? super E,T> provider)
    • registerEntityFallback

      public HandlerType<T> registerEntityFallback(HandlerType.EntityProvider<net.minecraft.world.entity.Entity,T> provider)
    • registerItem

      public HandlerType<T> registerItem(HandlerType.ItemProvider<T> provider, net.minecraft.world.level.ItemLike... items)
    • registerItemFallback

      public HandlerType<T> registerItemFallback(HandlerType.ItemProvider<T> provider)
    • find

      @Nullable public T find(net.minecraft.world.level.Level level, net.minecraft.core.BlockPos pos, @Nullable @Nullable net.minecraft.core.Direction side)
      The handler of the block at a position, through a face (null for the block as a whole).
    • find

      @Nullable public T find(net.minecraft.world.level.Level level, net.minecraft.core.BlockPos pos, net.minecraft.world.level.block.state.BlockState state, @Nullable @Nullable net.minecraft.world.level.block.entity.BlockEntity blockEntity, @Nullable @Nullable net.minecraft.core.Direction side)
    • find

      @Nullable public T find(net.minecraft.world.entity.Entity entity)
    • find

      @Nullable public T find(net.minecraft.world.entity.Entity entity, @Nullable @Nullable net.minecraft.core.Direction side)
      The entity's handler, through a face for entities that tell them apart (a minecart's hopper side).
    • find

      @Nullable public T find(net.minecraft.world.item.ItemStack stack)
    • createCache

      public BlockHandlerCache<T> createCache(net.minecraft.world.level.Level level, net.minecraft.core.BlockPos pos, @Nullable @Nullable net.minecraft.core.Direction side)
      A cache for looking up the same position repeatedly (a pipe's neighbour). It notices on its own when the block or block entity there changes; holders that change what they hand out call invalidate(net.minecraft.world.level.Level, net.minecraft.core.BlockPos).
    • createCache

      public BlockHandlerCache<T> createCache(net.minecraft.world.level.Level level, net.minecraft.core.BlockPos pos, @Nullable @Nullable net.minecraft.core.Direction side, Runnable onInvalidate)
      Like createCache(Level, BlockPos, Direction), running onInvalidate when it's invalidated.
    • invalidate

      public static void invalidate(net.minecraft.world.level.Level level, net.minecraft.core.BlockPos pos)
      Tells every cache at a position (of any handler type, CoolCatLib's and the loader's) to look again: call it when a block starts or stops handing out a handler, or hands out a different one.
    • toString

      public String toString()
      Overrides:
      toString in class Object