| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.Map;
|
| 4 | import java.util.OptionalDouble;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.resources.Identifier;
|
| 8 | import net.minecraft.sounds.SoundEvents;
|
| 9 | import net.minecraft.sounds.SoundSource;
|
| 10 | import net.minecraft.stats.Stats;
|
| 11 | import net.minecraft.util.Util;
|
| 12 | import net.minecraft.world.InteractionResult;
|
| 13 | import net.minecraft.world.attribute.BedRule;
|
| 14 | import net.minecraft.world.attribute.EnvironmentAttribute;
|
| 15 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 16 | import net.minecraft.world.entity.player.Player;
|
| 17 | import net.minecraft.world.level.BlockGetter;
|
| 18 | import net.minecraft.world.level.Level;
|
| 19 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 20 | import net.minecraft.world.level.block.state.BlockState;
|
| 21 | import net.minecraft.world.level.block.state.properties.BedPart;
|
| 22 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 23 | import net.minecraft.world.phys.shapes.Shapes;
|
| 24 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 25 |
|
| 26 | public class StrawBedBlock extends AbstractBedBlock {
|
| 27 | private static final VoxelShape BASE_SHAPE = Block.column(16.0, 0.0, 4.0);
|
| 28 | private static final VoxelShape PILLOW_SHAPE = Block.box(0.0, 0.0, 0.0, 16.0, 5.0, 8.0);
|
| 29 | private static final Map<Direction, VoxelShape> FOOT_SHAPES = Util.make(() -> Shapes.rotateHorizontal(BASE_SHAPE));
|
| 30 | private static final Map<Direction, VoxelShape> HEAD_SHAPES = Util.make(() -> Shapes.rotateHorizontal(Shapes.or(BASE_SHAPE, PILLOW_SHAPE)));
|
| 31 |
|
| 32 | public StrawBedBlock(final BlockBehaviour.Properties properties) {
|
| 33 | super(properties);
|
| 34 | }
|
| 35 |
|
| 36 | private void destroyBed(final Level level, final BlockPos pos) {
|
| 37 | level.playSound(null, pos, SoundEvents.STRAW_BED_BREAK_LEAVE, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 38 | level.setBlockAndUpdate(pos, Blocks.AIR.defaultBlockState());
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 43 | Map<Direction, VoxelShape> shapes = state.getValue(PART) == BedPart.HEAD ? HEAD_SHAPES : FOOT_SHAPES;
|
| 44 | return shapes.get(getConnectedDirection(state).getOpposite());
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | protected EnvironmentAttribute<BedRule> getBedEnvironmentAttribute() {
|
| 49 | return EnvironmentAttributes.STRAW_BED_RULE;
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public void spawnDestroyParticles(final Level level, final BlockPos pos, final BlockState state) {
|
| 54 | level.levelEvent(2014, pos, getId(state));
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public Identifier getSleptInBedStatType() {
|
| 59 | return Stats.SLEEP_IN_STRAW_BED;
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public OptionalDouble getSleepHeight(final BlockState state, final Level level, final BlockPos pos) {
|
| 64 | BlockPos layingOnPos;
|
| 65 | BlockState layingOnState;
|
| 66 | if (state.getValue(BedBlock.PART) == BedPart.HEAD) {
|
| 67 | layingOnPos = pos.relative(getConnectedDirection(state));
|
| 68 | layingOnState = level.getBlockState(layingOnPos);
|
| 69 | if (!layingOnState.is(this) || layingOnState.getValue(BedBlock.PART) != BedPart.FOOT) {
|
| 70 | return OptionalDouble.empty();
|
| 71 | }
|
| 72 | } else {
|
| 73 | layingOnPos = pos;
|
| 74 | layingOnState = state;
|
| 75 | }
|
| 76 |
|
| 77 | return super.getSleepHeight(layingOnState, level, layingOnPos);
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | protected InteractionResult destroyOnUse(final BlockState state, final Level level, final BlockPos pos, final Player player) {
|
| 82 | this.destroyBed(level, pos);
|
| 83 | return InteractionResult.SUCCESS_SERVER;
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | protected void destroyOnLeave(final Level level, final BlockPos pos) {
|
| 88 | this.destroyBed(level, pos);
|
| 89 | }
|
| 90 | }
|
| 91 |
|