FaceAttachedHorizontalDirectionalBlock .java

net.minecraft.world.level.block.FaceAttachedHorizontalDirectionalBlock (class): members: FACE, canAttach, getConnectedDirection, canSurvive, updateShape.

net.minecraft.world.level.blockclass77 linescommonextends HorizontalDirectionalBlock2.9 KB

API Surface (4)

boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos)
boolean canAttach(final LevelReader level, final BlockPos pos, final Direction direction)
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)
Direction getConnectedDirection(final BlockState state)

Fields & Constants (8)

FACE
state
level
ticks
pos
directionToNeighbour
neighbourPos
neighbourState

Full Source

FaceAttachedHorizontalDirectionalBlock.java77 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.core.Direction;
5
import net.minecraft.util.RandomSource;
6
import net.minecraft.world.item.context.BlockPlaceContext;
7
import net.minecraft.world.level.LevelReader;
8
import net.minecraft.world.level.ScheduledTickAccess;
9
import net.minecraft.world.level.block.state.BlockBehaviour;
10
import net.minecraft.world.level.block.state.BlockState;
11
import net.minecraft.world.level.block.state.properties.AttachFace;
12
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
13
import net.minecraft.world.level.block.state.properties.EnumProperty;
14
import org.jspecify.annotations.Nullable;
15
16
public abstract class FaceAttachedHorizontalDirectionalBlock extends HorizontalDirectionalBlock {
17
	public static final EnumProperty<AttachFace> FACE = BlockStateProperties.ATTACH_FACE;
18
19
	protected FaceAttachedHorizontalDirectionalBlock(final BlockBehaviour.Properties properties) {
20
		super(properties);
21
	}
22
23
	@Override
24
	protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
25
		return canAttach(level, pos, getConnectedDirection(state).getOpposite());
26
	}
27
28
	public static boolean canAttach(final LevelReader level, final BlockPos pos, final Direction direction) {
29
		BlockPos relative = pos.relative(direction);
30
		return level.getBlockState(relative).isFaceSturdy(level, relative, direction.getOpposite());
31
	}
32
33
	@Override
34
	public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
35
		for (Direction direction : context.getNearestLookingDirections()) {
36
			BlockState state;
37
			if (direction.getAxis() == Direction.Axis.Y) {
38
				state = this.defaultBlockState()
39
					.setValue(FACE, direction == Direction.UP ? AttachFace.CEILING : AttachFace.FLOOR)
40
					.setValue(FACING, context.getHorizontalDirection());
41
			} else {
42
				state = this.defaultBlockState().setValue(FACE, AttachFace.WALL).setValue(FACING, direction.getOpposite());
43
			}
44
45
			if (state.canSurvive(context.getLevel(), context.getClickedPos())) {
46
				return state;
47
			}
48
		}
49
50
		return null;
51
	}
52
53
	@Override
54
	protected BlockState updateShape(
55
		final BlockState state,
56
		final LevelReader level,
57
		final ScheduledTickAccess ticks,
58
		final BlockPos pos,
59
		final Direction directionToNeighbour,
60
		final BlockPos neighbourPos,
61
		final BlockState neighbourState,
62
		final RandomSource random
63
	) {
64
		return getConnectedDirection(state).getOpposite() == directionToNeighbour && !state.canSurvive(level, pos)
65
			? Blocks.AIR.defaultBlockState()
66
			: super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
67
	}
68
69
	protected static Direction getConnectedDirection(final BlockState state) {
70
		return switch ((AttachFace)state.getValue(FACE)) {
71
			case CEILING -> Direction.DOWN;
72
			case FLOOR -> Direction.UP;
73
			default -> (Direction)state.getValue(FACING);
74
		};
75
	}
76
}
77

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