SugarCaneBlock .java

net.minecraft.world.level.block.SugarCaneBlock (class): members: AGE, randomTick, canSurvive, SHAPE, neighbourState, directionToNeighbour, updateShape.

net.minecraft.world.level.blockclass114 linescommonextends Block3.6 KB

API Surface (6)

VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random)
void randomTick(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 canSurvive(final BlockState state, final LevelReader level, final BlockPos pos)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)

Fields & Constants (9)

AGE
SHAPE
state
level
ticks
pos
directionToNeighbour
neighbourPos
neighbourState

Full Source

SugarCaneBlock.java114 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.BlockTags;
7
import net.minecraft.tags.FluidTags;
8
import net.minecraft.util.RandomSource;
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.block.state.StateDefinition;
15
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
16
import net.minecraft.world.level.block.state.properties.IntegerProperty;
17
import net.minecraft.world.level.material.FluidState;
18
import net.minecraft.world.phys.shapes.CollisionContext;
19
import net.minecraft.world.phys.shapes.VoxelShape;
20
21
/**
22
 * Access widened by fabric-transitive-access-wideners-v1 to accessible
23
 */
24
public class SugarCaneBlock extends Block {
25
	public static final IntegerProperty AGE = BlockStateProperties.AGE_15;
26
	private static final VoxelShape SHAPE = Block.column(12.0, 0.0, 16.0);
27
28
	/**
29
	 * Access widened by fabric-transitive-access-wideners-v1 to accessible
30
	 */
31
	public SugarCaneBlock(final BlockBehaviour.Properties properties) {
32
		super(properties);
33
		this.registerDefaultState(this.stateDefinition.any().setValue(AGE, 0));
34
	}
35
36
	@Override
37
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
38
		return SHAPE;
39
	}
40
41
	@Override
42
	protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
43
		if (!state.canSurvive(level, pos)) {
44
			level.destroyBlock(pos, true);
45
		}
46
	}
47
48
	@Override
49
	protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
50
		if (level.isEmptyBlock(pos.above())) {
51
			int height = 1;
52
53
			while (level.getBlockState(pos.below(height)).is(this)) {
54
				height++;
55
			}
56
57
			if (height < 3) {
58
				int age = state.getValue(AGE);
59
				if (age == 15) {
60
					level.setBlockAndUpdate(pos.above(), this.defaultBlockState());
61
					level.setBlock(pos, state.setValue(AGE, 0), 260);
62
				} else {
63
					level.setBlock(pos, state.setValue(AGE, age + 1), 260);
64
				}
65
			}
66
		}
67
	}
68
69
	@Override
70
	protected BlockState updateShape(
71
		final BlockState state,
72
		final LevelReader level,
73
		final ScheduledTickAccess ticks,
74
		final BlockPos pos,
75
		final Direction directionToNeighbour,
76
		final BlockPos neighbourPos,
77
		final BlockState neighbourState,
78
		final RandomSource random
79
	) {
80
		if (!state.canSurvive(level, pos)) {
81
			ticks.scheduleTick(pos, this, 1);
82
		}
83
84
		return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
85
	}
86
87
	@Override
88
	protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
89
		BlockState stateBelow = level.getBlockState(pos.below());
90
		if (stateBelow.is(this)) {
91
			return true;
92
		}
93
94
		if (stateBelow.is(BlockTags.SUPPORTS_SUGAR_CANE)) {
95
			BlockPos below = pos.below();
96
97
			for (Direction direction : Direction.Plane.HORIZONTAL) {
98
				BlockState blockState = level.getBlockState(below.relative(direction));
99
				FluidState fluidState = level.getFluidState(below.relative(direction));
100
				if (fluidState.is(FluidTags.SUPPORTS_SUGAR_CANE_ADJACENTLY) || blockState.is(BlockTags.SUPPORTS_SUGAR_CANE_ADJACENTLY)) {
101
					return true;
102
				}
103
			}
104
		}
105
106
		return false;
107
	}
108
109
	@Override
110
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
111
		builder.add(AGE);
112
	}
113
}
114

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