DoorBlock .java

net.minecraft.world.level.block.DoorBlock (class): members: isWoodenDoor, HINGE, setOpen, OPEN, isOpen, HALF, onExplosionHit, playerWillDestroy, explosion.

net.minecraft.world.level.blockclass275 linescommonextends Block11.6 KB

API Surface (18)

BlockSetType type()
VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
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)
void onExplosionHit(final BlockState state, final ServerLevel level, final BlockPos pos, final Explosion explosion, final BiConsumer<ItemStack, BlockPos> onHit)
BlockState playerWillDestroy(final Level level, final BlockPos pos, final BlockState state, final Player player)
boolean isPathfindable(final BlockState state, final PathComputationType type)
void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack)
InteractionResult useWithoutItem(BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult)
boolean isOpen(final BlockState state)
void setOpen(final @Nullable Entity sourceEntity, final Level level, final BlockState state, final BlockPos pos, final boolean shouldOpen)
void neighborChanged(final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston)
boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos)
BlockState rotate(final BlockState state, final Rotation rotation)
BlockState mirror(final BlockState state, final Mirror mirror)
long getSeed(final BlockState state, final BlockPos pos)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)
boolean isWoodenDoor(final Level level, final BlockPos pos)
boolean isWoodenDoor(final BlockState state)

Fields & Constants (16)

FACING
HALF
HINGE
OPEN
POWERED
SHAPES
type
state
level
ticks
pos
directionToNeighbour
neighbourPos
neighbourState
explosion
orientation

Full Source

DoorBlock.java275 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import java.util.Map;
4
import java.util.function.BiConsumer;
5
import net.minecraft.core.BlockPos;
6
import net.minecraft.core.Direction;
7
import net.minecraft.server.level.ServerLevel;
8
import net.minecraft.sounds.SoundSource;
9
import net.minecraft.util.Mth;
10
import net.minecraft.util.RandomSource;
11
import net.minecraft.world.InteractionResult;
12
import net.minecraft.world.entity.Entity;
13
import net.minecraft.world.entity.LivingEntity;
14
import net.minecraft.world.entity.player.Player;
15
import net.minecraft.world.item.ItemStack;
16
import net.minecraft.world.item.context.BlockPlaceContext;
17
import net.minecraft.world.level.BlockGetter;
18
import net.minecraft.world.level.Explosion;
19
import net.minecraft.world.level.Level;
20
import net.minecraft.world.level.LevelReader;
21
import net.minecraft.world.level.ScheduledTickAccess;
22
import net.minecraft.world.level.block.state.BlockBehaviour;
23
import net.minecraft.world.level.block.state.BlockState;
24
import net.minecraft.world.level.block.state.StateDefinition;
25
import net.minecraft.world.level.block.state.properties.BlockSetType;
26
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
27
import net.minecraft.world.level.block.state.properties.BooleanProperty;
28
import net.minecraft.world.level.block.state.properties.DoorHingeSide;
29
import net.minecraft.world.level.block.state.properties.DoubleBlockHalf;
30
import net.minecraft.world.level.block.state.properties.EnumProperty;
31
import net.minecraft.world.level.gameevent.GameEvent;
32
import net.minecraft.world.level.pathfinder.PathComputationType;
33
import net.minecraft.world.level.redstone.Orientation;
34
import net.minecraft.world.phys.BlockHitResult;
35
import net.minecraft.world.phys.Vec3;
36
import net.minecraft.world.phys.shapes.CollisionContext;
37
import net.minecraft.world.phys.shapes.Shapes;
38
import net.minecraft.world.phys.shapes.VoxelShape;
39
import org.jspecify.annotations.Nullable;
40
41
/**
42
 * Access widened by fabric-transitive-access-wideners-v1 to accessible
43
 */
44
public class DoorBlock extends Block {
45
	public static final EnumProperty<Direction> FACING = HorizontalDirectionalBlock.FACING;
46
	public static final EnumProperty<DoubleBlockHalf> HALF = BlockStateProperties.DOUBLE_BLOCK_HALF;
47
	public static final EnumProperty<DoorHingeSide> HINGE = BlockStateProperties.DOOR_HINGE;
48
	public static final BooleanProperty OPEN = BlockStateProperties.OPEN;
49
	public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
50
	private static final Map<Direction, VoxelShape> SHAPES = Shapes.rotateHorizontal(Block.boxZ(16.0, 13.0, 16.0));
51
	private final BlockSetType type;
52
53
	/**
54
	 * Access widened by fabric-transitive-access-wideners-v1 to accessible
55
	 */
56
	public DoorBlock(final BlockSetType type, final BlockBehaviour.Properties properties) {
57
		super(properties.sound(type.soundType()));
58
		this.type = type;
59
		this.registerDefaultState(
60
			this.stateDefinition
61
				.any()
62
				.setValue(FACING, Direction.NORTH)
63
				.setValue(OPEN, false)
64
				.setValue(HINGE, DoorHingeSide.LEFT)
65
				.setValue(POWERED, false)
66
				.setValue(HALF, DoubleBlockHalf.LOWER)
67
		);
68
	}
69
70
	public BlockSetType type() {
71
		return this.type;
72
	}
73
74
	@Override
75
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
76
		Direction direction = state.getValue(FACING);
77
		Direction doorDirection = state.getValue(OPEN)
78
			? (state.getValue(HINGE) == DoorHingeSide.RIGHT ? direction.getCounterClockWise() : direction.getClockWise())
79
			: direction;
80
		return SHAPES.get(doorDirection);
81
	}
