CreakingHeartBlockEntity .java

net.minecraft.world.level.block.entity.CreakingHeartBlockEntity (class, 361 lines): members: DISTANCE_CREAKING_TOO_FAR, computeAnalogOutputSignal, emitter.

net.minecraft.world.level.block.entityclass361 linescommonextends BlockEntity13.6 KB

API Surface (13)

void serverTick(final Level level, final BlockPos pos, final BlockState state, final CreakingHeartBlockEntity entity)
void setCreakingInfo(final Creaking creaking)
void setCreakingInfo(final UUID uuid)
ClientboundBlockEntityDataPacket getUpdatePacket()
CompoundTag getUpdateTag(final HolderLookup.Provider registries)
void creakingHurt()
void preRemoveSideEffects(final BlockPos pos, final BlockState state)
void removeProtector(final @Nullable DamageSource damageSource)
boolean isProtector(final Creaking creaking)
int getAnalogOutputSignal()
int computeAnalogOutputSignal()
void loadAdditional(final ValueInput input)
void saveAdditional(final ValueOutput output)

Fields & Constants (22)

PLAYER_DETECTION_RANGE
CREAKING_ROAMING_RADIUS
DISTANCE_CREAKING_TOO_FAR
SPAWN_RANGE_XZ
SPAWN_RANGE_Y
ATTEMPTS_PER_SPAWN
UPDATE_TICKS
UPDATE_TICKS_VARIANCE
HURT_CALL_TOTAL_TICKS
NUMBER_OF_HURT_CALLS
HURT_CALL_INTERVAL
HURT_CALL_PARTICLE_TICKS
MAX_DEPTH
MAX_COUNT
TICKS_GRACE_PERIOD
NO_CREAKING
creakingInfo
ticksExisted
ticker
emitter
emitterTarget
outputSignal

String literals (1)

"creaking"

Full Source

CreakingHeartBlockEntity.java361 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block.entity;
2
3
import com.mojang.datafixers.util.Either;
4
import java.util.Optional;
5
import java.util.UUID;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.Direction;
8
import net.minecraft.core.HolderLookup;
9
import net.minecraft.core.UUIDUtil;
10
import net.minecraft.core.particles.TrailParticleOption;
11
import net.minecraft.nbt.CompoundTag;
12
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
13
import net.minecraft.server.level.ServerLevel;
14
import net.minecraft.sounds.SoundEvents;
15
import net.minecraft.sounds.SoundSource;
16
import net.minecraft.tags.BlockTags;
17
import net.minecraft.util.RandomSource;
18
import net.minecraft.util.SpawnUtil;
19
import net.minecraft.util.Util;
20
import net.minecraft.world.Difficulty;
21
import net.minecraft.world.attribute.EnvironmentAttributes;
22
import net.minecraft.world.damagesource.DamageSource;
23
import net.minecraft.world.entity.Entity;
24
import net.minecraft.world.entity.EntitySpawnReason;
25
import net.minecraft.world.entity.EntityTypes;
26
import net.minecraft.world.entity.monster.creaking.Creaking;
27
import net.minecraft.world.entity.player.Player;
28
import net.minecraft.world.level.Level;
29
import net.minecraft.world.level.block.Blocks;
30
import net.minecraft.world.level.block.CreakingHeartBlock;
31
import net.minecraft.world.level.block.MultifaceBlock;
32
import net.minecraft.world.level.block.state.BlockState;
33
import net.minecraft.world.level.block.state.properties.CreakingHeartState;
34
import net.minecraft.world.level.gameevent.GameEvent;
35
import net.minecraft.world.level.storage.ValueInput;
36
import net.minecraft.world.level.storage.ValueOutput;
37
import net.minecraft.world.phys.AABB;
38
import net.minecraft.world.phys.Vec3;
39
import org.apache.commons.lang3.mutable.Mutable;
40
import org.apache.commons.lang3.mutable.MutableObject;
41
import org.jspecify.annotations.Nullable;
42
43
public class CreakingHeartBlockEntity extends BlockEntity {
44
	private static final int PLAYER_DETECTION_RANGE = 32;
45
	public static final int CREAKING_ROAMING_RADIUS = 32;
46
	private static final int DISTANCE_CREAKING_TOO_FAR = 34;
47
	private static final int SPAWN_RANGE_XZ = 16;
48
	private static final int SPAWN_RANGE_Y = 8;
49
	private static final int ATTEMPTS_PER_SPAWN = 5;
50
	private static final int UPDATE_TICKS = 20;
51
	private static final int UPDATE_TICKS_VARIANCE = 5;
52
	private static final int HURT_CALL_TOTAL_TICKS = 100;
53
	private static final int NUMBER_OF_HURT_CALLS = 10;
54
	private static final int HURT_CALL_INTERVAL = 10;
55
	private static final int HURT_CALL_PARTICLE_TICKS = 50;
56
	private static final int MAX_DEPTH = 2;
57
	private static final int MAX_COUNT = 64;
58
	private static final int TICKS_GRACE_PERIOD = 30;
59
	private static final Optional<Creaking> NO_CREAKING = Optional.empty();
60
	private @Nullable Either<Creaking, UUID> creakingInfo;
61
	private long ticksExisted;
62
	private int ticker;
63
	private int emitter;
64
	private @Nullable Vec3 emitterTarget;
65
	private int outputSignal;
66
67
	public CreakingHeartBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
68
		super(BlockEntityTypes.CREAKING_HEART, worldPosition, blockState);
69
	}
