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;
}
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;
}
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;
}
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;
}
Aggregations