Search in sources :

Example 1 with BlockEntityAccessor

use of org.spongepowered.common.accessor.world.level.block.entity.BlockEntityAccessor in project SpongeCommon by SpongePowered.

the class VolumeStreamUtils method getBlockEntityOrCloneToBackingVolume.

@NonNull
public static BiConsumer<BlockPos, net.minecraft.world.level.block.entity.BlockEntity> getBlockEntityOrCloneToBackingVolume(final boolean shouldCarbonCopy, final ObjectArrayMutableBlockEntityBuffer backingVolume, @Nullable final Level level) {
    return shouldCarbonCopy ? (pos, tile) -> {
        final CompoundTag nbt = tile.save(new CompoundTag());
        final net.minecraft.world.level.block.entity.@Nullable BlockEntity cloned = tile.getType().create();
        final BlockState state = tile.getBlockState();
        Objects.requireNonNull(cloned, () -> String.format("TileEntityType[%s] creates a null TileEntity!", BlockEntityType.getKey(tile.getType()))).load(state, nbt);
        if (level != null) {
            ((BlockEntityAccessor) cloned).accessor$level(level);
        }
        backingVolume.addBlockEntity(pos.getX(), pos.getY(), pos.getZ(), (BlockEntity) cloned);
    } : (pos, tile) -> {
    };
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) CompoundTag(net.minecraft.nbt.CompoundTag) Nullable(org.checkerframework.checker.nullness.qual.Nullable) BlockEntityAccessor(org.spongepowered.common.accessor.world.level.block.entity.BlockEntityAccessor) MonotonicNonNull(org.checkerframework.checker.nullness.qual.MonotonicNonNull) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Aggregations

CompoundTag (net.minecraft.nbt.CompoundTag)1 BlockState (net.minecraft.world.level.block.state.BlockState)1 MonotonicNonNull (org.checkerframework.checker.nullness.qual.MonotonicNonNull)1 NonNull (org.checkerframework.checker.nullness.qual.NonNull)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1 BlockEntityAccessor (org.spongepowered.common.accessor.world.level.block.entity.BlockEntityAccessor)1