70
71
	public static void serverTick(final Level level, final BlockPos pos, final BlockState state, final CreakingHeartBlockEntity entity) {
72
		entity.ticksExisted++;
73
		if (level instanceof ServerLevel serverLevel) {
74
			int computedOutputSignal = entity.computeAnalogOutputSignal();
75
			if (entity.outputSignal != computedOutputSignal) {
76
				entity.outputSignal = computedOutputSignal;
77
				level.updateNeighbourForOutputSignal(pos, Blocks.CREAKING_HEART);
78
			}
79
80
			if (entity.emitter > 0) {
81
				if (entity.emitter > 50) {
82
					entity.emitParticles(serverLevel, 1, true);
83
					entity.emitParticles(serverLevel, 1, false);
84
				}
85
86
				if (entity.emitter % 10 == 0 && entity.emitterTarget != null) {
87
					entity.getCreakingProtector().ifPresent(creakingx -> entity.emitterTarget = creakingx.getBoundingBox().getCenter());
88
					Vec3 heartPosition = Vec3.atCenterOf(pos);
89
					float progress = 0.2F + 0.8F * (100 - entity.emitter) / 100.0F;
90
					Vec3 soundLocation = heartPosition.subtract(entity.emitterTarget).scale(progress).add(entity.emitterTarget);
91
					BlockPos soundPos = BlockPos.containing(soundLocation);
92
					float volume = entity.emitter / 2.0F / 100.0F + 0.5F;
93
					serverLevel.playSound(null, soundPos, SoundEvents.CREAKING_HEART_HURT, SoundSource.BLOCKS, volume, 1.0F);
94
				}
95
96
				entity.emitter--;
97
			}
98
99
			if (entity.ticker-- < 0) {
100
				entity.ticker = entity.level == null ? 20 : entity.level.getRandom().nextInt(5) + 20;
101
				BlockState updatedState = updateCreakingState(level, state, pos, entity);
102
				if (updatedState != state) {
103
					level.setBlockAndUpdate(pos, updatedState);
104
					if (updatedState.getValue(CreakingHeartBlock.STATE) == CreakingHeartState.UPROOTED) {
105
						return;
106
					}
107
				}
108
109
				if (entity.creakingInfo == null) {
110
					if (updatedState.getValue(CreakingHeartBlock.STATE) == CreakingHeartState.AWAKE) {
111
						if (serverLevel.isSpawningMonsters() && serverLevel.getLevelData().getDifficulty() != Difficulty.PEACEFUL) {
112
							Player player = level.getNearestPlayer(pos.getX(), pos.getY(), pos.getZ(), 32.0, false);
113
							if (player != null) {
114
								Creaking creaking = spawnProtector(serverLevel, entity);
115
								if (creaking != null) {
116
									entity.setCreakingInfo(creaking);
117
									creaking.makeSound(SoundEvents.CREAKING_SPAWN);
118
									level.playSound(null, entity.getBlockPos(), SoundEvents.CREAKING_HEART_SPAWN, SoundSource.BLOCKS, 1.0F, 1.0F);
119
								}
120
							}
121
						}
122
					}
123
				} else {
124
					Optional<Creaking> optionalCreaking = entity.getCreakingProtector();
125
					if (optionalCreaking.isPresent()) {
126
						Creaking creaking = optionalCreaking.get();
127
						if (!level.environmentAttributes().getValue(EnvironmentAttributes.CREAKING_ACTIVE, pos) && !creaking.isPersistenceRequired()
128
							|| entity.distanceToCreaking() > 34.0
129
							|| creaking.playerIsStuckInYou()) {
130
							entity.removeProtector(null);
131
						}
132
					}
133
				}
134
			}
135
		}
136
	}
