| 1 | package net.minecraft.world;
|
| 2 |
|
| 3 | import com.google.common.collect.Sets;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Random;
|
| 6 | import java.util.Set;
|
| 7 | import net.minecraft.block.Block;
|
| 8 | import net.minecraft.entity.EntityLiving;
|
| 9 | import net.minecraft.entity.EntitySpawnPlacementRegistry;
|
| 10 | import net.minecraft.entity.EnumCreatureType;
|
| 11 | import net.minecraft.entity.IEntityLivingData;
|
| 12 | import net.minecraft.entity.player.EntityPlayer;
|
| 13 | import net.minecraft.init.Blocks;
|
| 14 | import net.minecraft.util.BlockPos;
|
| 15 | import net.minecraft.util.MathHelper;
|
| 16 | import net.minecraft.util.WeightedRandom;
|
| 17 | import net.minecraft.world.biome.BiomeGenBase;
|
| 18 | import net.minecraft.world.chunk.Chunk;
|
| 19 |
|
| 20 | public final class SpawnerAnimals
|
| 21 | {
|
| 22 | private static final int MOB_COUNT_DIV = (int)Math.pow(17.0D, 2.0D);
|
| 23 | private final Set<ChunkCoordIntPair> eligibleChunksForSpawning = Sets.<ChunkCoordIntPair>newHashSet();
|
| 24 |
|
| 25 | /**
|
| 26 | * adds all chunks within the spawn radius of the players to eligibleChunksForSpawning. pars: the world,
|
| 27 | * hostileCreatures, passiveCreatures. returns number of eligible chunks.
|
| 28 | */
|
| 29 | public int findChunksForSpawning(WorldServer worldServerIn, boolean spawnHostileMobs, boolean spawnPeacefulMobs, boolean p_77192_4_)
|
| 30 | {
|
| 31 | if (!spawnHostileMobs && !spawnPeacefulMobs)
|
| 32 | {
|
| 33 | return 0;
|
| 34 | }
|
| 35 | else
|
| 36 | {
|
| 37 | this.eligibleChunksForSpawning.clear();
|
| 38 | int i = 0;
|
| 39 |
|
| 40 | for (EntityPlayer entityplayer : worldServerIn.playerEntities)
|
| 41 | {
|
| 42 | if (!entityplayer.isSpectator())
|
| 43 | {
|
| 44 | int j = MathHelper.floor_double(entityplayer.posX / 16.0D);
|
| 45 | int k = MathHelper.floor_double(entityplayer.posZ / 16.0D);
|
| 46 | int l = 8;
|
| 47 |
|
| 48 | for (int i1 = -l; i1 <= l; ++i1)
|
| 49 | {
|
| 50 | for (int j1 = -l; j1 <= l; ++j1)
|
| 51 | {
|
| 52 | boolean flag = i1 == -l || i1 == l || j1 == -l || j1 == l;
|
| 53 | ChunkCoordIntPair chunkcoordintpair = new ChunkCoordIntPair(i1 + j, j1 + k);
|
| 54 |
|
| 55 | if (!this.eligibleChunksForSpawning.contains(chunkcoordintpair))
|
| 56 | {
|
| 57 | ++i;
|
| 58 |
|
| 59 | if (!flag && worldServerIn.getWorldBorder().contains(chunkcoordintpair))
|
| 60 | {
|
| 61 | this.eligibleChunksForSpawning.add(chunkcoordintpair);
|
| 62 | }
|
| 63 | }
|
| 64 | }
|
| 65 | }
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | int i4 = 0;
|
| 70 | BlockPos blockpos2 = worldServerIn.getSpawnPoint();
|
| 71 |
|
| 72 | for (EnumCreatureType enumcreaturetype : EnumCreatureType.values())
|
| 73 | {
|
| 74 | if ((!enumcreaturetype.getPeacefulCreature() || spawnPeacefulMobs) && (enumcreaturetype.getPeacefulCreature() || spawnHostileMobs) && (!enumcreaturetype.getAnimal() || p_77192_4_))
|
| 75 | {
|
| 76 | int j4 = worldServerIn.countEntities(enumcreaturetype.getCreatureClass());
|
| 77 | int k4 = enumcreaturetype.getMaxNumberOfCreature() * i / MOB_COUNT_DIV;
|
| 78 |
|
| 79 | if (j4 <= k4)
|
| 80 | {
|
| 81 | label374:
|
| 82 |
|
| 83 | for (ChunkCoordIntPair chunkcoordintpair1 : this.eligibleChunksForSpawning)
|
| 84 | {
|
| 85 | BlockPos blockpos = getRandomChunkPosition(worldServerIn, chunkcoordintpair1.chunkXPos, chunkcoordintpair1.chunkZPos);
|
| 86 | int k1 = blockpos.getX();
|
| 87 | int l1 = blockpos.getY();
|
| 88 | int i2 = blockpos.getZ();
|
| 89 | Block block = worldServerIn.getBlockState(blockpos).getBlock();
|
| 90 |
|
| 91 | if (!block.isNormalCube())
|
| 92 | {
|
| 93 | int j2 = 0;
|
| 94 |
|
| 95 | for (int k2 = 0; k2 < 3; ++k2)
|
| 96 | {
|
| 97 | int l2 = k1;
|
| 98 | int i3 = l1;
|
| 99 | int j3 = i2;
|
| 100 | int k3 = 6;
|
| 101 | BiomeGenBase.SpawnListEntry biomegenbase$spawnlistentry = null;
|
| 102 | IEntityLivingData ientitylivingdata = null;
|
| 103 |
|
| 104 | for (int l3 = 0; l3 < 4; ++l3)
|
| 105 | {
|
| 106 | l2 += worldServerIn.rand.nextInt(k3) - worldServerIn.rand.nextInt(k3);
|
| 107 | i3 += worldServerIn.rand.nextInt(1) - worldServerIn.rand.nextInt(1);
|
| 108 | j3 += worldServerIn.rand.nextInt(k3) - worldServerIn.rand.nextInt(k3);
|
| 109 | BlockPos blockpos1 = new BlockPos(l2, i3, j3);
|
| 110 | float f = (float)l2 + 0.5F;
|
| 111 | float f1 = (float)j3 + 0.5F;
|
| 112 |
|
| 113 | if (!worldServerIn.isAnyPlayerWithinRangeAt((double)f, (double)i3, (double)f1, 24.0D) && blockpos2.distanceSq((double)f, (double)i3, (double)f1) >= 576.0D)
|
| 114 | {
|
| 115 | if (biomegenbase$spawnlistentry == null)
|
| 116 | {
|
| 117 | biomegenbase$spawnlistentry = worldServerIn.getSpawnListEntryForTypeAt(enumcreaturetype, blockpos1);
|
| 118 |
|
| 119 | if (biomegenbase$spawnlistentry == null)
|
| 120 | {
|
| 121 | break;
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | if (worldServerIn.canCreatureTypeSpawnHere(enumcreaturetype, biomegenbase$spawnlistentry, blockpos1) && canCreatureTypeSpawnAtLocation(EntitySpawnPlacementRegistry.getPlacementForEntity(biomegenbase$spawnlistentry.entityClass), worldServerIn, blockpos1))
|
| 126 | {
|
| 127 | EntityLiving entityliving;
|
| 128 |
|
| 129 | try
|
| 130 | {
|
| 131 | entityliving = (EntityLiving)biomegenbase$spawnlistentry.entityClass.getConstructor(new Class[] {World.class}).newInstance(new Object[] {worldServerIn});
|
| 132 | }
|
| 133 | catch (Exception exception)
|
| 134 | {
|
| 135 | exception.printStackTrace();
|
| 136 | return i4;
|
| 137 | }
|
| 138 |
|
| 139 | entityliving.setLocationAndAngles((double)f, (double)i3, (double)f1, worldServerIn.rand.nextFloat() * 360.0F, 0.0F);
|
| 140 |
|
| 141 | if (entityliving.getCanSpawnHere() && entityliving.isNotColliding())
|
| 142 | {
|
| 143 | ientitylivingdata = entityliving.onInitialSpawn(worldServerIn.getDifficultyForLocation(new BlockPos(entityliving)), ientitylivingdata);
|
| 144 |
|
| 145 | if (entityliving.isNotColliding())
|
| 146 | {
|
| 147 | ++j2;
|
| 148 | worldServerIn.spawnEntityInWorld(entityliving);
|
| 149 | }
|
| 150 |
|
| 151 | if (j2 >= entityliving.getMaxSpawnedInChunk())
|
| 152 | {
|
| 153 | continue label374;
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | i4 += j2;
|
| 158 | }
|
| 159 | }
|
| 160 | }
|
| 161 | }
|
| 162 | }
|
| 163 | }
|
| 164 | }
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | return i4;
|
| 169 | }
|
| 170 | }
|
| 171 |
|
| 172 | protected static BlockPos getRandomChunkPosition(World worldIn, int x, int z)
|
| 173 | {
|
| 174 | Chunk chunk = worldIn.getChunkFromChunkCoords(x, z);
|
| 175 | int i = x * 16 + worldIn.rand.nextInt(16);
|
| 176 | int j = z * 16 + worldIn.rand.nextInt(16);
|
| 177 | int k = MathHelper.roundUp(chunk.getHeight(new BlockPos(i, 0, j)) + 1, 16);
|
| 178 | int l = worldIn.rand.nextInt(k > 0 ? k : chunk.getTopFilledSegment() + 16 - 1);
|
| 179 | return new BlockPos(i, l, j);
|
| 180 | }
|
| 181 |
|
| 182 | public static boolean canCreatureTypeSpawnAtLocation(EntityLiving.SpawnPlacementType spawnPlacementTypeIn, World worldIn, BlockPos pos)
|
| 183 | {
|
| 184 | if (!worldIn.getWorldBorder().contains(pos))
|
| 185 | {
|
| 186 | return false;
|
| 187 | }
|
| 188 | else
|
| 189 | {
|
| 190 | Block block = worldIn.getBlockState(pos).getBlock();
|
| 191 |
|
| 192 | if (spawnPlacementTypeIn == EntityLiving.SpawnPlacementType.IN_WATER)
|
| 193 | {
|
| 194 | return block.getMaterial().isLiquid() && worldIn.getBlockState(pos.down()).getBlock().getMaterial().isLiquid() && !worldIn.getBlockState(pos.up()).getBlock().isNormalCube();
|
| 195 | }
|
| 196 | else
|
| 197 | {
|
| 198 | BlockPos blockpos = pos.down();
|
| 199 |
|
| 200 | if (!World.doesBlockHaveSolidTopSurface(worldIn, blockpos))
|
| 201 | {
|
| 202 | return false;
|
| 203 | }
|
| 204 | else
|
| 205 | {
|
| 206 | Block block1 = worldIn.getBlockState(blockpos).getBlock();
|
| 207 | boolean flag = block1 != Blocks.bedrock && block1 != Blocks.barrier;
|
| 208 | return flag && !block.isNormalCube() && !block.getMaterial().isLiquid() && !worldIn.getBlockState(pos.up()).getBlock().isNormalCube();
|
| 209 | }
|
| 210 | }
|
| 211 | }
|
| 212 | }
|
| 213 |
|
| 214 | /**
|
| 215 | * Called during chunk generation to spawn initial creatures.
|
| 216 | */
|
| 217 | public static void performWorldGenSpawning(World worldIn, BiomeGenBase biomeIn, int p_77191_2_, int p_77191_3_, int p_77191_4_, int p_77191_5_, Random randomIn)
|
| 218 | {
|
| 219 | List<BiomeGenBase.SpawnListEntry> list = biomeIn.getSpawnableList(EnumCreatureType.CREATURE);
|
| 220 |
|
| 221 | if (!list.isEmpty())
|
| 222 | {
|
| 223 | while (randomIn.nextFloat() < biomeIn.getSpawningChance())
|
| 224 | {
|
| 225 | BiomeGenBase.SpawnListEntry biomegenbase$spawnlistentry = (BiomeGenBase.SpawnListEntry)WeightedRandom.getRandomItem(worldIn.rand, list);
|
| 226 | int i = biomegenbase$spawnlistentry.minGroupCount + randomIn.nextInt(1 + biomegenbase$spawnlistentry.maxGroupCount - biomegenbase$spawnlistentry.minGroupCount);
|
| 227 | IEntityLivingData ientitylivingdata = null;
|
| 228 | int j = p_77191_2_ + randomIn.nextInt(p_77191_4_);
|
| 229 | int k = p_77191_3_ + randomIn.nextInt(p_77191_5_);
|
| 230 | int l = j;
|
| 231 | int i1 = k;
|
| 232 |
|
| 233 | for (int j1 = 0; j1 < i; ++j1)
|
| 234 | {
|
| 235 | boolean flag = false;
|
| 236 |
|
| 237 | for (int k1 = 0; !flag && k1 < 4; ++k1)
|
| 238 | {
|
| 239 | BlockPos blockpos = worldIn.getTopSolidOrLiquidBlock(new BlockPos(j, 0, k));
|
| 240 |
|
| 241 | if (canCreatureTypeSpawnAtLocation(EntityLiving.SpawnPlacementType.ON_GROUND, worldIn, blockpos))
|
| 242 | {
|
| 243 | EntityLiving entityliving;
|
| 244 |
|
| 245 | try
|
| 246 | {
|
| 247 | entityliving = (EntityLiving)biomegenbase$spawnlistentry.entityClass.getConstructor(new Class[] {World.class}).newInstance(new Object[] {worldIn});
|
| 248 | }
|
| 249 | catch (Exception exception)
|
| 250 | {
|
| 251 | exception.printStackTrace();
|
| 252 | continue;
|
| 253 | }
|
| 254 |
|
| 255 | entityliving.setLocationAndAngles((double)((float)j + 0.5F), (double)blockpos.getY(), (double)((float)k + 0.5F), randomIn.nextFloat() * 360.0F, 0.0F);
|
| 256 | worldIn.spawnEntityInWorld(entityliving);
|
| 257 | ientitylivingdata = entityliving.onInitialSpawn(worldIn.getDifficultyForLocation(new BlockPos(entityliving)), ientitylivingdata);
|
| 258 | flag = true;
|
| 259 | }
|
| 260 |
|
| 261 | j += randomIn.nextInt(5) - randomIn.nextInt(5);
|
| 262 |
|
| 263 | for (k += randomIn.nextInt(5) - randomIn.nextInt(5); j < p_77191_2_ || j >= p_77191_2_ + p_77191_4_ || k < p_77191_3_ || k >= p_77191_3_ + p_77191_4_; k = i1 + randomIn.nextInt(5) - randomIn.nextInt(5))
|
| 264 | {
|
| 265 | j = l + randomIn.nextInt(5) - randomIn.nextInt(5);
|
| 266 | }
|
| 267 | }
|
| 268 | }
|
| 269 | }
|
| 270 | }
|
| 271 | }
|
| 272 | }
|
| 273 |
|