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

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Collection;
import net.minecraft.world.entity.raid.Raids;
import com.google.common.collect.Sets;
import net.minecraft.world.entity.raid.Raid;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.util.RandomPos;
import net.minecraft.core.Vec3i;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.entity.Entity;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import java.util.EnumSet;
import net.minecraft.world.entity.raid.Raider;

public class PathfindToRaidGoal<T extends Raider> extends Goal {
    private final T mob;
    
    public PathfindToRaidGoal(final T azo) {
        this.mob = azo;
        this.setFlags(EnumSet.<Flag>of(Flag.MOVE));
    }
    
    @Override
    public boolean canUse() {
        return this.mob.getTarget() == null && !this.mob.isVehicle() && this.mob.hasActiveRaid() && !this.mob.getCurrentRaid().isOver() && !((ServerLevel)this.mob.level).isVillage(new BlockPos(this.mob));
    }
    
    @Override
    public boolean canContinueToUse() {
        return this.mob.hasActiveRaid() && !this.mob.getCurrentRaid().isOver() && this.mob.level instanceof ServerLevel && !((ServerLevel)this.mob.level).isVillage(new BlockPos(this.mob));
    }
    
    @Override
    public void tick() {
        if (this.mob.hasActiveRaid()) {
            final Raid azn2 = this.mob.getCurrentRaid();
            if (this.mob.tickCount % 20 == 0) {
                this.recruitNearby(azn2);
            }
            if (!this.mob.isPathFinding()) {
                final Vec3 cvl3 = RandomPos.getPosTowards(this.mob, 15, 4, new Vec3(azn2.getCenter()));
                if (cvl3 != null) {
                    this.mob.getNavigation().moveTo(cvl3.x, cvl3.y, cvl3.z, 1.0);
                }
            }
        }
    }
    
    private void recruitNearby(final Raid azn) {
        if (azn.isActive()) {
            final Set<Raider> set3 = Sets.newHashSet();
            final List<Raider> list4 = this.mob.level.<Raider>getEntitiesOfClass(Raider.class, this.mob.getBoundingBox().inflate(16.0), azo -> !azo.hasActiveRaid() && Raids.canJoinRaid(azo, azn));
            set3.addAll(list4);
            for (final Raider azo2 : set3) {
                azn.joinRaid(azn.getGroupsSpawned(), azo2, null, true);
            }
        }
    }
}
