CactusBlock .java

net.minecraft.world.level.block.CactusBlock (class): members: ATTEMPT_GROW_CACTUS_FLOWER_SMALL_CACTUS_CHANCE, ATTEMPT_GROW_CACTUS_FLOWER_AGE, MAX_AGE, AGE.

net.minecraft.world.level.blockclass141 linescommonextends Block5.0 KB

API Surface (9)

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)
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)
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 entityInside(final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)
boolean isPathfindable(final BlockState state, final PathComputationType type)

Fields & Constants (16)

AGE
MAX_AGE
SHAPE
SHAPE_COLLISION
MAX_CACTUS_GROWING_HEIGHT
ATTEMPT_GROW_CACTUS_FLOWER_AGE
ATTEMPT_GROW_CACTUS_FLOWER_SMALL_CACTUS_CHANCE
ATTEMPT_GROW_CACTUS_FLOWER_TALL_CACTUS_CHANCE
state
level
ticks
pos
directionToNeighbour
neighbourPos
neighbourState
effectApplier

Full Source

CactusBlock.java141 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.entity.Entity;
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.StateDefinition;
18
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
19
import net.minecraft.world.level.block.state.properties.IntegerProperty;
20
import net.minecraft.world.level.pathfinder.PathComputationType;
21
import net.minecraft.world.phys.shapes.CollisionContext;
22
import net.minecraft.world.phys.shapes.VoxelShape;
23
24
/**
25
 * Access widened by fabric-transitive-access-wideners-v1 to accessible
26
 */
27
public class CactusBlock extends Block {
28
	public static final IntegerProperty AGE = BlockStateProperties.AGE_15;
29
	public static final int MAX_AGE = 15;
30
	private static final VoxelShape SHAPE = Block.column(14.0, 0.0, 16.0);
31
	private static final VoxelShape SHAPE_COLLISION = Block.column(14.0, 0.0, 15.0);
32
	private static final int MAX_CACTUS_GROWING_HEIGHT = 3;
33
	private static final int ATTEMPT_GROW_CACTUS_FLOWER_AGE = 8;
34
	private static final double ATTEMPT_GROW_CACTUS_FLOWER_SMALL_CACTUS_CHANCE = 0.1;
35
	private static final double ATTEMPT_GROW_CACTUS_FLOWER_TALL_CACTUS_CHANCE = 0.25;
36
37
	/**
38
	 * Access widened by fabric-transitive-access-wideners-v1 to accessible
39
	 */
40
	public CactusBlock(final BlockBehaviour.Properties properties) {
41
		super(properties);
42
		this.registerDefaultState(this.stateDefinition.any().setValue(AGE, 0));
43
	}
44
45
	@Override
46
	protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
47
		if (!state.canSurvive(level, pos)) {
48
			level.destroyBlock(pos, true);
49
		}
50
	}
51
52
	@Override
53
	protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
54
		BlockPos above = pos.above();
55
		if (level.isEmptyBlock(above)) {
56
			int height = 1;
57
			int age = state.getValue(AGE);
58
59
			while (level.getBlockState(pos.below(height)).is(this)) {
60
				if (++height == 3 && age == 15) {
61
					return;
62
				}
63
			}
64
65
			if (age == 8 && this.canSurvive(this.defaultBlockState(), level, pos.above())) {
66
				double chanceToGrowFlower = height >= 3 ? 0.25 : 0.1;
67
				if (random.nextDouble() <= chanceToGrowFlower) {
68
					level.setBlockAndUpdate(above, Blocks.CACTUS_FLOWER.defaultBlockState());
69
				}
70
			} else if (age == 15 && height < 3) {
71
				level.setBlockAndUpdate(above, this.defaultBlockState());
72
				BlockState aboveBlock = state.setValue(AGE, 0);
73
				level.setBlock(pos, aboveBlock, 260);
74
				level.neighborChanged(aboveBlock, above, this, null, false);
75
			}
76
77
			if (age < 15) {
78
				level.setBlock(pos, state.setValue(AGE, age + 1), 260);
79
			}
80
		}
81
	}
82
83
	@Override
84
	protected VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
85
		return SHAPE_COLLISION;
86
	}
87
88
	@Override
89
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
90
		return SHAPE;
91
	}
92
93
	@Override
94
	protected BlockState updateShape(
95
		final BlockState state,
96
		final LevelReader level,
97
		final ScheduledTickAccess ticks,
98
		final BlockPos pos,
99
		final Direction directionToNeighbour,
100
		final BlockPos neighbourPos,
101
		final BlockState neighbourState,
102
		final RandomSource random
103
	) {
104
		if (!state.canSurvive(level, pos)) {
105
			ticks.scheduleTick(pos, this, 1);
106
		}
107
108
		return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
109
	}
110
111
	@Override
112
	protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
113
		for (Direction direction : Direction.Plane.HORIZONTAL) {
114
			BlockState neighbor = level.getBlockState(pos.relative(direction));
115
			if (neighbor.isSolid() || level.getFluidState(pos.relative(direction)).is(FluidTags.LAVA)) {
116
				return false;
117
			}
118
		}
119
120
		BlockState belowState = level.getBlockState(pos.below());
121
		return (belowState.is(this) || belowState.is(BlockTags.SUPPORTS_CACTUS)) && !level.getBlockState(pos.above()).liquid();
122
	}
123
124
	@Override
125
	protected void entityInside(
126
		final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise
127
	) {
128
		entity.hurt(level.damageSources().cactus(), 1.0F);
129
	}
130
131
	@Override
132
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
133
		builder.add(AGE);
134
	}
135
136
	@Override
137
	protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
138
		return false;
139
	}
140
}
141

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