CoralBlock .java

net.minecraft.world.level.block.CoralBlock (class, 70 lines): members: scanForWater, deadBlock, neighbourState, directionToNeighbour, updateShape, ticks.

net.minecraft.world.level.blockclass70 linescommonextends Block2.2 KB

API Surface (3)

void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random)
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 scanForWater(final BlockGetter level, final BlockPos blockPos)

Fields & Constants (8)

deadBlock
state
level
ticks
pos
directionToNeighbour
neighbourPos
neighbourState

Full Source

CoralBlock.java70 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.tags.FluidTags;
7
import net.minecraft.util.RandomSource;
8
import net.minecraft.world.item.context.BlockPlaceContext;
9
import net.minecraft.world.level.BlockGetter;
10
import net.minecraft.world.level.LevelReader;
11
import net.minecraft.world.level.ScheduledTickAccess;
12
import net.minecraft.world.level.block.state.BlockBehaviour;
13
import net.minecraft.world.level.block.state.BlockState;
14
import net.minecraft.world.level.material.FluidState;
15
import org.jspecify.annotations.Nullable;
16
17
public class CoralBlock extends Block {
18
	private final Block deadBlock;
19
20
	public CoralBlock(final Block deadBlock, final BlockBehaviour.Properties properties) {
21
		super(properties);
22
		this.deadBlock = deadBlock;
23
	}
24
25
	@Override
26
	protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
27
		if (!this.scanForWater(level, pos)) {
28
			level.setBlock(pos, this.deadBlock.defaultBlockState(), 2);
29
		}
30
	}
31
32
	@Override
33
	protected BlockState updateShape(
34
		final BlockState state,
35
		final LevelReader level,
36
		final ScheduledTickAccess ticks,
37
		final BlockPos pos,
38
		final Direction directionToNeighbour,
39
		final BlockPos neighbourPos,
40
		final BlockState neighbourState,
41
		final RandomSource random
42
	) {
43
		if (!this.scanForWater(level, pos)) {
44
			ticks.scheduleTick(pos, this, 60 + random.nextInt(40));
45
		}
46
47
		return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
48
	}
49
50
	protected boolean scanForWater(final BlockGetter level, final BlockPos blockPos) {
51
		for (Direction direction : Direction.values()) {
52
			FluidState fluidState = level.getFluidState(blockPos.relative(direction));
53
			if (fluidState.is(FluidTags.WATER)) {
54
				return true;
55
			}
56
		}
57
58
		return false;
59
	}
60
61
	@Override
62
	public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
63
		if (!this.scanForWater(context.getLevel(), context.getClickedPos())) {
64
			context.getLevel().scheduleTick(context.getClickedPos(), this, 60 + context.getLevel().getRandom().nextInt(40));
65
		}
66
67
		return this.defaultBlockState();
68
	}
69
}
70

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