| 1 | package net.minecraft.world.effect;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.List;
|
| 6 | import java.util.function.ToIntFunction;
|
| 7 | import net.minecraft.core.particles.ParticleTypes;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.util.Mth;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 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.monster.cubemob.Slime;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 | import net.minecraft.world.level.gamerules.GameRules;
|
| 18 |
|
| 19 | class OozingMobEffect extends MobEffect {
|
| 20 | private static final int RADIUS_TO_CHECK_SLIMES = 2;
|
| 21 | public static final int SLIME_SIZE = 2;
|
| 22 | private final ToIntFunction<RandomSource> spawnedCount;
|
| 23 |
|
| 24 | protected OozingMobEffect(final MobEffectCategory category, final int color, final ToIntFunction<RandomSource> spawnedCount) {
|
| 25 | super(category, color, ParticleTypes.ITEM_SLIME);
|
| 26 | this.spawnedCount = spawnedCount;
|
| 27 | }
|
| 28 |
|
| 29 | @VisibleForTesting
|
| 30 | protected static int numberOfSlimesToSpawn(final int maxEntityCramming, final OozingMobEffect.NearbySlimes nearbySlimes, final int numberRequested) {
|
| 31 | return maxEntityCramming < 1 ? numberRequested : Mth.clamp(0, maxEntityCramming - nearbySlimes.count(maxEntityCramming), numberRequested);
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public void onMobRemoved(final ServerLevel level, final LivingEntity mob, final int amplifier, final Entity.RemovalReason reason) {
|
| 36 | if (reason == Entity.RemovalReason.KILLED) {
|
| 37 | int requestedSlimesToSpawn = this.spawnedCount.applyAsInt(mob.getRandom());
|
| 38 | int maxEntityCramming = level.getGameRules().get(GameRules.MAX_ENTITY_CRAMMING);
|
| 39 | int numberOfSlimesToSpawn = numberOfSlimesToSpawn(maxEntityCramming, OozingMobEffect.NearbySlimes.closeTo(mob), requestedSlimesToSpawn);
|
| 40 |
|
| 41 | for (int i = 0; i < numberOfSlimesToSpawn; i++) {
|
| 42 | this.spawnSlimeOffspring(mob.level(), mob.getX(), mob.getY() + 0.5, mob.getZ());
|
| 43 | }
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | private void spawnSlimeOffspring(final Level level, final double x, final double y, final double z) {
|
| 48 | Slime slime = EntityTypes.SLIME.create(level, EntitySpawnReason.TRIGGERED);
|
| 49 | if (slime != null) {
|
| 50 | slime.setSize(2, true);
|
| 51 | slime.snapTo(x, y, z, level.getRandom().nextFloat() * 360.0F, 0.0F);
|
| 52 | level.addFreshEntity(slime);
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | @FunctionalInterface
|
| 57 | protected interface NearbySlimes {
|
| 58 | int count(final int maxResults);
|
| 59 |
|
| 60 | private static OozingMobEffect.NearbySlimes closeTo(final LivingEntity mob) {
|
| 61 | return maxResults -> {
|
| 62 | List<Slime> slimesNearby = new ArrayList<>();
|
| 63 | mob.level().getEntities(EntityTypes.SLIME, mob.getBoundingBox().inflate(2.0), slime -> slime != mob, slimesNearby, maxResults);
|
| 64 | return slimesNearby.size();
|
| 65 | };
|
| 66 | }
|
| 67 | }
|
| 68 | }
|
| 69 |
|