Search in sources :

Example 1 with IMultiTileContainer

use of com.simibubi.create.foundation.tileEntity.IMultiTileContainer in project Create by Creators-of-Create.

the class PonderWorld method fixControllerTileEntities.

public void fixControllerTileEntities() {
    for (BlockEntity tileEntity : tileEntities.values()) {
        if (tileEntity instanceof BeltTileEntity) {
            BeltTileEntity beltTileEntity = (BeltTileEntity) tileEntity;
            if (!beltTileEntity.isController())
                continue;
            BlockPos controllerPos = tileEntity.getBlockPos();
            for (BlockPos blockPos : BeltBlock.getBeltChain(this, controllerPos)) {
                BlockEntity tileEntity2 = getBlockEntity(blockPos);
                if (!(tileEntity2 instanceof BeltTileEntity))
                    continue;
                BeltTileEntity belt2 = (BeltTileEntity) tileEntity2;
                belt2.setController(controllerPos);
            }
        }
        if (tileEntity instanceof IMultiTileContainer) {
            IMultiTileContainer multiTile = (IMultiTileContainer) tileEntity;
            BlockPos lastKnown = multiTile.getLastKnownPos();
            BlockPos current = tileEntity.getBlockPos();
            if (lastKnown == null || current == null)
                continue;
            if (multiTile.isController())
                continue;
            if (!lastKnown.equals(current)) {
                BlockPos newControllerPos = multiTile.getController().offset(current.subtract(lastKnown));
                multiTile.setController(newControllerPos);
            }
        }
    }
}
Also used : BeltTileEntity(com.simibubi.create.content.contraptions.relays.belt.BeltTileEntity) IMultiTileContainer(com.simibubi.create.foundation.tileEntity.IMultiTileContainer) BlockPos(net.minecraft.core.BlockPos) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 2 with IMultiTileContainer

use of com.simibubi.create.foundation.tileEntity.IMultiTileContainer 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

IMultiTileContainer (com.simibubi.create.foundation.tileEntity.IMultiTileContainer)2 BlockPos (net.minecraft.core.BlockPos)2 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)2 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 BeltTileEntity (com.simibubi.create.content.contraptions.relays.belt.BeltTileEntity)1 Direction (net.minecraft.core.Direction)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 SimpleWaterloggedBlock (net.minecraft.world.level.block.SimpleWaterloggedBlock)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