TurtleEggBlock .java

net.minecraft.world.level.block.TurtleEggBlock (class, 179 lines): members: SHAPE_MULTIPLE, SHAPE_SINGLE, MAX_EGGS, MIN_EGGS, isSand, onSand, HATCH, EGGS.

net.minecraft.world.level.blockclass179 linescommonextends Block7.3 KB

API Surface (10)

void stepOn(final Level level, final BlockPos pos, final BlockState onState, final Entity entity)
void fallOn(final Level level, final BlockState state, final BlockPos pos, final Entity entity, final double fallDistance)
void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random)
boolean onSand(final BlockGetter level, final BlockPos pos)
boolean isSand(final BlockGetter level, final BlockPos pos)
void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston)
void playerDestroy(final ServerLevel level, final ServerPlayer player, final BlockPos pos, final BlockState state, final @Nullable BlockEntity blockEntity, final ItemStack destroyedWith)
boolean canBeReplaced(final BlockState state, final BlockPlaceContext context)
VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)

Fields & Constants (12)

HATCH
EGGS
MAX_HATCH_LEVEL
MIN_EGGS
MAX_EGGS
SHAPE_SINGLE
SHAPE_MULTIPLE
level
player
pos
state
blockEntity

Full Source

TurtleEggBlock.java179 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.server.level.ServerLevel;
5
import net.minecraft.server.level.ServerPlayer;
6
import net.minecraft.sounds.SoundEvents;
7
import net.minecraft.sounds.SoundSource;
8
import net.minecraft.tags.BlockTags;
9
import net.minecraft.util.RandomSource;
10
import net.minecraft.world.attribute.EnvironmentAttributes;
11
import net.minecraft.world.entity.Entity;
12
import net.minecraft.world.entity.EntitySpawnReason;
13
import net.minecraft.world.entity.EntityTypes;
14
import net.minecraft.world.entity.LivingEntity;
15
import net.minecraft.world.entity.ambient.Bat;
16
import net.minecraft.world.entity.animal.turtle.Turtle;
17
import net.minecraft.world.entity.monster.zombie.Zombie;
18
import net.minecraft.world.entity.player.Player;
19
import net.minecraft.world.item.ItemStack;
20
import net.minecraft.world.item.context.BlockPlaceContext;
21
import net.minecraft.world.level.BlockGetter;
22
import net.minecraft.world.level.Level;
23
import net.minecraft.world.level.block.entity.BlockEntity;
24
import net.minecraft.world.level.block.state.BlockBehaviour;
25
import net.minecraft.world.level.block.state.BlockState;
26
import net.minecraft.world.level.block.state.StateDefinition;
27
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
28
import net.minecraft.world.level.block.state.properties.IntegerProperty;
29
import net.minecraft.world.level.gameevent.GameEvent;
30
import net.minecraft.world.level.gamerules.GameRules;
31
import net.minecraft.world.phys.shapes.CollisionContext;
32
import net.minecraft.world.phys.shapes.VoxelShape;
33
import org.jspecify.annotations.Nullable;
34
35
public class TurtleEggBlock extends Block {
36
	public static final IntegerProperty HATCH = BlockStateProperties.HATCH;
37
	public static final IntegerProperty EGGS = BlockStateProperties.EGGS;
38
	public static final int MAX_HATCH_LEVEL = 2;
39
	public static final int MIN_EGGS = 1;
40
	public static final int MAX_EGGS = 4;
41
	private static final VoxelShape SHAPE_SINGLE = Block.box(3.0, 0.0, 3.0, 12.0, 7.0, 12.0);
42
	private static final VoxelShape SHAPE_MULTIPLE = Block.column(14.0, 0.0, 7.0);
43
44
	public TurtleEggBlock(final BlockBehaviour.Properties properties) {
45
		super(properties);
46
		this.registerDefaultState(this.stateDefinition.any().setValue(HATCH, 0).setValue(EGGS, 1));
47
	}
48
49
	@Override
50
	public void stepOn(final Level level, final BlockPos pos, final BlockState onState, final Entity entity) {
51
		if (!entity.isSteppingCarefully()) {
52
			this.destroyEgg(level, onState, pos, entity, 100);
53
		}
54
55
		super.stepOn(level, pos, onState, entity);
56
	}
57
58
	@Override
59
	public void fallOn(final Level level, final BlockState state, final BlockPos pos, final Entity entity, final double fallDistance) {
60
		if (!(entity instanceof Zombie)) {
61
			this.destroyEgg(level, state, pos, entity, 3);
62
		}
63
64
		super.fallOn(level, state, pos, entity, fallDistance);
65
	}
66
67
	private void destroyEgg(final Level level, final BlockState state, final BlockPos pos, final Entity entity, final int randomness) {
68
		if (state.is(Blocks.TURTLE_EGG)
69
			&& level instanceof ServerLevel serverLevel
70
			&& this.canDestroyEgg(serverLevel, pos, entity)
71
			&& level.getRandom().nextInt(randomness) == 0) {
72
			this.decreaseEggs(serverLevel, pos, state);
73
		}
74
	}
75
76
	private void decreaseEggs(final Level level, final BlockPos pos, final BlockState state) {
77
		level.playSound(null, pos, SoundEvents.TURTLE_EGG_BREAK, SoundSource.BLOCKS, 0.7F, 0.9F + level.getRandom().nextFloat() * 0.2F);
78
		int numberOfEggs = state.getValue(EGGS);
79
		if (numberOfEggs <= 1) {
80
			level.destroyBlock(pos, false);
81
		} else {
82
			level.setBlock(pos, state.setValue(EGGS, numberOfEggs - 1), 2);
83
			level.gameEvent(GameEvent.BLOCK_DESTROY, pos, GameEvent.Context.of(state));
84
			level.levelEvent(2001, pos, Block.getId(state));
85
		}
86
	}
87
88
	@Override
89
	protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
90
		if (this.shouldUpdateHatchLevel(level, pos) && onSand(level, pos)) {
91
			int hatch = state.getValue(HATCH);
92
			if (hatch < 2) {
93
				level.playSound(null, pos, SoundEvents.TURTLE_EGG_CRACK, SoundSource.BLOCKS, 0.7F, 0.9F + random.nextFloat() * 0.2F);
94
				level.setBlock(pos, state.setValue(HATCH, hatch + 1), 2);
95
				level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(state));
96
			} else {
97
				level.playSound(null, pos, SoundEvents.TURTLE_EGG_HATCH, SoundSource.BLOCKS, 0.7F, 0.9F + random.nextFloat() * 0.2F);
98
				level.removeBlock(pos, false);
99
				level.gameEvent(GameEvent.BLOCK_DESTROY, pos, GameEvent.Context.of(state));
100
101
				for (int i = 0; i < state.getValue(EGGS); i++) {
102
					level.levelEvent(2001, pos, Block.getId(state));
103
					Turtle turtle = EntityTypes.TURTLE.create(level, EntitySpawnReason.BREEDING);
104
					if (turtle != null) {
105
						turtle.setAge(-24000);
106
						turtle.setHomePos(pos);
107
						turtle.snapTo(pos.getX() + 0.3 + i * 0.2, pos.getY(), pos.getZ() + 0.3, 0.0F, 0.0F);
108
						level.addFreshEntity(turtle);
109
					}
110
				}
111
			}
112
		}
