Search in sources :

Example 56 with BlockState

use of net.minecraft.world.level.block.state.BlockState in project Tropicraft by Tropicraft.

the class TropicraftSpawnEgg method useOn.

public InteractionResult useOn(UseOnContext context) {
    Level world = context.getLevel();
    if (world.isClientSide) {
        return InteractionResult.SUCCESS;
    } else {
        ItemStack itemStack = context.getItemInHand();
        BlockPos pos = context.getClickedPos();
        Direction dir = context.getClickedFace();
        BlockState state = world.getBlockState(pos);
        Block block = state.getBlock();
        if (block == Blocks.SPAWNER) {
            BlockEntity te = world.getBlockEntity(pos);
            if (te instanceof SpawnerBlockEntity) {
                BaseSpawner spawner = ((SpawnerBlockEntity) te).getSpawner();
                EntityType<?> spawnType = typeIn.get();
                spawner.setEntityId(spawnType);
                te.setChanged();
                world.sendBlockUpdated(pos, state, state, 3);
                itemStack.shrink(1);
                return InteractionResult.SUCCESS;
            }
        }
        BlockPos spawnPos;
        if (state.getCollisionShape(world, pos).isEmpty()) {
            spawnPos = pos;
        } else {
            spawnPos = pos.relative(dir);
        }
        EntityType<?> type3 = typeIn.get();
        if (type3.spawn((ServerLevel) world, itemStack, context.getPlayer(), spawnPos, MobSpawnType.SPAWN_EGG, true, !Objects.equals(pos, spawnPos) && dir == Direction.UP) != null) {
            itemStack.shrink(1);
        }
        return InteractionResult.SUCCESS;
    }
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) SpawnerBlockEntity(net.minecraft.world.level.block.entity.SpawnerBlockEntity) BlockState(net.minecraft.world.level.block.state.BlockState) BaseSpawner(net.minecraft.world.level.BaseSpawner) LiquidBlock(net.minecraft.world.level.block.LiquidBlock) Block(net.minecraft.world.level.block.Block) ServerLevel(net.minecraft.server.level.ServerLevel) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack) Direction(net.minecraft.core.Direction) SpawnerBlockEntity(net.minecraft.world.level.block.entity.SpawnerBlockEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 57 with BlockState

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

the class BuildingMethods method getBedState.

public static Tuple<BlockState, BlockState> getBedState(BlockPos bedHeadPos, BlockPos bedFootPos, DyeColor bedColor) {
    BlockState bedHead = null;
    BlockState bedFoot = null;
    switch(bedColor) {
        case BLACK:
            {
                bedHead = Blocks.BLACK_BED.defaultBlockState().setValue(BedBlock.PART, BedPart.HEAD);
                bedFoot = Blocks.BLACK_BED.defaultBlockState();
                break;
            }
        case BLUE:
            {
                bedHead = Blocks.BLUE_BED.defaultBlockState().setValue(BedBlock.PART, BedPart.HEAD);
                bedFoot = Blocks.BLUE_BED.defaultBlockState();
                break;
            }
        case BROWN:
            {
                bedHead = Blocks.BROWN_BED.defaultBlockState().setValue(BedBlock.PART, BedPart.HEAD);
                bedFoot = Blocks.BROWN_BED.defaultBlockState();
                break;
            }
        case CYAN:
            {
                bedHead = Blocks.CYAN_BED.defaultBlockState().setValue(BedBlock.PART, BedPart.HEAD);
                bedFoot = Blocks.CYAN_BED.defaultBlockState();
                break;
            }
        case GRAY:
            {
                bedHead = Blocks.GRAY_BED.defaultBlockState().setValue(BedBlock.PART, BedPart.HEAD);
                bedFoot = Blocks.GRAY_BED.defaultBlockState();
                break;
            }
        case GREEN:
            {
                bedHead = Blocks.GREEN_BED.defaultBlockState().setValue(BedBlock.PART, BedPart.HEAD);
                bedFoot = Blocks.GREEN_BED.defaultBlockState();
                break;
            }
        case LIGHT_BLUE:
            {
                bedHead = Blocks.LIGHT_BLUE_BED.defaultBlockState().setValue(BedBlock.PART, BedPart.HEAD);
                bedFoot = Blocks.LIGHT_BLUE_BED.defaultBlockState();
                break;
            }
        case LIGHT_GRAY:
            {
                bedHead = Blocks.LIGHT_GRAY_BED.defaultBlockState().setValue(BedBlock.PART, BedPart.HEAD);
                bedFoot = Blocks.LIGHT_GRAY_BED.defaultBlockState();
                break;
            }
        case LIME:
            {
                bedHead = Blocks.LIME_BED.defaultBlockState().setValue(BedBlock.PART, BedPart.HEAD);
                bedFoot = Blocks.LIME_BED.defaultBlockState();
                break;
            }
        case MAGENTA:
            {
                bedHead = Blocks.MAGENTA_BED.defaultBlockState().setValue(BedBlock.PART, BedPart.HEAD);
                bedFoot = Blocks.MAGENTA_BED.defaultBlockState();
                break;
            }
        case ORANGE:
            {
                bedHead = Blocks.ORANGE_BED.defaultBlockState().setValue(BedBlock.PART, BedPart.HEAD);
                bedFoot = Blocks.ORANGE_BED.defaultBlockState();
                break;
            }
        case PINK:
            {
                bedHead = Blocks.PINK_BED.defaultBlockState().setValue(BedBlock.PART, BedPart.HEAD);
                bedFoot = Blocks.PINK_BED.defaultBlockState();
                break;
            }
        case PURPLE:
            {
                bedHead = Blocks.PURPLE_BED.defaultBlockState().setValue(BedBlock.PART, BedPart.HEAD);
                bedFoot = Blocks.PURPLE_BED.defaultBlockState();
                break;
            }
        case RED:
            {
                bedHead = Blocks.RED_BED.defaultBlockState().setValue(BedBlock.PART, BedPart.HEAD);
                bedFoot = Blocks.RED_BED.defaultBlockState();
                break;
            }
        case WHITE:
            {
                bedHead = Blocks.WHITE_BED.defaultBlockState().setValue(BedBlock.PART, BedPart.HEAD);
                bedFoot = Blocks.WHITE_BED.defaultBlockState();
                break;
            }
        case YELLOW:
            {
                bedHead = Blocks.YELLOW_BED.defaultBlockState().setValue(BedBlock.PART, BedPart.HEAD);
                bedFoot = Blocks.YELLOW_BED.defaultBlockState();
                break;
            }
    }
    Direction direction = Direction.NORTH;
    BlockPos tempPos = bedHeadPos.relative(Direction.NORTH);
    while (tempPos.getX() != bedFootPos.getX() || tempPos.getZ() != bedFootPos.getZ()) {
        direction = direction.getClockWise();
        tempPos = bedHeadPos.relative(direction);
    }
    bedHead = bedHead.setValue(BedBlock.FACING, direction.getOpposite());
    bedFoot = bedFoot.setValue(BedBlock.FACING, direction.getOpposite());
    return new Tuple<>(bedHead, bedFoot);
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) BlockPos(net.minecraft.core.BlockPos) Direction(net.minecraft.core.Direction) Tuple(com.wuest.prefab.Tuple)

Example 58 with BlockState

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

the class BuildingMethods method PlaceMineShaft.

/**
 * Places a mineshaft with ladder in the world.
 *
 * @param world          - The world where the mineshaft will be added.
 * @param pos            - The starting position for the mineshaft.
 * @param facing         - The direction where the ladder will be placed.
 * @param onlyGatherOres - Determines if vanilla non-ore blocks will be gathered.
 */
public static void PlaceMineShaft(ServerLevel world, BlockPos pos, Direction facing, boolean onlyGatherOres) {
    // Keep track of all the items to add to the chest at the end of the
    // shaft.
    ArrayList<ItemStack> stacks = new ArrayList<>();
    ArrayList<Item> blocksToNotAdd = new ArrayList<>();
    if (onlyGatherOres) {
        blocksToNotAdd.add(Item.byBlock(Blocks.SAND));
        blocksToNotAdd.add(Item.byBlock(Blocks.SANDSTONE));
        blocksToNotAdd.add(Item.byBlock(Blocks.COBBLESTONE));
        blocksToNotAdd.add(Item.byBlock(Blocks.STONE));
        blocksToNotAdd.add(Item.byBlock(Blocks.DIRT));
        blocksToNotAdd.add(Item.byBlock(Blocks.GRANITE));
        blocksToNotAdd.add(Item.byBlock(Blocks.ANDESITE));
        blocksToNotAdd.add(Item.byBlock(Blocks.DIORITE));
        blocksToNotAdd.add(Item.byBlock(Blocks.RED_SAND));
        blocksToNotAdd.add(Item.byBlock(Blocks.RED_SANDSTONE));
        blocksToNotAdd.add(Item.byBlock(Blocks.MOSSY_COBBLESTONE));
        blocksToNotAdd.add(Item.byBlock(Blocks.MOSSY_STONE_BRICKS));
    }
    int minimumHeightForMineshaft = world.getMinBuildHeight() + 21;
    Triple<ArrayList<ItemStack>, ArrayList<BlockPos>, ArrayList<BlockPos>> ladderShaftResults = BuildingMethods.CreateLadderShaft(world, pos, stacks, facing, blocksToNotAdd, minimumHeightForMineshaft);
    ArrayList<BlockPos> blockPositions = new ArrayList<>(ladderShaftResults.getThird());
    stacks = ladderShaftResults.getFirst();
    ArrayList<BlockPos> torchPositions = ladderShaftResults.getSecond();
    // Get 20 blocks above the void.
    pos = pos.below(pos.getY() - minimumHeightForMineshaft);
    ArrayList<ItemStack> tempStacks = new ArrayList<>();
    BlockPos ceilingLevel = pos.above(4);
    BuildingMethods.SetFloor(world, ceilingLevel.relative(facing, 2).relative(facing.getClockWise(), 2).relative(facing.getOpposite()), Blocks.STONE, 4, 4, tempStacks, facing.getOpposite(), blocksToNotAdd);
    // After setting the floor, make sure to replace the ladder.
    BuildingMethods.ReplaceBlock(world, ceilingLevel, Blocks.LADDER.defaultBlockState().setValue(LadderBlock.FACING, facing), 2);
    blockPositions.add(ceilingLevel);
    BlockState torchState = Blocks.TORCH.defaultBlockState();
    // Place the torches at this point since the entire shaft has been set.
    for (BlockPos torchPos : torchPositions) {
        BlockState surroundingState = world.getBlockState(torchPos);
        Block surroundingBlock = surroundingState.getBlock();
        BuildingMethods.ConsolidateDrops(world, torchPos, surroundingState, tempStacks, blocksToNotAdd);
        BuildingMethods.ReplaceBlock(world, torchPos, torchState, 2);
        blockPositions.add(torchPos);
    }
    // The entire ladder has been created. Create a platform at this level
    // and place a chest next to the ladder.
    tempStacks.addAll(BuildingMethods.SetFloor(world, pos.relative(facing).relative(facing.getClockWise()), Blocks.STONE, 3, 4, tempStacks, facing.getOpposite(), blocksToNotAdd));
    // Remove the ladder stack since they shouldn't be getting that.
    for (int i = 0; i < tempStacks.size(); i++) {
        ItemStack stack = tempStacks.get(i);
        if (stack.getItem() == Item.byBlock(Blocks.LADDER)) {
            tempStacks.remove(i);
            i--;
        }
    }
    // Now that the floor has been set, go up 1 block to star creating the
    // walls.
    pos = pos.above();
    // Clear a space around the ladder pillar and make walls. The walls are
    // necessary if there is a lot of lava down here.
    // Make a wall of air then a wall of stone.
    // South wall.
    tempStacks.addAll(BuildingMethods.CreateWall(world, 3, 3, facing.getClockWise(), pos.relative(facing.getOpposite(), 2).relative(facing.getCounterClockWise()), Blocks.AIR, blocksToNotAdd));
    tempStacks.addAll(BuildingMethods.CreateWall(world, 3, 3, facing.getClockWise(), pos.relative(facing.getOpposite(), 3).relative(facing.getCounterClockWise()), Blocks.STONE, blocksToNotAdd));
    // East wall.
    tempStacks.addAll(BuildingMethods.CreateWall(world, 3, 4, facing, pos.relative(facing.getOpposite(), 2).relative(facing.getClockWise()), Blocks.AIR, blocksToNotAdd));
    tempStacks.addAll(BuildingMethods.CreateWall(world, 3, 4, facing, pos.relative(facing.getOpposite(), 2).relative(facing.getClockWise(), 2), Blocks.STONE, blocksToNotAdd));
    // North wall.
    tempStacks.addAll(BuildingMethods.CreateWall(world, 3, 3, facing.getCounterClockWise(), pos.relative(facing).relative(facing.getClockWise()), Blocks.AIR, blocksToNotAdd));
    tempStacks.addAll(BuildingMethods.CreateWall(world, 3, 3, facing.getCounterClockWise(), pos.relative(facing, 2).relative(facing.getClockWise()), Blocks.STONE, blocksToNotAdd));
    // West wall.
    tempStacks.addAll(BuildingMethods.CreateWall(world, 3, 4, facing.getOpposite(), pos.relative(facing).relative(facing.getCounterClockWise()), Blocks.AIR, blocksToNotAdd));
    tempStacks.addAll(BuildingMethods.CreateWall(world, 3, 4, facing.getOpposite(), pos.relative(facing, 1).relative(facing.getCounterClockWise(), 2), Blocks.STONE, blocksToNotAdd));
    // Consolidate the stacks.
    for (ItemStack tempStack : tempStacks) {
        boolean foundStack = false;
        for (ItemStack existingStack : stacks) {
            if (ItemStack.isSame(existingStack, tempStack)) {
                // the max.
                if (existingStack.getCount() + tempStack.getCount() <= tempStack.getMaxStackSize()) {
                    existingStack.setCount(existingStack.getCount() + tempStack.getCount());
                    foundStack = true;
                    break;
                }
            }
        }
        if (!foundStack) {
            stacks.add(tempStack);
        }
    }
    // Place a torch to the left of the ladder.
    BlockState blockState = Blocks.TORCH.defaultBlockState();
    BuildingMethods.ReplaceBlock(world, pos.relative(facing.getCounterClockWise()), blockState);
    if (CommonProxy.proxyConfiguration.serverConfiguration.includeMineshaftChest) {
        // Place a chest to the right of the ladder.
        BlockState chestState = Blocks.CHEST.defaultBlockState().setValue(ChestBlock.FACING, facing);
        BuildingMethods.ReplaceBlock(world, pos.relative(facing.getClockWise()), chestState, 2);
        blockPositions.add(pos.relative(facing.getClockWise()));
        if (stacks.size() > 27) {
            // Add another chest to south of the existing chest.
            BuildingMethods.ReplaceBlock(world, pos.relative(facing.getClockWise()).relative(facing.getOpposite()), chestState, 2);
            blockPositions.add(pos.relative(facing.getClockWise()).relative(facing.getOpposite()));
        }
        BlockEntity tileEntity = world.getBlockEntity(pos.relative(facing.getClockWise()));
        BlockEntity tileEntity2 = world.getBlockEntity(pos.relative(facing.getClockWise()).relative(facing.getOpposite()));
        if (tileEntity instanceof ChestBlockEntity) {
            ChestBlockEntity chestTile = (ChestBlockEntity) tileEntity;
            ChestBlockEntity chestTile2 = (ChestBlockEntity) tileEntity2;
            int i = 0;
            boolean fillSecond = false;
            // All the stacks should be consolidated at this point.
            for (ItemStack stack : stacks) {
                if (i == 27 && !fillSecond) {
                    // Start filling the second chest.
                    fillSecond = true;
                    i = 0;
                    chestTile = chestTile2;
                }
                if (i >= 27 && fillSecond) {
                    // Too many items, discard the rest.
                    break;
                } else {
                    chestTile.setItem(i, stack);
                }
                i++;
            }
        }
    }
    for (BlockPos currentPos : blockPositions) {
        Block block = world.getBlockState(pos).getBlock();
        world.blockUpdated(pos, block);
    }
}
Also used : ArrayList(java.util.ArrayList) Item(net.minecraft.world.item.Item) BlockState(net.minecraft.world.level.block.state.BlockState) ChestBlockEntity(net.minecraft.world.level.block.entity.ChestBlockEntity) BlockPos(net.minecraft.core.BlockPos) ItemStack(net.minecraft.world.item.ItemStack) RandomizableContainerBlockEntity(net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity) ChestBlockEntity(net.minecraft.world.level.block.entity.ChestBlockEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) FurnaceBlockEntity(net.minecraft.world.level.block.entity.FurnaceBlockEntity)

Example 59 with BlockState

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

the class Structure method processedGlassBlock.

protected boolean processedGlassBlock(StructureConfiguration configuration, BuildBlock block, Level world, BlockPos originalPos, Block foundBlock) {
    if (!this.hasGlassColor(configuration)) {
        return false;
    }
    if (foundBlock.getRegistryName().getNamespace().equals(Blocks.WHITE_STAINED_GLASS.getRegistryName().getNamespace()) && foundBlock.getRegistryName().getPath().endsWith("glass")) {
        BlockState blockState = BuildingMethods.getStainedGlassBlock(this.getGlassColor(configuration));
        block.setBlockState(blockState);
        return true;
    } else if (foundBlock.getRegistryName().getNamespace().equals(Blocks.WHITE_STAINED_GLASS_PANE.getRegistryName().getNamespace()) && foundBlock.getRegistryName().getPath().endsWith("glass_pane")) {
        BlockState blockState = BuildingMethods.getStainedGlassPaneBlock(this.getGlassColor(configuration));
        BuildBlock.SetBlockState(configuration, originalPos, block, foundBlock, blockState, this.getClearSpace().getShape().getDirection());
        return true;
    }
    return false;
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState)

Example 60 with BlockState

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

the class StructureEventHandler method removeWaterLogging.

private static void removeWaterLogging(Structure structure) {
    if (structure.hasAirBlocks) {
        for (BlockPos currentPos : structure.allBlockPositions) {
            BlockState currentState = structure.world.getBlockState(currentPos);
            if (currentState.hasProperty(BlockStateProperties.WATERLOGGED)) {
                // This is a water loggable block and there were air blocks, make sure that it's no longer water logged.
                currentState = currentState.setValue((BlockStateProperties.WATERLOGGED), false);
                structure.world.setBlock(currentPos, currentState, 3);
            } else if (currentState.getMaterial() == Material.WATER) {
                structure.world.setBlock(currentPos, Blocks.AIR.defaultBlockState(), 3);
            }
        }
    }
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) BlockPos(net.minecraft.core.BlockPos)

Aggregations

BlockState (net.minecraft.world.level.block.state.BlockState)141 BlockPos (net.minecraft.core.BlockPos)75 Direction (net.minecraft.core.Direction)18 Level (net.minecraft.world.level.Level)14 Block (net.minecraft.world.level.block.Block)14 ArrayList (java.util.ArrayList)10 ServerLevel (net.minecraft.server.level.ServerLevel)10 SoundType (net.minecraft.world.level.block.SoundType)10 Vec3 (net.minecraft.world.phys.Vec3)10 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)9 Random (java.util.Random)8 ItemStack (net.minecraft.world.item.ItemStack)7 FluidState (net.minecraft.world.level.material.FluidState)7 AABB (net.minecraft.world.phys.AABB)6 BuildBlock (com.wuest.prefab.structures.base.BuildBlock)5 LevelChunk (net.minecraft.world.level.chunk.LevelChunk)5 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)5 LocationTag (com.denizenscript.denizen.objects.LocationTag)4 FakeBlock (com.denizenscript.denizen.utilities.blocks.FakeBlock)4 HashMap (java.util.HashMap)4