PistonBaseBlock .java

net.minecraft.world.level.block.piston.PistonBaseBlock (class): members: TRIGGER_CONTRACT, allowDestroyable, TRIGGER_EXTEND, TRIGGER_DROP, isSticky.

net.minecraft.world.level.block.pistonclass379 linescommonextends DirectionalBlock13.9 KB

API Surface (12)

VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack)
void neighborChanged(final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston)
void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston)
BlockState getStateForPlacement(final BlockPlaceContext context)
boolean triggerEvent(final BlockState state, final Level level, final BlockPos pos, final int b0, final int b1)
boolean isPushable(final BlockState state, final Level level, final BlockPos pos, final Direction direction, final boolean allowDestroyable, final Direction connectionDirection)
BlockState rotate(final BlockState state, final Rotation rotation)
BlockState mirror(final BlockState state, final Mirror mirror)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)
boolean useShapeForLightOcclusion(final BlockState state)
boolean isPathfindable(final BlockState state, final PathComputationType type)

Fields & Constants (9)

EXTENDED
TRIGGER_EXTEND
TRIGGER_CONTRACT
TRIGGER_DROP
PLATFORM_THICKNESS
SHAPES
isSticky
orientation
allowDestroyable

Full Source

PistonBaseBlock.java379 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block.piston;
2
3
import com.google.common.collect.Lists;
4
import com.google.common.collect.Maps;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.Map.Entry;
8
import net.minecraft.core.BlockPos;
9
import net.minecraft.core.Direction;
10
import net.minecraft.server.level.ServerLevel;
11
import net.minecraft.sounds.SoundEvents;
12
import net.minecraft.sounds.SoundSource;
13
import net.minecraft.tags.BlockTags;
14
import net.minecraft.util.RandomSource;
15
import net.minecraft.world.entity.LivingEntity;
16
import net.minecraft.world.item.ItemStack;
17
import net.minecraft.world.item.context.BlockPlaceContext;
18
import net.minecraft.world.level.BlockGetter;
19
import net.minecraft.world.level.Level;
20
import net.minecraft.world.level.SignalGetter;
21
import net.minecraft.world.level.block.Block;
22
import net.minecraft.world.level.block.Blocks;
23
import net.minecraft.world.level.block.DirectionalBlock;
24
import net.minecraft.world.level.block.Mirror;
25
import net.minecraft.world.level.block.Rotation;
26
import net.minecraft.world.level.block.entity.BlockEntity;
27
import net.minecraft.world.level.block.state.BlockBehaviour;
28
import net.minecraft.world.level.block.state.BlockState;
29
import net.minecraft.world.level.block.state.StateDefinition;
30
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
31
import net.minecraft.world.level.block.state.properties.BooleanProperty;
32
import net.minecraft.world.level.block.state.properties.PistonType;
33
import net.minecraft.world.level.gameevent.GameEvent;
34
import net.minecraft.world.level.material.PushReaction;
35
import net.minecraft.world.level.pathfinder.PathComputationType;
36
import net.minecraft.world.level.redstone.ExperimentalRedstoneUtils;
37
import net.minecraft.world.level.redstone.Orientation;
38
import net.minecraft.world.phys.shapes.CollisionContext;
39
import net.minecraft.world.phys.shapes.Shapes;
40
import net.minecraft.world.phys.shapes.VoxelShape;
41
import org.jspecify.annotations.Nullable;
42
43
public class PistonBaseBlock extends DirectionalBlock {
44
	public static final BooleanProperty EXTENDED = BlockStateProperties.EXTENDED;
45
	public static final int TRIGGER_EXTEND = 0;
46
	public static final int TRIGGER_CONTRACT = 1;
47
	public static final int TRIGGER_DROP = 2;
48
	public static final int PLATFORM_THICKNESS = 4;
49
	private static final Map<Direction, VoxelShape> SHAPES = Shapes.rotateAll(Block.boxZ(16.0, 4.0, 16.0));
50
	private final boolean isSticky;
51
52
	public PistonBaseBlock(final boolean isSticky, final BlockBehaviour.Properties properties) {
53
		super(properties);
54
		this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(EXTENDED, false));
55
		this.isSticky = isSticky;
56
	}
57
58
	@Override
59
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
60
		return state.getValue(EXTENDED) ? SHAPES.get(state.getValue(FACING)) : Shapes.block();
61
	}
62
63
	@Override
64
	public void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack) {
65
		if (!level.isClientSide()) {
66
			this.checkIfExtend(level, pos, state);
67
		}
68
	}
69
70
	@Override
