Class ExtendedBlockEntity

java.lang.Object
net.minecraft.world.level.block.entity.BlockEntity
net.ixdarklord.coolcatcore.api.block.ExtendedBlockEntity
All Implemented Interfaces:
HandlerProvider
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.