MultifaceBlock .java

net.minecraft.world.level.block.MultifaceBlock (class): members: isValidStateForPlacement, getFaceProperty, isFaceSupported, availableFaces, placementPos.

net.minecraft.world.level.blockclass295 linescommonextends Block9.9 KB

Implements

API Surface (18)

Set<Direction> availableFaces(final BlockState state)
Set<Direction> unpack(final byte data)
byte pack(final Collection<Direction> directions)
boolean isFaceSupported(final Direction faceDirection)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)
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)
FluidState getFluidState(final BlockState state)
VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos)
boolean canBeReplaced(final BlockState state, final BlockPlaceContext context)
boolean isValidStateForPlacement(final BlockGetter level, final BlockState oldState, final BlockPos placementPos, final Direction placementDirection)
BlockState rotate(final BlockState state, final Rotation rotation)
BlockState mirror(final BlockState state, final Mirror mirror)
boolean hasFace(final BlockState state, final Direction faceDirection)
boolean canAttachTo(final BlockGetter level, final BlockPos pos, final Direction directionTowardsNeighbour)
boolean canAttachTo(final BlockGetter level, final Direction directionTowardsNeighbour, final BlockPos neighbourPos, final BlockState neighbourState)
BooleanProperty getFaceProperty(final Direction faceDirection)
boolean hasAnyFace(final BlockState state)

Fields & Constants (15)

WATERLOGGED
PROPERTY_BY_DIRECTION
DIRECTIONS
shapes
canRotate
canMirrorX
canMirrorZ
state
level
ticks
pos
directionToNeighbour
neighbourPos
neighbourState
placementPos

Full Source

MultifaceBlock.java295 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import java.util.Arrays;
4
import java.util.Collection;
5
import java.util.EnumSet;
6
import java.util.Map;
7
import java.util.Objects;
8
import java.util.Set;
9
import java.util.function.Function;
10
import net.minecraft.core.BlockPos;
11
import net.minecraft.core.Direction;
12
import net.minecraft.util.RandomSource;
13
import net.minecraft.world.item.context.BlockPlaceContext;
14
import net.minecraft.world.level.BlockGetter;
15
import net.minecraft.world.level.Level;
16
import net.minecraft.world.level.LevelReader;
17
import net.minecraft.world.level.ScheduledTickAccess;
18
import net.minecraft.world.level.block.state.BlockBehaviour;
19
import net.minecraft.world.level.block.state.BlockState;
20
import net.minecraft.world.level.block.state.StateDefinition;
21
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
22
import net.minecraft.world.level.block.state.properties.BooleanProperty;
23
import net.minecraft.world.level.material.FluidState;
24
import net.minecraft.world.level.material.Fluids;
25
import net.minecraft.world.phys.shapes.CollisionContext;
26
import net.minecraft.world.phys.shapes.Shapes;
27
import net.minecraft.world.phys.shapes.VoxelShape;
28
import org.jspecify.annotations.Nullable;
29
30
public class MultifaceBlock extends Block implements SimpleWaterloggedBlock {
31
	public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
32
	private static final Map<Direction, BooleanProperty> PROPERTY_BY_DIRECTION = PipeBlock.PROPERTY_BY_DIRECTION;
33
	protected static final Direction[] DIRECTIONS = Direction.values();
34
	private final Function<BlockState, VoxelShape> shapes;
35
	private final boolean canRotate;
36
	private final boolean canMirrorX;
37
	private final boolean canMirrorZ;
38
39
	public MultifaceBlock(final BlockBehaviour.Properties properties) {
40
		super(properties);
41
		this.registerDefaultState(getDefaultMultifaceState(this.stateDefinition));
42
		this.shapes = this.makeShapes();
43
		this.canRotate = Direction.Plane.HORIZONTAL.stream().allMatch(this::isFaceSupported);
44
		this.canMirrorX = Direction.Plane.HORIZONTAL.stream().filter(Direction.Axis.X).filter(this::isFaceSupported).count() % 2L == 0L;
45
		this.canMirrorZ = Direction.Plane.HORIZONTAL.stream().filter(Direction.Axis.Z).filter(this::isFaceSupported).count() % 2L == 0L;
46
	}
47
48
	private Function<BlockState, VoxelShape> makeShapes() {
49
		Map<Direction, VoxelShape> shapes = Shapes.rotateAll(Block.boxZ(16.0, 0.0, 1.0));
50
		return this.getShapeForEachState(state -> {
51
			VoxelShape shape = Shapes.empty();
52
53
			for (Direction direction : DIRECTIONS) {
54
				if (hasFace(state, direction)) {
55
					shape = Shapes.or(shape, shapes.get(direction));
56
				}
57
			}
58
59
			return shape.isEmpty() ? Shapes.block() : shape;
60
		}, WATERLOGGED);
61
	}
62
63
	public static Set<Direction> availableFaces(final BlockState state) {
64
		if (!(state.getBlock() instanceof MultifaceBlock)) {
65
			return Set.of();
66
		}
67
68
		Set<Direction> faces = EnumSet.noneOf(Direction.class);
69
70
		for (Direction direction : Direction.values()) {
71
			if (hasFace(state, direction)) {
72
				faces.add(direction);
73
			}
74
		}
75
76
		return faces;
77
	}
78
79
	public static Set<Direction> unpack(final byte data) {
80
		Set<Direction> presentDirections = EnumSet.noneOf(Direction.class);
81
82
		for (Direction direction : Direction.values()) {
83
			if ((data & (byte)(1 << direction.ordinal())) > 0) {
84
				presentDirections.add(direction);
85
			}
86
		}
87
88
		return presentDirections;
89
	}
90
91
	public static byte pack(final Collection<Direction> directions) {
92
		byte code = 0;
93
94
		for (Direction direction : directions) {
95
			code = (byte)(code | 1 << direction.ordinal());
96
		}
97
98
		return code;
99
	}
100
101
	protected boolean isFaceSupported(final Direction faceDirection) {
102
		return true;
103
	}
104
105
	@Override
106
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
107
		for (Direction direction : DIRECTIONS) {
108
			if (this.isFaceSupported(direction)) {
109
				builder.add(getFaceProperty(direction));
110
			}
111
		}
112
113
		builder.add(WATERLOGGED);
114
	}
