BellBlock .java

net.minecraft.world.level.block.BellBlock (class): members: SHAPE_DOUBLE_WALL, SHAPE_SINGLE_WALL, EVENT_BELL_RING, SHAPE_CEILING, attemptToRing, explosion.

net.minecraft.world.level.blockclass287 linescommonextends BaseEntityBlock11.9 KB

API Surface (15)

void neighborChanged(final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston)
void onProjectileHit(final Level level, final BlockState state, final BlockHitResult hitResult, final Projectile projectile)
InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult)
boolean onHit(final Level level, final BlockState state, final BlockHitResult hitResult, final @Nullable Player player, final boolean requireHitFromCorrectSide)
boolean attemptToRing(final Level level, final BlockPos pos, final @Nullable Direction direction)
boolean attemptToRing(final @Nullable Entity ringingEntity, final Level level, final BlockPos pos, @Nullable Direction direction)
VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
void onExplosionHit(final BlockState state, final ServerLevel level, final BlockPos pos, final Explosion explosion, final BiConsumer<ItemStack, BlockPos> onHit)
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 createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)
boolean isPathfindable(final BlockState state, final PathComputationType type)
BlockState rotate(final BlockState state, final Rotation rotation)
BlockState mirror(final BlockState state, final Mirror mirror)

Fields & Constants (19)

FACING
ATTACHMENT
POWERED
BELL_SHAPE
SHAPE_CEILING
SHAPE_FLOOR
SHAPE_DOUBLE_WALL
SHAPE_SINGLE_WALL
EVENT_BELL_RING
orientation
player
explosion
state
level
ticks
pos
directionToNeighbour
neighbourPos
neighbourState

Full Source

BellBlock.java287 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.SoundEvents;
9
import net.minecraft.sounds.SoundSource;
10
import net.minecraft.stats.Stats;
11
import net.minecraft.util.RandomSource;
12
import net.minecraft.world.InteractionResult;
13
import net.minecraft.world.entity.Entity;
14
import net.minecraft.world.entity.player.Player;
15
import net.minecraft.world.entity.projectile.Projectile;
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.Explosion;
20
import net.minecraft.world.level.Level;
21
import net.minecraft.world.level.LevelReader;
22
import net.minecraft.world.level.ScheduledTickAccess;
23
import net.minecraft.world.level.block.entity.BellBlockEntity;
24
import net.minecraft.world.level.block.entity.BlockEntity;
25
import net.minecraft.world.level.block.entity.BlockEntityTicker;
26
import net.minecraft.world.level.block.entity.BlockEntityType;
27
import net.minecraft.world.level.block.entity.BlockEntityTypes;
28
import net.minecraft.world.level.block.state.BlockBehaviour;
29
import net.minecraft.world.level.block.state.BlockState;
30
import net.minecraft.world.level.block.state.StateDefinition;
31
import net.minecraft.world.level.block.state.properties.BellAttachType;
32
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
33
import net.minecraft.world.level.block.state.properties.BooleanProperty;
34
import net.minecraft.world.level.block.state.properties.EnumProperty;
35
import net.minecraft.world.level.gameevent.GameEvent;
36
import net.minecraft.world.level.pathfinder.PathComputationType;
37
import net.minecraft.world.level.redstone.Orientation;
38
import net.minecraft.world.phys.BlockHitResult;
39
import net.minecraft.world.phys.shapes.CollisionContext;
40
import net.minecraft.world.phys.shapes.Shapes;
41
import net.minecraft.world.phys.shapes.VoxelShape;
42
import org.jspecify.annotations.Nullable;
43
44
public class BellBlock extends BaseEntityBlock {
45
	public static final EnumProperty<Direction> FACING = HorizontalDirectionalBlock.FACING;
46
	public static final EnumProperty<BellAttachType> ATTACHMENT = BlockStateProperties.BELL_ATTACHMENT;
47
	public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
48
	private static final VoxelShape BELL_SHAPE = Shapes.or(Block.column(6.0, 6.0, 13.0), Block.column(8.0, 4.0, 6.0));
49
	private static final VoxelShape SHAPE_CEILING = Shapes.or(BELL_SHAPE, Block.column(2.0, 13.0, 16.0));
50
	private static final Map<Direction.Axis, VoxelShape> SHAPE_FLOOR = Shapes.rotateHorizontalAxis(Block.cube(16.0, 16.0, 8.0));
51
	private static final Map<Direction.Axis, VoxelShape> SHAPE_DOUBLE_WALL = Shapes.rotateHorizontalAxis(
52
		Shapes.or(BELL_SHAPE, Block.column(2.0, 16.0, 13.0, 15.0))
53
	);
54
	private static final Map<Direction, VoxelShape> SHAPE_SINGLE_WALL = Shapes.rotateHorizontal(Shapes.or(BELL_SHAPE, Block.boxZ(2.0, 13.0, 15.0, 0.0, 13.0)));
55
	public static final int EVENT_BELL_RING = 1;
56
57
	public BellBlock(final BlockBehaviour.Properties properties) {
58
		super(properties);
59
		this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(ATTACHMENT, BellAttachType.FLOOR).setValue(POWERED, false));
60
	}
