Search in sources :

Example 1 with ChestBlock

use of net.minecraft.world.level.block.ChestBlock 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 ChestBlock

use of net.minecraft.world.level.block.ChestBlock in project Mohist by MohistMC.

the class CraftChest method getInventory.

@Override
public Inventory getInventory() {
    CraftInventory inventory = (CraftInventory) this.getBlockInventory();
    if (!isPlaced() || isWorldGeneration()) {
        return inventory;
    }
    // The logic here is basically identical to the logic in ChestBlock.interact
    CraftWorld world = (CraftWorld) this.getWorld();
    ChestBlock blockChest = (ChestBlock) (this.getType() == Material.CHEST ? Blocks.CHEST : Blocks.TRAPPED_CHEST);
    MenuProvider nms = blockChest.getMenuProvider(data, world.getHandle(), this.getPosition(), true);
    if (nms instanceof ChestBlock.DoubleInventory) {
        inventory = new CraftInventoryDoubleChest((ChestBlock.DoubleInventory) nms);
    }
    return inventory;
}
Also used : CraftInventory(org.bukkit.craftbukkit.v1_18_R2.inventory.CraftInventory) ChestBlock(net.minecraft.world.level.block.ChestBlock) CraftInventoryDoubleChest(org.bukkit.craftbukkit.v1_18_R2.inventory.CraftInventoryDoubleChest) CraftWorld(org.bukkit.craftbukkit.v1_18_R2.CraftWorld) MenuProvider(net.minecraft.world.MenuProvider)

Example 3 with ChestBlock

use of net.minecraft.world.level.block.ChestBlock in project additionaladditions by Dqu1J.

the class WrenchItem method dispenserUse.

