Search in sources :

Example 1 with BedBlock

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

the class CraftVillager method sleep.

@Override
public boolean sleep(Location location) {
    Preconditions.checkArgument(location != null, "Location cannot be null");
    Preconditions.checkArgument(location.getWorld() != null, "Location needs to be in a world");
    Preconditions.checkArgument(location.getWorld().equals(getWorld()), "Cannot sleep across worlds");
    Preconditions.checkState(!getHandle().generation, "Cannot sleep during world generation");
    BlockPos position = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
    BlockState iblockdata = getHandle().level.getBlockState(position);
    if (!(iblockdata.getBlock() instanceof BedBlock)) {
        return false;
    }
    getHandle().startSleeping(position);
    return true;
}
Also used : BedBlock(net.minecraft.world.level.block.BedBlock) BlockState(net.minecraft.world.level.block.state.BlockState) BlockPos(net.minecraft.core.BlockPos)

Example 2 with BedBlock

use of net.minecraft.world.level.block.BedBlock in project fabric-carpet by gnembon.

the class BlockRotator method dispenserRotate.

public static ItemStack dispenserRotate(BlockSource source, ItemStack stack) {
    Direction sourceFace = source.getBlockState().getValue(DispenserBlock.FACING);
    Level world = source.getLevel();
    // offset
    BlockPos blockpos = source.getPos().relative(sourceFace);
    BlockState iblockstate = world.getBlockState(blockpos);
    Block block = iblockstate.getBlock();
    // Block rotation for blocks that can be placed in all 6 or 4 rotations.
    if (block instanceof DirectionalBlock || block instanceof DispenserBlock) {
        Direction face = iblockstate.getValue(DirectionalBlock.FACING);
        if (block instanceof PistonBaseBlock && (iblockstate.getValue(PistonBaseBlock.EXTENDED) || (((PistonBlockInterface) block).publicShouldExtend(world, blockpos, face) && (new PistonStructureResolver(world, blockpos, face, true)).resolve())))
            return stack;
        Direction rotated_face = rotateClockwise(face, sourceFace.getAxis());
        if (sourceFace.get3DDataValue() % 2 == 0 || rotated_face == face) {
            // Flip to make blocks always rotate clockwise relative to the dispenser
            // when index is equal to zero. when index is equal to zero the dispenser is in the opposite direction.
            rotated_face = rotated_face.getOpposite();
        }
        world.setBlock(blockpos, iblockstate.setValue(DirectionalBlock.FACING, rotated_face), 3);
    } else if (// Block rotation for blocks that can be placed in only 4 horizontal rotations.
    block instanceof HorizontalDirectionalBlock) {
        if (block instanceof BedBlock)
            return stack;
        Direction face = iblockstate.getValue(HorizontalDirectionalBlock.FACING);
        face = rotateClockwise(face, Direction.Axis.Y);
        if (sourceFace == Direction.DOWN) {
            // same as above.
            face = face.getOpposite();
        }
        world.setBlock(blockpos, iblockstate.setValue(HorizontalDirectionalBlock.FACING, face), 3);
    } else if (block == Blocks.HOPPER) {
        Direction face = iblockstate.getValue(HopperBlock.FACING);
        if (face != Direction.DOWN) {
            face = rotateClockwise(face, Direction.Axis.Y);
            world.setBlock(blockpos, iblockstate.setValue(HopperBlock.FACING, face), 3);
        }
    }
    // Send block update to the block that just have been rotated.
    world.neighborChanged(blockpos, block, source.getPos());
    return stack;
}
Also used : BedBlock(net.minecraft.world.level.block.BedBlock) HorizontalDirectionalBlock(net.minecraft.world.level.block.HorizontalDirectionalBlock) BlockState(net.minecraft.world.level.block.state.BlockState) HorizontalDirectionalBlock(net.minecraft.world.level.block.HorizontalDirectionalBlock) DirectionalBlock(net.minecraft.world.level.block.DirectionalBlock) DispenserBlock(net.minecraft.world.level.block.DispenserBlock) PistonStructureResolver(net.minecraft.world.level.block.piston.PistonStructureResolver) BaseRailBlock(net.minecraft.world.level.block.BaseRailBlock) FenceGateBlock(net.minecraft.world.level.block.FenceGateBlock) TrapDoorBlock(net.minecraft.world.level.block.TrapDoorBlock) LeverBlock(net.minecraft.world.level.block.LeverBlock) ObserverBlock(net.minecraft.world.level.block.ObserverBlock) SlabBlock(net.minecraft.world.level.block.SlabBlock) BedBlock(net.minecraft.world.level.block.BedBlock) EndRodBlock(net.minecraft.world.level.block.EndRodBlock) RepeaterBlock(net.minecraft.world.level.block.RepeaterBlock) GlazedTerracottaBlock(net.minecraft.world.level.block.GlazedTerracottaBlock) DiodeBlock(net.minecraft.world.level.block.DiodeBlock) DispenserBlock(net.minecraft.world.level.block.DispenserBlock) ComparatorBlock(net.minecraft.world.level.block.ComparatorBlock) HopperBlock(net.minecraft.world.level.block.HopperBlock) PistonBaseBlock(net.minecraft.world.level.block.piston.PistonBaseBlock) Block(net.minecraft.world.level.block.Block) RotatedPillarBlock(net.minecraft.world.level.block.RotatedPillarBlock) HorizontalDirectionalBlock(net.minecraft.world.level.block.HorizontalDirectionalBlock) StairBlock(net.minecraft.world.level.block.StairBlock) DirectionalBlock(net.minecraft.world.level.block.DirectionalBlock) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) Direction(net.minecraft.core.Direction) PistonBaseBlock(net.minecraft.world.level.block.piston.PistonBaseBlock)

