AbstractBedBlock .java

net.minecraft.world.level.block.AbstractBedBlock (class): members: onStopSleeping, getBedRule, PART, getSleptInBedStatType, findStandUpPosition, OCCUPIED.

net.minecraft.world.level.blockclass308 linescommonextends HorizontalDirectionalBlock12.2 KB

API Surface (14)

BedRule getBedRule(final Level level, final BlockPos pos)
Identifier getSleptInBedStatType()
OptionalDouble getSleepHeight(final BlockState state, final Level level, final BlockPos pos)
InteractionResult useWithoutItem(BlockState state, final Level level, BlockPos pos, final Player player, final BlockHitResult hitResult)
void onStopSleeping(final Level level, final BlockPos pos)
BlockState updateShape(final BlockState state, final LevelReader level, final ScheduledTickAccess ticks, final BlockPos pos, final Direction directionToNeighbour, final BlockPos neighbourPos, final BlockState neighbourState, final RandomSource random)
BlockState playerWillDestroy(final Level level, final BlockPos pos, final BlockState state, final Player player)
Direction getConnectedDirection(final BlockState state)
DoubleBlockCombiner.BlockType getBlockType(final BlockState state)
Optional<Vec3> findStandUpPosition(final EntityType<?> type, final CollisionGetter level, final BlockPos pos, final Direction forward, final float yaw)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)
void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack)
long getSeed(final BlockState state, final BlockPos pos)
boolean isPathfindable(final BlockState state, final PathComputationType type)

Fields & Constants (11)

PART
OCCUPIED
state
level
ticks
pos
directionToNeighbour
neighbourPos
neighbourState
forward
offsets

String literals (1)

"block.minecraft.bed.occupied"

Full Source