61
62
	@Override
63
	protected void neighborChanged(
64
		final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
65
	) {
66
		boolean signal = level.hasNeighborSignal(pos);
67
		if (signal != state.getValue(POWERED)) {
68
			if (signal) {
69
				this.attemptToRing(level, pos, null);
70
			}
71
72
			level.setBlockAndUpdate(pos, state.setValue(POWERED, signal));
73
		}
74
	}
75
76
	@Override
77
	protected void onProjectileHit(final Level level, final BlockState state, final BlockHitResult hitResult, final Projectile projectile) {
78
		Player playerOwner = projectile.getOwner() instanceof Player player ? player : null;
79
		this.onHit(level, state, hitResult, playerOwner, true);
80
	}
81
82
	@Override
83
	protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
84
		return this.onHit(level, state, hitResult, player, true) ? InteractionResult.SUCCESS : InteractionResult.PASS;
85
	}
86
87
	public boolean onHit(
88
		final Level level, final BlockState state, final BlockHitResult hitResult, final @Nullable Player player, final boolean requireHitFromCorrectSide
89
	) {
90
		Direction direction = hitResult.getDirection();
91
		BlockPos blockPos = hitResult.getBlockPos();
92
		boolean properHit = !requireHitFromCorrectSide || this.isProperHit(state, direction, hitResult.getLocation().y - blockPos.getY());
93
		if (properHit) {
94
			boolean didRing = this.attemptToRing(player, level, blockPos, direction);
95
			if (didRing && player != null) {
96
				player.awardStat(Stats.BELL_RING);
97
			}
98
99
			return true;
100
		} else {
101
			return false;
102
		}
103
	}
104
105
	private boolean isProperHit(final BlockState state, final Direction clickedDirection, final double clickY) {
106
		if (clickedDirection.getAxis() != Direction.Axis.Y && !(clickY > 0.8124F)) {
107
			Direction facing = state.getValue(FACING);
108
			BellAttachType attachType = state.getValue(ATTACHMENT);
109
110
			return switch (attachType) {
111
				case FLOOR -> facing.getAxis() == clickedDirection.getAxis();
112
				case SINGLE_WALL, DOUBLE_WALL -> facing.getAxis() != clickedDirection.getAxis();
113
				case CEILING -> true;
114
				default -> false;
115
			};
116
		} else {
117
			return false;
118
		}
119
	}
120
121
	public boolean attemptToRing(final Level level, final BlockPos pos, final @Nullable Direction direction) {
122
		return this.attemptToRing(null, level, pos, direction);
123
	}
124
125
	public boolean attemptToRing(final @Nullable Entity ringingEntity, final Level level, final BlockPos pos, @Nullable Direction direction) {
126
		if (!level.isClientSide() && level.getBlockEntity(pos) instanceof BellBlockEntity bellBlockEntity) {
127
			if (direction == null) {
128
				direction = level.getBlockState(pos).getValue(FACING);
129
			}
130
131
			bellBlockEntity.onHit(direction);
132
			level.playSound(null, pos, SoundEvents.BELL_BLOCK, SoundSource.BLOCKS, 2.0F, 1.0F);
133
			level.gameEvent(ringingEntity, GameEvent.BLOCK_CHANGE, pos);
134
			return true;
135
		} else {
136
			return false;
137
		}
138
	}
139
140
	private VoxelShape getVoxelShape(final BlockState state) {
141
		Direction facing = state.getValue(FACING);
142
143
		return switch ((BellAttachType)state.getValue(ATTACHMENT)) {
144
			case FLOOR -> (VoxelShape)SHAPE_FLOOR.get(facing.getAxis());
145
			case SINGLE_WALL -> (VoxelShape)SHAPE_SINGLE_WALL.get(facing);
146
			case DOUBLE_WALL -> (VoxelShape)SHAPE_DOUBLE_WALL.get(facing.getAxis());
147
			case CEILING -> SHAPE_CEILING;
148
		};
149
	}
150
151
	@Override
152
	protected VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
153
		return this.getVoxelShape(state);
154
	}
155
156
	@Override
157
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
158
		return this.getVoxelShape(state);
159
	}
160
161
	@Override
162
	public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
163
		Direction clickedFace = context.getClickedFace();
164
		BlockPos pos = context.getClickedPos();
165
		Level level = context.getLevel();
166
		Direction.Axis axis = clickedFace.getAxis();