115
116
	@Override
117
	protected BlockState updateShape(
118
		final BlockState state,
119
		final LevelReader level,
120
		final ScheduledTickAccess ticks,
121
		final BlockPos pos,
122
		final Direction directionToNeighbour,
123
		final BlockPos neighbourPos,
124
		final BlockState neighbourState,
125
		final RandomSource random
126
	) {
127
		if (state.getValue(WATERLOGGED)) {
128
			ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
129
		}
130
131
		if (!hasAnyFace(state)) {
132
			return Blocks.AIR.defaultBlockState();
133
		} else {
134
			return hasFace(state, directionToNeighbour) && !canAttachTo(level, directionToNeighbour, neighbourPos, neighbourState)
135
				? removeFace(state, getFaceProperty(directionToNeighbour))
136
				: state;
137
		}
138
	}
139
140
	@Override
141
	protected FluidState getFluidState(final BlockState state) {
142
		return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
143
	}
144
145
	@Override
146
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
147
		return this.shapes.apply(state);
148
	}
149
150
	@Override
151
	protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
152
		boolean hasAtLeastOneFace = false;
153
154
		for (Direction directionToNeighbour : DIRECTIONS) {
155
			if (hasFace(state, directionToNeighbour)) {
156
				if (!canAttachTo(level, pos, directionToNeighbour)) {
157
					return false;
158
				}
159
160
				hasAtLeastOneFace = true;
161
			}
162
		}
163
164
		return hasAtLeastOneFace;
165
	}
166
167
	@Override
168
	protected boolean canBeReplaced(final BlockState state, final BlockPlaceContext context) {
169
		return !context.getItemInHand().is(this.asItem()) || hasAnyVacantFace(state);
170
	}
171
172
	@Override
173
	public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
174
		Level level = context.getLevel();
175
		BlockPos placePos = context.getClickedPos();
176
		BlockState oldState = level.getBlockState(placePos);
177
		return Arrays.stream(context.getNearestLookingDirections())
178
			.map(direction -> this.getStateForPlacement(oldState, level, placePos, direction))
179
			.filter(Objects::nonNull)
180
			.findFirst()
181
			.orElse(null);
182
	}
183
184
	public boolean isValidStateForPlacement(final BlockGetter level, final BlockState oldState, final BlockPos placementPos, final Direction placementDirection) {
185
		if (this.isFaceSupported(placementDirection) && (!oldState.is(this) || !hasFace(oldState, placementDirection))) {
186
			BlockPos neighbourPos = placementPos.relative(placementDirection);
187
			return canAttachTo(level, placementDirection, neighbourPos, level.getBlockState(neighbourPos));
188
		} else {
189
			return false;
190
		}
191
	}
