SPIGOT-4534: CreatureSpawnEvent not being called for CHUNK_GEN

This commit is contained in:
md_5 2018-12-17 12:30:16 +11:00
parent b446cb5d74
commit 38cf676e32
3 changed files with 100 additions and 71 deletions

View file

@ -1,15 +1,20 @@
--- a/net/minecraft/server/Chunk.java --- a/net/minecraft/server/Chunk.java
+++ b/net/minecraft/server/Chunk.java +++ b/net/minecraft/server/Chunk.java
@@ -22,6 +22,8 @@ @@ -22,6 +22,13 @@
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
+import com.google.common.collect.Lists; // CraftBukkit +// CraftBukkit start
+import com.google.common.collect.Lists;
+import java.util.LinkedList;
+import org.bukkit.craftbukkit.event.CraftEventFactory;
+import org.bukkit.event.entity.CreatureSpawnEvent;
+// CraftBukkit end
+ +
public class Chunk implements IChunkAccess { public class Chunk implements IChunkAccess {
private static final Logger d = LogManager.getLogger(); private static final Logger d = LogManager.getLogger();
@@ -57,6 +59,35 @@ @@ -57,6 +64,35 @@
private final AtomicInteger E; private final AtomicInteger E;
private final ChunkCoordIntPair F; private final ChunkCoordIntPair F;
@ -45,7 +50,7 @@
public Chunk(World world, int i, int j, BiomeBase[] abiomebase, ChunkConverter chunkconverter, TickList<Block> ticklist, TickList<FluidType> ticklist1, long k) { public Chunk(World world, int i, int j, BiomeBase[] abiomebase, ChunkConverter chunkconverter, TickList<Block> ticklist, TickList<FluidType> ticklist1, long k) {
this.sections = new ChunkSection[16]; this.sections = new ChunkSection[16];
this.g = new boolean[256]; this.g = new boolean[256];
@@ -95,8 +126,16 @@ @@ -95,8 +131,16 @@
this.s = ticklist; this.s = ticklist;
this.t = ticklist1; this.t = ticklist1;
this.z = k; this.z = k;
@ -62,7 +67,7 @@
public Chunk(World world, ProtoChunk protochunk, int i, int j) { public Chunk(World world, ProtoChunk protochunk, int i, int j) {
this(world, i, j, protochunk.getBiomeIndex(), protochunk.v(), protochunk.k(), protochunk.l(), protochunk.m()); this(world, i, j, protochunk.getBiomeIndex(), protochunk.v(), protochunk.k(), protochunk.l(), protochunk.m());
@@ -136,14 +175,15 @@ @@ -136,14 +180,15 @@
HeightMap.Type heightmap_type = (HeightMap.Type) iterator.next(); HeightMap.Type heightmap_type = (HeightMap.Type) iterator.next();
if (heightmap_type.c() == HeightMap.Use.LIVE_WORLD) { if (heightmap_type.c() == HeightMap.Use.LIVE_WORLD) {
@ -80,7 +85,7 @@
} }
public Set<BlockPosition> t() { public Set<BlockPosition> t() {
@@ -413,8 +453,15 @@ @@ -413,8 +458,15 @@
} }
} }
@ -96,7 +101,7 @@
int i = blockposition.getX() & 15; int i = blockposition.getX() & 15;
int j = blockposition.getY(); int j = blockposition.getY();
int k = blockposition.getZ() & 15; int k = blockposition.getZ() & 15;
@@ -474,7 +521,8 @@ @@ -474,7 +526,8 @@
} }
} }
@ -106,7 +111,7 @@
iblockdata.onPlace(this.world, blockposition, iblockdata1); iblockdata.onPlace(this.world, blockposition, iblockdata1);
} }
@@ -654,7 +702,12 @@ @@ -654,7 +707,12 @@
@Nullable @Nullable
public TileEntity a(BlockPosition blockposition, Chunk.EnumTileEntityState chunk_enumtileentitystate) { public TileEntity a(BlockPosition blockposition, Chunk.EnumTileEntityState chunk_enumtileentitystate) {
@ -120,7 +125,7 @@
if (tileentity == null) { if (tileentity == null) {
NBTTagCompound nbttagcompound = (NBTTagCompound) this.h.remove(blockposition); NBTTagCompound nbttagcompound = (NBTTagCompound) this.h.remove(blockposition);
@@ -701,6 +754,13 @@ @@ -701,6 +759,13 @@
tileentity.z(); tileentity.z();
this.tileEntities.put(blockposition.h(), tileentity); this.tileEntities.put(blockposition.h(), tileentity);
@ -134,8 +139,24 @@
} }
} }
@@ -733,6 +793,40 @@ @@ -726,13 +791,55 @@
int i = aentityslice.length;
for (int j = 0; j < i; ++j) {
- EntitySlice entityslice = aentityslice[j];
+ // CraftBukkit start
+ EntitySlice<Entity> entityslice = aentityslice[j];
+ List<Entity> toRemove = new LinkedList<>();
this.world.a(entityslice.stream().filter((entity) -> {
+ if (!CraftEventFactory.doEntityAddEventCalling(this.world, entity, CreatureSpawnEvent.SpawnReason.CHUNK_GEN)) {
+ toRemove.add(entity);
+ return false;
+ }
return !(entity instanceof EntityHuman);
})); }));
+ entityslice.removeAll(toRemove);
+ // CraftBukkit end
} }
+ // CraftBukkit start + // CraftBukkit start
@ -175,7 +196,7 @@
} }
public void removeEntities() { public void removeEntities() {
@@ -749,9 +843,21 @@ @@ -749,9 +856,21 @@
int i = aentityslice.length; int i = aentityslice.length;
for (int j = 0; j < i; ++j) { for (int j = 0; j < i; ++j) {
@ -199,7 +220,7 @@
} }
} }
@@ -813,8 +919,8 @@ @@ -813,8 +932,8 @@
while (iterator.hasNext()) { while (iterator.hasNext()) {
Entity entity = (Entity) iterator.next(); Entity entity = (Entity) iterator.next();
@ -210,7 +231,7 @@
} }
} }
} }
@@ -1020,13 +1126,13 @@ @@ -1020,13 +1139,13 @@
@Nullable @Nullable
public LongSet b(String s) { public LongSet b(String s) {
@ -226,7 +247,7 @@
return new LongOpenHashSet(); return new LongOpenHashSet();
})).add(i); })).add(i);
} }
@@ -1074,14 +1180,14 @@ @@ -1074,14 +1193,14 @@
} }
if (this.s instanceof ProtoChunkTickList) { if (this.s instanceof ProtoChunkTickList) {

View file

@ -296,7 +296,7 @@
if (k(blockposition)) { if (k(blockposition)) {
return Blocks.VOID_AIR.getBlockData(); return Blocks.VOID_AIR.getBlockData();
} else { } else {
@@ -649,6 +854,49 @@ @@ -649,6 +854,16 @@
} }
public boolean addEntity(Entity entity) { public boolean addEntity(Entity entity) {
@ -305,40 +305,7 @@
+ } + }
+ +
+ public boolean addEntity(Entity entity, SpawnReason spawnReason) { // Changed signature, added SpawnReason + public boolean addEntity(Entity entity, SpawnReason spawnReason) { // Changed signature, added SpawnReason
+ if (entity == null) return false; + if (!CraftEventFactory.doEntityAddEventCalling(this, entity, spawnReason)) {
+
+ org.bukkit.event.Cancellable event = null;
+ if (entity instanceof EntityLiving && !(entity instanceof EntityPlayer)) {
+ boolean isAnimal = entity instanceof EntityAnimal || entity instanceof EntityWaterAnimal || entity instanceof EntityGolem;
+ boolean isMonster = entity instanceof EntityMonster || entity instanceof EntityGhast || entity instanceof EntitySlime;
+ boolean isNpc = entity instanceof NPC;
+
+ if (spawnReason != SpawnReason.CUSTOM) {
+ if (isAnimal && !allowAnimals || isMonster && !allowMonsters || isNpc && !getServer().getServer().getSpawnNPCs()) {
+ entity.dead = true;
+ return false;
+ }
+ }
+
+ event = CraftEventFactory.callCreatureSpawnEvent((EntityLiving) entity, spawnReason);
+ } else if (entity instanceof EntityItem) {
+ event = CraftEventFactory.callItemSpawnEvent((EntityItem) entity);
+ } else if (entity.getBukkitEntity() instanceof org.bukkit.entity.Projectile) {
+ // Not all projectiles extend EntityProjectile, so check for Bukkit interface instead
+ event = CraftEventFactory.callProjectileLaunchEvent(entity);
+ } else if (entity.getBukkitEntity() instanceof org.bukkit.entity.Vehicle){
+ event = CraftEventFactory.callVehicleCreateEvent(entity);
+ }
+
+ if (event != null && (event.isCancelled() || entity.dead)) {
+ Entity vehicle = entity.getVehicle();
+ if (vehicle != null) {
+ vehicle.dead = true;
+ }
+ for (Entity passenger : entity.getAllPassengers()) {
+ passenger.dead = true;
+ }
+ entity.dead = true;
+ return false; + return false;
+ } + }
+ // CraftBukkit end + // CraftBukkit end
@ -346,7 +313,7 @@
int i = MathHelper.floor(entity.locX / 16.0D); int i = MathHelper.floor(entity.locX / 16.0D);
int j = MathHelper.floor(entity.locZ / 16.0D); int j = MathHelper.floor(entity.locZ / 16.0D);
boolean flag = entity.attachedToPlayer; boolean flag = entity.attachedToPlayer;
@@ -679,6 +927,7 @@ @@ -679,6 +894,7 @@
((IWorldAccess) this.v.get(i)).a(entity); ((IWorldAccess) this.v.get(i)).a(entity);
} }
@ -354,7 +321,7 @@
} }
protected void c(Entity entity) { protected void c(Entity entity) {
@@ -686,6 +935,7 @@ @@ -686,6 +902,7 @@
((IWorldAccess) this.v.get(i)).b(entity); ((IWorldAccess) this.v.get(i)).b(entity);
} }
@ -362,7 +329,7 @@
} }
public void kill(Entity entity) { public void kill(Entity entity) {
@@ -721,7 +971,15 @@ @@ -721,7 +938,15 @@
this.getChunkAt(i, j).b(entity); this.getChunkAt(i, j).b(entity);
} }
@ -379,7 +346,7 @@
this.c(entity); this.c(entity);
} }
@@ -756,6 +1014,11 @@ @@ -756,6 +981,11 @@
for (i = 0; i < this.k.size(); ++i) { for (i = 0; i < this.k.size(); ++i) {
entity = (Entity) this.k.get(i); entity = (Entity) this.k.get(i);
@ -391,7 +358,7 @@
try { try {
++entity.ticksLived; ++entity.ticksLived;
@@ -804,8 +1067,10 @@ @@ -804,8 +1034,10 @@
CrashReport crashreport1; CrashReport crashreport1;
CrashReportSystemDetails crashreportsystemdetails1; CrashReportSystemDetails crashreportsystemdetails1;
@ -404,7 +371,7 @@
Entity entity1 = entity.getVehicle(); Entity entity1 = entity.getVehicle();
if (entity1 != null) { if (entity1 != null) {
@@ -838,7 +1103,7 @@ @@ -838,7 +1070,7 @@
this.getChunkAt(j, l).b(entity); this.getChunkAt(j, l).b(entity);
} }
@ -413,7 +380,7 @@
this.c(entity); this.c(entity);
} }
@@ -893,9 +1158,11 @@ @@ -893,9 +1125,11 @@
TileEntity tileentity1 = (TileEntity) this.c.get(i1); TileEntity tileentity1 = (TileEntity) this.c.get(i1);
if (!tileentity1.x()) { if (!tileentity1.x()) {
@ -425,7 +392,7 @@
if (this.isLoaded(tileentity1.getPosition())) { if (this.isLoaded(tileentity1.getPosition())) {
Chunk chunk = this.getChunkAtWorldCoords(tileentity1.getPosition()); Chunk chunk = this.getChunkAtWorldCoords(tileentity1.getPosition());
@@ -903,6 +1170,12 @@ @@ -903,6 +1137,12 @@
chunk.a(tileentity1.getPosition(), tileentity1); chunk.a(tileentity1.getPosition(), tileentity1);
this.notify(tileentity1.getPosition(), iblockdata, iblockdata, 3); this.notify(tileentity1.getPosition(), iblockdata, iblockdata, 3);
@ -438,7 +405,7 @@
} }
} }
} }
@@ -956,15 +1229,13 @@ @@ -956,15 +1196,13 @@
int i; int i;
int j; int j;
@ -460,7 +427,7 @@
entity.N = entity.locX; entity.N = entity.locX;
entity.O = entity.locY; entity.O = entity.locY;
@@ -980,6 +1251,7 @@ @@ -980,6 +1218,7 @@
return IRegistry.ENTITY_TYPE.getKey(entity.P()).toString(); return IRegistry.ENTITY_TYPE.getKey(entity.P()).toString();
}); });
entity.tick(); entity.tick();
@ -468,7 +435,7 @@
this.methodProfiler.exit(); this.methodProfiler.exit();
} }
} }
@@ -1310,11 +1582,18 @@ @@ -1310,11 +1549,18 @@
} }
} }
@ -487,7 +454,7 @@
TileEntity tileentity = null; TileEntity tileentity = null;
if (this.J) { if (this.J) {
@@ -1349,6 +1628,14 @@ @@ -1349,6 +1595,14 @@
public void setTileEntity(BlockPosition blockposition, @Nullable TileEntity tileentity) { public void setTileEntity(BlockPosition blockposition, @Nullable TileEntity tileentity) {
if (!k(blockposition)) { if (!k(blockposition)) {
if (tileentity != null && !tileentity.x()) { if (tileentity != null && !tileentity.x()) {
@ -502,7 +469,7 @@
if (this.J) { if (this.J) {
tileentity.setPosition(blockposition); tileentity.setPosition(blockposition);
Iterator iterator = this.c.iterator(); Iterator iterator = this.c.iterator();
@@ -1509,6 +1796,14 @@ @@ -1509,6 +1763,14 @@
} }
this.p = MathHelper.a(this.p, 0.0F, 1.0F); this.p = MathHelper.a(this.p, 0.0F, 1.0F);
@ -517,7 +484,7 @@
} }
} }
} }
@@ -1594,7 +1889,10 @@ @@ -1594,7 +1856,10 @@
} }
public boolean c(EnumSkyBlock enumskyblock, BlockPosition blockposition) { public boolean c(EnumSkyBlock enumskyblock, BlockPosition blockposition) {
@ -529,7 +496,7 @@
return false; return false;
} else { } else {
int i = 0; int i = 0;
@@ -1737,7 +2035,7 @@ @@ -1737,7 +2002,7 @@
} }
public Stream<VoxelShape> a(@Nullable Entity entity, VoxelShape voxelshape, VoxelShape voxelshape1, Set<Entity> set) { public Stream<VoxelShape> a(@Nullable Entity entity, VoxelShape voxelshape, VoxelShape voxelshape1, Set<Entity> set) {
@ -538,7 +505,7 @@
return entity == null ? stream : Stream.concat(stream, this.a(entity, voxelshape, set)); return entity == null ? stream : Stream.concat(stream, this.a(entity, voxelshape, set));
} }
@@ -1767,7 +2065,7 @@ @@ -1767,7 +2032,7 @@
while (iterator.hasNext()) { while (iterator.hasNext()) {
Entity entity = (Entity) iterator.next(); Entity entity = (Entity) iterator.next();
@ -547,7 +514,7 @@
arraylist.add(entity); arraylist.add(entity);
} }
} }
@@ -1782,7 +2080,7 @@ @@ -1782,7 +2047,7 @@
while (iterator.hasNext()) { while (iterator.hasNext()) {
Entity entity = (Entity) iterator.next(); Entity entity = (Entity) iterator.next();
@ -556,7 +523,7 @@
arraylist.add(entity); arraylist.add(entity);
} }
} }
@@ -1831,7 +2129,7 @@ @@ -1831,7 +2096,7 @@
} }
} }
@ -565,7 +532,7 @@
} }
@Nullable @Nullable
@@ -1852,8 +2150,16 @@ @@ -1852,8 +2117,16 @@
while (iterator.hasNext()) { while (iterator.hasNext()) {
Entity entity = (Entity) iterator.next(); Entity entity = (Entity) iterator.next();
@ -583,7 +550,7 @@
if (oclass.isAssignableFrom(entity.getClass())) { if (oclass.isAssignableFrom(entity.getClass())) {
++j; ++j;
} }
@@ -1972,6 +2278,11 @@ @@ -1972,6 +2245,11 @@
for (int i = 0; i < this.players.size(); ++i) { for (int i = 0; i < this.players.size(); ++i) {
EntityHuman entityhuman1 = (EntityHuman) this.players.get(i); EntityHuman entityhuman1 = (EntityHuman) this.players.get(i);
@ -595,7 +562,7 @@
if (predicate.test(entityhuman1)) { if (predicate.test(entityhuman1)) {
double d5 = entityhuman1.d(d0, d1, d2); double d5 = entityhuman1.d(d0, d1, d2);
@@ -2185,6 +2496,16 @@ @@ -2185,6 +2463,16 @@
public void everyoneSleeping() {} public void everyoneSleeping() {}
@ -612,7 +579,7 @@
public float g(float f) { public float g(float f) {
return (this.q + (this.r - this.q) * f) * this.i(f); return (this.q + (this.r - this.q) * f) * this.i(f);
} }
@@ -2346,7 +2667,7 @@ @@ -2346,7 +2634,7 @@
int l = j * 16 + 8 - blockposition.getZ(); int l = j * 16 + 8 - blockposition.getZ();
boolean flag = true; boolean flag = true;

View file

@ -359,6 +359,47 @@ public class CraftEventFactory {
return event; return event;
} }
public static boolean doEntityAddEventCalling(World world, Entity entity, SpawnReason spawnReason){
if (entity == null) return false;
org.bukkit.event.Cancellable event = null;
if (entity instanceof EntityLiving && !(entity instanceof EntityPlayer)) {
boolean isAnimal = entity instanceof EntityAnimal || entity instanceof EntityWaterAnimal || entity instanceof EntityGolem;
boolean isMonster = entity instanceof EntityMonster || entity instanceof EntityGhast || entity instanceof EntitySlime;
boolean isNpc = entity instanceof NPC;
if (spawnReason != SpawnReason.CUSTOM) {
if (isAnimal && !world.allowAnimals || isMonster && !world.allowMonsters || isNpc && !world.getServer().getServer().getSpawnNPCs()) {
entity.dead = true;
return false;
}
}
event = CraftEventFactory.callCreatureSpawnEvent((EntityLiving) entity, spawnReason);
} else if (entity instanceof EntityItem) {
event = CraftEventFactory.callItemSpawnEvent((EntityItem) entity);
} else if (entity.getBukkitEntity() instanceof org.bukkit.entity.Projectile) {
// Not all projectiles extend EntityProjectile, so check for Bukkit interface instead
event = CraftEventFactory.callProjectileLaunchEvent(entity);
} else if (entity.getBukkitEntity() instanceof org.bukkit.entity.Vehicle){
event = CraftEventFactory.callVehicleCreateEvent(entity);
}
if (event != null && (event.isCancelled() || entity.dead)) {
Entity vehicle = entity.getVehicle();
if (vehicle != null) {
vehicle.dead = true;
}
for (Entity passenger : entity.getAllPassengers()) {
passenger.dead = true;
}
entity.dead = true;
return false;
}
return true;
}
/** /**
* CreatureSpawnEvent * CreatureSpawnEvent
*/ */