package net.minecraft.world.entity;

import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.Level;
import java.util.Objects;
import net.minecraft.world.entity.player.Player;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.nbt.CompoundTag;
import javax.annotation.Nullable;
import java.util.UUID;

public interface NeutralMob {
    int getRemainingPersistentAngerTime();
    
    void setRemainingPersistentAngerTime(final int integer);
    
    @Nullable
    UUID getPersistentAngerTarget();
    
    void setPersistentAngerTarget(@Nullable final UUID uUID);
    
    void startPersistentAngerTimer();
    
    default void addPersistentAngerSaveData(final CompoundTag le) {
        le.putInt("AngerTime", this.getRemainingPersistentAngerTime());
        if (this.getPersistentAngerTarget() != null) {
            le.putUUID("AngryAt", this.getPersistentAngerTarget());
        }
    }
    
    default void readPersistentAngerSaveData(final ServerLevel zd, final CompoundTag le) {
        this.setRemainingPersistentAngerTime(le.getInt("AngerTime"));
        if (!le.hasUUID("AngryAt")) {
            this.setPersistentAngerTarget(null);
            return;
        }
        final UUID uUID4 = le.getUUID("AngryAt");
        this.setPersistentAngerTarget(uUID4);
        final Entity aom5 = zd.getEntity(uUID4);
        if (aom5 == null) {
            return;
        }
        if (aom5 instanceof Mob) {
            this.setLastHurtByMob((LivingEntity)aom5);
        }
        if (aom5.getType() == EntityType.PLAYER) {
            this.setLastHurtByPlayer((Player)aom5);
        }
    }
    
    default void updatePersistentAnger(final ServerLevel zd, final boolean boolean2) {
        final LivingEntity aoy4 = this.getTarget();
        final UUID uUID5 = this.getPersistentAngerTarget();
        if ((aoy4 == null || aoy4.isDeadOrDying()) && uUID5 != null && zd.getEntity(uUID5) instanceof Mob) {
            this.stopBeingAngry();
            return;
        }
        if (aoy4 != null && !Objects.equals(uUID5, aoy4.getUUID())) {
            this.setPersistentAngerTarget(aoy4.getUUID());
            this.startPersistentAngerTimer();
        }
        if (this.getRemainingPersistentAngerTime() > 0 && (aoy4 == null || aoy4.getType() != EntityType.PLAYER || !boolean2)) {
            this.setRemainingPersistentAngerTime(this.getRemainingPersistentAngerTime() - 1);
            if (this.getRemainingPersistentAngerTime() == 0) {
                this.stopBeingAngry();
            }
        }
    }
    
    default boolean isAngryAt(final LivingEntity aoy) {
        return EntitySelector.ATTACK_ALLOWED.test(aoy) && ((aoy.getType() == EntityType.PLAYER && this.isAngryAtAllPlayers(aoy.level)) || aoy.getUUID().equals(this.getPersistentAngerTarget()));
    }
    
    default boolean isAngryAtAllPlayers(final Level bqb) {
        return bqb.getGameRules().getBoolean(GameRules.RULE_UNIVERSAL_ANGER) && this.isAngry() && this.getPersistentAngerTarget() == null;
    }
    
    default boolean isAngry() {
        return this.getRemainingPersistentAngerTime() > 0;
    }
    
    default void playerDied(final Player bec) {
        if (!bec.level.getGameRules().getBoolean(GameRules.RULE_FORGIVE_DEAD_PLAYERS)) {
            return;
        }
        if (!bec.getUUID().equals(this.getPersistentAngerTarget())) {
            return;
        }
        this.stopBeingAngry();
    }
    
    default void forgetCurrentTargetAndRefreshUniversalAnger() {
        this.stopBeingAngry();
        this.startPersistentAngerTimer();
    }
    
    default void stopBeingAngry() {
        this.setLastHurtByMob(null);
        this.setPersistentAngerTarget(null);
        this.setTarget(null);
        this.setRemainingPersistentAngerTime(0);
    }
    
    void setLastHurtByMob(@Nullable final LivingEntity aoy);
    
    void setLastHurtByPlayer(@Nullable final Player bec);
    
    void setTarget(@Nullable final LivingEntity aoy);
    
    @Nullable
    LivingEntity getTarget();
}
