Class ExtendedBlockEntity

java.lang.Object
net.minecraft.world.level.block.entity.BlockEntity
net.ixdarklord.coolcatcore.api.block.ExtendedBlockEntity
All Implemented Interfaces:
HandlerProvider, net.minecraft.core.TypedInstance<net.minecraft.world.level.block.entity.BlockEntityType<?>>, net.minecraft.util.debug.DebugValueSource
Direct Known Subclasses:
ExtendedContainerBlockEntity

public abstract class ExtendedBlockEntity extends net.minecraft.world.level.block.entity.BlockEntity implements HandlerProvider
A block entity with Attachment data, handlers and ticking made simple. Its data saves and syncs on its own (every block entity's does; this class adds shortcuts), so a typical subclass is only its logic:
public class GeneratorBlockEntity extends ExtendedBlockEntity {
    public static final Attachment<Integer> ENERGY = MyMod.ATTACHMENTS.builder("energy", Codec.INT, 0)
            .sync(SyncPolicy.ALL).keepOnDrop().build();

    public GeneratorBlockEntity(BlockPos pos, BlockState state) {
        super(MyBlockEntities.GENERATOR.get(), pos, state);
        this.handlers.addSided(MyHandlers.ENERGY, side -> side == Direction.UP ? null : this.storage);
    }

    @Override
    protected void serverTick(ServerLevel level) {
        this.update(ENERGY, energy -> Math.min(energy + 1, 1000));
    }
}

// In the block: public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level l, BlockState s, BlockEntityType<T> type)
//                   { return ExtendedBlockEntity.ticker(type, MyBlockEntities.GENERATOR.get()); }
For an inventory, extend ExtendedContainerBlockEntity.
  • Nested Class Summary

    Nested classes/interfaces inherited from interface net.minecraft.util.debug.DebugValueSource

    net.minecraft.util.debug.DebugValueSource.Registration, net.minecraft.util.debug.DebugValueSource.ValueGetter<T>
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final HandlerMap
    What getHandler(HandlerType, Direction) hands out; add to it in the constructor.

    Fields inherited from class net.minecraft.world.level.block.entity.BlockEntity

    level, remove, worldPosition
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    ExtendedBlockEntity(net.minecraft.world.level.block.entity.BlockEntityType<?> type, net.minecraft.core.BlockPos pos, net.minecraft.world.level.block.state.BlockState state)
     
  • Method Summary

    Modifier and Type
    Method
    Description
     
    protected void
    clientTick(net.minecraft.world.level.Level level)
    Runs every tick on clients, when the block uses ticker(BlockEntityType, BlockEntityType).
    <T> T
    get(Attachment<T> attachment)
     
    <T> T
    getHandler(HandlerType<T> type, @Nullable net.minecraft.core.Direction side)
     
    void
    Tells handler caches at this position to look again, after changing what getHandler(HandlerType, Direction) hands out.
    <T> void
    modify(Attachment<T> attachment, Consumer<T> mutator)
     
    protected void
    serverTick(net.minecraft.server.level.ServerLevel level)
    Runs every tick on the server, when the block uses ticker(BlockEntityType, BlockEntityType).
    <T> void
    set(Attachment<T> attachment, T value)
     
    static <T extends net.minecraft.world.level.block.entity.BlockEntity>
    @Nullable net.minecraft.world.level.block.entity.BlockEntityTicker<T>
    ticker(net.minecraft.world.level.block.entity.BlockEntityType<T> type, net.minecraft.world.level.block.entity.BlockEntityType<? extends ExtendedBlockEntity> expected)
    The ticker for a block's getTicker: ticks expected block entities through serverTick(ServerLevel)/clientTick(Level), and nothing else.
    <T> T
    update(Attachment<T> attachment, UnaryOperator<T> function)
     

    Methods inherited from class net.minecraft.world.level.block.entity.BlockEntity

    addEntityType, applyComponents, applyComponentsFromItemStack, applyImplicitComponents, clearRemoved, collectComponents, collectImplicitComponents, components, fillCrashReportCategory, getBlockPos, getBlockState, getLevel, getNameForReporting, getPosFromTag, getType, getUpdatePacket, getUpdateTag, hasLevel, isRemoved, isValidBlockState, loadAdditional, loadCustomOnly, loadStatic, loadWithComponents, parseCustomNameSafe, preRemoveSideEffects, problemPath, registerDebugValues, removeComponentsFromTag, saveAdditional, saveCustomOnly, saveCustomOnly, saveWithFullMetadata, saveWithFullMetadata, saveWithId, saveWithoutMetadata, saveWithoutMetadata, setBlockState, setChanged, setChanged, setComponents, setLevel, setRemoved, triggerEvent, typeHolder

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface net.minecraft.core.TypedInstance

    is, is, is, is, is, tags
  • Field Details

  • Constructor Details

    • ExtendedBlockEntity

      protected ExtendedBlockEntity(net.minecraft.world.level.block.entity.BlockEntityType<?> type, net.minecraft.core.BlockPos pos, net.minecraft.world.level.block.state.BlockState state)
  • Method Details

    • attachments

      public AttachmentHolder attachments()
    • get

      public <T> T get(Attachment<T> attachment)
    • set

      public <T> void set(Attachment<T> attachment, T value)
    • update

      public <T> T update(Attachment<T> attachment, UnaryOperator<T> function)
    • modify

      public <T> void modify(Attachment<T> attachment, Consumer<T> mutator)
    • getHandler

      @Nullable public <T> T getHandler(HandlerType<T> type, @Nullable @Nullable net.minecraft.core.Direction side)
      Specified by:
      getHandler in interface HandlerProvider
      Parameters:
      side - the face asked through, or null for the holder as a whole (always null for most entity lookups)
      Returns:
      the handler, or null if this holder has none of that type on that side
    • invalidateHandlers

      public void invalidateHandlers()
      Tells handler caches at this position to look again, after changing what getHandler(HandlerType, Direction) hands out.
    • serverTick

      protected void serverTick(net.minecraft.server.level.ServerLevel level)
      Runs every tick on the server, when the block uses ticker(BlockEntityType, BlockEntityType).
    • clientTick

      protected void clientTick(net.minecraft.world.level.Level level)
      Runs every tick on clients, when the block uses ticker(BlockEntityType, BlockEntityType).
    • ticker

      @Nullable public static <T extends net.minecraft.world.level.block.entity.BlockEntity> @Nullable net.minecraft.world.level.block.entity.BlockEntityTicker<T> ticker(net.minecraft.world.level.block.entity.BlockEntityType<T> type, net.minecraft.world.level.block.entity.BlockEntityType<? extends ExtendedBlockEntity> expected)
      The ticker for a block's getTicker: ticks expected block entities through serverTick(ServerLevel)/clientTick(Level), and nothing else.