Paper/src/main/java/net/minecraft/server/SpawnerCreature.java

256 Zeilen
12 KiB
Java

2011-06-22 17:55:04 +02:00
package net.minecraft.server;
2011-09-15 02:23:52 +02:00
import java.util.Collection;
2011-11-20 09:01:14 +01:00
import java.util.HashMap;
2011-06-22 17:55:04 +02:00
import java.util.Iterator;
import java.util.List;
2011-09-15 02:23:52 +02:00
import java.util.Random;
2011-06-22 17:55:04 +02:00
2012-02-10 06:43:12 +01:00
// CraftBukkit start
import org.bukkit.craftbukkit.util.LongHash;
import org.bukkit.craftbukkit.util.LongObjectHashMap;
2011-06-22 17:55:04 +02:00
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
2012-02-10 06:43:12 +01:00
// CraftBukkit end
2011-06-22 17:55:04 +02:00
public final class SpawnerCreature {
2013-07-01 13:03:00 +02:00
private LongObjectHashMap<Boolean> a = new LongObjectHashMap<Boolean>(); // CraftBukkit - HashMap -> LongObjectHashMap
public SpawnerCreature() {}
2011-06-22 17:55:04 +02:00
protected static ChunkPosition getRandomPosition(World world, int i, int j) {
2012-03-01 11:49:23 +01:00
Chunk chunk = world.getChunkAt(i, j);
int k = i * 16 + world.random.nextInt(16);
2012-07-29 09:33:13 +02:00
int l = j * 16 + world.random.nextInt(16);
2014-03-21 05:26:30 +01:00
int i1 = world.random.nextInt(chunk == null ? world.S() : chunk.h() + 16 - 1);
2011-06-22 17:55:04 +02:00
2012-07-29 09:33:13 +02:00
return new ChunkPosition(k, i1, l);
2011-06-22 17:55:04 +02:00
}
2013-07-01 13:03:00 +02:00
public int spawnEntities(WorldServer worldserver, boolean flag, boolean flag1, boolean flag2) {
2011-06-22 17:55:04 +02:00
if (!flag && !flag1) {
return 0;
} else {
2013-07-01 13:03:00 +02:00
this.a.clear();
2011-06-22 17:55:04 +02:00
int i;
int j;
2012-07-29 09:33:13 +02:00
for (i = 0; i < worldserver.players.size(); ++i) {
EntityHuman entityhuman = (EntityHuman) worldserver.players.get(i);
2011-06-22 17:55:04 +02:00
int k = MathHelper.floor(entityhuman.locX / 16.0D);
j = MathHelper.floor(entityhuman.locZ / 16.0D);
byte b0 = 8;
for (int l = -b0; l <= b0; ++l) {
for (int i1 = -b0; i1 <= b0; ++i1) {
boolean flag3 = l == -b0 || l == b0 || i1 == -b0 || i1 == b0;
2014-03-21 05:26:30 +01:00
// CraftBukkit start - use LongHash and LongObjectHashMap
long chunkCoords = LongHash.toLong(l + k, i1 + j);
2011-11-20 09:01:14 +01:00
if (!flag3) {
2013-07-01 13:03:00 +02:00
this.a.put(chunkCoords, false);
} else if (!this.a.containsKey(chunkCoords)) {
this.a.put(chunkCoords, true);
2011-11-20 09:01:14 +01:00
}
// CraftBukkit end
2011-06-22 17:55:04 +02:00
}
}
}
i = 0;
2012-07-29 09:33:13 +02:00
ChunkCoordinates chunkcoordinates = worldserver.getSpawn();
2011-06-22 17:55:04 +02:00
EnumCreatureType[] aenumcreaturetype = EnumCreatureType.values();
j = aenumcreaturetype.length;
for (int j1 = 0; j1 < j; ++j1) {
EnumCreatureType enumcreaturetype = aenumcreaturetype[j1];
2013-03-25 05:22:32 +01:00
// CraftBukkit start - Use per-world spawn limits
int limit = enumcreaturetype.b();
switch (enumcreaturetype) {
case MONSTER:
2012-07-29 09:33:13 +02:00
limit = worldserver.getWorld().getMonsterSpawnLimit();
break;
case CREATURE:
2012-07-29 09:33:13 +02:00
limit = worldserver.getWorld().getAnimalSpawnLimit();
break;
case WATER_CREATURE:
2012-07-29 09:33:13 +02:00
limit = worldserver.getWorld().getWaterAnimalSpawnLimit();
break;
case AMBIENT:
limit = worldserver.getWorld().getAmbientSpawnLimit();
break;
}
if (limit == 0) {
continue;
}
// CraftBukkit end
2013-07-01 13:03:00 +02:00
if ((!enumcreaturetype.d() || flag1) && (enumcreaturetype.d() || flag) && (!enumcreaturetype.e() || flag2) && worldserver.a(enumcreaturetype.a()) <= limit * this.a.size() / 256) { // CraftBukkit - use per-world limits
Iterator iterator = this.a.keySet().iterator();
2011-06-22 17:55:04 +02:00
label110:
while (iterator.hasNext()) {
2014-03-21 05:26:30 +01:00
// CraftBukkit start = use LongHash and LongObjectHashMap
long key = ((Long) iterator.next()).longValue();
2013-07-01 13:03:00 +02:00
if (!this.a.get(key)) {
ChunkPosition chunkposition = getRandomPosition(worldserver, LongHash.msw(key), LongHash.lsw(key));
// CraftBukkit end
2011-09-15 02:23:52 +02:00
int k1 = chunkposition.x;
int l1 = chunkposition.y;
int i2 = chunkposition.z;
2011-06-22 17:55:04 +02:00
2013-11-04 14:07:38 +01:00
if (!worldserver.getType(k1, l1, i2).r() && worldserver.getType(k1, l1, i2).getMaterial() == enumcreaturetype.c()) {
2011-09-15 02:23:52 +02:00
int j2 = 0;
2011-11-20 09:01:14 +01:00
int k2 = 0;
2011-06-22 17:55:04 +02:00
2011-11-20 09:01:14 +01:00
while (k2 < 3) {
2011-09-15 02:23:52 +02:00
int l2 = k1;
int i3 = l1;
2011-06-22 17:55:04 +02:00
int j3 = i2;
byte b1 = 6;
2011-11-20 09:01:14 +01:00
BiomeMeta biomemeta = null;
2013-07-01 13:03:00 +02:00
GroupDataEntity groupdataentity = null;
2011-11-20 09:01:14 +01:00
int k3 = 0;
while (true) {
if (k3 < 4) {
label103: {
2012-07-29 09:33:13 +02:00
l2 += worldserver.random.nextInt(b1) - worldserver.random.nextInt(b1);
i3 += worldserver.random.nextInt(1) - worldserver.random.nextInt(1);
j3 += worldserver.random.nextInt(b1) - worldserver.random.nextInt(b1);
if (a(enumcreaturetype, worldserver, l2, i3, j3)) {
2011-11-20 09:01:14 +01:00
float f = (float) l2 + 0.5F;
float f1 = (float) i3;
float f2 = (float) j3 + 0.5F;
2012-07-29 09:33:13 +02:00
if (worldserver.findNearbyPlayer((double) f, (double) f1, (double) f2, 24.0D) == null) {
2011-11-20 09:01:14 +01:00
float f3 = f - (float) chunkcoordinates.x;
float f4 = f1 - (float) chunkcoordinates.y;
float f5 = f2 - (float) chunkcoordinates.z;
float f6 = f3 * f3 + f4 * f4 + f5 * f5;
if (f6 >= 576.0F) {
if (biomemeta == null) {
2012-07-29 09:33:13 +02:00
biomemeta = worldserver.a(enumcreaturetype, l2, i3, j3);
2011-11-20 09:01:14 +01:00
if (biomemeta == null) {
break label103;
2011-11-20 09:01:14 +01:00
}
}
2013-07-01 13:03:00 +02:00
EntityInsentient entityinsentient;
2011-11-20 09:01:14 +01:00
try {
2013-07-01 13:03:00 +02:00
entityinsentient = (EntityInsentient) biomemeta.b.getConstructor(new Class[] { World.class}).newInstance(new Object[] { worldserver});
2011-11-20 09:01:14 +01:00
} catch (Exception exception) {
exception.printStackTrace();
return i;
}
2013-07-01 13:03:00 +02:00
entityinsentient.setPositionRotation((double) f, (double) f1, (double) f2, worldserver.random.nextFloat() * 360.0F, 0.0F);
if (entityinsentient.canSpawn()) {
2011-11-20 09:01:14 +01:00
++j2;
2013-07-01 13:03:00 +02:00
// CraftBukkit start - Added a reason for spawning this creature, moved entityinsentient.a(groupdataentity) up
groupdataentity = entityinsentient.a(groupdataentity);
worldserver.addEntity(entityinsentient, SpawnReason.NATURAL);
// CraftBukkit end
2014-03-21 05:26:30 +01:00
if (j2 >= entityinsentient.bB()) {
continue label110;
2011-11-20 09:01:14 +01:00
}
}
i += j2;
2011-06-22 17:55:04 +02:00
}
}
}
2011-11-20 09:01:14 +01:00
++k3;
continue;
2011-06-22 17:55:04 +02:00
}
}
2011-11-20 09:01:14 +01:00
++k2;
break;
2011-06-22 17:55:04 +02:00
}
}
}
}
}
}
}
return i;
}
}
2012-03-01 11:49:23 +01:00
public static boolean a(EnumCreatureType enumcreaturetype, World world, int i, int j, int k) {
if (enumcreaturetype.c() == Material.WATER) {
2013-11-04 14:07:38 +01:00
return world.getType(i, j, k).getMaterial().isLiquid() && world.getType(i, j - 1, k).getMaterial().isLiquid() && !world.getType(i, j + 1, k).r();
} else if (!World.a((IBlockAccess) world, i, j - 1, k)) {
2012-07-29 09:33:13 +02:00
return false;
2012-03-01 11:49:23 +01:00
} else {
2013-11-04 14:07:38 +01:00
Block block = world.getType(i, j - 1, k);
2012-03-01 11:49:23 +01:00
2013-11-04 14:07:38 +01:00
return block != Blocks.BEDROCK && !world.getType(i, j, k).r() && !world.getType(i, j, k).getMaterial().isLiquid() && !world.getType(i, j + 1, k).r();
2012-03-01 11:49:23 +01:00
}
2011-06-22 17:55:04 +02:00
}
2011-09-15 02:23:52 +02:00
public static void a(World world, BiomeBase biomebase, int i, int j, int k, int l, Random random) {
2012-01-12 16:27:39 +01:00
List list = biomebase.getMobs(EnumCreatureType.CREATURE);
2011-09-15 02:23:52 +02:00
if (!list.isEmpty()) {
2013-11-04 14:07:38 +01:00
while (random.nextFloat() < biomebase.g()) {
2011-09-15 02:23:52 +02:00
BiomeMeta biomemeta = (BiomeMeta) WeightedRandom.a(world.random, (Collection) list);
2013-07-01 13:03:00 +02:00
GroupDataEntity groupdataentity = null;
2012-07-29 09:33:13 +02:00
int i1 = biomemeta.c + random.nextInt(1 + biomemeta.d - biomemeta.c);
2011-09-15 02:23:52 +02:00
int j1 = i + random.nextInt(k);
int k1 = j + random.nextInt(l);
int l1 = j1;
int i2 = k1;
for (int j2 = 0; j2 < i1; ++j2) {
boolean flag = false;
for (int k2 = 0; !flag && k2 < 4; ++k2) {
int l2 = world.i(j1, k1);
2011-09-15 02:23:52 +02:00
if (a(EnumCreatureType.CREATURE, world, j1, l2, k1)) {
float f = (float) j1 + 0.5F;
float f1 = (float) l2;
float f2 = (float) k1 + 0.5F;
2013-07-01 13:03:00 +02:00
EntityInsentient entityinsentient;
2011-09-15 02:23:52 +02:00
try {
2013-07-01 13:03:00 +02:00
entityinsentient = (EntityInsentient) biomemeta.b.getConstructor(new Class[] { World.class}).newInstance(new Object[] { world});
2011-09-15 02:23:52 +02:00
} catch (Exception exception) {
exception.printStackTrace();
continue;
}
2013-07-01 13:03:00 +02:00
entityinsentient.setPositionRotation((double) f, (double) f1, (double) f2, random.nextFloat() * 360.0F, 0.0F);
// CraftBukkit start - Added a reason for spawning this creature, moved entityinsentient.a(groupdataentity) up
groupdataentity = entityinsentient.a(groupdataentity);
world.addEntity(entityinsentient, SpawnReason.CHUNK_GEN);
// CraftBukkit end
2011-09-15 02:23:52 +02:00
flag = true;
}
j1 += random.nextInt(5) - random.nextInt(5);
for (k1 += random.nextInt(5) - random.nextInt(5); j1 < i || j1 >= i + k || k1 < j || k1 >= j + k; k1 = i2 + random.nextInt(5) - random.nextInt(5)) {
j1 = l1 + random.nextInt(5) - random.nextInt(5);
}
}
}
}
}
}
}