71
	protected void neighborChanged(
72
		final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
73
	) {
74
		if (!level.isClientSide()) {
75
			this.checkIfExtend(level, pos, state);
76
		}
77
	}
78
79
	@Override
80
	protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
81
		if (!oldState.is(state.getBlock())) {
82
			if (!level.isClientSide() && level.getBlockEntity(pos) == null) {
83
				this.checkIfExtend(level, pos, state);
84
			}
85
		}
86
	}
87
88
	@Override
89
	public BlockState getStateForPlacement(final BlockPlaceContext context) {
90
		return this.defaultBlockState().setValue(FACING, context.getNearestLookingDirection().getOpposite()).setValue(EXTENDED, false);
91
	}
92
93
	private void checkIfExtend(final Level level, final BlockPos pos, final BlockState state) {
94
		Direction direction = state.getValue(FACING);
95
		boolean extend = this.getNeighborSignal(level, pos, direction);
96
		if (extend && !state.getValue(EXTENDED)) {
97
			if (new PistonStructureResolver(level, pos, direction, true).resolve()) {
98
				level.blockEvent(pos, this, 0, direction.get3DDataValue());
99
			}
100
		} else if (!extend && state.getValue(EXTENDED)) {
101
			BlockPos pushedPos = pos.relative(direction, 2);
102
			BlockState pushedState = level.getBlockState(pushedPos);
103
			int event = 1;
104
			if (pushedState.is(Blocks.MOVING_PISTON)
105
				&& pushedState.getValue(FACING) == direction
106
				&& level.getBlockEntity(pushedPos) instanceof PistonMovingBlockEntity pistonEntity
107
				&& pistonEntity.isExtending()
108
				&& (pistonEntity.getProgress(0.0F) < 0.5F || level.getGameTime() == pistonEntity.getLastTicked() || ((ServerLevel)level).isHandlingTick())) {
109
				event = 2;
110
			}
111
112
			level.blockEvent(pos, this, event, direction.get3DDataValue());
113
		}
114
	}
115
116
	private boolean getNeighborSignal(final SignalGetter level, final BlockPos pos, final Direction pushDirection) {
117
		for (Direction direction : Direction.values()) {
118
			if (direction != pushDirection && level.hasSignal(pos.relative(direction), direction)) {
119
				return true;
120
			}
121
		}
122
123
		if (level.hasSignal(pos, Direction.DOWN)) {
124
			return true;
125
		}
126
127
		BlockPos above = pos.above();
128
129
		for (Direction direction : Direction.values()) {
130
			if (direction != Direction.DOWN && level.hasSignal(above.relative(direction), direction)) {
131
				return true;
132
			}
133
		}
134
135
		return false;
136
	}
137
138
	@Override
139
	protected boolean triggerEvent(final BlockState state, final Level level, final BlockPos pos, final int b0, final int b1) {
140
		Direction direction = state.getValue(FACING);
141
		BlockState extendedState = state.setValue(EXTENDED, true);
142
		if (!level.isClientSide()) {
143
			boolean extend = this.getNeighborSignal(level, pos, direction);
144
			if (extend && (b0 == 1 || b0 == 2)) {
145
				level.setBlock(pos, extendedState, 2);
146
				return false;
147
			}
148
149
			if (!extend && b0 == 0) {
150
				return false;
151
			}
152
		}
153
154
		RandomSource random = level.getRandom();
155
		if (b0 == 0) {
156
			if (!this.moveBlocks(level, pos, direction, true)) {
157
				return false;
158
			}
159
160
			level.setBlock(pos, extendedState, 67);
161
			level.playSound(null, pos, SoundEvents.PISTON_EXTEND, SoundSource.BLOCKS, 0.5F, random.nextFloat() * 0.25F + 0.6F);
162
			level.gameEvent(GameEvent.BLOCK_ACTIVATE, pos, GameEvent.Context.of(extendedState));
163
		} else if (b0 == 1 || b0 == 2) {
164
			if (level.getBlockEntity(pos.relative(direction)) instanceof PistonMovingBlockEntity pistonMovingBlockEntity) {
165
				pistonMovingBlockEntity.finalTick();
166
			}
167
168
			BlockState movingPistonState = Blocks.MOVING_PISTON
169
				.defaultBlockState()
170
				.setValue(MovingPistonBlock.FACING, direction)
171
				.setValue(MovingPistonBlock.TYPE, this.isSticky ? PistonType.STICKY : PistonType.DEFAULT);
172
			level.setBlock(pos, movingPistonState, 276);
173
			level.setBlockEntity(
174
				MovingPistonBlock.newMovingBlockEntity(
175
					pos, movingPistonState, this.defaultBlockState().setValue(FACING, Direction.from3DDataValue(b1 & 7)), direction, false, true
176
				)
177
			);
178
			level.updateNeighborsAt(pos, movingPistonState.getBlock());
179
			movingPistonState.updateNeighbourShapes(level, pos, 2);
180
			if (this.isSticky) {
181
				BlockPos twoPos = pos.offset(direction.getStepX() * 2, direction.getStepY() * 2, direction.getStepZ() * 2);
182
				BlockState movingState = level.getBlockState(twoPos);
183
				boolean pistonPiece = false;
184
				if (movingState.is(Blocks.MOVING_PISTON)
185
					&& level.getBlockEntity(twoPos) instanceof PistonMovingBlockEntity entity
186
					&& entity.getDirection() == direction
187
					&& entity.isExtending()) {
188
					entity.finalTick();
189
					pistonPiece = true;
190
				}
191
192
				if (!pistonPiece) {
193
					if (b0 != 1
194
						|| movingState.isAir()
195
						|| !isPushable(movingState, level, twoPos, direction.getOpposite(), false, direction)
196
						|| movingState.getPistonPushReaction() != PushReaction.PUSH_PULL && !movingState.is(Blocks.PISTON) && !movingState.is(Blocks.STICKY_PISTON)) {
197
						level.removeBlock(pos.relative(direction), false);
198
					} else {
199
						this.moveBlocks(level, pos, direction, false);
200
					}
201
				}
202
			} else {
203
				level.removeBlock(pos.relative(direction), false);
204
			}
205
206
			level.playSound(null, pos, SoundEvents.PISTON_CONTRACT, SoundSource.BLOCKS, 0.5F, random.nextFloat() * 0.15F + 0.6F);
207
			level.gameEvent(GameEvent.BLOCK_DEACTIVATE, pos, GameEvent.Context.of(movingPistonState));
208
		}
209
210
		return true;
211
	}
