Search in sources :

Example 1 with BedPart

use of net.minecraft.world.level.block.state.properties.BedPart in project MC-Prefab by Brian-Wuest.

the class Structure method ScanStructure.

public static void ScanStructure(Level world, BlockPos originalPos, BlockPos cornerPos1, BlockPos cornerPos2, String fileLocation, BuildClear clearedSpace, Direction playerFacing, boolean includeAir, boolean excludeWater) {
    Structure scannedStructure = new Structure();
    scannedStructure.setClearSpace(clearedSpace);
    for (BlockPos currentPos : BlockPos.betweenClosed(cornerPos1, cornerPos2)) {
        if (world.isEmptyBlock(currentPos) && !includeAir) {
            continue;
        }
        BlockState currentState = world.getBlockState(currentPos);
        Block currentBlock = currentState.getBlock();
        if (currentState.getMaterial() == Material.WATER && excludeWater) {
            continue;
        }
        BuildBlock buildBlock = Structure.createBuildBlockFromBlockState(currentState, currentBlock, currentPos, originalPos);
        if (currentBlock instanceof DoorBlock) {
            DoubleBlockHalf blockHalf = currentState.getValue(DoorBlock.HALF);
            if (blockHalf == DoubleBlockHalf.LOWER) {
                BlockState upperHalfState = world.getBlockState(currentPos.above());
                if (upperHalfState.getBlock() instanceof DoorBlock) {
                    Block upperBlock = upperHalfState.getBlock();
                    BuildBlock upperHalf = Structure.createBuildBlockFromBlockState(upperHalfState, upperBlock, currentPos.above(), originalPos);
                    buildBlock.setSubBlock(upperHalf);
                }
            } else {
                // Don't process upper door halves. These were already done.
                continue;
            }
        } else if (currentBlock instanceof BedBlock) {
            BedPart bedPart = currentState.getValue(BedBlock.PART);
            if (bedPart == BedPart.HEAD) {
                BlockState bedFoot = null;
                boolean foundFoot = false;
                Direction facing = Direction.NORTH;
                while (!foundFoot) {
                    bedFoot = world.getBlockState(currentPos.relative(facing));
                    if (bedFoot.getBlock() instanceof BedBlock && bedFoot.getValue(BedBlock.PART) == BedPart.FOOT) {
                        foundFoot = true;
                        break;
                    }
                    facing = facing.getClockWise();
                    if (facing == Direction.NORTH) {
                        // Got back to north, break out to avoid infinite loop.
                        break;
                    }
                }
                if (foundFoot) {
                    Block footBedBlock = bedFoot.getBlock();
                    BuildBlock bed = Structure.createBuildBlockFromBlockState(bedFoot, footBedBlock, currentPos.relative(facing), originalPos);
                    buildBlock.setSubBlock(bed);
                }
            } else {
                // Don't process foot of bed, it was already done.
                continue;
            }
        }
        scannedStructure.getBlocks().add(buildBlock);
        BlockEntity tileEntity = world.getBlockEntity(currentPos);
        if (tileEntity != null) {
            // Don't write data for empty tile entities.
            if ((tileEntity instanceof ChestBlockEntity && ((ChestBlockEntity) tileEntity).isEmpty()) || (tileEntity instanceof FurnaceBlockEntity && ((FurnaceBlockEntity) tileEntity).isEmpty())) {
                continue;
            }
            ResourceLocation resourceLocation = ForgeRegistries.BLOCK_ENTITIES.getKey(tileEntity.getType());
            CompoundTag tagCompound = tileEntity.saveWithFullMetadata();
            BuildTileEntity buildTileEntity = new BuildTileEntity();
            assert resourceLocation != null;
            buildTileEntity.setEntityDomain(resourceLocation.getNamespace());
            buildTileEntity.setEntityName(resourceLocation.getPath());
            buildTileEntity.setStartingPosition(Structure.getStartingPositionFromOriginalAndCurrentPosition(currentPos, originalPos));
            buildTileEntity.setEntityNBTData(tagCompound);
            scannedStructure.tileEntities.add(buildTileEntity);
        }
    }
    int x_radiusRangeBegin = Math.min(cornerPos1.getX(), cornerPos2.getX());
    int x_radiusRangeEnd = Math.max(cornerPos1.getX(), cornerPos2.getX());
    int y_radiusRangeBegin = Math.min(cornerPos1.getY(), cornerPos2.getY());
    int y_radiusRangeEnd = Math.max(cornerPos1.getY(), cornerPos2.getY());
    int z_radiusRangeBegin = Math.min(cornerPos1.getZ(), cornerPos2.getZ());
    int z_radiusRangeEnd = Math.max(cornerPos1.getZ(), cornerPos2.getZ());
    AABB axis = new AABB(cornerPos1, cornerPos2);
    for (Entity entity : world.getEntities(null, axis)) {
        // TODO: This was the "getPosition" method.
        BlockPos entityPos = entity.blockPosition();
        if (entity instanceof HangingEntity) {
            // Use the HangingEntity getPos function instead since it is more accurate for itemframes and paintings.
            entityPos = ((HangingEntity) entity).getPos();
        }
        if (entityPos.getX() >= x_radiusRangeBegin && entityPos.getX() <= x_radiusRangeEnd && entityPos.getZ() >= z_radiusRangeBegin && entityPos.getZ() <= z_radiusRangeEnd && entityPos.getY() >= y_radiusRangeBegin && entityPos.getY() <= y_radiusRangeEnd) {
            BuildEntity buildEntity = new BuildEntity();
            buildEntity.setEntityResourceString(ForgeRegistries.ENTITIES.getKey(entity.getType()));
            buildEntity.setStartingPosition(Structure.getStartingPositionFromOriginalAndCurrentPosition(entityPos, originalPos));
            // The function calls below get the following fields from the "entity" class. posX, posY, posZ.
            // This will probably have to change when the mappings get updated.
            buildEntity.entityXAxisOffset = entityPos.getX() - entity.getX();
            buildEntity.entityYAxisOffset = entityPos.getY() - entity.getY();
            buildEntity.entityZAxisOffset = entityPos.getZ() - entity.getZ();
            if (entity instanceof ItemFrame) {
                buildEntity.entityYAxisOffset = buildEntity.entityYAxisOffset * -1;
            }
            if (entity instanceof HangingEntity) {
                buildEntity.entityFacing = entity.getDirection();
            }
            CompoundTag entityTagCompound = new CompoundTag();
            entity.saveAsPassenger(entityTagCompound);
            buildEntity.setEntityNBTData(entityTagCompound);
            scannedStructure.entities.add(buildEntity);
        }
    }
    Structure.CreateStructureFile(scannedStructure, fileLocation);
}
Also used : ChestBlockEntity(net.minecraft.world.level.block.entity.ChestBlockEntity) HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) FurnaceBlockEntity(net.minecraft.world.level.block.entity.FurnaceBlockEntity) Entity(net.minecraft.world.entity.Entity) HangingEntity(net.minecraft.world.entity.decoration.HangingEntity) FurnaceBlockEntity(net.minecraft.world.level.block.entity.FurnaceBlockEntity) ItemFrame(net.minecraft.world.entity.decoration.ItemFrame) Direction(net.minecraft.core.Direction) BedPart(net.minecraft.world.level.block.state.properties.BedPart) BlockState(net.minecraft.world.level.block.state.BlockState) ChestBlockEntity(net.minecraft.world.level.block.entity.ChestBlockEntity) DoubleBlockHalf(net.minecraft.world.level.block.state.properties.DoubleBlockHalf) ResourceLocation(net.minecraft.resources.ResourceLocation) BlockPos(net.minecraft.core.BlockPos) CompoundTag(net.minecraft.nbt.CompoundTag) AABB(net.minecraft.world.phys.AABB) ChestBlockEntity(net.minecraft.world.level.block.entity.ChestBlockEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) FurnaceBlockEntity(net.minecraft.world.level.block.entity.FurnaceBlockEntity)

Aggregations

BlockPos (net.minecraft.core.BlockPos)1 Direction (net.minecraft.core.Direction)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 ResourceLocation (net.minecraft.resources.ResourceLocation)1 Entity (net.minecraft.world.entity.Entity)1 HangingEntity (net.minecraft.world.entity.decoration.HangingEntity)1 ItemFrame (net.minecraft.world.entity.decoration.ItemFrame)1 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)1 ChestBlockEntity (net.minecraft.world.level.block.entity.ChestBlockEntity)1 FurnaceBlockEntity (net.minecraft.world.level.block.entity.FurnaceBlockEntity)1 BlockState (net.minecraft.world.level.block.state.BlockState)1 BedPart (net.minecraft.world.level.block.state.properties.BedPart)1 DoubleBlockHalf (net.minecraft.world.level.block.state.properties.DoubleBlockHalf)1 AABB (net.minecraft.world.phys.AABB)1