137
138
	private static BlockState updateCreakingState(final Level level, final BlockState state, final BlockPos pos, final CreakingHeartBlockEntity entity) {
139
		if (!CreakingHeartBlock.hasRequiredLogs(state, level, pos) && entity.creakingInfo == null) {
140
			return state.setValue(CreakingHeartBlock.STATE, CreakingHeartState.UPROOTED);
141
		}
142
143
		CreakingHeartState heartState = level.environmentAttributes().getValue(EnvironmentAttributes.CREAKING_ACTIVE, pos)
144
			? CreakingHeartState.AWAKE
145
			: CreakingHeartState.DORMANT;
146
		return state.setValue(CreakingHeartBlock.STATE, heartState);
147
	}
148
149
	private double distanceToCreaking() {
150
		return this.getCreakingProtector().map(creaking -> Math.sqrt(creaking.distanceToSqr(Vec3.atBottomCenterOf(this.getBlockPos())))).orElse(0.0);
151
	}
152
153
	private void clearCreakingInfo() {
154
		this.creakingInfo = null;
155
		this.setChanged();
156
	}
157
158
	public void setCreakingInfo(final Creaking creaking) {
159
		this.creakingInfo = Either.left(creaking);
160
		this.setChanged();
161
	}
162
163
	public void setCreakingInfo(final UUID uuid) {
164
		this.creakingInfo = Either.right(uuid);
165
		this.ticksExisted = 0L;
166
		this.setChanged();
167
	}
168
169
	private Optional<Creaking> getCreakingProtector() {
170
		if (this.creakingInfo == null) {
171
			return NO_CREAKING;
172
		}
173
174
		if (this.creakingInfo.left().isPresent()) {
175
			Creaking creaking = this.creakingInfo.left().get();
176
			if (!creaking.isRemoved()) {
177
				return Optional.of(creaking);
178
			}
179
180
			this.setCreakingInfo(creaking.getUUID());
181
		}
182
183
		if (this.level instanceof ServerLevel serverLevel && this.creakingInfo.right().isPresent()) {
184
			UUID uuid = this.creakingInfo.right().get();
185
			if (serverLevel.getEntity(uuid) instanceof Creaking resolvedCreaking) {
186
				this.setCreakingInfo(resolvedCreaking);
187
				return Optional.of(resolvedCreaking);
188
			} else {
189
				if (this.ticksExisted >= 30L) {
190
					this.clearCreakingInfo();
191
				}
192
193
				return NO_CREAKING;
194
			}
195
		} else {
196
			return NO_CREAKING;
197
		}
198
	}