167
		if (axis == Direction.Axis.Y) {
168
			BlockState state = this.defaultBlockState()
169
				.setValue(ATTACHMENT, clickedFace == Direction.DOWN ? BellAttachType.CEILING : BellAttachType.FLOOR)
170
				.setValue(FACING, context.getHorizontalDirection());
171
			if (state.canSurvive(context.getLevel(), pos)) {
172
				return state;
173
			}
174
		} else {
175
			boolean doubleAttached = axis == Direction.Axis.X
176
					&& level.getBlockState(pos.west()).isFaceSturdy(level, pos.west(), Direction.EAST)
177
					&& level.getBlockState(pos.east()).isFaceSturdy(level, pos.east(), Direction.WEST)
178
				|| axis == Direction.Axis.Z
179
					&& level.getBlockState(pos.north()).isFaceSturdy(level, pos.north(), Direction.SOUTH)
180
					&& level.getBlockState(pos.south()).isFaceSturdy(level, pos.south(), Direction.NORTH);
181
			BlockState state = this.defaultBlockState()
182
				.setValue(FACING, clickedFace.getOpposite())
183
				.setValue(ATTACHMENT, doubleAttached ? BellAttachType.DOUBLE_WALL : BellAttachType.SINGLE_WALL);
184
			if (state.canSurvive(context.getLevel(), context.getClickedPos())) {
185
				return state;
186
			}
187
188
			boolean canAttachBelow = level.getBlockState(pos.below()).isFaceSturdy(level, pos.below(), Direction.UP);
189
			state = state.setValue(ATTACHMENT, canAttachBelow ? BellAttachType.FLOOR : BellAttachType.CEILING);
190
			if (state.canSurvive(context.getLevel(), context.getClickedPos())) {
191
				return state;
192
			}
193
		}
194
195
		return null;
196
	}
197
198
	@Override
199
	protected void onExplosionHit(
200
		final BlockState state, final ServerLevel level, final BlockPos pos, final Explosion explosion, final BiConsumer<ItemStack, BlockPos> onHit
201
	) {
202
		if (explosion.canTriggerBlocks()) {
203
			this.attemptToRing(level, pos, null);
204
		}
205
206
		super.onExplosionHit(state, level, pos, explosion, onHit);
207
	}
208
209
	@Override
210
	protected BlockState updateShape(
211
		final BlockState state,
212
		final LevelReader level,
213
		final ScheduledTickAccess ticks,
214
		final BlockPos pos,
215
		final Direction directionToNeighbour,
216
		final BlockPos neighbourPos,
217
		final BlockState neighbourState,
218
		final RandomSource random
219
	) {
220
		BellAttachType attachment = state.getValue(ATTACHMENT);
221
		Direction connectedDirection = getConnectedDirection(state).getOpposite();
222
		if (connectedDirection == directionToNeighbour && !state.canSurvive(level, pos) && attachment != BellAttachType.DOUBLE_WALL) {
223
			return Blocks.AIR.defaultBlockState();
224
		}
225
226
		if (directionToNeighbour.getAxis() == state.getValue(FACING).getAxis()) {
227
			if (attachment == BellAttachType.DOUBLE_WALL && !neighbourState.isFaceSturdy(level, neighbourPos, directionToNeighbour)) {
228
				return state.setValue(ATTACHMENT, BellAttachType.SINGLE_WALL).setValue(FACING, directionToNeighbour.getOpposite());
229
			}
230
231
			if (attachment == BellAttachType.SINGLE_WALL
232
				&& connectedDirection.getOpposite() == directionToNeighbour
233
				&& neighbourState.isFaceSturdy(level, neighbourPos, state.getValue(FACING))) {
234
				return state.setValue(ATTACHMENT, BellAttachType.DOUBLE_WALL);
235
			}
236
		}
237
238
		return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
239
	}
240
241
	@Override
242
	protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
243
		Direction connectionDir = getConnectedDirection(state).getOpposite();
244
		return connectionDir == Direction.UP
245
			? Block.canSupportCenter(level, pos.above(), Direction.DOWN)
246
			: FaceAttachedHorizontalDirectionalBlock.canAttach(level, pos, connectionDir);
247
	}
248
249
	private static Direction getConnectedDirection(final BlockState state) {
250
		return switch ((BellAttachType)state.getValue(ATTACHMENT)) {
251
			case FLOOR -> Direction.UP;
252
			case CEILING -> Direction.DOWN;
253
			default -> state.getValue(FACING).getOpposite();
254
		};
255
	}
256
257
	@Override
258
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
259
		builder.add(FACING, ATTACHMENT, POWERED);
260
	}
261
262
	@Override
263
	public @Nullable BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
264
		return new BellBlockEntity(worldPosition, blockState);
265
	}
266
267
	@Override
268
	public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(final Level level, final BlockState blockState, final BlockEntityType<T> type) {
269
		return createTickerHelper(type, BlockEntityTypes.BELL, level.isClientSide() ? BellBlockEntity::clientTick : BellBlockEntity::serverTick);
270
	}
271
272
	@Override
273
	protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
274
		return false;
275
	}
276
277
	@Override
278
	public BlockState rotate(final BlockState state, final Rotation rotation) {
279
		return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
280
	}
281
282
	@Override
283
	public BlockState mirror(final BlockState state, final Mirror mirror) {
284
		return state.rotate(mirror.getRotation(state.getValue(FACING)));
285
	}
286
}
287

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