113
	}
114
115
	public static boolean onSand(final BlockGetter level, final BlockPos pos) {
116
		return isSand(level, pos.below());
117
	}
118
119
	public static boolean isSand(final BlockGetter level, final BlockPos pos) {
120
		return level.getBlockState(pos).is(BlockTags.SAND);
121
	}
122
123
	@Override
124
	protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
125
		if (onSand(level, pos) && !level.isClientSide()) {
126
			level.levelEvent(2012, pos, 15);
127
		}
128
	}
129
130
	private boolean shouldUpdateHatchLevel(final Level level, final BlockPos pos) {
131
		float chance = level.environmentAttributes().getValue(EnvironmentAttributes.TURTLE_EGG_HATCH_CHANCE, pos);
132
		return chance > 0.0F && level.getRandom().nextFloat() < chance;
133
	}
134
135
	@Override
136
	public void playerDestroy(
137
		final ServerLevel level,
138
		final ServerPlayer player,
139
		final BlockPos pos,
140
		final BlockState state,
141
		final @Nullable BlockEntity blockEntity,
142
		final ItemStack destroyedWith
143
	) {
144
		super.playerDestroy(level, player, pos, state, blockEntity, destroyedWith);
145
		this.decreaseEggs(level, pos, state);
146
	}
147
148
	@Override
149
	protected boolean canBeReplaced(final BlockState state, final BlockPlaceContext context) {
150
		return !context.isSecondaryUseActive() && context.getItemInHand().is(this.asItem()) && state.getValue(EGGS) < 4 ? true : super.canBeReplaced(state, context);
151
	}
152
153
	@Override
154
	public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
155
		BlockState state = context.getLevel().getBlockState(context.getClickedPos());
156
		return state.is(this) ? state.setValue(EGGS, Math.min(4, state.getValue(EGGS) + 1)) : super.getStateForPlacement(context);
157
	}
158
159
	@Override
160
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
161
		return state.getValue(EGGS) == 1 ? SHAPE_SINGLE : SHAPE_MULTIPLE;
162
	}
163
164
	@Override
165
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
166
		builder.add(HATCH, EGGS);
167
	}
168
169
	private boolean canDestroyEgg(final ServerLevel level, final BlockPos pos, final Entity entity) {
170
		if (entity instanceof Turtle || entity instanceof Bat) {
171
			return false;
172
		} else if (entity instanceof Player player) {
173
			return !level.getServer().isUnderSpawnProtection(level, pos, player);
174
		} else {
175
			return entity instanceof LivingEntity ? level.getGameRules().get(GameRules.MOB_GRIEFING) : false;
176
		}
177
	}
178
}
179

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