AbstractBedBlock.java308 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import java.util.List;
4
import java.util.Optional;
5
import java.util.OptionalDouble;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.Direction;
8
import net.minecraft.network.chat.Component;
9
import net.minecraft.resources.Identifier;
10
import net.minecraft.stats.Stats;
11
import net.minecraft.util.Mth;
12
import net.minecraft.util.RandomSource;
13
import net.minecraft.world.InteractionResult;
14
import net.minecraft.world.attribute.BedRule;
15
import net.minecraft.world.attribute.EnvironmentAttribute;
16
import net.minecraft.world.entity.EntityType;
17
import net.minecraft.world.entity.LivingEntity;
18
import net.minecraft.world.entity.npc.villager.Villager;
19
import net.minecraft.world.entity.player.Player;
20
import net.minecraft.world.entity.vehicle.DismountHelper;
21
import net.minecraft.world.item.ItemStack;
22
import net.minecraft.world.item.context.BlockPlaceContext;
23
import net.minecraft.world.level.BlockGetter;
24
import net.minecraft.world.level.CollisionGetter;
25
import net.minecraft.world.level.Level;
26
import net.minecraft.world.level.LevelReader;
27
import net.minecraft.world.level.ScheduledTickAccess;
28
import net.minecraft.world.level.block.state.BlockBehaviour;
29
import net.minecraft.world.level.block.state.BlockState;
30
import net.minecraft.world.level.block.state.StateDefinition;
31
import net.minecraft.world.level.block.state.properties.BedPart;
32
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
33
import net.minecraft.world.level.block.state.properties.BooleanProperty;
34
import net.minecraft.world.level.block.state.properties.EnumProperty;
35
import net.minecraft.world.level.pathfinder.PathComputationType;
36
import net.minecraft.world.phys.AABB;
37
import net.minecraft.world.phys.BlockHitResult;
38
import net.minecraft.world.phys.Vec3;
39
import net.minecraft.world.phys.shapes.VoxelShape;
40
import org.apache.commons.lang3.ArrayUtils;
41
import org.jspecify.annotations.Nullable;
42
43
public abstract class AbstractBedBlock extends HorizontalDirectionalBlock {
44
	public static final EnumProperty<BedPart> PART = BlockStateProperties.BED_PART;
45
	public static final BooleanProperty OCCUPIED = BlockStateProperties.OCCUPIED;
46
47
	public AbstractBedBlock(final BlockBehaviour.Properties properties) {
48
		super(properties);
49
		this.registerDefaultState(this.stateDefinition.any().setValue(PART, BedPart.FOOT).setValue(OCCUPIED, false));
50
	}
51
52
	public static @Nullable Direction getBedOrientation(final BlockGetter level, final BlockPos pos) {
53
		BlockState blockState = level.getBlockState(pos);
54
		return blockState.getBlock() instanceof AbstractBedBlock ? blockState.getValue(FACING) : null;
55
	}
56
57
	protected abstract EnvironmentAttribute<BedRule> getBedEnvironmentAttribute();
58
59
	protected abstract InteractionResult destroyOnUse(final BlockState state, final Level level, BlockPos pos, final Player player);
60
61
	protected abstract void destroyOnLeave(final Level level, BlockPos pos);
62
63
	public BedRule getBedRule(final Level level, final BlockPos pos) {
64
		return level.environmentAttributes().getValue(this.getBedEnvironmentAttribute(), pos);
65
	}
66
67
	public Identifier getSleptInBedStatType() {
68
		return Stats.SLEEP_IN_BED;
69
	}
70
71
	public OptionalDouble getSleepHeight(final BlockState state, final Level level, final BlockPos pos) {
72
		if (!state.is(this)) {
73
			return OptionalDouble.empty();
74
		}
75
76
		VoxelShape shape = state.getShape(level, pos);
77
		return shape.isEmpty() ? OptionalDouble.empty() : OptionalDouble.of(shape.max(Direction.Axis.Y));
78
	}
79
80
	@Override
81
	protected InteractionResult useWithoutItem(BlockState state, final Level level, BlockPos pos, final Player player, final BlockHitResult hitResult) {
82
		if (level.isClientSide()) {
83
			return InteractionResult.SUCCESS_SERVER;
84
		}
85
86
		BedPart part = state.getValue(PART);
87
		BlockState otherState = level.getBlockState(pos.relative(getNeighbourDirection(part, state.getValue(FACING))));
88
		if (otherState.is(this) && otherState.getValue(PART) != part) {
89
			if (part != BedPart.HEAD) {
90
				pos = pos.relative(state.getValue(FACING));
91
				state = level.getBlockState(pos);
92
			}
93
94
			BedRule bedRule = this.getBedRule(level, pos);
95
			if (bedRule.destroyOnUse()) {
96
				bedRule.errorMessage().ifPresent(player::sendOverlayMessage);
97
				return this.destroyOnUse(state, level, pos, player);
98
			}
99
100
			if (state.getValue(OCCUPIED)) {
101
				if (!this.kickVillagerOutOfBed(level, pos)) {
102
					player.sendOverlayMessage(Component.translatable("block.minecraft.bed.occupied"));
103
				}
104
105
				return InteractionResult.SUCCESS_SERVER;
106
			} else {
107
				player.startSleepInBed(this, state, bedRule, pos).ifLeft(problem -> {
108
					if (problem.message() != null) {
109
						player.sendOverlayMessage(problem.message());
110
					}
111
				});
112
				return InteractionResult.SUCCESS_SERVER;
113
			}
114
		} else {
115
			return InteractionResult.CONSUME;
116
		}
117
	}
118
119
	public void onStopSleeping(final Level level, final BlockPos pos) {
120
		BedRule bedRule = this.getBedRule(level, pos);
121
		if (bedRule.destroyOnLeave()) {
122
			this.destroyOnLeave(level, pos);
123
		}
124
	}
125
126
	private boolean kickVillagerOutOfBed(final Level level, final BlockPos pos) {
127
		List<Villager> villagers = level.getEntitiesOfClass(Villager.class, new AABB(pos), LivingEntity::isSleeping);
128
		if (villagers.isEmpty()) {
129
			return false;
130
		}
131
132
		villagers.get(0).stopSleeping();
133
		return true;
134
	}
135
136
	@Override
137
	protected BlockState updateShape(
138
		final BlockState state,
139
		final LevelReader level,
140
		final ScheduledTickAccess ticks,
141
		final BlockPos pos,
142
		final Direction directionToNeighbour,
143
		final BlockPos neighbourPos,
144
		final BlockState neighbourState,
145
		final RandomSource random
146
	) {
147
		if (directionToNeighbour == getNeighbourDirection(state.getValue(PART), state.getValue(FACING))) {
148
			return neighbourState.is(this) && neighbourState.getValue(PART) != state.getValue(PART)
149
				? state.setValue(OCCUPIED, neighbourState.getValue(OCCUPIED))
150
				: Blocks.AIR.defaultBlockState();
151
		} else {
152
			return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
153
		}
154
	}
155
156
	private static Direction getNeighbourDirection(final BedPart part, final Direction facing) {
157
		return part == BedPart.FOOT ? facing : facing.getOpposite();
158
	}
159
160
	@Override
161
	public BlockState playerWillDestroy(final Level level, final BlockPos pos, final BlockState state, final Player player) {
162
		if (!level.isClientSide() && player.preventsBlockDrops()) {
163
			BedPart part = state.getValue(PART);
164
			if (part == BedPart.FOOT) {
165
				BlockPos headPos = pos.relative(getNeighbourDirection(part, state.getValue(FACING)));
166
				BlockState headState = level.getBlockState(headPos);
167
				if (headState.is(this) && headState.getValue(PART) == BedPart.HEAD) {
168
					level.setBlock(headPos, Blocks.AIR.defaultBlockState(), 35);
169
					level.levelEvent(player, 2001, headPos, Block.getId(headState));
170
				}
171
			}
172
		}
173
174
		return super.playerWillDestroy(level, pos, state, player);
175
	}
176
177
	@Override
178
	public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
179
		Direction facing = context.getHorizontalDirection();
180
		BlockPos pos = context.getClickedPos();
181
		BlockPos relative = pos.relative(facing);
182
		Level level = context.getLevel();
183
		return level.getBlockState(relative).canBeReplaced(context) && level.getWorldBorder().isWithinBounds(relative)
184
			? this.defaultBlockState().setValue(FACING, facing)
185
			: null;
186
	}
