package net.minecraft.world.entity.monster;

import java.util.function.Predicate;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.Items;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ProjectileWeaponItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.Difficulty;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.level.LightLayer;
import java.util.Random;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.core.BlockPos;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.PathfinderMob;

public abstract class Monster extends PathfinderMob implements Enemy {
    protected Monster(final EntityType<? extends Monster> aku, final Level bjw) {
        super(aku, bjw);
        this.xpReward = 5;
    }
    
    @Override
    public SoundSource getSoundSource() {
        return SoundSource.HOSTILE;
    }
    
    @Override
    public void aiStep() {
        this.updateSwingTime();
        this.updateNoActionTime();
        super.aiStep();
    }
    
    protected void updateNoActionTime() {
        final float float2 = this.getBrightness();
        if (float2 > 0.5f) {
            this.noActionTime += 2;
        }
    }
    
    @Override
    protected boolean shouldDespawnInPeaceful() {
        return true;
    }
    
    @Override
    protected SoundEvent getSwimSound() {
        return SoundEvents.HOSTILE_SWIM;
    }
    
    @Override
    protected SoundEvent getSwimSplashSound() {
        return SoundEvents.HOSTILE_SPLASH;
    }
    
    @Override
    public boolean hurt(final DamageSource ajz, final float float2) {
        return !this.isInvulnerableTo(ajz) && super.hurt(ajz, float2);
    }
    
    @Override
    protected SoundEvent getHurtSound(final DamageSource ajz) {
        return SoundEvents.HOSTILE_HURT;
    }
    
    @Override
    protected SoundEvent getDeathSound() {
        return SoundEvents.HOSTILE_DEATH;
    }
    
    @Override
    protected SoundEvent getFallDamageSound(final int integer) {
        if (integer > 4) {
            return SoundEvents.HOSTILE_BIG_FALL;
        }
        return SoundEvents.HOSTILE_SMALL_FALL;
    }
    
    @Override
    public float getWalkTargetValue(final BlockPos fk, final LevelReader bjz) {
        return 0.5f - bjz.getBrightness(fk);
    }
    
    public static boolean isDarkEnoughToSpawn(final LevelAccessor bjx, final BlockPos fk, final Random random) {
        if (bjx.getBrightness(LightLayer.SKY, fk) > random.nextInt(32)) {
            return false;
        }
        final int integer4 = bjx.getLevel().isThundering() ? bjx.getMaxLocalRawBrightness(fk, 10) : bjx.getMaxLocalRawBrightness(fk);
        return integer4 <= random.nextInt(8);
    }
    
    public static boolean checkMonsterSpawnRules(final EntityType<? extends Monster> aku, final LevelAccessor bjx, final MobSpawnType alc, final BlockPos fk, final Random random) {
        return bjx.getDifficulty() != Difficulty.PEACEFUL && isDarkEnoughToSpawn(bjx, fk, random) && Mob.checkMobSpawnRules(aku, bjx, alc, fk, random);
    }
    
    public static boolean checkAnyLightMonsterSpawnRules(final EntityType<? extends Monster> aku, final LevelAccessor bjx, final MobSpawnType alc, final BlockPos fk, final Random random) {
        return bjx.getDifficulty() != Difficulty.PEACEFUL && Mob.checkMobSpawnRules(aku, bjx, alc, fk, random);
    }
    
    @Override
    protected void registerAttributes() {
        super.registerAttributes();
        this.getAttributes().registerAttribute(SharedMonsterAttributes.ATTACK_DAMAGE);
    }
    
    @Override
    protected boolean shouldDropExperience() {
        return true;
    }
    
    public boolean isPreventingPlayerRest(final Player ayj) {
        return true;
    }
    
    @Override
    public ItemStack getProjectile(final ItemStack ben) {
        if (ben.getItem() instanceof ProjectileWeaponItem) {
            final Predicate<ItemStack> predicate3 = ((ProjectileWeaponItem)ben.getItem()).getSupportedHeldProjectiles();
            final ItemStack ben2 = ProjectileWeaponItem.getHeldProjectile(this, predicate3);
            return ben2.isEmpty() ? new ItemStack(Items.ARROW) : ben2;
        }
        return ItemStack.EMPTY;
    }
}
