BasePressurePlateBlock .java

net.minecraft.world.level.block.BasePressurePlateBlock (class, 152 lines): members: updateNeighbours, SHAPE_PRESSED, TOUCH_AABB, getEntityCount, ownSignal.

net.minecraft.world.level.blockclass152 linescommonextends Block5.5 KB

API Surface (13)

VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
int getPressedTime()
boolean isPossibleToRespawnInThis(final BlockState state)
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)
boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos)
void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random)
void entityInside(final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise)
void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston)
void updateNeighbours(final Level level, final BlockPos pos)
int getDirectSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction direction)
boolean isSignalSource(final BlockState state)
int ownSignal(final BlockState state, final BlockGetter level, final BlockPos pos)
int getEntityCount(final Level level, final AABB entityDetectionBox, final Class<? extends Entity> entityClass)

Fields & Constants (12)

SHAPE_PRESSED
SHAPE
TOUCH_AABB
type
state
level
ticks
pos
directionToNeighbour
neighbourPos
neighbourState
effectApplier

Full Source

BasePressurePlateBlock.java152 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.server.level.ServerLevel;
6
import net.minecraft.sounds.SoundSource;
7
import net.minecraft.util.RandomSource;
8
import net.minecraft.world.entity.Entity;
9
import net.minecraft.world.entity.EntitySelector;
10
import net.minecraft.world.entity.InsideBlockEffectApplier;
11
import net.minecraft.world.level.BlockGetter;
12
import net.minecraft.world.level.Level;
13
import net.minecraft.world.level.LevelReader;
14
import net.minecraft.world.level.ScheduledTickAccess;
15
import net.minecraft.world.level.block.state.BlockBehaviour;
16
import net.minecraft.world.level.block.state.BlockState;
17
import net.minecraft.world.level.block.state.properties.BlockSetType;
18
import net.minecraft.world.level.gameevent.GameEvent;
19
import net.minecraft.world.phys.AABB;
20
import net.minecraft.world.phys.shapes.CollisionContext;
21
import net.minecraft.world.phys.shapes.VoxelShape;
22
import org.jspecify.annotations.Nullable;
23
24
public abstract class BasePressurePlateBlock extends Block {
25
	private static final VoxelShape SHAPE_PRESSED = Block.column(14.0, 0.0, 0.5);
26
	private static final VoxelShape SHAPE = Block.column(14.0, 0.0, 1.0);
27
	protected static final AABB TOUCH_AABB = Block.column(14.0, 0.0, 4.0).toAabbs().getFirst();
28
	protected final BlockSetType type;
29
30
	protected BasePressurePlateBlock(final BlockBehaviour.Properties properties, final BlockSetType type) {
31
		super(properties.sound(type.soundType()));
32
		this.type = type;
33
	}
34
35
	@Override
36
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
37
		return this.getSignalForState(state) > 0 ? SHAPE_PRESSED : SHAPE;
38
	}
39
40
	protected int getPressedTime() {
41
		return 20;
42
	}
43
44
	@Override
45
	public boolean isPossibleToRespawnInThis(final BlockState state) {
46
		return true;
47
	}
48
49
	@Override
50
	protected BlockState updateShape(
51
		final BlockState state,
52
		final LevelReader level,
53
		final ScheduledTickAccess ticks,
54
		final BlockPos pos,
55
		final Direction directionToNeighbour,
56
		final BlockPos neighbourPos,
57
		final BlockState neighbourState,
58
		final RandomSource random
59
	) {
60
		return directionToNeighbour == Direction.DOWN && !state.canSurvive(level, pos)
61
			? Blocks.AIR.defaultBlockState()
62
			: super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
63
	}
64
65
	@Override
66
	protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
67
		BlockPos below = pos.below();
68
		return canSupportRigidBlock(level, below) || canSupportCenter(level, below, Direction.UP);
69
	}
70
71
	@Override
72
	protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
73
		int signal = this.getSignalForState(state);
74
		if (signal > 0) {
75
			this.checkPressed(null, level, pos, state, signal);
76
		}
77
	}
78
79
	@Override
80
	protected void entityInside(
81
		final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise
82
	) {
83
		if (!level.isClientSide()) {
84
			int signal = this.getSignalForState(state);
85
			if (signal == 0) {
86
				this.checkPressed(entity, level, pos, state, signal);
87
			}
88
		}
89
	}
90
91
	private void checkPressed(final @Nullable Entity sourceEntity, final Level level, final BlockPos pos, final BlockState state, final int oldSignal) {
92
		int signal = this.getSignalStrength(level, pos);
93
		boolean wasPressed = oldSignal > 0;
94
		boolean isPressed = signal > 0;
95
		if (oldSignal != signal) {
96
			BlockState newState = this.setSignalForState(state, signal);
97
			level.setBlock(pos, newState, 2);
98
			this.updateNeighbours(level, pos);
99
			level.setBlocksDirty(pos, state, newState);
100
		}
101
102
		if (!isPressed && wasPressed) {
103
			level.playSound(null, pos, this.type.pressurePlateClickOff(), SoundSource.BLOCKS);
104
			level.gameEvent(sourceEntity, GameEvent.BLOCK_DEACTIVATE, pos);
105
		} else if (isPressed && !wasPressed) {
106
			level.playSound(null, pos, this.type.pressurePlateClickOn(), SoundSource.BLOCKS);
107
			level.gameEvent(sourceEntity, GameEvent.BLOCK_ACTIVATE, pos);
108
		}
109
110
		if (isPressed) {
111
			level.scheduleTick(pos, this, this.getPressedTime());
112
		}
113
	}
114
115
	@Override
116
	protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
117
		if (!movedByPiston && this.getSignalForState(state) > 0) {
118
			this.updateNeighbours(level, pos);
119
		}
120
	}
121
122
	protected void updateNeighbours(final Level level, final BlockPos pos) {
123
		level.updateNeighborsAt(pos, this);
124
		level.updateNeighborsAt(pos.below(), this);
125
	}
126
127
	@Override
128
	protected int getDirectSignal(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction direction) {
129
		return direction == Direction.UP ? this.getSignalForState(state) : 0;
130
	}
131
132
	@Override
133
	protected boolean isSignalSource(final BlockState state) {
134
		return true;
135
	}
136
137
	@Override
138
	protected int ownSignal(final BlockState state, final BlockGetter level, final BlockPos pos) {
139
		return this.getSignalForState(state);
140
	}
141
142
	protected static int getEntityCount(final Level level, final AABB entityDetectionBox, final Class<? extends Entity> entityClass) {
143
		return level.getEntitiesOfClass(entityClass, entityDetectionBox, EntitySelector.NO_SPECTATORS.and(e -> !e.isIgnoringBlockTriggers())).size();
144
	}
145
146
	protected abstract int getSignalStrength(Level level, BlockPos pos);
147
148
	protected abstract int getSignalForState(BlockState state);
149
150
	protected abstract BlockState setSignalForState(BlockState state, int signal);
151
}
152

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