187
188
	public static Direction getConnectedDirection(final BlockState state) {
189
		Direction facing = state.getValue(FACING);
190
		return state.getValue(PART) == BedPart.HEAD ? facing.getOpposite() : facing;
191
	}
192
193
	public static DoubleBlockCombiner.BlockType getBlockType(final BlockState state) {
194
		BedPart part = state.getValue(PART);
195
		return part == BedPart.HEAD ? DoubleBlockCombiner.BlockType.FIRST : DoubleBlockCombiner.BlockType.SECOND;
196
	}
197
198
	private static boolean isBunkBed(final BlockGetter level, final BlockPos pos) {
199
		return level.getBlockState(pos.below()).getBlock() instanceof AbstractBedBlock;
200
	}
201
202
	public static Optional<Vec3> findStandUpPosition(
203
		final EntityType<?> type, final CollisionGetter level, final BlockPos pos, final Direction forward, final float yaw
204
	) {
205
		Direction right = forward.getClockWise();
206
		Direction side = right.isFacingAngle(yaw) ? right.getOpposite() : right;
207
		if (isBunkBed(level, pos)) {
208
			return findBunkBedStandUpPosition(type, level, pos, forward, side);
209
		}
210
211
		int[][] offsets = bedStandUpOffsets(forward, side);
212
		Optional<Vec3> safePosition = findStandUpPositionAtOffset(type, level, pos, offsets, true);
213
		return safePosition.isPresent() ? safePosition : findStandUpPositionAtOffset(type, level, pos, offsets, false);
214
	}
