public class DemoBlockEntity extends BlockEntity { public int number = 0; public DemoBlockEntity(BlockPos pos, BlockState state) { super(ExampleMod.DEMO_BLOCK_ENTITY, pos, state); } @Override public void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registries) { nbt.putInt("number", number); super.writeNbt(nbt, registries); } @Override public void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registries) { super.readNbt(nbt, registries); // 对于 1.21.5 之前的版本,请使用 nbt.getInt("number"); number = nbt.getInt("number", 0); } }