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

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import net.minecraft.core.BlockPos;
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.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 bfi) {
        this.mob = bfi;
        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(this.mob.blockPosition());
    }
    
    @Override
    public boolean canContinueToUse() {
        return this.mob.hasActiveRaid() && !this.mob.getCurrentRaid().isOver() && this.mob.level instanceof ServerLevel && !((ServerLevel)this.mob.level).isVillage(this.mob.blockPosition());
    }
    
    @Override
    public void tick() {
        if (this.mob.hasActiveRaid()) {
            final Raid bfh2 = this.mob.getCurrentRaid();
            if (this.mob.tickCount % 20 == 0) {
                this.recruitNearby(bfh2);
            }
            if (!this.mob.isPathFinding()) {
                final Vec3 dem3 = RandomPos.getPosTowards(this.mob, 15, 4, Vec3.atBottomCenterOf(bfh2.getCenter()));
                if (dem3 != null) {
                    this.mob.getNavigation().moveTo(dem3.x, dem3.y, dem3.z, 1.0);
                }
            }
        }
    }
    
    private void recruitNearby(final Raid bfh) {
        if (bfh.isActive()) {
            final Set<Raider> set3 = Sets.newHashSet();
            final List<Raider> list4 = this.mob.level.<Raider>getEntitiesOfClass(Raider.class, this.mob.getBoundingBox().inflate(16.0), bfi -> !bfi.hasActiveRaid() && Raids.canJoinRaid(bfi, bfh));
            set3.addAll(list4);
            for (final Raider bfi2 : set3) {
                bfh.joinRaid(bfh.getGroupsSpawned(), bfi2, null, true);
            }
        }
    }
}
