Search in sources :

Example 6 with EnumCreatureType

use of net.minecraft.entity.EnumCreatureType in project Cavern2 by kegare.

the class CaveEntitySpawner method findChunksForSpawning.

public int findChunksForSpawning(WorldServer world, boolean spawnHostileMobs, boolean spawnPeacefulMobs, boolean spawnOnSetTickRate) {
    if (!spawnHostileMobs && !spawnPeacefulMobs) {
        return 0;
    } else {
        eligibleChunksForSpawning.clear();
        int playerCount = 0;
        double playerHeight = 0.0D;
        for (EntityPlayer player : world.playerEntities) {
            if (!player.isSpectator()) {
                int j = MathHelper.floor(player.posX / 16.0D);
                int k = MathHelper.floor(player.posZ / 16.0D);
                int range = 8;
                for (int i1 = -range; i1 <= range; ++i1) {
                    for (int j1 = -range; j1 <= range; ++j1) {
                        boolean flag = i1 == -range || i1 == range || j1 == -range || j1 == range;
                        ChunkPos pos = new ChunkPos(i1 + j, j1 + k);
                        if (!eligibleChunksForSpawning.contains(pos)) {
                            ++playerCount;
                            playerHeight += player.posY;
                            if (!flag && world.getWorldBorder().contains(pos)) {
                                PlayerChunkMapEntry entry = world.getPlayerChunkMap().getEntry(pos.x, pos.z);
                                if (entry != null && entry.isSentToPlayers()) {
                                    eligibleChunksForSpawning.add(pos);
                                }
                            }
                        }
                    }
                }
            }
        }
        int playerY = playerHeight > 0.0D ? MathHelper.ceil(playerHeight / playerCount) : 50;
        int totalCount = 0;
        BlockPos spawnPos = world.getSpawnPoint();
        for (EnumCreatureType type : EnumCreatureType.values()) {
            int maxNumber = getMaxNumberOfCreature(world, spawnHostileMobs, spawnPeacefulMobs, spawnOnSetTickRate, type);
            double range = getSpawnRange(world, spawnHostileMobs, spawnPeacefulMobs, spawnOnSetTickRate, type);
            if (maxNumber > 0 && canSpawnCreature(world, spawnHostileMobs, spawnPeacefulMobs, spawnOnSetTickRate, type)) {
                int max = maxNumber * playerCount / MOB_COUNT_DIV;
                if (world.countEntities(type, true) <= max) {
                    List<ChunkPos> shuffled = Lists.newArrayList(eligibleChunksForSpawning);
                    Collections.shuffle(shuffled);
                    MutableBlockPos pos = new MutableBlockPos();
                    outside: for (ChunkPos chunkpos : shuffled) {
                        BlockPos blockpos = getRandomPosition(world, chunkpos.x, playerY, chunkpos.z);
                        int originX = blockpos.getX();
                        int originY = blockpos.getY();
                        int originZ = blockpos.getZ();
                        IBlockState state = world.getBlockState(blockpos);
                        if (!state.isNormalCube()) {
                            int mobCount = 0;
                            for (int l = 0; l < 3; ++l) {
                                int x = originX;
                                int y = originY;
                                int z = originZ;
                                int n = 6;
                                Biome.SpawnListEntry entry = null;
                                IEntityLivingData data = null;
                                int f = MathHelper.ceil(Math.random() * 4.0D);
                                for (int m = 0; m < f; ++m) {
                                    x += world.rand.nextInt(n) - world.rand.nextInt(n);
                                    y += world.rand.nextInt(1) - world.rand.nextInt(1);
                                    z += world.rand.nextInt(n) - world.rand.nextInt(n);
                                    pos.setPos(x, y, z);
                                    float posX = x + 0.5F;
                                    float posZ = z + 0.5F;
                                    if (!world.isAnyPlayerWithinRangeAt(posX, y, posZ, range) && spawnPos.distanceSq(posX, y, posZ) >= range * range) {
                                        if (entry == null) {
                                            entry = world.getSpawnListEntryForTypeAt(type, pos);
                                            if (entry == null) {
                                                break;
                                            }
                                        }
                                        if (world.canCreatureTypeSpawnHere(type, entry, pos) && WorldEntitySpawner.canCreatureTypeSpawnAtLocation(EntitySpawnPlacementRegistry.getPlacementForEntity(entry.entityClass), world, pos)) {
                                            EntityLiving entity;
                                            try {
                                                entity = entry.newInstance(world);
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                                return totalCount;
                                            }
                                            entity.setLocationAndAngles(posX, y, posZ, world.rand.nextFloat() * 360.0F, 0.0F);
                                            Result canSpawn = ForgeEventFactory.canEntitySpawn(entity, world, posX, y, posZ, null);
                                            if (canSpawn == Result.ALLOW || canSpawn == Result.DEFAULT && entity.getCanSpawnHere() && entity.isNotColliding()) {
                                                if (!ForgeEventFactory.doSpecialSpawn(entity, world, posX, y, posZ, null)) {
                                                    data = entity.onInitialSpawn(world.getDifficultyForLocation(entity.getPosition()), data);
                                                }
                                                if (entity.isNotColliding()) {
                                                    ++mobCount;
                                                    world.spawnEntity(entity);
                                                } else {
                                                    entity.setDead();
                                                }
                                                if (mobCount >= ForgeEventFactory.getMaxSpawnPackSize(entity)) {
                                                    continue outside;
                                                }
                                            }
                                            totalCount += mobCount;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return totalCount;
    }
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) EnumCreatureType(net.minecraft.entity.EnumCreatureType) EntityLiving(net.minecraft.entity.EntityLiving) IEntityLivingData(net.minecraft.entity.IEntityLivingData) PlayerChunkMapEntry(net.minecraft.server.management.PlayerChunkMapEntry) Result(net.minecraftforge.fml.common.eventhandler.Event.Result) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ChunkPos(net.minecraft.util.math.ChunkPos) BlockPos(net.minecraft.util.math.BlockPos) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos)

Aggregations

EnumCreatureType (net.minecraft.entity.EnumCreatureType)6 IBlockState (net.minecraft.block.state.IBlockState)3 EntityLiving (net.minecraft.entity.EntityLiving)3 IEntityLivingData (net.minecraft.entity.IEntityLivingData)3 EntityPlayer (net.minecraft.entity.player.EntityPlayer)3 PlayerChunkMapEntry (net.minecraft.server.management.PlayerChunkMapEntry)3 BlockPos (net.minecraft.util.math.BlockPos)3 ChunkPos (net.minecraft.util.math.ChunkPos)3 Biome (net.minecraft.world.biome.Biome)2 IConfigObj (biomesoplenty.api.config.IConfigObj)1 BOPClimates (biomesoplenty.api.enums.BOPClimates)1 Vector3d (com.flowpowered.math.vector.Vector3d)1 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 BiomeTweakEvent (me.superckl.api.biometweaker.event.BiomeTweakEvent)1 Property (me.superckl.api.biometweaker.property.Property)1 BiomeTweakerBiome (me.superckl.biometweaker.common.world.biome.BiomeTweakerBiome)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 MutableBlockPos (net.minecraft.util.math.BlockPos.MutableBlockPos)1 World (net.minecraft.world.World)1