| 1 | package net.minecraft.world.entity.monster.cubemob;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.particles.ParticleOptions;
|
| 5 | import net.minecraft.core.particles.ParticleTypes;
|
| 6 | import net.minecraft.sounds.SoundEvent;
|
| 7 | import net.minecraft.sounds.SoundEvents;
|
| 8 | import net.minecraft.tags.BiomeTags;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.world.Difficulty;
|
| 11 | import net.minecraft.world.DifficultyInstance;
|
| 12 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 13 | import net.minecraft.world.damagesource.DamageSource;
|
| 14 | import net.minecraft.world.entity.AgeableMob;
|
| 15 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 16 | import net.minecraft.world.entity.EntityType;
|
| 17 | import net.minecraft.world.entity.SpawnGroupData;
|
| 18 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 19 | import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
|
| 20 | import net.minecraft.world.entity.animal.golem.IronGolem;
|
| 21 | import net.minecraft.world.entity.monster.Enemy;
|
| 22 | import net.minecraft.world.entity.player.Player;
|
| 23 | import net.minecraft.world.level.ChunkPos;
|
| 24 | import net.minecraft.world.level.Level;
|
| 25 | import net.minecraft.world.level.LevelAccessor;
|
| 26 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 27 | import net.minecraft.world.level.WorldGenLevel;
|
| 28 | import net.minecraft.world.level.levelgen.WorldgenRandom;
|
| 29 | import org.jspecify.annotations.Nullable;
|
| 30 |
|
| 31 | public class Slime extends AbstractCubeMob implements Enemy {
|
| 32 | public Slime(final EntityType<? extends Slime> type, final Level level) {
|
| 33 | super(type, level);
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | protected void addBehaviourGoals() {
|
| 38 | this.goalSelector.addGoal(2, new AbstractCubeMob.CubeMobAttackGoal(this));
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | protected void addTargetingGoals() {
|
| 43 | this.targetSelector
|
| 44 | .addGoal(1, new NearestAttackableTargetGoal<>(this, Player.class, 10, true, false, (target, level) -> Math.abs(target.getY() - this.getY()) <= 4.0));
|
| 45 | this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, IronGolem.class, true));
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 50 | return this.isTiny() ? SoundEvents.SLIME_HURT_SMALL : SoundEvents.SLIME_HURT;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | protected SoundEvent getDeathSound() {
|
| 55 | return this.isTiny() ? SoundEvents.SLIME_DEATH_SMALL : SoundEvents.SLIME_DEATH;
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | protected ParticleOptions getParticleType() {
|
| 60 | return ParticleTypes.ITEM_SLIME;
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | protected SoundEvent getSquishSound() {
|
| 65 | return this.isTiny() ? SoundEvents.SLIME_SQUISH_SMALL : SoundEvents.SLIME_SQUISH;
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | protected SoundEvent getJumpSound() {
|
| 70 | return this.isTiny() ? SoundEvents.SLIME_JUMP_SMALL : SoundEvents.SLIME_JUMP;
|
| 71 | }
|
| 72 |
|
| 73 | public static boolean checkSlimeSpawnRules(
|
| 74 | final EntityType<Slime> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
|
| 75 | ) {
|
| 76 | if (level.getDifficulty() != Difficulty.PEACEFUL) {
|
| 77 | if (EntitySpawnReason.isSpawner(spawnReason)) {
|
| 78 | return checkMobSpawnRules(type, level, spawnReason, pos, random);
|
| 79 | }
|
| 80 |
|
| 81 | if (level.getBiome(pos).is(BiomeTags.ALLOWS_SURFACE_SLIME_SPAWNS) && pos.getY() > 50 && pos.getY() < 70) {
|
| 82 | float surfaceSlimeSpawnChance = level.environmentAttributes().getValue(EnvironmentAttributes.SURFACE_SLIME_SPAWN_CHANCE, pos);
|
| 83 | if (random.nextFloat() < surfaceSlimeSpawnChance && level.getMaxLocalRawBrightness(pos) <= random.nextInt(8)) {
|
| 84 | return checkMobSpawnRules(type, level, spawnReason, pos, random);
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | if (!(level instanceof WorldGenLevel worldGenLevel)) {
|
| 89 | return false;
|
| 90 | }
|
| 91 |
|
| 92 | ChunkPos chunkPos = ChunkPos.containing(pos);
|
| 93 | boolean slimeChunk = WorldgenRandom.seedSlimeChunk(chunkPos.x(), chunkPos.z(), worldGenLevel.getSeed(), 987234911L).nextInt(10) == 0;
|
| 94 | if (random.nextInt(10) == 0 && slimeChunk && pos.getY() < 40) {
|
| 95 | return checkMobSpawnRules(type, level, spawnReason, pos, random);
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | return false;
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | protected boolean canBeABaby() {
|
| 104 | return false;
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | public @Nullable SpawnGroupData finalizeSpawn(
|
| 109 | final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, @Nullable SpawnGroupData groupData
|
| 110 | ) {
|
| 111 | if (groupData == null) {
|
| 112 | groupData = new AgeableMob.AgeableMobGroupData(false);
|
| 113 | }
|
| 114 |
|
| 115 | return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | public void setSize(final int size, final boolean updateHealth) {
|
| 120 | super.setSize(size, updateHealth);
|
| 121 | int actualSize = this.entityData.get(ID_SIZE);
|
| 122 | this.getAttribute(Attributes.ATTACK_DAMAGE).setBaseValue(actualSize);
|
| 123 | this.xpReward = actualSize;
|
| 124 | }
|
| 125 | }
|
| 126 |
|