RemoveBlockGoal .java

net.minecraft.world.entity.ai.goal.RemoveBlockGoal (class): members: playDestroyProgressSound, WAIT_AFTER_BLOCK_FOUND, ticksSinceReachedGoal, removerMob.

net.minecraft.world.entity.ai.goalclass145 linescommonextends MoveToBlockGoal4.6 KB

API Surface (7)

boolean canUse()
void stop()
void start()
void playDestroyProgressSound(final LevelAccessor level, final BlockPos pos)
void playBreakSound(final Level level, final BlockPos pos)
void tick()
boolean isValidTarget(final LevelReader level, final BlockPos pos)

Fields & Constants (4)

WAIT_AFTER_BLOCK_FOUND
blockToRemove
removerMob
ticksSinceReachedGoal

Full Source

RemoveBlockGoal.java145 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.goal;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.core.SectionPos;
5
import net.minecraft.core.particles.ItemParticleOption;
6
import net.minecraft.core.particles.ParticleTypes;
7
import net.minecraft.server.level.ServerLevel;
8
import net.minecraft.util.RandomSource;
9
import net.minecraft.world.entity.Mob;
10
import net.minecraft.world.entity.PathfinderMob;
11
import net.minecraft.world.item.Items;
12
import net.minecraft.world.level.BlockGetter;
13
import net.minecraft.world.level.Level;
14
import net.minecraft.world.level.LevelAccessor;
15
import net.minecraft.world.level.LevelReader;
16
import net.minecraft.world.level.block.Block;
17
import net.minecraft.world.level.chunk.ChunkAccess;
18
import net.minecraft.world.level.chunk.status.ChunkStatus;
19
import net.minecraft.world.level.gamerules.GameRules;
20
import net.minecraft.world.phys.Vec3;
21
import org.jspecify.annotations.Nullable;
22
23
public class RemoveBlockGoal extends MoveToBlockGoal {
24
	private final Block blockToRemove;
25
	private final Mob removerMob;
26
	private int ticksSinceReachedGoal;
27
	private static final int WAIT_AFTER_BLOCK_FOUND = 20;
28
29
	public RemoveBlockGoal(final Block blockToRemove, final PathfinderMob mob, final double speedModifier, final int verticalSearchRange) {
30
		super(mob, speedModifier, 24, verticalSearchRange);
31
		this.blockToRemove = blockToRemove;
32
		this.removerMob = mob;
33
	}
34
35
	@Override
36
	public boolean canUse() {
37
		if (!getServerLevel(this.removerMob).getGameRules().get(GameRules.MOB_GRIEFING)) {
38
			return false;
39
		} else if (this.nextStartTick > 0) {
40
			this.nextStartTick--;
41
			return false;
42
		} else if (this.findNearestBlock()) {
43
			this.nextStartTick = reducedTickDelay(20);
44
			return true;
45
		} else {
46
			this.nextStartTick = this.nextStartTick(this.mob);
47
			return false;
48
		}
49
	}
50
51
	@Override
52
	public void stop() {
53
		super.stop();
54
		this.removerMob.fallDistance = 1.0;
55
	}
56
57
	@Override
58
	public void start() {
59
		super.start();
60
		this.ticksSinceReachedGoal = 0;
61
	}
62
63
	public void playDestroyProgressSound(final LevelAccessor level, final BlockPos pos) {
64
	}
65
66
	public void playBreakSound(final Level level, final BlockPos pos) {
67
	}
68
69
	@Override
70
	public void tick() {
71
		super.tick();
72
		Level level = this.removerMob.level();
73
		BlockPos mobPos = this.removerMob.blockPosition();
74
		BlockPos eatPos = this.getPosWithBlock(mobPos, level);
75
		RandomSource random = this.removerMob.getRandom();
76
		if (this.isReachedTarget() && eatPos != null) {
77
			if (this.ticksSinceReachedGoal > 0) {
78
				Vec3 movement = this.removerMob.getDeltaMovement();
79
				this.removerMob.setDeltaMovement(movement.x, 0.3, movement.z);
80
				if (level instanceof ServerLevel serverLevel) {
81
					serverLevel.sendParticles(
82
						new ItemParticleOption(ParticleTypes.ITEM, Items.EGG),
83
						eatPos.getX() + 0.5,
84
						eatPos.getY() + 0.7,
85
						eatPos.getZ() + 0.5,
86
						3,
87
						(random.nextFloat() - 0.5) * 0.08,
88
						(random.nextFloat() - 0.5) * 0.08,
89
						(random.nextFloat() - 0.5) * 0.08,
90
						0.15F
91
					);
92
				}
93
			}
94
95
			if (this.ticksSinceReachedGoal % 2 == 0) {
96
				Vec3 movement = this.removerMob.getDeltaMovement();
97
				this.removerMob.setDeltaMovement(movement.x, -0.3, movement.z);
98
				if (this.ticksSinceReachedGoal % 6 == 0) {
99
					this.playDestroyProgressSound(level, this.blockPos);
100
				}
101
			}
102
103
			if (this.ticksSinceReachedGoal > 60) {
104
				level.removeBlock(eatPos, false);
105
				if (!level.isClientSide()) {
106
					for (int i = 0; i < 20; i++) {
107
						double xa = random.nextGaussian() * 0.02;
108
						double ya = random.nextGaussian() * 0.02;
109
						double za = random.nextGaussian() * 0.02;
110
						((ServerLevel)level).sendParticles(ParticleTypes.POOF, eatPos.getX() + 0.5, eatPos.getY(), eatPos.getZ() + 0.5, 1, xa, ya, za, 0.15F);
111
					}
112
113
					this.playBreakSound(level, eatPos);
114
				}
115
			}
116
117
			this.ticksSinceReachedGoal++;
118
		}
119
	}
120
121
	private @Nullable BlockPos getPosWithBlock(final BlockPos pos, final BlockGetter level) {
122
		if (level.getBlockState(pos).is(this.blockToRemove)) {
123
			return pos;
124
		}
125
126
		BlockPos[] neighbours = new BlockPos[]{pos.below(), pos.west(), pos.east(), pos.north(), pos.south(), pos.below().below()};
127
128
		for (BlockPos neighborPos : neighbours) {
129
			if (level.getBlockState(neighborPos).is(this.blockToRemove)) {
130
				return neighborPos;
131
			}
132
		}
133
134
		return null;
135
	}
136
137
	@Override
138
	protected boolean isValidTarget(final LevelReader level, final BlockPos pos) {
139
		ChunkAccess chunk = level.getChunk(SectionPos.blockToSectionCoord(pos.getX()), SectionPos.blockToSectionCoord(pos.getZ()), ChunkStatus.FULL, false);
140
		return chunk == null
141
			? false
142
			: chunk.getBlockState(pos).is(this.blockToRemove) && chunk.getBlockState(pos.above()).isAir() && chunk.getBlockState(pos.above(2)).isAir();
143
	}
144
}
145

Decompiled Minecraft 26.3 source of RemoveBlockGoal (net/minecraft/world/entity/ai/goal/RemoveBlockGoal.java) with Mojang official mappings, stored verbatim on the site.