SPIGOT-4768: ChunkUnloadEvent not called

This commit is contained in:
md_5 2019-04-26 16:56:49 +10:00
parent 735f4ea33e
commit f5285abf2b

View file

@ -1,14 +1,10 @@
--- a/net/minecraft/server/PlayerChunkMap.java --- a/net/minecraft/server/PlayerChunkMap.java
+++ b/net/minecraft/server/PlayerChunkMap.java +++ b/net/minecraft/server/PlayerChunkMap.java
@@ -33,6 +33,16 @@ @@ -33,6 +33,12 @@
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
+// CraftBukkit start +// CraftBukkit start
+import java.util.concurrent.CompletionException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.bukkit.craftbukkit.event.CraftEventFactory; +import org.bukkit.craftbukkit.event.CraftEventFactory;
+import org.bukkit.entity.Player; +import org.bukkit.entity.Player;
+import org.bukkit.event.entity.CreatureSpawnEvent; +import org.bukkit.event.entity.CreatureSpawnEvent;
@ -17,7 +13,7 @@
public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d { public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
@@ -179,9 +189,12 @@ @@ -179,9 +185,12 @@
return completablefuture1.thenApply((list1) -> { return completablefuture1.thenApply((list1) -> {
List<IChunkAccess> list2 = Lists.newArrayList(); List<IChunkAccess> list2 = Lists.newArrayList();
@ -32,7 +28,7 @@
final Either<IChunkAccess, PlayerChunk.Failure> either = (Either) iterator.next(); final Either<IChunkAccess, PlayerChunk.Failure> either = (Either) iterator.next();
Optional<IChunkAccess> optional = either.left(); Optional<IChunkAccess> optional = either.left();
@@ -257,9 +270,9 @@ @@ -257,9 +266,9 @@
}).forEach((completablefuture) -> { }).forEach((completablefuture) -> {
if (flag) { if (flag) {
this.executor.awaitTasks(completablefuture::isDone); this.executor.awaitTasks(completablefuture::isDone);
@ -44,64 +40,39 @@
} }
}); });
@@ -280,13 +293,27 @@ @@ -268,7 +277,6 @@
for (int i = 0; longiterator.hasNext() && (booleansupplier.getAsBoolean() || i < 200 || this.unloadQueue.size() > 2000); longiterator.remove()) {
long j = longiterator.nextLong();
- PlayerChunk playerchunk = (PlayerChunk) this.updatingChunks.remove(j);
+ PlayerChunk playerchunk = (PlayerChunk) this.updatingChunks.get(j); // CraftBukkit
if (playerchunk != null) {
+ // CraftBukkit start
+ ChunkUnloadEvent event = null;
+ IChunkAccess access = playerchunk.getChunk();
+
+ if (access instanceof Chunk) {
+ event = new ChunkUnloadEvent(((Chunk) access).bukkitChunk, access.isNeedsSaving());
+ this.world.getServer().getPluginManager().callEvent(event);
+
+ if (event.isCancelled()) {
+ continue;
+ }
+ }
+ this.updatingChunks.remove(j);
+ // CraftBukkit end
this.g.put(j, playerchunk);
this.updatingChunksModified = true;
++i;
- this.a(j, playerchunk);
+ this.a(j, playerchunk, event); // CraftBukkit
}
}
} }
@@ -294,17 +321,23 @@
gameprofilerfiller.exit();
} }
-
protected void unloadChunks(BooleanSupplier booleansupplier) {
GameProfilerFiller gameprofilerfiller = this.world.getMethodProfiler();
- private void a(long i, PlayerChunk playerchunk) { @@ -304,13 +312,22 @@
+ private void a(long i, PlayerChunk playerchunk, ChunkUnloadEvent event) { // CraftBukkit - add event this.a(i, playerchunk);
CompletableFuture<IChunkAccess> completablefuture = playerchunk.getChunkSave();
completablefuture.thenAcceptAsync((ichunkaccess) -> {
CompletableFuture<IChunkAccess> completablefuture1 = playerchunk.getChunkSave();
if (completablefuture1 != completablefuture) {
- this.a(i, playerchunk);
+ this.a(i, playerchunk, event); // CraftBukkit
} else { } else {
if (this.g.remove(i, playerchunk) && ichunkaccess != null) { if (this.g.remove(i, playerchunk) && ichunkaccess != null) {
- this.saveChunk(ichunkaccess); - this.saveChunk(ichunkaccess);
+ // CraftBukkit start
+ if (event == null) {
+ this.saveChunk(ichunkaccess);
+ } else {
+ this.saveChunk(ichunkaccess, event.isSaveChunk());
+ }
+ // CraftBukkit end
if (this.h.remove(i) && ichunkaccess instanceof Chunk) { if (this.h.remove(i) && ichunkaccess instanceof Chunk) {
Chunk chunk = (Chunk) ichunkaccess; Chunk chunk = (Chunk) ichunkaccess;
@@ -394,7 +427,7 @@ + // CraftBukkit start
+ ChunkUnloadEvent event = new ChunkUnloadEvent(chunk.bukkitChunk, chunk.isNeedsSaving());
+ this.world.getServer().getPluginManager().callEvent(event);
+ this.saveChunk(ichunkaccess, event.isSaveChunk());
+ // CraftBukkit end
+
chunk.c(false);
this.world.unloadChunk(chunk);
+ // CraftBukkit start
+ } else {
+ this.saveChunk(ichunkaccess);
}
+ // CraftBukkit end
this.lightEngine.a(ichunkaccess.getPos());
this.lightEngine.queueUpdate();
@@ -394,7 +411,7 @@
return CompletableFuture.completedFuture(Either.right(playerchunk_failure)); return CompletableFuture.completedFuture(Either.right(playerchunk_failure));
}); });
}, (runnable) -> { }, (runnable) -> {
@ -110,7 +81,7 @@
}); });
} }
} }
@@ -454,7 +487,10 @@ @@ -454,7 +471,10 @@
while (iterator.hasNext()) { while (iterator.hasNext()) {
Entity entity = (Entity) iterator.next(); Entity entity = (Entity) iterator.next();
@ -122,7 +93,7 @@
if (list == null) { if (list == null) {
list = Lists.newArrayList(new Entity[] { entity}); list = Lists.newArrayList(new Entity[] { entity});
} else { } else {
@@ -476,7 +512,7 @@ @@ -476,7 +496,7 @@
long i = playerchunk.h().pair(); long i = playerchunk.h().pair();
playerchunk.getClass(); playerchunk.getClass();
@ -131,7 +102,7 @@
}); });
} }
@@ -493,7 +529,7 @@ @@ -493,7 +513,7 @@
return Either.left(chunk); return Either.left(chunk);
}); });
}, (runnable) -> { }, (runnable) -> {
@ -140,7 +111,7 @@
}); });
completablefuture1.thenAcceptAsync((either) -> { completablefuture1.thenAcceptAsync((either) -> {
@@ -507,7 +543,7 @@ @@ -507,7 +527,7 @@
return Either.left(chunk); return Either.left(chunk);
}); });
}, (runnable) -> { }, (runnable) -> {
@ -149,7 +120,7 @@
}); });
return completablefuture1; return completablefuture1;
} }
@@ -517,8 +553,14 @@ @@ -517,8 +537,14 @@
} }
public void saveChunk(IChunkAccess ichunkaccess) { public void saveChunk(IChunkAccess ichunkaccess) {
@ -165,7 +136,7 @@
try { try {
this.world.checkSession(); this.world.checkSession();
} catch (ExceptionWorldConflict exceptionworldconflict) { } catch (ExceptionWorldConflict exceptionworldconflict) {
@@ -569,9 +611,10 @@ @@ -569,9 +595,10 @@
ChunkCoordIntPair chunkcoordintpair = playerchunk.h(); ChunkCoordIntPair chunkcoordintpair = playerchunk.h();
Packet<?>[] apacket = new Packet[2]; Packet<?>[] apacket = new Packet[2];
@ -177,7 +148,7 @@
boolean flag1 = i1 <= this.A; boolean flag1 = i1 <= this.A;
this.sendChunk(entityplayer, chunkcoordintpair, apacket, flag, flag1); this.sendChunk(entityplayer, chunkcoordintpair, apacket, flag, flag1);
@@ -626,7 +669,7 @@ @@ -626,7 +653,7 @@
private NBTTagCompound f(ChunkCoordIntPair chunkcoordintpair) throws IOException { private NBTTagCompound f(ChunkCoordIntPair chunkcoordintpair) throws IOException {
NBTTagCompound nbttagcompound = this.read(chunkcoordintpair); NBTTagCompound nbttagcompound = this.read(chunkcoordintpair);
@ -186,7 +157,7 @@
} }
boolean d(ChunkCoordIntPair chunkcoordintpair) { boolean d(ChunkCoordIntPair chunkcoordintpair) {
@@ -946,7 +989,7 @@ @@ -946,7 +973,7 @@
public final Set<EntityPlayer> trackedPlayers = Sets.newHashSet(); public final Set<EntityPlayer> trackedPlayers = Sets.newHashSet();
public EntityTracker(Entity entity, int i, int j, boolean flag) { public EntityTracker(Entity entity, int i, int j, boolean flag) {
@ -195,7 +166,7 @@
this.tracker = entity; this.tracker = entity;
this.trackingDistance = i; this.trackingDistance = i;
this.e = SectionPosition.a(entity); this.e = SectionPosition.a(entity);
@@ -1015,6 +1058,17 @@ @@ -1015,6 +1042,17 @@
} }
} }