Search in sources :

Example 1 with SpawnerBlock

use of net.minecraft.world.level.block.SpawnerBlock in project RepurposedStructures by TelepathicGrunt.

the class NbtDungeon method spawnLootBlocks.

/**
 * Places and connects chests on walls of dungeon space
 */
private void spawnLootBlocks(WorldGenLevel world, Random random, BlockPos position, NbtDungeonConfig config, BlockPos fullLengths, BlockPos halfLengths, BlockPos.MutableBlockPos mutable) {
    boolean isPlacingChestLikeBlock = config.lootBlock.getBlock() instanceof ChestBlock;
    // Add chests that are wall based
    for (int currentChestAttempt = 0; currentChestAttempt < config.maxNumOfChests; ) {
        boolean addedChestThisAttempt = false;
        for (int currentChestPosAttempt = 0; currentChestPosAttempt < fullLengths.getX() + fullLengths.getZ() + halfLengths.getY(); ++currentChestPosAttempt) {
            mutable.set(position).move(random.nextInt(Math.max(fullLengths.getX() - 2, 1)) - halfLengths.getX() + 1, random.nextInt(Math.max(fullLengths.getY() - 1, 1)), random.nextInt(Math.max(fullLengths.getZ() - 2, 1)) - halfLengths.getZ() + 1);
            BlockState currentBlock = world.getBlockState(mutable);
            if (isValidNonSolidBlock(config, currentBlock)) {
                BlockState belowState = world.getBlockState(mutable.move(Direction.DOWN));
                if (belowState.isFaceSturdy(world, mutable, Direction.UP) && belowState.getBlock() != config.lootBlock.getBlock()) {
                    mutable.move(Direction.UP);
                    boolean isOnWall = false;
                    for (Direction neighborDirection : Direction.Plane.HORIZONTAL) {
                        mutable.move(neighborDirection);
                        BlockState neighboringState = world.getBlockState(mutable);
                        mutable.move(neighborDirection.getOpposite());
                        if (isPlacingChestLikeBlock && neighboringState.getBlock() instanceof ChestBlock) {
                            // Only connect to single chests
                            if (neighboringState.getValue(ChestBlock.TYPE) == ChestType.SINGLE) {
                                BlockState currentStateForChest = GeneralUtils.orientateChest(world, mutable, config.lootBlock);
                                Direction currentDirection = currentStateForChest.getValue(HorizontalDirectionalBlock.FACING);
                                // If oriented is on same axis as neighboring chest, find a new direction on sides.
                                if (neighborDirection.getAxis() == currentDirection.getAxis()) {
                                    currentDirection = currentDirection.getClockWise();
                                    BlockPos wallCheckPos = mutable.relative(currentDirection);
                                    BlockPos wallCheckPos2 = wallCheckPos.relative(neighborDirection);
                                    BlockState blockState = world.getBlockState(wallCheckPos);
                                    BlockState blockState2 = world.getBlockState(wallCheckPos2);
                                    // If first side is solid wall we are facing or neighbor is facing, switch to other side
                                    if ((blockState.getMaterial().isSolid() && !(blockState.getBlock() instanceof SpawnerBlock)) || (blockState2.getMaterial().isSolid() && !(blockState2.getBlock() instanceof SpawnerBlock))) {
                                        currentDirection = currentDirection.getOpposite();
                                    }
                                }
                                boolean chestTyping = neighborDirection.getAxisDirection() == currentDirection.getAxisDirection();
                                if (neighborDirection.getAxis() == Direction.Axis.Z) {
                                    chestTyping = !chestTyping;
                                }
                                // Place chest
                                world.setBlock(mutable, config.lootBlock.setValue(ChestBlock.WATERLOGGED, currentBlock.getFluidState().is(FluidTags.WATER)).setValue(ChestBlock.FACING, currentDirection).setValue(ChestBlock.TYPE, chestTyping ? ChestType.RIGHT : ChestType.LEFT), 2);
                                RandomizableContainerBlockEntity.setLootTable(world, random, mutable, config.chestResourcelocation);
                                // Set neighboring chest to face same way too
                                world.setBlock(mutable.move(neighborDirection), neighboringState.setValue(ChestBlock.FACING, currentDirection).setValue(ChestBlock.TYPE, chestTyping ? ChestType.LEFT : ChestType.RIGHT), 2);
                                RandomizableContainerBlockEntity.setLootTable(world, random, mutable, config.chestResourcelocation);
                                SolidifyBlock(world, mutable.below());
                                // Skip wall code as we already placed chest
                                isOnWall = false;
                                currentChestAttempt++;
                                addedChestThisAttempt = true;
                                if (currentChestAttempt == config.maxNumOfChests) {
                                    return;
                                }
                                break;
                            }
                        } else if (GeneralUtils.isFullCube(world, mutable, neighboringState) && !(neighboringState.getBlock() instanceof SpawnerBlock)) {
                            isOnWall = true;
                        }
                    }
                    // Is not next to another chest.
                    if (isOnWall) {
                        BlockState lootBlock = config.lootBlock;
                        if (lootBlock.hasProperty(BlockStateProperties.WATERLOGGED)) {
                            lootBlock.setValue(BlockStateProperties.WATERLOGGED, currentBlock.getFluidState().is(FluidTags.WATER));
                        }
                        if (lootBlock.hasProperty(BlockStateProperties.HORIZONTAL_FACING)) {
                            lootBlock = GeneralUtils.orientateChest(world, mutable, lootBlock);
                        }
                        // Set chest to face away from wall.
                        world.setBlock(mutable, lootBlock, 2);
                        currentChestAttempt++;
                        addedChestThisAttempt = true;
                        RandomizableContainerBlockEntity.setLootTable(world, random, mutable, config.chestResourcelocation);
                        mutable.move(Direction.DOWN);
                        if (lootBlock.getBlock() == Blocks.SHULKER_BOX && world.getBlockEntity(mutable) == null) {
                            world.setBlock(mutable, Blocks.SPAWNER.defaultBlockState(), 2);
                            BlockEntity blockEntity = world.getBlockEntity(mutable);
                            if (blockEntity instanceof SpawnerBlockEntity) {
                                SetMobSpawnerEntity(random, config, (SpawnerBlockEntity) blockEntity);
                            }
                        } else {
                            SolidifyBlock(world, mutable);
                        }
                        break;
                    }
                }
            }
        }
        if (!addedChestThisAttempt)
            currentChestAttempt++;
    }
}
Also used : SpawnerBlockEntity(net.minecraft.world.level.block.entity.SpawnerBlockEntity) BlockState(net.minecraft.world.level.block.state.BlockState) BlockPos(net.minecraft.core.BlockPos) SpawnerBlock(net.minecraft.world.level.block.SpawnerBlock) ChestBlock(net.minecraft.world.level.block.ChestBlock) Direction(net.minecraft.core.Direction) RandomizableContainerBlockEntity(net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity) SpawnerBlockEntity(net.minecraft.world.level.block.entity.SpawnerBlockEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 2 with SpawnerBlock

use of net.minecraft.world.level.block.SpawnerBlock in project RepurposedStructures by TelepathicGrunt.

the class SpawnerRandomizingProcessor method processBlock.

@Override
public StructureTemplate.StructureBlockInfo processBlock(LevelReader worldView, BlockPos pos, BlockPos blockPos, StructureTemplate.StructureBlockInfo structureBlockInfoLocal, StructureTemplate.StructureBlockInfo structureBlockInfoWorld, StructurePlaceSettings structurePlacementData) {
    if (structureBlockInfoWorld.state.getBlock() instanceof SpawnerBlock) {
        BlockPos worldPos = structureBlockInfoWorld.pos;
        Random random = new WorldgenRandom(new LegacyRandomSource(0L));
        random.setSeed(worldPos.asLong() * worldPos.getY());
        CompoundTag spawnerNBT = SetMobSpawnerEntity(random, structureBlockInfoWorld.nbt);
        if (spawnerNBT == null) {
            return new StructureTemplate.StructureBlockInfo(worldPos, Blocks.AIR.defaultBlockState(), null);
        } else {
            return new StructureTemplate.StructureBlockInfo(worldPos, structureBlockInfoWorld.state, spawnerNBT);
        }
    }
    return structureBlockInfoWorld;
}
Also used : WorldgenRandom(net.minecraft.world.level.levelgen.WorldgenRandom) Random(java.util.Random) LegacyRandomSource(net.minecraft.world.level.levelgen.LegacyRandomSource) BlockPos(net.minecraft.core.BlockPos) WorldgenRandom(net.minecraft.world.level.levelgen.WorldgenRandom) SpawnerBlock(net.minecraft.world.level.block.SpawnerBlock) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

BlockPos (net.minecraft.core.BlockPos)2 SpawnerBlock (net.minecraft.world.level.block.SpawnerBlock)2 Random (java.util.Random)1 Direction (net.minecraft.core.Direction)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 ChestBlock (net.minecraft.world.level.block.ChestBlock)1 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)1 RandomizableContainerBlockEntity (net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity)1 SpawnerBlockEntity (net.minecraft.world.level.block.entity.SpawnerBlockEntity)1 BlockState (net.minecraft.world.level.block.state.BlockState)1 LegacyRandomSource (net.minecraft.world.level.levelgen.LegacyRandomSource)1 WorldgenRandom (net.minecraft.world.level.levelgen.WorldgenRandom)1