public void dispenserUse(Level world, BlockPos pos, BlockState state, ItemStack stack) {
    if (state.getBlock() instanceof ChestBlock)
        return;
    if (state.hasProperty(BlockStateProperties.FACING)) {
        if (tryPlacing(pos, state.cycle(BlockStateProperties.FACING), world)) {
            success(stack, world, pos);
        }
    }
    if (state.hasProperty(BlockStateProperties.FACING_HOPPER)) {
        BlockState newstate = state.cycle(BlockStateProperties.FACING_HOPPER);
        world.setBlockAndUpdate(pos, newstate);
        if (AdditionalAdditions.lithiumInstalled && !world.isClientSide()) {
            /*
                 * Lithium mod caches hopper's output and input inventories
                 * Which causes an issue where the hopper keeps transferring to the old location
                 * This replaces the block entity, which fixes that.
                 */
            HopperBlockEntity hopperBlockEntity = (HopperBlockEntity) world.getBlockEntity(pos);
            CompoundTag nbt = hopperBlockEntity.saveWithoutMetadata();
            world.removeBlockEntity(pos);
            HopperBlockEntity blockEntity = new HopperBlockEntity(pos, newstate);
            blockEntity.load(nbt);
            world.setBlockEntity(blockEntity);
        }
        success(stack, world, pos);
    }
    if (state.hasProperty(BlockStateProperties.HORIZONTAL_FACING)) {
        if (tryPlacing(pos, state.cycle(BlockStateProperties.HORIZONTAL_FACING), world)) {
            success(stack, world, pos);
        }
    }
    if (state.hasProperty(BlockStateProperties.AXIS)) {
        if (tryPlacing(pos, state.cycle(BlockStateProperties.AXIS), world)) {
            success(stack, world, pos);
        }
    }
    if (state.hasProperty(BlockStateProperties.HORIZONTAL_AXIS)) {
        if (tryPlacing(pos, state.cycle(BlockStateProperties.HORIZONTAL_AXIS), world)) {
            success(stack, world, pos);
        }
    }
    if (state.getBlock() instanceof SlabBlock) {
        world.setBlockAndUpdate(pos, state.cycle(BlockStateProperties.SLAB_TYPE));
        BlockState news = null;
        if (state.getValue(BlockStateProperties.SLAB_TYPE) == SlabType.BOTTOM)
            news = state.setValue(BlockStateProperties.SLAB_TYPE, SlabType.TOP);
        if (state.getValue(BlockStateProperties.SLAB_TYPE) == SlabType.TOP)
            news = state.setValue(BlockStateProperties.SLAB_TYPE, SlabType.BOTTOM);
        if (news != null) {
            world.setBlockAndUpdate(pos, news);
            success(stack, world, pos);
        }
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) HopperBlockEntity(net.minecraft.world.level.block.entity.HopperBlockEntity) SlabBlock(net.minecraft.world.level.block.SlabBlock) ChestBlock(net.minecraft.world.level.block.ChestBlock) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 4 with ChestBlock

use of net.minecraft.world.level.block.ChestBlock in project additionaladditions by Dqu1J.

the class WrenchItem method useOn.

@Override
public InteractionResult useOn(UseOnContext context) {
    if (!Config.getBool(ConfigValues.WRENCH)) {
        return InteractionResult.FAIL;
    }
    Level world = context.getLevel();
    BlockPos pos = context.getClickedPos();
    BlockState state = world.getBlockState(pos);
    if (context.getPlayer() == null)
        return InteractionResult.PASS;
    if (world.isClientSide())
        return InteractionResult.PASS;
    if (state.getBlock() instanceof ChestBlock || state.getBlock() instanceof BedBlock)
        return InteractionResult.PASS;
    if (state.hasProperty(BlockStateProperties.FACING)) {
        if (tryPlacing(pos, state.cycle(BlockStateProperties.FACING), world)) {
            success(context.getItemInHand(), world, pos, context.getPlayer(), context.getHand());
            return InteractionResult.SUCCESS;
        }
    }
    if (state.hasProperty(BlockStateProperties.FACING_HOPPER)) {
        BlockState newstate = state.cycle(BlockStateProperties.FACING_HOPPER);
        world.setBlockAndUpdate(pos, newstate);
        if (AdditionalAdditions.lithiumInstalled && !world.isClientSide()) {
            /*
                * Lithium mod caches hopper's output and input inventories
                * Which causes an issue where the hopper keeps transferring to the old location
                * This replaces the block entity, which fixes that.
                */
            HopperBlockEntity hopperBlockEntity = (HopperBlockEntity) world.getBlockEntity(pos);
            CompoundTag nbt = hopperBlockEntity.saveWithoutMetadata();
            world.removeBlockEntity(pos);
            HopperBlockEntity blockEntity = new HopperBlockEntity(pos, newstate);
            blockEntity.load(nbt);
            world.setBlockEntity(blockEntity);
        }
        success(context.getItemInHand(), world, pos, context.getPlayer(), context.getHand());
        return InteractionResult.SUCCESS;
    }
    if (state.hasProperty(BlockStateProperties.HORIZONTAL_FACING)) {
        if (tryPlacing(pos, state.cycle(BlockStateProperties.HORIZONTAL_FACING), world)) {
            success(context.getItemInHand(), world, pos, context.getPlayer(), context.getHand());
            return InteractionResult.SUCCESS;
        }
    }
    if (state.hasProperty(BlockStateProperties.AXIS)) {
        if (tryPlacing(pos, state.cycle(BlockStateProperties.AXIS), world)) {
            success(context.getItemInHand(), world, pos, context.getPlayer(), context.getHand());
            return InteractionResult.SUCCESS;
        }
    }
    if (state.hasProperty(BlockStateProperties.HORIZONTAL_AXIS)) {
        if (tryPlacing(pos, state.cycle(BlockStateProperties.HORIZONTAL_AXIS), world)) {
            success(context.getItemInHand(), world, pos, context.getPlayer(), context.getHand());
            return InteractionResult.SUCCESS;
        }
    }
    if (state.getBlock() instanceof SlabBlock) {
        BlockState newState = state;
        if (state.getValue(BlockStateProperties.SLAB_TYPE).equals(SlabType.DOUBLE))
            return InteractionResult.PASS;
        if (state.getValue(BlockStateProperties.SLAB_TYPE).equals(SlabType.BOTTOM))
            newState = state.setValue(BlockStateProperties.SLAB_TYPE, SlabType.TOP);
        if (state.getValue(BlockStateProperties.SLAB_TYPE).equals(SlabType.TOP))
            newState = state.setValue(BlockStateProperties.SLAB_TYPE, SlabType.BOTTOM);
        world.setBlockAndUpdate(pos, newState);
        success(context.getItemInHand(), world, pos, context.getPlayer(), context.getHand());
        return InteractionResult.SUCCESS;
    }
    return InteractionResult.PASS;
}
Also used : BedBlock(net.minecraft.world.level.block.BedBlock) BlockState(net.minecraft.world.level.block.state.BlockState) HopperBlockEntity(net.minecraft.world.level.block.entity.HopperBlockEntity) SlabBlock(net.minecraft.world.level.block.SlabBlock) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) ChestBlock(net.minecraft.world.level.block.ChestBlock) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

ChestBlock (net.minecraft.world.level.block.ChestBlock)4 BlockState (net.minecraft.world.level.block.state.BlockState)3 BlockPos (net.minecraft.core.BlockPos)2 CompoundTag (net.minecraft.nbt.CompoundTag)2 SlabBlock (net.minecraft.world.level.block.SlabBlock)2 HopperBlockEntity (net.minecraft.world.level.block.entity.HopperBlockEntity)2 Direction (net.minecraft.core.Direction)1 MenuProvider (net.minecraft.world.MenuProvider)1 Level (net.minecraft.world.level.Level)1 BedBlock (net.minecraft.world.level.block.BedBlock)1 SpawnerBlock (net.minecraft.world.level.block.SpawnerBlock)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 CraftWorld (org.bukkit.craftbukkit.v1_18_R2.CraftWorld)1 CraftInventory (org.bukkit.craftbukkit.v1_18_R2.inventory.CraftInventory)1 CraftInventoryDoubleChest (org.bukkit.craftbukkit.v1_18_R2.inventory.CraftInventoryDoubleChest)1