From 79337d3aaba12e48d0c20b64eb2b83ed1111ff17 Mon Sep 17 00:00:00 2001 From: EvilSeph Date: Tue, 21 Jun 2011 16:20:11 -0400 Subject: [PATCH] Added SpawnReasons to CreatureSpawn events. Thanks winsock! --- .../java/net/minecraft/server/EntityEgg.java | 3 +- .../java/net/minecraft/server/EntityPig.java | 6 +- .../net/minecraft/server/SpawnerCreature.java | 255 ++++++++++++++++++ .../server/TileEntityMobSpawner.java | 6 +- src/main/java/net/minecraft/server/World.java | 10 +- .../org/bukkit/craftbukkit/CraftWorld.java | 3 +- .../craftbukkit/event/CraftEventFactory.java | 5 +- 7 files changed, 279 insertions(+), 9 deletions(-) create mode 100644 src/main/java/net/minecraft/server/SpawnerCreature.java diff --git a/src/main/java/net/minecraft/server/EntityEgg.java b/src/main/java/net/minecraft/server/EntityEgg.java index 10b03af01e..fa2caeb059 100644 --- a/src/main/java/net/minecraft/server/EntityEgg.java +++ b/src/main/java/net/minecraft/server/EntityEgg.java @@ -10,6 +10,7 @@ import org.bukkit.entity.Projectile; import org.bukkit.craftbukkit.CraftServer; import org.bukkit.event.entity.EntityDamageByProjectileEvent; import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; import org.bukkit.event.entity.EntityDamageEvent.DamageCause; import org.bukkit.event.player.PlayerEggThrowEvent; // CraftBukkit end @@ -259,7 +260,7 @@ public class EntityEgg extends Entity { boolean isAnimal = entity instanceof EntityAnimal || entity instanceof EntityWaterAnimal; if ((isAnimal && this.world.allowAnimals) || (!isAnimal && this.world.allowMonsters)) { entity.setPositionRotation(this.locX, this.locY, this.locZ, this.yaw, 0.0F); - this.world.addEntity(entity); + this.world.addEntity(entity, SpawnReason.EGG); } // CraftBukkit end } diff --git a/src/main/java/net/minecraft/server/EntityPig.java b/src/main/java/net/minecraft/server/EntityPig.java index 62853e3a5b..9238a69914 100644 --- a/src/main/java/net/minecraft/server/EntityPig.java +++ b/src/main/java/net/minecraft/server/EntityPig.java @@ -2,8 +2,7 @@ package net.minecraft.server; // CraftBukkit start import org.bukkit.craftbukkit.CraftServer; -import org.bukkit.craftbukkit.event.CraftEventFactory; -import org.bukkit.event.entity.EntityCombustEvent; +import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; import org.bukkit.event.entity.PigZapEvent; // CraftBukkit end @@ -83,7 +82,8 @@ public class EntityPig extends EntityAnimal { // CraftBukkit end entitypigzombie.setPositionRotation(this.locX, this.locY, this.locZ, this.yaw, this.pitch); - this.world.addEntity(entitypigzombie); + // CraftBukkit - added a reason for spawning this creature + this.world.addEntity(entitypigzombie, SpawnReason.LIGHTNING); this.die(); } } diff --git a/src/main/java/net/minecraft/server/SpawnerCreature.java b/src/main/java/net/minecraft/server/SpawnerCreature.java new file mode 100644 index 0000000000..7e04139609 --- /dev/null +++ b/src/main/java/net/minecraft/server/SpawnerCreature.java @@ -0,0 +1,255 @@ +package net.minecraft.server; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +// CraftBukkit +import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; + +public final class SpawnerCreature { + + private static Set b = new HashSet(); + protected static final Class[] a = new Class[] { EntitySpider.class, EntityZombie.class, EntitySkeleton.class}; + + public SpawnerCreature() {} + + protected static ChunkPosition a(World world, int i, int j) { + int k = i + world.random.nextInt(16); + int l = world.random.nextInt(128); + int i1 = j + world.random.nextInt(16); + + return new ChunkPosition(k, l, i1); + } + + public static final int spawnEntities(World world, boolean flag, boolean flag1) { + if (!flag && !flag1) { + return 0; + } else { + b.clear(); + + int i; + int j; + + for (i = 0; i < world.players.size(); ++i) { + EntityHuman entityhuman = (EntityHuman) world.players.get(i); + 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) { + b.add(new ChunkCoordIntPair(l + k, i1 + j)); + } + } + } + + i = 0; + ChunkCoordinates chunkcoordinates = world.getSpawn(); + EnumCreatureType[] aenumcreaturetype = EnumCreatureType.values(); + + j = aenumcreaturetype.length; + + for (int j1 = 0; j1 < j; ++j1) { + EnumCreatureType enumcreaturetype = aenumcreaturetype[j1]; + + if ((!enumcreaturetype.d() || flag1) && (enumcreaturetype.d() || flag) && world.a(enumcreaturetype.a()) <= enumcreaturetype.b() * b.size() / 256) { + Iterator iterator = b.iterator(); + + label113: + while (iterator.hasNext()) { + ChunkCoordIntPair chunkcoordintpair = (ChunkCoordIntPair) iterator.next(); + BiomeBase biomebase = world.getWorldChunkManager().a(chunkcoordintpair); + List list = biomebase.a(enumcreaturetype); + + if (list != null && !list.isEmpty()) { + int k1 = 0; + + BiomeMeta biomemeta; + + for (Iterator iterator1 = list.iterator(); iterator1.hasNext(); k1 += biomemeta.b) { + biomemeta = (BiomeMeta) iterator1.next(); + } + + int l1 = world.random.nextInt(k1); + + biomemeta = (BiomeMeta) list.get(0); + Iterator iterator2 = list.iterator(); + + while (iterator2.hasNext()) { + BiomeMeta biomemeta1 = (BiomeMeta) iterator2.next(); + + l1 -= biomemeta1.b; + if (l1 < 0) { + biomemeta = biomemeta1; + break; + } + } + + ChunkPosition chunkposition = a(world, chunkcoordintpair.x * 16, chunkcoordintpair.z * 16); + int i2 = chunkposition.x; + int j2 = chunkposition.y; + int k2 = chunkposition.z; + + if (!world.d(i2, j2, k2) && world.getMaterial(i2, j2, k2) == enumcreaturetype.c()) { + int l2 = 0; + + for (int i3 = 0; i3 < 3; ++i3) { + int j3 = i2; + int k3 = j2; + int l3 = k2; + byte b1 = 6; + + for (int i4 = 0; i4 < 4; ++i4) { + j3 += world.random.nextInt(b1) - world.random.nextInt(b1); + k3 += world.random.nextInt(1) - world.random.nextInt(1); + l3 += world.random.nextInt(b1) - world.random.nextInt(b1); + if (a(enumcreaturetype, world, j3, k3, l3)) { + float f = (float) j3 + 0.5F; + float f1 = (float) k3; + float f2 = (float) l3 + 0.5F; + + if (world.a((double) f, (double) f1, (double) f2, 24.0D) == null) { + 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) { + EntityLiving entityliving; + + try { + entityliving = (EntityLiving) biomemeta.a.getConstructor(new Class[] { World.class}).newInstance(new Object[] { world}); + } catch (Exception exception) { + exception.printStackTrace(); + return i; + } + + entityliving.setPositionRotation((double) f, (double) f1, (double) f2, world.random.nextFloat() * 360.0F, 0.0F); + if (entityliving.d()) { + ++l2; + // CraftBukkit - added a reason for spawning this creature + world.addEntity(entityliving, SpawnReason.NATURAL); + a(entityliving, world, f, f1, f2); + if (l2 >= entityliving.l()) { + continue label113; + } + } + + i += l2; + } + } + } + } + } + } + } + } + } + } + + return i; + } + } + + private static boolean a(EnumCreatureType enumcreaturetype, World world, int i, int j, int k) { + return enumcreaturetype.c() == Material.WATER ? world.getMaterial(i, j, k).isLiquid() && !world.d(i, j + 1, k) : world.d(i, j - 1, k) && !world.d(i, j, k) && !world.getMaterial(i, j, k).isLiquid() && !world.d(i, j + 1, k); + } + + private static void a(EntityLiving entityliving, World world, float f, float f1, float f2) { + if (entityliving instanceof EntitySpider && world.random.nextInt(100) == 0) { + EntitySkeleton entityskeleton = new EntitySkeleton(world); + + entityskeleton.setPositionRotation((double) f, (double) f1, (double) f2, entityliving.yaw, 0.0F); + // CraftBukkit - added a reason for spawning this creature + world.addEntity(entityskeleton, SpawnReason.NATURAL); + entityskeleton.mount(entityliving); + } else if (entityliving instanceof EntitySheep) { + ((EntitySheep) entityliving).setColor(EntitySheep.a(world.random)); + } + } + + public static boolean a(World world, List list) { + boolean flag = false; + Pathfinder pathfinder = new Pathfinder(world); + Iterator iterator = list.iterator(); + + while (iterator.hasNext()) { + EntityHuman entityhuman = (EntityHuman) iterator.next(); + Class[] aclass = a; + + if (aclass != null && aclass.length != 0) { + boolean flag1 = false; + + for (int i = 0; i < 20 && !flag1; ++i) { + int j = MathHelper.floor(entityhuman.locX) + world.random.nextInt(32) - world.random.nextInt(32); + int k = MathHelper.floor(entityhuman.locZ) + world.random.nextInt(32) - world.random.nextInt(32); + int l = MathHelper.floor(entityhuman.locY) + world.random.nextInt(16) - world.random.nextInt(16); + + if (l < 1) { + l = 1; + } else if (l > 128) { + l = 128; + } + + int i1 = world.random.nextInt(aclass.length); + + int j1; + + for (j1 = l; j1 > 2 && !world.d(j, j1 - 1, k); --j1) { + ; + } + + while (!a(EnumCreatureType.MONSTER, world, j, j1, k) && j1 < l + 16 && j1 < 128) { + ++j1; + } + + if (j1 < l + 16 && j1 < 128) { + float f = (float) j + 0.5F; + float f1 = (float) j1; + float f2 = (float) k + 0.5F; + + EntityLiving entityliving; + + try { + entityliving = (EntityLiving) aclass[i1].getConstructor(new Class[] { World.class}).newInstance(new Object[] { world}); + } catch (Exception exception) { + exception.printStackTrace(); + return flag; + } + + entityliving.setPositionRotation((double) f, (double) f1, (double) f2, world.random.nextFloat() * 360.0F, 0.0F); + if (entityliving.d()) { + PathEntity pathentity = pathfinder.a(entityliving, entityhuman, 32.0F); + + if (pathentity != null && pathentity.a > 1) { + PathPoint pathpoint = pathentity.c(); + + if (Math.abs((double) pathpoint.a - entityhuman.locX) < 1.5D && Math.abs((double) pathpoint.c - entityhuman.locZ) < 1.5D && Math.abs((double) pathpoint.b - entityhuman.locY) < 1.5D) { + ChunkCoordinates chunkcoordinates = BlockBed.g(world, MathHelper.floor(entityhuman.locX), MathHelper.floor(entityhuman.locY), MathHelper.floor(entityhuman.locZ), 1); + + if (chunkcoordinates == null) { + chunkcoordinates = new ChunkCoordinates(j, j1 + 1, k); + } + + entityliving.setPositionRotation((double) ((float) chunkcoordinates.x + 0.5F), (double) chunkcoordinates.y, (double) ((float) chunkcoordinates.z + 0.5F), 0.0F, 0.0F); + // CraftBukkit - added a reason for spawning this creature + world.addEntity(entityliving, SpawnReason.BED); + a(entityliving, world, (float) chunkcoordinates.x + 0.5F, (float) chunkcoordinates.y, (float) chunkcoordinates.z + 0.5F); + entityhuman.a(true, false, false); + entityliving.P(); + flag = true; + flag1 = true; + } + } + } + } + } + } + } + + return flag; + } +} \ No newline at end of file diff --git a/src/main/java/net/minecraft/server/TileEntityMobSpawner.java b/src/main/java/net/minecraft/server/TileEntityMobSpawner.java index 153cae6c67..72fc8b1f26 100644 --- a/src/main/java/net/minecraft/server/TileEntityMobSpawner.java +++ b/src/main/java/net/minecraft/server/TileEntityMobSpawner.java @@ -1,5 +1,8 @@ package net.minecraft.server; +// CraftBukkit +import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; + public class TileEntityMobSpawner extends TileEntity { public int spawnDelay = -1; @@ -70,7 +73,8 @@ public class TileEntityMobSpawner extends TileEntity { entityliving.setPositionRotation(d3, d4, d5, this.world.random.nextFloat() * 360.0F, 0.0F); if (entityliving.d()) { - this.world.addEntity(entityliving); + // CraftBukkit - added a reason for spawning this creature + this.world.addEntity(entityliving, SpawnReason.SPAWNER); for (int k = 0; k < 20; ++k) { d0 = (double) this.e + 0.5D + ((double) this.world.random.nextFloat() - 0.5D) * 2.0D; diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java index b9750a8701..59b088f7d7 100644 --- a/src/main/java/net/minecraft/server/World.java +++ b/src/main/java/net/minecraft/server/World.java @@ -21,6 +21,7 @@ import org.bukkit.event.block.BlockPhysicsEvent; import org.bukkit.event.block.SnowFormEvent; import org.bukkit.event.block.BlockFormEvent; import org.bukkit.event.entity.CreatureSpawnEvent; +import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; import org.bukkit.event.entity.ItemSpawnEvent; import org.bukkit.event.weather.WeatherChangeEvent; import org.bukkit.event.weather.ThunderChangeEvent; @@ -824,7 +825,14 @@ public class World implements IBlockAccess { return true; } + // CraftBukkit start - used for entities other than creatures public boolean addEntity(Entity entity) { + return addEntity(entity, SpawnReason.CUSTOM); // Set reason as Custom by default + } + + + public boolean addEntity(Entity entity, SpawnReason spawnReason) { // Changed signature, added SpawnReason + // CraftBukkit end int i = MathHelper.floor(entity.locX / 16.0D); int j = MathHelper.floor(entity.locZ / 16.0D); boolean flag = false; @@ -835,7 +843,7 @@ public class World implements IBlockAccess { // CraftBukkit start if (entity instanceof EntityLiving && !(entity instanceof EntityPlayer)) { - CreatureSpawnEvent event = CraftEventFactory.callCreatureSpawnEvent((EntityLiving) entity); + CreatureSpawnEvent event = CraftEventFactory.callCreatureSpawnEvent((EntityLiving) entity, spawnReason); if (event.isCancelled()) { return false; diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java index 4f08d39483..d019e00b7b 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -12,6 +12,7 @@ import net.minecraft.server.*; import org.bukkit.entity.Arrow; import org.bukkit.Effect; +import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; import org.bukkit.event.weather.WeatherChangeEvent; import org.bukkit.event.weather.ThunderChangeEvent; import org.bukkit.event.world.SpawnChangeEvent; @@ -346,7 +347,7 @@ public class CraftWorld implements World { EntityLiving entityCreature = (EntityLiving) EntityTypes.a(creatureType.getName(), world); entityCreature.setPosition(loc.getX(), loc.getY(), loc.getZ()); creature = (LivingEntity) CraftEntity.getEntity(server, entityCreature); - world.addEntity(entityCreature); + world.addEntity(entityCreature, SpawnReason.CUSTOM); } catch (Exception e) { // if we fail, for any reason, return null. creature = null; diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java index 474cd3f460..bb6baac87d 100644 --- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java @@ -41,6 +41,7 @@ import org.bukkit.event.block.Action; import org.bukkit.event.block.BlockDamageEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.CreatureSpawnEvent; +import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; import org.bukkit.event.entity.EntityTameEvent; import org.bukkit.event.entity.ItemSpawnEvent; import org.bukkit.event.player.PlayerBucketEmptyEvent; @@ -190,7 +191,7 @@ public class CraftEventFactory { /** * CreatureSpawnEvent */ - public static CreatureSpawnEvent callCreatureSpawnEvent(EntityLiving entityliving) { + public static CreatureSpawnEvent callCreatureSpawnEvent(EntityLiving entityliving, SpawnReason spawnReason) { org.bukkit.entity.Entity entity = entityliving.getBukkitEntity(); CraftServer craftServer = (CraftServer) entity.getServer(); @@ -229,7 +230,7 @@ public class CraftEventFactory { type = CreatureType.MONSTER; } - CreatureSpawnEvent event = new CreatureSpawnEvent(entity, type, entity.getLocation()); + CreatureSpawnEvent event = new CreatureSpawnEvent(entity, type, entity.getLocation(), spawnReason); craftServer.getPluginManager().callEvent(event); return event; }