212
213
	public static boolean isPushable(
214
		final BlockState state, final Level level, final BlockPos pos, final Direction direction, final boolean allowDestroyable, final Direction connectionDirection
215
	) {
216
		if (pos.getY() < level.getMinY() || pos.getY() > level.getMaxY() || !level.getWorldBorder().isWithinBounds(pos)) {
217
			return false;
218
		}
219
220
		if (state.isAir()) {
221
			return true;
222
		}
223
224
		if (direction == Direction.DOWN && pos.getY() == level.getMinY()) {
225
			return false;
226
		}
227
228
		if (direction == Direction.UP && pos.getY() == level.getMaxY()) {
229
			return false;
230
		}
231
232
		if (!state.is(Blocks.PISTON) && !state.is(Blocks.STICKY_PISTON)) {
233
			if (state.getDestroySpeed(level, pos) == -1.0F) {
234
				return false;
235
			}
236
237
			switch (state.getPistonPushReaction()) {
238
				case IMMOVEABLE:
239
					return false;
240
				case POPPED:
241
					return allowDestroyable;
242
				case PUSH:
243
					return direction == connectionDirection;
244
			}
245
		} else if (state.getValue(EXTENDED)) {
246
			return false;
247
		}
248
249
		return !state.hasBlockEntity();
250
	}
