package net.minecraft.world.entity.npc;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.SpawnGroupData;
import net.minecraft.world.entity.MobSpawnType;
import java.util.List;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.entity.animal.Cat;
import net.minecraft.world.entity.ai.village.poi.PoiManager;
import net.minecraft.world.entity.ai.village.poi.PoiType;
import java.util.Random;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.levelgen.feature.Feature;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.NaturalSpawner;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.SpawnPlacements;
import net.minecraft.world.entity.Entity;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.GameRules;
import net.minecraft.server.level.ServerLevel;

public class CatSpawner {
    private int nextTick;
    
    public int tick(final ServerLevel xd, final boolean boolean2, final boolean boolean3) {
        if (!boolean3 || !xd.getGameRules().getBoolean(GameRules.RULE_DOMOBSPAWNING)) {
            return 0;
        }
        --this.nextTick;
        if (this.nextTick > 0) {
            return 0;
        }
        this.nextTick = 1200;
        final Player ayj5 = xd.getRandomPlayer();
        if (ayj5 == null) {
            return 0;
        }
        final Random random6 = xd.random;
        final int integer7 = (8 + random6.nextInt(24)) * (random6.nextBoolean() ? -1 : 1);
        final int integer8 = (8 + random6.nextInt(24)) * (random6.nextBoolean() ? -1 : 1);
        final BlockPos fk9 = new BlockPos(ayj5).offset(integer7, 0, integer8);
        if (!xd.hasChunksAt(fk9.getX() - 10, fk9.getY() - 10, fk9.getZ() - 10, fk9.getX() + 10, fk9.getY() + 10, fk9.getZ() + 10)) {
            return 0;
        }
        if (NaturalSpawner.isSpawnPositionOk(SpawnPlacements.Type.ON_GROUND, xd, fk9, EntityType.CAT)) {
            if (xd.closeToVillage(fk9, 2)) {
                return this.spawnInVillage(xd, fk9);
            }
            if (Feature.SWAMP_HUT.isInsideFeature(xd, fk9)) {
                return this.spawnInHut(xd, fk9);
            }
        }
        return 0;
    }
    
    private int spawnInVillage(final ServerLevel xd, final BlockPos fk) {
        final int integer4 = 48;
        if (xd.getPoiManager().getCountInRange(PoiType.HOME.getPredicate(), fk, 48, PoiManager.Occupancy.IS_OCCUPIED) > 4L) {
            final List<Cat> list5 = xd.<Cat>getEntitiesOfClass(Cat.class, new AABB(fk).inflate(48.0, 8.0, 48.0));
            if (list5.size() < 5) {
                return this.spawnCat(fk, xd);
            }
        }
        return 0;
    }
    
    private int spawnInHut(final Level bjw, final BlockPos fk) {
        final int integer4 = 16;
        final List<Cat> list5 = bjw.<Cat>getEntitiesOfClass(Cat.class, new AABB(fk).inflate(16.0, 8.0, 16.0));
        if (list5.size() < 1) {
            return this.spawnCat(fk, bjw);
        }
        return 0;
    }
    
    private int spawnCat(final BlockPos fk, final Level bjw) {
        final Cat ate4 = EntityType.CAT.create(bjw);
        if (ate4 == null) {
            return 0;
        }
        ate4.finalizeSpawn(bjw, bjw.getCurrentDifficultyAt(fk), MobSpawnType.NATURAL, null, null);
        ate4.moveTo(fk, 0.0f, 0.0f);
        bjw.addFreshEntity(ate4);
        return 1;
    }
}