199
200
	private static @Nullable Creaking spawnProtector(final ServerLevel level, final CreakingHeartBlockEntity entity) {
201
		BlockPos pos = entity.getBlockPos();
202
		Optional<Creaking> spawnedMob = SpawnUtil.trySpawnMob(
203
			EntityTypes.CREAKING, EntitySpawnReason.SPAWNER, level, pos, 5, 16, 8, SpawnUtil.Strategy.ON_TOP_OF_COLLIDER_NO_LEAVES, true
204
		);
205
		if (spawnedMob.isEmpty()) {
206
			return null;
207
		}
208
209
		Creaking spawnedCreaking = spawnedMob.get();
210
		level.gameEvent(spawnedCreaking, GameEvent.ENTITY_PLACE, spawnedCreaking.position());
211
		level.broadcastEntityEvent(spawnedCreaking, (byte)60);
212
		spawnedCreaking.setTransient(pos);
213
		return spawnedCreaking;
214
	}
215
216
	public ClientboundBlockEntityDataPacket getUpdatePacket() {
217
		return ClientboundBlockEntityDataPacket.create(this);
218
	}
219
220
	@Override
221
	public CompoundTag getUpdateTag(final HolderLookup.Provider registries) {
222
		return this.saveCustomOnly(registries);
223
	}
224
225
	public void creakingHurt() {
226
		Optional<Creaking> creaking = this.getCreakingProtector();
227
		if (!creaking.isEmpty()) {
228
			if (this.level instanceof ServerLevel serverLevel) {
229
				if (this.emitter <= 0) {
230
					this.emitParticles(serverLevel, 20, false);
231
					if (this.getBlockState().getValue(CreakingHeartBlock.STATE) == CreakingHeartState.AWAKE) {
232
						int numberOfClumps = this.level.getRandom().nextIntBetweenInclusive(2, 3);
233
234
						for (int i = 0; i < numberOfClumps; i++) {
235
							this.spreadResin(serverLevel).ifPresent(blockPos -> {
236
								this.level.playSound(null, blockPos, SoundEvents.RESIN_PLACE, SoundSource.BLOCKS, 1.0F, 1.0F);
237
								this.level.gameEvent(GameEvent.BLOCK_PLACE, blockPos, GameEvent.Context.of(this.getBlockState()));
238
							});
239
						}
240
					}
241
242
					this.emitter = 100;
243
					this.emitterTarget = creaking.get().getBoundingBox().getCenter();
244
				}
245
			}
246
		}
247
	}
248
249
	private Optional<BlockPos> spreadResin(final ServerLevel level) {
250
		RandomSource random = level.getRandom();
251
		Mutable<BlockPos> placedResin = new MutableObject<>(null);
252
		BlockPos.breadthFirstTraversal(this.worldPosition, 2, 64, (pos, acceptor) -> {
253
			for (Direction dir : Util.shuffledCopy(Direction.values(), random)) {
254
				BlockPos neighbourPos = pos.relative(dir);
255
				if (level.getBlockState(neighbourPos).is(BlockTags.PALE_OAK_LOGS)) {
256
					acceptor.accept(neighbourPos);
257
				}
258
			}
259
		}, pos -> {
260
			if (!level.getBlockState(pos).is(BlockTags.PALE_OAK_LOGS)) {
261
				return BlockPos.TraversalNodeStatus.ACCEPT;
262
			}
263
264
			for (Direction dir : Util.shuffledCopy(Direction.values(), random)) {
265
				BlockPos neightbourPos = pos.relative(dir);
266
				BlockState neighbourState = level.getBlockState(neightbourPos);
267
				Direction opposite = dir.getOpposite();
268
				if (neighbourState.isAir()) {
269
					neighbourState = Blocks.RESIN_CLUMP.defaultBlockState();
270
				} else if (neighbourState.is(Blocks.WATER) && neighbourState.getFluidState().isSource()) {
271
					neighbourState = Blocks.RESIN_CLUMP.defaultBlockState().setValue(MultifaceBlock.WATERLOGGED, true);
272
				}
273
274
				if (neighbourState.is(Blocks.RESIN_CLUMP) && !MultifaceBlock.hasFace(neighbourState, opposite)) {
275
					level.setBlockAndUpdate(neightbourPos, neighbourState.setValue(MultifaceBlock.getFaceProperty(opposite), true));
276
					placedResin.setValue(neightbourPos);
277
					return BlockPos.TraversalNodeStatus.STOP;
278
				}
279
			}
280
281
			return BlockPos.TraversalNodeStatus.ACCEPT;
282
		});
283
		return Optional.ofNullable(placedResin.get());
284
	}