192
193
	public @Nullable BlockState getStateForPlacement(
194
		final BlockState oldState, final BlockGetter level, final BlockPos placementPos, final Direction placementDirection
195
	) {
196
		if (!this.isValidStateForPlacement(level, oldState, placementPos, placementDirection)) {
197
			return null;
198
		}
199
200
		BlockState newState;
201
		if (oldState.is(this)) {
202
			newState = oldState;
203
		} else if (oldState.getFluidState().isSourceOfType(Fluids.WATER)) {
204
			newState = this.defaultBlockState().setValue(BlockStateProperties.WATERLOGGED, true);
205
		} else {
206
			newState = this.defaultBlockState();
207
		}
208
209
		return newState.setValue(getFaceProperty(placementDirection), true);
210
	}
211
212
	@Override
213
	protected BlockState rotate(final BlockState state, final Rotation rotation) {
214
		return !this.canRotate ? state : this.mapDirections(state, rotation::rotate);
215
	}
216
217
	@Override
218
	protected BlockState mirror(final BlockState state, final Mirror mirror) {
219
		if (mirror == Mirror.FRONT_BACK && !this.canMirrorX) {
220
			return state;
221
		} else {
222
			return mirror == Mirror.LEFT_RIGHT && !this.canMirrorZ ? state : this.mapDirections(state, mirror::mirror);
223
		}
224
	}
225
226
	private BlockState mapDirections(final BlockState state, final Function<Direction, Direction> mapping) {
227
		BlockState newState = state;
228
229
		for (Direction direction : DIRECTIONS) {
230
			if (this.isFaceSupported(direction)) {
231
				newState = newState.setValue(getFaceProperty(mapping.apply(direction)), state.getValue(getFaceProperty(direction)));
232
			}
233
		}
234
235
		return newState;
236
	}
237
238
	public static boolean hasFace(final BlockState state, final Direction faceDirection) {
239
		BooleanProperty property = getFaceProperty(faceDirection);
240
		return state.getValueOrElse(property, false);
241
	}
242
243
	public static boolean canAttachTo(final BlockGetter level, final BlockPos pos, final Direction directionTowardsNeighbour) {
244
		BlockPos neighbourPos = pos.relative(directionTowardsNeighbour);
245
		BlockState blockState = level.getBlockState(neighbourPos);
246
		return canAttachTo(level, directionTowardsNeighbour, neighbourPos, blockState);
247
	}
248
249
	public static boolean canAttachTo(
250
		final BlockGetter level, final Direction directionTowardsNeighbour, final BlockPos neighbourPos, final BlockState neighbourState
251
	) {
252
		return Block.isFaceFull(neighbourState.getBlockSupportShape(level, neighbourPos), directionTowardsNeighbour.getOpposite())
253
			|| Block.isFaceFull(neighbourState.getCollisionShape(level, neighbourPos), directionTowardsNeighbour.getOpposite());
254
	}
255
256
	private static BlockState removeFace(final BlockState state, final BooleanProperty property) {
257
		BlockState newState = state.setValue(property, false);
258
		return hasAnyFace(newState) ? newState : Blocks.AIR.defaultBlockState();
259
	}
260
261
	public static BooleanProperty getFaceProperty(final Direction faceDirection) {
262
		return PROPERTY_BY_DIRECTION.get(faceDirection);
263
	}
264
265
	private static BlockState getDefaultMultifaceState(final StateDefinition<Block, BlockState> stateDefinition) {
266
		BlockState state = stateDefinition.any().setValue(WATERLOGGED, false);
267
268
		for (BooleanProperty faceProperty : PROPERTY_BY_DIRECTION.values()) {
269
			state = state.trySetValue(faceProperty, false);
270
		}
271
272
		return state;
273
	}
274
275
	protected static boolean hasAnyFace(final BlockState state) {
276
		for (Direction direction : DIRECTIONS) {
277
			if (hasFace(state, direction)) {
278
				return true;
279
			}
280
		}
281
282
		return false;
283
	}
284
285
	private static boolean hasAnyVacantFace(final BlockState state) {
286
		for (Direction direction : DIRECTIONS) {
287
			if (!hasFace(state, direction)) {
288
				return true;
289
			}
290
		}
291
292
		return false;
293
	}
294
}
295

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