package net.minecraft.world.level.levelgen;

import net.minecraft.world.entity.SpawnGroupData;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.stats.ServerStatsCounter;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.entity.player.Player;
import java.util.Iterator;
import java.util.Random;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.monster.Phantom;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.NaturalSpawner;
import net.minecraft.util.Mth;
import net.minecraft.stats.Stat;
import net.minecraft.stats.Stats;
import net.minecraft.world.entity.Entity;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.GameRules;
import net.minecraft.server.level.ServerLevel;

public class PhantomSpawner {
    private int nextTick;
    
    public int tick(final ServerLevel xd, final boolean boolean2, final boolean boolean3) {
        if (!boolean2) {
            return 0;
        }
        if (!xd.getGameRules().getBoolean(GameRules.RULE_DOINSOMNIA)) {
            return 0;
        }
        final Random random5 = xd.random;
        --this.nextTick;
        if (this.nextTick > 0) {
            return 0;
        }
        this.nextTick += (60 + random5.nextInt(60)) * 20;
        if (xd.getSkyDarken() < 5 && xd.dimension.isHasSkyLight()) {
            return 0;
        }
        int integer6 = 0;
        for (final Player ayj8 : xd.players()) {
            if (ayj8.isSpectator()) {
                continue;
            }
            final BlockPos fk9 = new BlockPos(ayj8);
            if (xd.dimension.isHasSkyLight()) {
                if (fk9.getY() < xd.getSeaLevel()) {
                    continue;
                }
                if (!xd.canSeeSky(fk9)) {
                    continue;
                }
            }
            final DifficultyInstance ajj10 = xd.getCurrentDifficultyAt(fk9);
            if (!ajj10.isHarderThan(random5.nextFloat() * 3.0f)) {
                continue;
            }
            final ServerStatsCounter aan11 = ((ServerPlayer)ayj8).getStats();
            final int integer7 = Mth.clamp(aan11.getValue(Stats.CUSTOM.get(Stats.TIME_SINCE_REST)), 1, Integer.MAX_VALUE);
            final int integer8 = 24000;
            if (random5.nextInt(integer7) < 72000) {
                continue;
            }
            final BlockPos fk10 = fk9.above(20 + random5.nextInt(15)).east(-10 + random5.nextInt(21)).south(-10 + random5.nextInt(21));
            final BlockState byj15 = xd.getBlockState(fk10);
            final FluidState coj16 = xd.getFluidState(fk10);
            if (!NaturalSpawner.isValidEmptySpawnBlock(xd, fk10, byj15, coj16)) {
                continue;
            }
            SpawnGroupData alm17 = null;
            final int integer9 = 1 + random5.nextInt(ajj10.getDifficulty().getId() + 1);
            for (int integer10 = 0; integer10 < integer9; ++integer10) {
                final Phantom awy20 = EntityType.PHANTOM.create(xd);
                awy20.moveTo(fk10, 0.0f, 0.0f);
                alm17 = awy20.finalizeSpawn(xd, ajj10, MobSpawnType.NATURAL, alm17, null);
                xd.addFreshEntity(awy20);
            }
            integer6 += integer9;
        }
        return integer6;
    }
}
