Search in sources :

Example 1 with PulleyTileEntity

use of com.simibubi.create.content.contraptions.components.structureMovement.pulley.PulleyTileEntity in project Create by Creators-of-Create.

the class Contraption method addBlocksToWorld.

public void addBlocksToWorld(Level world, StructureTransform transform) {
    for (boolean nonBrittles : Iterate.trueAndFalse) {
        for (StructureBlockInfo block : blocks.values()) {
            if (nonBrittles == BlockMovementChecks.isBrittle(block.state))
                continue;
            BlockPos targetPos = transform.apply(block.pos);
            BlockState state = transform.apply(block.state);
            if (customBlockPlacement(world, targetPos, state))
                continue;
            if (nonBrittles)
                for (Direction face : Iterate.directions) state = state.updateShape(face, world.getBlockState(targetPos.relative(face)), world, targetPos, targetPos.relative(face));
            BlockState blockState = world.getBlockState(targetPos);
            if (blockState.getDestroySpeed(world, targetPos) == -1 || (state.getCollisionShape(world, targetPos).isEmpty() && !blockState.getCollisionShape(world, targetPos).isEmpty())) {
                if (targetPos.getY() == 0)
                    targetPos = targetPos.above();
                world.levelEvent(2001, targetPos, Block.getId(state));
                Block.dropResources(state, world, targetPos, null);
                continue;
            }
            if (state.getBlock() instanceof SimpleWaterloggedBlock && state.hasProperty(BlockStateProperties.WATERLOGGED)) {
                FluidState FluidState = world.getFluidState(targetPos);
                state = state.setValue(BlockStateProperties.WATERLOGGED, FluidState.getType() == Fluids.WATER);
            }
            world.destroyBlock(targetPos, true);
            world.setBlock(targetPos, state, Block.UPDATE_MOVE_BY_PISTON | Block.UPDATE_ALL);
            boolean verticalRotation = transform.rotationAxis == null || transform.rotationAxis.isHorizontal();
            verticalRotation = verticalRotation && transform.rotation != Rotation.NONE;
            if (verticalRotation) {
                if (state.getBlock() instanceof RopeBlock || state.getBlock() instanceof MagnetBlock)
                    world.destroyBlock(targetPos, true);
            }
            BlockEntity tileEntity = world.getBlockEntity(targetPos);
            CompoundTag tag = block.nbt;
            if (tileEntity != null)
                tag = NBTProcessors.process(tileEntity, tag, false);
            if (tileEntity != null && tag != null) {
                tag.putInt("x", targetPos.getX());
                tag.putInt("y", targetPos.getY());
                tag.putInt("z", targetPos.getZ());
                if (verticalRotation && tileEntity instanceof PulleyTileEntity) {
                    tag.remove("Offset");
                    tag.remove("InitialOffset");
                }
                if (tileEntity instanceof IMultiTileContainer && tag.contains("LastKnownPos"))
                    tag.put("LastKnownPos", NbtUtils.writeBlockPos(BlockPos.ZERO.below(Integer.MAX_VALUE - 1)));
                tileEntity.load(tag);
                if (storage.containsKey(block.pos)) {
                    MountedStorage mountedStorage = storage.get(block.pos);
                    if (mountedStorage.isValid())
                        mountedStorage.addStorageToWorld(tileEntity);
                }
                if (fluidStorage.containsKey(block.pos)) {
                    MountedFluidStorage mountedStorage = fluidStorage.get(block.pos);
                    if (mountedStorage.isValid())
                        mountedStorage.addStorageToWorld(tileEntity);
                }
            }
            transform.apply(tileEntity);
        }
    }
    for (StructureBlockInfo block : blocks.values()) {
        if (!shouldUpdateAfterMovement(block))
            continue;
        BlockPos targetPos = transform.apply(block.pos);
        world.markAndNotifyBlock(targetPos, world.getChunkAt(targetPos), block.state, block.state, Block.UPDATE_MOVE_BY_PISTON | Block.UPDATE_ALL, 512);
    }
    for (int i = 0; i < inventory.getSlots(); i++) {
        if (!inventory.isSlotExternal(i))
            inventory.setStackInSlot(i, ItemStack.EMPTY);
    }
    for (int i = 0; i < fluidInventory.getTanks(); i++) fluidInventory.drain(fluidInventory.getFluidInTank(i), FluidAction.EXECUTE);
    for (Pair<BlockPos, Direction> pair : superglue) {
        BlockPos targetPos = transform.apply(pair.getKey());
        Direction targetFacing = transform.transformFacing(pair.getValue());
        SuperGlueEntity entity = new SuperGlueEntity(world, targetPos, targetFacing);
        if (entity.onValidSurface()) {
            if (!world.isClientSide)
                world.addFreshEntity(entity);
        }
    }
}
Also used : MagnetBlock(com.simibubi.create.content.contraptions.components.structureMovement.pulley.PulleyBlock.MagnetBlock) SimpleWaterloggedBlock(net.minecraft.world.level.block.SimpleWaterloggedBlock) IMultiTileContainer(com.simibubi.create.foundation.tileEntity.IMultiTileContainer) Direction(net.minecraft.core.Direction) RopeBlock(com.simibubi.create.content.contraptions.components.structureMovement.pulley.PulleyBlock.RopeBlock) PulleyTileEntity(com.simibubi.create.content.contraptions.components.structureMovement.pulley.PulleyTileEntity) BlockState(net.minecraft.world.level.block.state.BlockState) SuperGlueEntity(com.simibubi.create.content.contraptions.components.structureMovement.glue.SuperGlueEntity) BlockPos(net.minecraft.core.BlockPos) StructureBlockInfo(net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate.StructureBlockInfo) CompoundTag(net.minecraft.nbt.CompoundTag) FluidState(net.minecraft.world.level.material.FluidState) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Aggregations

SuperGlueEntity (com.simibubi.create.content.contraptions.components.structureMovement.glue.SuperGlueEntity)1 MagnetBlock (com.simibubi.create.content.contraptions.components.structureMovement.pulley.PulleyBlock.MagnetBlock)1 RopeBlock (com.simibubi.create.content.contraptions.components.structureMovement.pulley.PulleyBlock.RopeBlock)1 PulleyTileEntity (com.simibubi.create.content.contraptions.components.structureMovement.pulley.PulleyTileEntity)1 IMultiTileContainer (com.simibubi.create.foundation.tileEntity.IMultiTileContainer)1 BlockPos (net.minecraft.core.BlockPos)1 Direction (net.minecraft.core.Direction)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 SimpleWaterloggedBlock (net.minecraft.world.level.block.SimpleWaterloggedBlock)1 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)1 BlockState (net.minecraft.world.level.block.state.BlockState)1 StructureBlockInfo (net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate.StructureBlockInfo)1 FluidState (net.minecraft.world.level.material.FluidState)1