82
83
	@Override
84
	protected BlockState updateShape(
85
		final BlockState state,
86
		final LevelReader level,
87
		final ScheduledTickAccess ticks,
88
		final BlockPos pos,
89
		final Direction directionToNeighbour,
90
		final BlockPos neighbourPos,
91
		final BlockState neighbourState,
92
		final RandomSource random
93
	) {
94
		DoubleBlockHalf half = state.getValue(HALF);
95
		if (directionToNeighbour.getAxis() != Direction.Axis.Y || half == DoubleBlockHalf.LOWER != (directionToNeighbour == Direction.UP)) {
96
			return half == DoubleBlockHalf.LOWER && directionToNeighbour == Direction.DOWN && !state.canSurvive(level, pos)
97
				? Blocks.AIR.defaultBlockState()
98
				: super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
99
		} else {
100
			return neighbourState.getBlock() instanceof DoorBlock && neighbourState.getValue(HALF) != half
101
				? neighbourState.setValue(HALF, half)
102
				: Blocks.AIR.defaultBlockState();
103
		}
104
	}
105
106
	@Override
107
	protected void onExplosionHit(
108
		final BlockState state, final ServerLevel level, final BlockPos pos, final Explosion explosion, final BiConsumer<ItemStack, BlockPos> onHit
109
	) {
110
		if (explosion.canTriggerBlocks() && state.getValue(HALF) == DoubleBlockHalf.LOWER && this.type.canOpenByWindCharge() && !state.getValue(POWERED)) {
111
			this.setOpen(null, level, state, pos, !this.isOpen(state));
112
		}
113
114
		super.onExplosionHit(state, level, pos, explosion, onHit);
115
	}
116
117
	@Override
118
	public BlockState playerWillDestroy(final Level level, final BlockPos pos, final BlockState state, final Player player) {
119
		if (!level.isClientSide() && (player.preventsBlockDrops() || !player.hasCorrectToolForDrops(state))) {
120
			DoublePlantBlock.preventDropFromBottomPart(level, pos, state, player);
121
		}
122
123
		return super.playerWillDestroy(level, pos, state, player);
124
	}
125
126
	@Override
127
	protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
128
		return switch (type) {
129
			case LAND, AIR -> state.getValue(OPEN);
130
			case WATER -> false;
131
		};
132
	}
133
134
	@Override
135
	public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
136
		BlockPos pos = context.getClickedPos();
137
		Level level = context.getLevel();
138
		if (pos.getY() < level.getMaxY() && level.getBlockState(pos.above()).canBeReplaced(context)) {
139
			boolean powered = level.hasNeighborSignal(pos) || level.hasNeighborSignal(pos.above());
140
			return this.defaultBlockState()
141
				.setValue(FACING, context.getHorizontalDirection())
142
				.setValue(HINGE, this.getHinge(context))
143
				.setValue(POWERED, powered)
144
				.setValue(OPEN, powered)
145
				.setValue(HALF, DoubleBlockHalf.LOWER);
146
		} else {
147
			return null;
148
		}
149
	}
150
151
	@Override
152
	public void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack) {
153
		level.setBlockAndUpdate(pos.above(), state.setValue(HALF, DoubleBlockHalf.UPPER));
154
	}
155
156
	private DoorHingeSide getHinge(final BlockPlaceContext context) {
157
		BlockGetter level = context.getLevel();
158
		BlockPos pos = context.getClickedPos();
159
		Direction placeDirection = context.getHorizontalDirection();
160
		BlockPos abovePos = pos.above();
161
		Direction leftDirection = placeDirection.getCounterClockWise();
162
		BlockPos leftPos = pos.relative(leftDirection);
163
		BlockState leftState = level.getBlockState(leftPos);
164
		BlockPos leftAbovePos = abovePos.relative(leftDirection);
165
		BlockState leftAboveState = level.getBlockState(leftAbovePos);
166
		Direction rightDirection = placeDirection.getClockWise();
167
		BlockPos rightPos = pos.relative(rightDirection);
168
		BlockState rightState = level.getBlockState(rightPos);
169
		BlockPos rightAbovePos = abovePos.relative(rightDirection);
170
		BlockState rightAboveState = level.getBlockState(rightAbovePos);
171
		int solidBlockBalance = (leftState.isCollisionShapeFullBlock(level, leftPos) ? -1 : 0)
172
			+ (leftAboveState.isCollisionShapeFullBlock(level, leftAbovePos) ? -1 : 0)
173
			+ (rightState.isCollisionShapeFullBlock(level, rightPos) ? 1 : 0)
174
			+ (rightAboveState.isCollisionShapeFullBlock(level, rightAbovePos) ? 1 : 0);
175
		boolean doorLeft = leftState.getBlock() instanceof DoorBlock && leftState.getValue(HALF) == DoubleBlockHalf.LOWER;
176
		boolean doorRight = rightState.getBlock() instanceof DoorBlock && rightState.getValue(HALF) == DoubleBlockHalf.LOWER;
177
		if ((!doorLeft || doorRight) && solidBlockBalance <= 0) {
178
			if ((!doorRight || doorLeft) && solidBlockBalance >= 0) {
179
				int stepX = placeDirection.getStepX();
180
				int stepZ = placeDirection.getStepZ();
181
				Vec3 clickLocation = context.getClickLocation();
182
				double clickX = clickLocation.x - pos.getX();
183
				double clickZ = clickLocation.z - pos.getZ();
184
				return (stepX >= 0 || !(clickZ < 0.5)) && (stepX <= 0 || !(clickZ > 0.5)) && (stepZ >= 0 || !(clickX > 0.5)) && (stepZ <= 0 || !(clickX < 0.5))
185
					? DoorHingeSide.LEFT
186
					: DoorHingeSide.RIGHT;
187
			} else {
188
				return DoorHingeSide.LEFT;
189
			}
190
		} else {
191
			return DoorHingeSide.RIGHT;
192
		}
193
	}
