package net.minecraft.world.entity.ai.behavior;

import net.minecraft.world.item.Item;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.item.ProjectileWeaponItem;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.server.level.ServerLevel;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import net.minecraft.world.entity.ai.memory.MemoryStatus;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.Mob;

public class MeleeAttack extends Behavior<Mob> {
    private final int cooldownBetweenAttacks;
    
    public MeleeAttack(final int integer) {
        super(ImmutableMap.<MemoryModuleType<PositionTracker>, MemoryStatus>of(MemoryModuleType.LOOK_TARGET, MemoryStatus.REGISTERED, (MemoryModuleType<PositionTracker>)MemoryModuleType.ATTACK_TARGET, MemoryStatus.VALUE_PRESENT, (MemoryModuleType<PositionTracker>)MemoryModuleType.ATTACK_COOLING_DOWN, MemoryStatus.VALUE_ABSENT));
        this.cooldownBetweenAttacks = integer;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel zd, final Mob aoz) {
        final LivingEntity aoy4 = this.getAttackTarget(aoz);
        return !this.isHoldingUsableProjectileWeapon(aoz) && BehaviorUtils.canSee(aoz, aoy4) && BehaviorUtils.isWithinMeleeAttackRange(aoz, aoy4);
    }
    
    private boolean isHoldingUsableProjectileWeapon(final Mob aoz) {
        return aoz.isHolding(bke -> bke instanceof ProjectileWeaponItem && aoz.canFireProjectileWeapon(bke));
    }
    
    @Override
    protected void start(final ServerLevel zd, final Mob aoz, final long long3) {
        final LivingEntity aoy6 = this.getAttackTarget(aoz);
        BehaviorUtils.lookAtEntity(aoz, aoy6);
        aoz.swing(InteractionHand.MAIN_HAND);
        aoz.doHurtTarget(aoy6);
        aoz.getBrain().<Boolean>setMemoryWithExpiry(MemoryModuleType.ATTACK_COOLING_DOWN, true, this.cooldownBetweenAttacks);
    }
    
    private LivingEntity getAttackTarget(final Mob aoz) {
        return aoz.getBrain().<LivingEntity>getMemory(MemoryModuleType.ATTACK_TARGET).get();
    }
}