251
252
	private boolean moveBlocks(final Level level, final BlockPos pistonPos, final Direction direction, final boolean extending) {
253
		BlockPos armPos = pistonPos.relative(direction);
254
		if (!extending && level.getBlockState(armPos).is(Blocks.PISTON_HEAD)) {
255
			level.setBlock(armPos, Blocks.AIR.defaultBlockState(), 276);
256
		}
257
258
		PistonStructureResolver resolver = new PistonStructureResolver(level, pistonPos, direction, extending);
259
		if (!resolver.resolve()) {
260
			return false;
261
		}
262
263
		Map<BlockPos, BlockState> deleteAfterMove = Maps.newHashMap();
264
		List<BlockPos> toPush = resolver.getToPush();
265
		List<BlockState> toPushShapes = Lists.newArrayList();
266
267
		for (BlockPos pos : toPush) {
268
			BlockState state = level.getBlockState(pos);
269
			toPushShapes.add(state);
270
			deleteAfterMove.put(pos, state);
271
		}
272
273
		List<BlockPos> toDestroy = resolver.getToDestroy();
274
		BlockState[] toUpdate = new BlockState[toPush.size() + toDestroy.size()];
275
		Direction pushDirection = extending ? direction : direction.getOpposite();
276
		int updateIndex = 0;
277
278
		for (int i = toDestroy.size() - 1; i >= 0; i--) {
279
			BlockPos pos = toDestroy.get(i);
280
			BlockState state = level.getBlockState(pos);
281
			BlockEntity blockEntity = state.hasBlockEntity() ? level.getBlockEntity(pos) : null;
282
			dropResources(state, level, pos, blockEntity);
283
			if (!state.is(BlockTags.FIRE) && level.isClientSide()) {
284
				level.levelEvent(2001, pos, getId(state));
285
			}
286
287
			level.setBlock(pos, Blocks.AIR.defaultBlockState(), 18);
288
			level.gameEvent(GameEvent.BLOCK_DESTROY, pos, GameEvent.Context.of(state));
289
			toUpdate[updateIndex++] = state;
290
		}
291
292
		for (int i = toPush.size() - 1; i >= 0; i--) {
293
			BlockPos pos = toPush.get(i);
294
			BlockState blockState = level.getBlockState(pos);
295
			pos = pos.relative(pushDirection);
296
			deleteAfterMove.remove(pos);
297
			BlockState actualState = Blocks.MOVING_PISTON.defaultBlockState().setValue(FACING, direction);
298
			level.setBlock(pos, actualState, 324);
299
			level.setBlockEntity(MovingPistonBlock.newMovingBlockEntity(pos, actualState, toPushShapes.get(i), direction, extending, false));
300
			toUpdate[updateIndex++] = blockState;
301
		}
302
303
		if (extending) {
304
			PistonType type = this.isSticky ? PistonType.STICKY : PistonType.DEFAULT;
305
			BlockState state = Blocks.PISTON_HEAD.defaultBlockState().setValue(PistonHeadBlock.FACING, direction).setValue(PistonHeadBlock.TYPE, type);
306
			BlockState blockState = Blocks.MOVING_PISTON
307
				.defaultBlockState()
308
				.setValue(MovingPistonBlock.FACING, direction)
309
				.setValue(MovingPistonBlock.TYPE, this.isSticky ? PistonType.STICKY : PistonType.DEFAULT);
310
			deleteAfterMove.remove(armPos);
311
			level.setBlock(armPos, blockState, 324);
312
			level.setBlockEntity(MovingPistonBlock.newMovingBlockEntity(armPos, blockState, state, direction, true, true));
313
		}
314
315
		BlockState air = Blocks.AIR.defaultBlockState();
316
317
		for (BlockPos pos : deleteAfterMove.keySet()) {
318
			level.setBlock(pos, air, 82);
319
		}
320
321
		for (Entry<BlockPos, BlockState> entry : deleteAfterMove.entrySet()) {
322
			BlockPos pos = entry.getKey();
323
			BlockState oldState = entry.getValue();
324
			oldState.updateIndirectNeighbourShapes(level, pos, 2);
325
			air.updateNeighbourShapes(level, pos, 2);
326
			air.updateIndirectNeighbourShapes(level, pos, 2);
327
		}
328
329
		Orientation orientation = ExperimentalRedstoneUtils.initialOrientation(level, resolver.getPushDirection(), null);
330
		updateIndex = 0;
331
332
		for (int i = toDestroy.size() - 1; i >= 0; i--) {
333
			BlockState state = toUpdate[updateIndex++];
334
			BlockPos pos = toDestroy.get(i);
335
			if (level instanceof ServerLevel serverLevel) {
336
				state.affectNeighborsAfterRemoval(serverLevel, pos, false);
337
			}
338
339
			state.updateIndirectNeighbourShapes(level, pos, 2);
340
			level.updateNeighborsAt(pos, state.getBlock(), orientation);
341
		}
342
343
		for (int i = toPush.size() - 1; i >= 0; i--) {
344
			level.updateNeighborsAt(toPush.get(i), toUpdate[updateIndex++].getBlock(), orientation);
345
		}
346
347
		if (extending) {
348
			level.updateNeighborsAt(armPos, Blocks.PISTON_HEAD, orientation);
349
		}
350
351
		return true;
352
	}
353
354
	@Override
355
	protected BlockState rotate(final BlockState state, final Rotation rotation) {
356
		return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
357
	}
358
359
	@Override
360
	protected BlockState mirror(final BlockState state, final Mirror mirror) {
361
		return state.rotate(mirror.getRotation(state.getValue(FACING)));
362
	}
363
364
	@Override
365
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
366
		builder.add(FACING, EXTENDED);
367
	}
368
369
	@Override
370
	protected boolean useShapeForLightOcclusion(final BlockState state) {
371
		return state.getValue(EXTENDED);
372
	}
373
374
	@Override
375
	protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
376
		return false;
377
	}
378
}
379

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