InfestedMobEffect .java

net.minecraft.world.effect.InfestedMobEffect (class): members: chanceToSpawn, spawnedCount, onMobHurt. imports: Silverfish, Vector3f, EntitySpawnReason.

net.minecraft.world.effectclass52 linescommonextends MobEffect2.2 KB

API Surface (1)

void onMobHurt(final ServerLevel level, final LivingEntity mob, final int amplifier, final DamageSource source, final float damage)

Fields & Constants (2)

chanceToSpawn
spawnedCount

Full Source

InfestedMobEffect.java52 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.effect;
2
3
import java.util.function.ToIntFunction;
4
import net.minecraft.core.particles.ParticleTypes;
5
import net.minecraft.server.level.ServerLevel;
6
import net.minecraft.sounds.SoundEvents;
7
import net.minecraft.util.Mth;
8
import net.minecraft.util.RandomSource;
9
import net.minecraft.world.damagesource.DamageSource;
10
import net.minecraft.world.entity.EntitySpawnReason;
11
import net.minecraft.world.entity.EntityTypes;
12
import net.minecraft.world.entity.LivingEntity;
13
import net.minecraft.world.entity.monster.Silverfish;
14
import net.minecraft.world.phys.Vec3;
15
import org.joml.Vector3f;
16
17
class InfestedMobEffect extends MobEffect {
18
	private final float chanceToSpawn;
19
	private final ToIntFunction<RandomSource> spawnedCount;
20
21
	protected InfestedMobEffect(final MobEffectCategory category, final int color, final float chanceToSpawn, final ToIntFunction<RandomSource> spawnedCount) {
22
		super(category, color, ParticleTypes.INFESTED);
23
		this.chanceToSpawn = chanceToSpawn;
24
		this.spawnedCount = spawnedCount;
25
	}
26
27
	@Override
28
	public void onMobHurt(final ServerLevel level, final LivingEntity mob, final int amplifier, final DamageSource source, final float damage) {
29
		if (mob.getRandom().nextFloat() <= this.chanceToSpawn) {
30
			int count = this.spawnedCount.applyAsInt(mob.getRandom());
31
32
			for (int i = 0; i < count; i++) {
33
				this.spawnSilverfish(level, mob, mob.getX(), mob.getY() + mob.getBbHeight() / 2.0, mob.getZ());
34
			}
35
		}
36
	}
37
38
	private void spawnSilverfish(final ServerLevel level, final LivingEntity mob, final double x, final double y, final double z) {
39
		Silverfish silverfish = EntityTypes.SILVERFISH.create(level, EntitySpawnReason.TRIGGERED);
40
		if (silverfish != null) {
41
			RandomSource random = mob.getRandom();
42
			float angle = (float) (Math.PI / 2);
43
			float randomAngle = Mth.randomBetween(random, (float) (-Math.PI / 2), (float) (Math.PI / 2));
44
			Vector3f viewDirection = mob.getLookAngle().toVector3f().mul(0.3F).mul(1.0F, 1.5F, 1.0F).rotateY(randomAngle);
45
			silverfish.snapTo(x, y, z, level.getRandom().nextFloat() * 360.0F, 0.0F);
46
			silverfish.setDeltaMovement(new Vec3(viewDirection));
47
			level.addFreshEntity(silverfish);
48
			silverfish.playSound(SoundEvents.SILVERFISH_HURT);
49
		}
50
	}
51
}
52

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