285
286
	private void emitParticles(final ServerLevel serverLevel, final int count, final boolean towardsCreaking) {
287
		Optional<Creaking> creaking = this.getCreakingProtector();
288
		if (!creaking.isEmpty()) {
289
			int color = towardsCreaking ? 16545810 : 6250335;
290
			RandomSource random = serverLevel.getRandom();
291
292
			for (double i = 0.0; i < count; i++) {
293
				AABB box = creaking.get().getBoundingBox();
294
				Vec3 source = box.getMinPosition().add(random.nextDouble() * box.getXsize(), random.nextDouble() * box.getYsize(), random.nextDouble() * box.getZsize());
295
				Vec3 destination = Vec3.atLowerCornerOf(this.getBlockPos()).add(random.nextDouble(), random.nextDouble(), random.nextDouble());
296
				if (towardsCreaking) {
297
					Vec3 foo = source;
298
					source = destination;
299
					destination = foo;
300
				}
301
302
				TrailParticleOption particleOption = new TrailParticleOption(destination, color, random.nextInt(40) + 10);
303
				serverLevel.sendParticles(particleOption, true, true, source.x, source.y, source.z, 1, 0.0, 0.0, 0.0, 0.0);
304
			}
305
		}
306
	}
307
308
	@Override
309
	public void preRemoveSideEffects(final BlockPos pos, final BlockState state) {
310
		this.removeProtector(null);
311
	}
312
313
	public void removeProtector(final @Nullable DamageSource damageSource) {
314
		Optional<Creaking> creakingProtector = this.getCreakingProtector();
315
		if (creakingProtector.isPresent()) {
316
			Creaking creaking = creakingProtector.get();
317
			if (damageSource == null) {
318
				creaking.tearDown();
319
			} else {
320
				creaking.creakingDeathEffects(damageSource);
321
				creaking.setTearingDown();
322
				creaking.setHealth(0.0F);
323
			}
324
325
			this.clearCreakingInfo();
326
		}
327
	}
328
329
	public boolean isProtector(final Creaking creaking) {
330
		return this.getCreakingProtector().map(c -> c == creaking).orElse(false);
331
	}
332
333
	public int getAnalogOutputSignal() {
334
		return this.outputSignal;
335
	}
336
337
	public int computeAnalogOutputSignal() {
338
		if (this.creakingInfo != null && !this.getCreakingProtector().isEmpty()) {
339
			double distance = this.distanceToCreaking();
340
			double scaledDistance = Math.clamp(distance, 0.0, 32.0) / 32.0;
341
			return 15 - (int)Math.floor(scaledDistance * 15.0);
342
		} else {
343
			return 0;
344
		}
345
	}
346
347
	@Override
348
	protected void loadAdditional(final ValueInput input) {
349
		super.loadAdditional(input);
350
		input.read("creaking", UUIDUtil.CODEC).ifPresentOrElse(this::setCreakingInfo, this::clearCreakingInfo);
351
	}
352
353
	@Override
354
	protected void saveAdditional(final ValueOutput output) {
355
		super.saveAdditional(output);
356
		if (this.creakingInfo != null) {
357
			output.store("creaking", UUIDUtil.CODEC, this.creakingInfo.map(Entity::getUUID, uuid -> (UUID)uuid));
358
		}
359
	}
360
}
361

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