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.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import java.util.Iterator;
import java.util.Random;
import net.minecraft.world.entity.Entity;
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.monster.Phantom;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.NaturalSpawner;
import net.minecraft.world.entity.EntityType;
import net.minecraft.util.Mth;
import net.minecraft.stats.Stat;
import net.minecraft.stats.Stats;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.GameRules;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.CustomSpawner;

public class PhantomSpawner implements CustomSpawner {
    private int nextTick;
    
    @Override
    public int tick(final ServerLevel zd, final boolean boolean2, final boolean boolean3) {
        if (!boolean2) {
            return 0;
        }
        if (!zd.getGameRules().getBoolean(GameRules.RULE_DOINSOMNIA)) {
            return 0;
        }
        final Random random5 = zd.random;
        --this.nextTick;
        if (this.nextTick > 0) {
            return 0;
        }
        this.nextTick += (60 + random5.nextInt(60)) * 20;
        if (zd.getSkyDarken() < 5 && zd.dimensionType().hasSkyLight()) {
            return 0;
        }
        int integer6 = 0;
        for (final Player bec8 : zd.players()) {
            if (bec8.isSpectator()) {
                continue;
            }
            final BlockPos fu9 = bec8.blockPosition();
            if (zd.dimensionType().hasSkyLight()) {
                if (fu9.getY() < zd.getSeaLevel()) {
                    continue;
                }
                if (!zd.canSeeSky(fu9)) {
                    continue;
                }
            }
            final DifficultyInstance ane10 = zd.getCurrentDifficultyAt(fu9);
            if (!ane10.isHarderThan(random5.nextFloat() * 3.0f)) {
                continue;
            }
            final ServerStatsCounter acq11 = ((ServerPlayer)bec8).getStats();
            final int integer7 = Mth.clamp(acq11.getValue(Stats.CUSTOM.get(Stats.TIME_SINCE_REST)), 1, Integer.MAX_VALUE);
            final int integer8 = 24000;
            if (random5.nextInt(integer7) < 72000) {
                continue;
            }
            final BlockPos fu10 = fu9.above(20 + random5.nextInt(15)).east(-10 + random5.nextInt(21)).south(-10 + random5.nextInt(21));
            final BlockState cfj15 = zd.getBlockState(fu10);
            final FluidState cxa16 = zd.getFluidState(fu10);
            if (!NaturalSpawner.isValidEmptySpawnBlock(zd, fu10, cfj15, cxa16, EntityType.PHANTOM)) {
                continue;
            }
            SpawnGroupData apo17 = null;
            final int integer9 = 1 + random5.nextInt(ane10.getDifficulty().getId() + 1);
            for (int integer10 = 0; integer10 < integer9; ++integer10) {
                final Phantom bcd20 = EntityType.PHANTOM.create(zd);
                bcd20.moveTo(fu10, 0.0f, 0.0f);
                apo17 = bcd20.finalizeSpawn(zd, ane10, MobSpawnType.NATURAL, apo17, null);
                zd.addFreshEntity(bcd20);
            }
            integer6 += integer9;
        }
        return integer6;
    }
}
