minecraft-src/net/minecraft/world/effect/InfestedMobEffect.java
2025-07-04 01:41:11 +03:00

50 lines
2.1 KiB
Java

package net.minecraft.world.effect;
import java.util.function.ToIntFunction;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.monster.Silverfish;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import org.joml.Vector3f;
class InfestedMobEffect extends MobEffect {
private final float chanceToSpawn;
private final ToIntFunction<RandomSource> spawnedCount;
protected InfestedMobEffect(MobEffectCategory category, int color, float chanceToSpawn, ToIntFunction<RandomSource> spawnedCount) {
super(category, color, ParticleTypes.INFESTED);
this.chanceToSpawn = chanceToSpawn;
this.spawnedCount = spawnedCount;
}
@Override
public void onMobHurt(LivingEntity livingEntity, int amplifier, DamageSource damageSource, float amount) {
if (livingEntity.getRandom().nextFloat() <= this.chanceToSpawn) {
int i = this.spawnedCount.applyAsInt(livingEntity.getRandom());
for (int j = 0; j < i; j++) {
this.spawnSilverfish(livingEntity.level(), livingEntity, livingEntity.getX(), livingEntity.getY() + livingEntity.getBbHeight() / 2.0, livingEntity.getZ());
}
}
}
private void spawnSilverfish(Level level, LivingEntity entity, double x, double y, double z) {
Silverfish silverfish = EntityType.SILVERFISH.create(level);
if (silverfish != null) {
RandomSource randomSource = entity.getRandom();
float f = (float) (Math.PI / 2);
float g = Mth.randomBetween(randomSource, (float) (-Math.PI / 2), (float) (Math.PI / 2));
Vector3f vector3f = entity.getLookAngle().toVector3f().mul(0.3F).mul(1.0F, 1.5F, 1.0F).rotateY(g);
silverfish.moveTo(x, y, z, level.getRandom().nextFloat() * 360.0F, 0.0F);
silverfish.setDeltaMovement(new Vec3(vector3f));
level.addFreshEntity(silverfish);
silverfish.playSound(SoundEvents.SILVERFISH_HURT);
}
}
}