194
195
	@Override
196
	protected InteractionResult useWithoutItem(BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
197
		if (!this.type.canOpenByHand()) {
198
			return InteractionResult.PASS;
199
		}
200
201
		state = state.cycle(OPEN);
202
		level.setBlock(pos, state, 10);
203
		this.playSound(player, level, pos, state.getValue(OPEN));
204
		level.gameEvent(player, this.isOpen(state) ? GameEvent.BLOCK_OPEN : GameEvent.BLOCK_CLOSE, pos);
205
		return InteractionResult.SUCCESS;
206
	}
207
208
	public boolean isOpen(final BlockState state) {
209
		return state.getValue(OPEN);
210
	}
211
212
	public void setOpen(final @Nullable Entity sourceEntity, final Level level, final BlockState state, final BlockPos pos, final boolean shouldOpen) {
213
		if (state.is(this) && state.getValue(OPEN) != shouldOpen) {
214
			level.setBlock(pos, state.setValue(OPEN, shouldOpen), 10);
215
			this.playSound(sourceEntity, level, pos, shouldOpen);
216
			level.gameEvent(sourceEntity, shouldOpen ? GameEvent.BLOCK_OPEN : GameEvent.BLOCK_CLOSE, pos);
217
		}
218
	}
219
220
	@Override
221
	protected void neighborChanged(
222
		final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
223
	) {
224
		boolean signal = level.hasNeighborSignal(pos)
225
			|| level.hasNeighborSignal(pos.relative(state.getValue(HALF) == DoubleBlockHalf.LOWER ? Direction.UP : Direction.DOWN));
226
		if (!this.defaultBlockState().is(block) && signal != state.getValue(POWERED)) {
227
			if (signal != state.getValue(OPEN)) {
228
				this.playSound(null, level, pos, signal);
229
				level.gameEvent(null, signal ? GameEvent.BLOCK_OPEN : GameEvent.BLOCK_CLOSE, pos);
230
			}
231
232
			level.setBlock(pos, state.setValue(POWERED, signal).setValue(OPEN, signal), 2);
233
		}
234
	}
235
236
	@Override
237
	protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
238
		BlockPos below = pos.below();
239
		BlockState belowState = level.getBlockState(below);
240
		return state.getValue(HALF) == DoubleBlockHalf.LOWER ? belowState.isFaceSturdy(level, below, Direction.UP) : belowState.is(this);
241
	}
242
243
	private void playSound(final @Nullable Entity entity, final Level level, final BlockPos pos, final boolean open) {
244
		level.playSound(entity, pos, open ? this.type.doorOpen() : this.type.doorClose(), SoundSource.BLOCKS, 1.0F, level.getRandom().nextFloat() * 0.1F + 0.9F);
245
	}
246
247
	@Override
248
	protected BlockState rotate(final BlockState state, final Rotation rotation) {
249
		return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
250
	}
251
252
	@Override
253
	protected BlockState mirror(final BlockState state, final Mirror mirror) {
254
		return mirror == Mirror.NONE ? state : state.rotate(mirror.getRotation(state.getValue(FACING))).cycle(HINGE);
255
	}
256
257
	@Override
258
	protected long getSeed(final BlockState state, final BlockPos pos) {
259
		return Mth.getSeed(pos.getX(), pos.below(state.getValue(HALF) == DoubleBlockHalf.LOWER ? 0 : 1).getY(), pos.getZ());
260
	}
261
262
	@Override
263
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
264
		builder.add(HALF, FACING, OPEN, HINGE, POWERED);
265
	}
266
267
	public static boolean isWoodenDoor(final Level level, final BlockPos pos) {
268
		return isWoodenDoor(level.getBlockState(pos));
269
	}
270
271
	public static boolean isWoodenDoor(final BlockState state) {
272
		return state.getBlock() instanceof DoorBlock door && door.type().canOpenByHand();
273
	}
274
}
275

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