| 1 | package net.minecraft.world.entity.monster;
|
| 2 |
|
| 3 | import java.util.function.Predicate;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.sounds.SoundEvent;
|
| 7 | import net.minecraft.sounds.SoundEvents;
|
| 8 | import net.minecraft.sounds.SoundSource;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.world.damagesource.DamageSource;
|
| 11 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 12 | import net.minecraft.world.entity.EntityType;
|
| 13 | import net.minecraft.world.entity.LivingEntity;
|
| 14 | import net.minecraft.world.entity.Mob;
|
| 15 | import net.minecraft.world.entity.PathfinderMob;
|
| 16 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 17 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 18 | import net.minecraft.world.entity.player.Player;
|
| 19 | import net.minecraft.world.item.ItemStack;
|
| 20 | import net.minecraft.world.item.Items;
|
| 21 | import net.minecraft.world.item.ProjectileWeaponItem;
|
| 22 | import net.minecraft.world.level.Level;
|
| 23 | import net.minecraft.world.level.LevelAccessor;
|
| 24 | import net.minecraft.world.level.LevelReader;
|
| 25 | import net.minecraft.world.level.LightLayer;
|
| 26 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 27 | import net.minecraft.world.level.dimension.DimensionType;
|
| 28 | import net.minecraft.world.level.gamerules.GameRules;
|
| 29 |
|
| 30 | public abstract class Monster extends PathfinderMob implements Enemy {
|
| 31 | protected Monster(final EntityType<? extends Monster> type, final Level level) {
|
| 32 | super(type, level);
|
| 33 | this.xpReward = 5;
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | public SoundSource getSoundSource() {
|
| 38 | return SoundSource.HOSTILE;
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public void aiStep() {
|
| 43 | this.updateNoActionTime();
|
| 44 | super.aiStep();
|
| 45 | }
|
| 46 |
|
| 47 | protected void updateNoActionTime() {
|
| 48 | float br = this.getLightLevelDependentMagicValue();
|
| 49 | if (br > 0.5F) {
|
| 50 | this.noActionTime += 2;
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | protected SoundEvent getSwimSound() {
|
| 56 | return SoundEvents.HOSTILE_SWIM;
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | protected SoundEvent getSwimSplashSound() {
|
| 61 | return SoundEvents.HOSTILE_SPLASH;
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 66 | return SoundEvents.HOSTILE_HURT;
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | protected SoundEvent getDeathSound() {
|
| 71 | return SoundEvents.HOSTILE_DEATH;
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public LivingEntity.Fallsounds getFallSounds() {
|
| 76 | return new LivingEntity.Fallsounds(SoundEvents.HOSTILE_SMALL_FALL, SoundEvents.HOSTILE_BIG_FALL);
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public float getWalkTargetValue(final BlockPos pos, final LevelReader level) {
|
| 81 | return -level.getPathfindingCostFromLightLevels(pos);
|
| 82 | }
|
| 83 |
|
| 84 | public static boolean isDarkEnoughToSpawn(final ServerLevelAccessor level, final BlockPos pos, final RandomSource random) {
|
| 85 | if (level.getBrightness(LightLayer.SKY, pos) > random.nextInt(32)) {
|
| 86 | return false;
|
| 87 | }
|
| 88 |
|
| 89 | DimensionType dimensionType = level.dimensionType();
|
| 90 | int blockLightLimit = dimensionType.monsterSpawnBlockLightLimit();
|
| 91 | if (blockLightLimit < 15 && level.getBrightness(LightLayer.BLOCK, pos) > blockLightLimit) {
|
| 92 | return false;
|
| 93 | }
|
| 94 |
|
| 95 | int brightness = level.getLevel().isThundering() ? level.getMaxLocalRawBrightness(pos, 10) : level.getMaxLocalRawBrightness(pos);
|
| 96 | return brightness <= dimensionType.monsterSpawnLightTest().sample(random);
|
| 97 | }
|
| 98 |
|
| 99 | public static boolean checkMonsterSpawnRules(
|
| 100 | final EntityType<? extends Mob> type, final ServerLevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
|
| 101 | ) {
|
| 102 | return (EntitySpawnReason.ignoresLightRequirements(spawnReason) || isDarkEnoughToSpawn(level, pos, random))
|
| 103 | && checkMobSpawnRules(type, level, spawnReason, pos, random);
|
| 104 | }
|
| 105 |
|
| 106 | public static boolean checkAnyLightMonsterSpawnRules(
|
| 107 | final EntityType<? extends Monster> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
|
| 108 | ) {
|
| 109 | return checkMobSpawnRules(type, level, spawnReason, pos, random);
|
| 110 | }
|
| 111 |
|
| 112 | public static boolean checkSurfaceMonstersSpawnRules(
|
| 113 | final EntityType<? extends Mob> type, final ServerLevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
|
| 114 | ) {
|
| 115 | return checkMonsterSpawnRules(type, level, spawnReason, pos, random) && (EntitySpawnReason.isSpawner(spawnReason) || level.canSeeSky(pos));
|
| 116 | }
|
| 117 |
|
| 118 | public static AttributeSupplier.Builder createMonsterAttributes() {
|
| 119 | return Mob.createMobAttributes().add(Attributes.ATTACK_DAMAGE);
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | public boolean shouldDropExperience() {
|
| 124 | return true;
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | protected boolean shouldDropLoot(final ServerLevel level) {
|
| 129 | return level.getGameRules().get(GameRules.MOB_DROPS);
|
| 130 | }
|
| 131 |
|
| 132 | public boolean isPreventingPlayerRest(final ServerLevel level, final Player player) {
|
| 133 | return true;
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | public ItemStack getProjectile(final ItemStack heldWeapon) {
|
| 138 | if (heldWeapon.getItem() instanceof ProjectileWeaponItem) {
|
| 139 | Predicate<ItemStack> supportedProjectiles = ((ProjectileWeaponItem)heldWeapon.getItem()).getSupportedHeldProjectiles();
|
| 140 | ItemStack heldProjectile = ProjectileWeaponItem.getHeldProjectile(this, supportedProjectiles);
|
| 141 | return heldProjectile.isEmpty() ? new ItemStack(Items.ARROW) : heldProjectile;
|
| 142 | } else {
|
| 143 | return ItemStack.EMPTY;
|
| 144 | }
|
| 145 | }
|
| 146 | }
|
| 147 |
|