215
216
	private static Optional<Vec3> findBunkBedStandUpPosition(
217
		final EntityType<?> type, final CollisionGetter level, final BlockPos pos, final Direction forward, final Direction side
218
	) {
219
		int[][] offsets = bedSurroundStandUpOffsets(forward, side);
220
		Optional<Vec3> safePosition = findStandUpPositionAtOffset(type, level, pos, offsets, true);
221
		if (safePosition.isPresent()) {
222
			return safePosition;
223
		}
224
225
		BlockPos below = pos.below();
226
		Optional<Vec3> belowSafePosition = findStandUpPositionAtOffset(type, level, below, offsets, true);
227
		if (belowSafePosition.isPresent()) {
228
			return belowSafePosition;
229
		}
230
231
		int[][] aboveOffsets = bedAboveStandUpOffsets(forward);
232
		Optional<Vec3> aboveSafePosition = findStandUpPositionAtOffset(type, level, pos, aboveOffsets, true);
233
		if (aboveSafePosition.isPresent()) {
234
			return aboveSafePosition;
235
		}
236
237
		Optional<Vec3> unsafePosition = findStandUpPositionAtOffset(type, level, pos, offsets, false);
238
		if (unsafePosition.isPresent()) {
239
			return unsafePosition;
240
		}
241
242
		Optional<Vec3> belowUnsafePosition = findStandUpPositionAtOffset(type, level, below, offsets, false);
243
		return belowUnsafePosition.isPresent() ? belowUnsafePosition : findStandUpPositionAtOffset(type, level, pos, aboveOffsets, false);
244
	}
245
246
	private static Optional<Vec3> findStandUpPositionAtOffset(
247
		final EntityType<?> type, final CollisionGetter level, final BlockPos pos, final int[][] offsets, final boolean checkDangerous
248
	) {
249
		BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos();
250
251
		for (int[] offset : offsets) {
252
			blockPos.set(pos.getX() + offset[0], pos.getY(), pos.getZ() + offset[1]);
253
			Vec3 position = DismountHelper.findSafeDismountLocation(type, level, blockPos, checkDangerous);
254
			if (position != null) {
255
				return Optional.of(position);
256
			}
257
		}
258
259
		return Optional.empty();
260
	}
261
262
	@Override
263
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
264
		builder.add(FACING, PART, OCCUPIED);
265
	}
266
267
	@Override
268
	public void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack) {
269
		super.setPlacedBy(level, pos, state, by, itemStack);
270
		BlockPos otherPos = pos.relative(state.getValue(FACING));
271
		level.setBlockAndUpdate(otherPos, state.setValue(PART, BedPart.HEAD));
272
	}
273
274
	@Override
275
	protected long getSeed(final BlockState state, final BlockPos pos) {
276
		BlockPos sourcePos = pos.relative(state.getValue(FACING), state.getValue(PART) == BedPart.HEAD ? 0 : 1);
277
		return Mth.getSeed(sourcePos.getX(), pos.getY(), sourcePos.getZ());
278
	}
279
280
	@Override
281
	protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
282
		return false;
283
	}
284
285
	private static int[][] bedStandUpOffsets(final Direction forward, final Direction side) {
286
		return ArrayUtils.addAll(bedSurroundStandUpOffsets(forward, side), bedAboveStandUpOffsets(forward));
287
	}
288
289
	private static int[][] bedSurroundStandUpOffsets(final Direction forward, final Direction side) {
290
		return new int[][]{
291
			{side.getStepX(), side.getStepZ()},
292
			{side.getStepX() - forward.getStepX(), side.getStepZ() - forward.getStepZ()},
293
			{side.getStepX() - forward.getStepX() * 2, side.getStepZ() - forward.getStepZ() * 2},
294
			{-forward.getStepX() * 2, -forward.getStepZ() * 2},
295
			{-side.getStepX() - forward.getStepX() * 2, -side.getStepZ() - forward.getStepZ() * 2},
296
			{-side.getStepX() - forward.getStepX(), -side.getStepZ() - forward.getStepZ()},
297
			{-side.getStepX(), -side.getStepZ()},
298
			{-side.getStepX() + forward.getStepX(), -side.getStepZ() + forward.getStepZ()},
299
			{forward.getStepX(), forward.getStepZ()},
300
			{side.getStepX() + forward.getStepX(), side.getStepZ() + forward.getStepZ()}
301
		};
302
	}
303
304
	private static int[][] bedAboveStandUpOffsets(final Direction forward) {
305
		return new int[][]{{0, 0}, {-forward.getStepX(), -forward.getStepZ()}};
306
	}
307
}
308

Decompiled Minecraft 26.3 source of AbstractBedBlock (net/minecraft/world/level/block/AbstractBedBlock.java) with Mojang official mappings, stored verbatim on the site.