Example 3 with BedBlock

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

Example 4 with BedBlock

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

the class CraftHumanEntity method sleep.

@Override
public boolean sleep(Location location, boolean force) {
    Preconditions.checkArgument(location != null, "Location cannot be null");
    Preconditions.checkArgument(location.getWorld() != null, "Location needs to be in a world");
    Preconditions.checkArgument(location.getWorld().equals(getWorld()), "Cannot sleep across worlds");
    BlockPos blockposition = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
    BlockState iblockdata = getHandle().level.getBlockState(blockposition);
    if (!(iblockdata.getBlock() instanceof BedBlock)) {
        return false;
    }
    if (getHandle().startSleepInBed(blockposition, force).left().isPresent()) {
        return false;
    }
    // From BedBlock
    iblockdata = (BlockState) iblockdata.setValue(BedBlock.OCCUPIED, true);
    getHandle().level.setBlock(blockposition, iblockdata, 4);
    return true;
}
Also used : BedBlock(net.minecraft.world.level.block.BedBlock) BlockState(net.minecraft.world.level.block.state.BlockState) BlockPos(net.minecraft.core.BlockPos)

Aggregations

BlockPos (net.minecraft.core.BlockPos)4 BedBlock (net.minecraft.world.level.block.BedBlock)4 BlockState (net.minecraft.world.level.block.state.BlockState)4 Level (net.minecraft.world.level.Level)2 SlabBlock (net.minecraft.world.level.block.SlabBlock)2 Direction (net.minecraft.core.Direction)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 BaseRailBlock (net.minecraft.world.level.block.BaseRailBlock)1 Block (net.minecraft.world.level.block.Block)1 ChestBlock (net.minecraft.world.level.block.ChestBlock)1 ComparatorBlock (net.minecraft.world.level.block.ComparatorBlock)1 DiodeBlock (net.minecraft.world.level.block.DiodeBlock)1 DirectionalBlock (net.minecraft.world.level.block.DirectionalBlock)1 DispenserBlock (net.minecraft.world.level.block.DispenserBlock)1 EndRodBlock (net.minecraft.world.level.block.EndRodBlock)1 FenceGateBlock (net.minecraft.world.level.block.FenceGateBlock)1 GlazedTerracottaBlock (net.minecraft.world.level.block.GlazedTerracottaBlock)1 HopperBlock (net.minecraft.world.level.block.HopperBlock)1 HorizontalDirectionalBlock (net.minecraft.world.level.block.HorizontalDirectionalBlock)1 LeverBlock (net.minecraft.world.level.block.LeverBlock)1