Improve chunk unload queue to maintain some previous expectations

While the previous logic was logically correct, some CB API's before
would request a chunk without removing it from the unload queue.

While this is logically wrong, some plugins seem to be causing unload issues.

This change will make anything using that one API that use to not remove from
queue, no longer remove from queue.

Hopefully other activities on the server will touch the chunk if it REALLY is in use.
This commit is contained in:
Aikar 2016-03-20 00:13:20 -04:00
parent 3acf9df3be
commit 480a87933a
2 changed files with 72 additions and 25 deletions

View file

@ -1,4 +1,4 @@
From 3f28611a24fa1d3b92132c6755774d90f0cce970 Mon Sep 17 00:00:00 2001
From f3feb831b366194cd979e43409b6c0180cfbd58f Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Fri, 18 Mar 2016 17:57:25 -0400
Subject: [PATCH] Optimize Chunk Unload Queue
@ -50,7 +50,7 @@ index b6d84d7..a4c0b4e 100644
Iterator iterator = this.tileEntities.values().iterator();
diff --git a/src/main/java/net/minecraft/server/ChunkProviderServer.java b/src/main/java/net/minecraft/server/ChunkProviderServer.java
index 9ef6246..247a6dd 100644
index 9ef6246..f696e27 100644
--- a/src/main/java/net/minecraft/server/ChunkProviderServer.java
+++ b/src/main/java/net/minecraft/server/ChunkProviderServer.java
@@ -21,7 +21,7 @@ import org.bukkit.event.world.ChunkUnloadEvent;
@ -62,26 +62,30 @@ index 9ef6246..247a6dd 100644
public final ChunkGenerator chunkGenerator; // CraftBukkit - public
private final IChunkLoader chunkLoader;
public LongObjectHashMap<Chunk> chunks = new LongObjectHashMap<Chunk>(); // CraftBukkit
@@ -79,18 +79,24 @@ public class ChunkProviderServer implements IChunkProvider {
@@ -79,18 +79,27 @@ public class ChunkProviderServer implements IChunkProvider {
// CraftBukkit start - Add async variant, provide compatibility
public Chunk getOrCreateChunkFast(int x, int z) {
- Chunk chunk = chunks.get(LongHash.toLong(x, z));
- return (chunk == null) ? getChunkAt(x, z) : chunk;
+ return getChunkAt(x, z); // Paper
}
+ }
+
+ // Paper start
+ public Chunk getLoadedChunkAtWithoutMarkingActive(int i, int j) {
+ return chunks.get(LongHash.toLong(i, j));
+ }
+ // Paper end
+
public Chunk getChunkIfLoaded(int x, int z) {
- return chunks.get(LongHash.toLong(x, z));
+ return getLoadedChunkAt(x, z); // Paper - Bukkit has a duplicate method now.
}
- public Chunk getChunkIfLoaded(int x, int z) {
- return chunks.get(LongHash.toLong(x, z));
+ // getChunkIfLoaded -> getChunkIfActive
+ // this is only used by CraftBukkit now, and plugins shouldnt mark things active
+ public Chunk getChunkIfActive(int x, int z) {
+ Chunk chunk = chunks.get(LongHash.toLong(x, z));
+ return (chunk != null && chunk.isChunkActive) ? chunk : null;
}
+ // Paper end
public Chunk getLoadedChunkAt(int i, int j) {
Chunk chunk = chunks.get(LongHash.toLong(i, j)); // CraftBukkit
@ -90,7 +94,7 @@ index 9ef6246..247a6dd 100644
return chunk;
}
@@ -151,6 +157,7 @@ public class ChunkProviderServer implements IChunkProvider {
@@ -151,6 +160,7 @@ public class ChunkProviderServer implements IChunkProvider {
runnable.run();
}
@ -98,7 +102,7 @@ index 9ef6246..247a6dd 100644
return chunk;
}
@@ -201,7 +208,7 @@ public class ChunkProviderServer implements IChunkProvider {
@@ -201,7 +211,7 @@ public class ChunkProviderServer implements IChunkProvider {
continue;
}
@ -107,7 +111,7 @@ index 9ef6246..247a6dd 100644
if (neighbor != null) {
neighbor.setNeighborLoaded(-x, -z);
chunk.setNeighborLoaded(x, z);
@@ -300,10 +307,17 @@ public class ChunkProviderServer implements IChunkProvider {
@@ -300,10 +310,17 @@ public class ChunkProviderServer implements IChunkProvider {
if (!this.world.savingDisabled) {
// CraftBukkit start
Server server = this.world.getServer();
@ -129,7 +133,7 @@ index 9ef6246..247a6dd 100644
ChunkUnloadEvent event = new ChunkUnloadEvent(chunk.bukkitChunk);
server.getPluginManager().callEvent(event);
@@ -325,7 +339,7 @@ public class ChunkProviderServer implements IChunkProvider {
@@ -325,7 +342,7 @@ public class ChunkProviderServer implements IChunkProvider {
continue;
}
@ -138,7 +142,7 @@ index 9ef6246..247a6dd 100644
if (neighbor != null) {
neighbor.setNeighborUnloaded(-x, -z);
chunk.setNeighborUnloaded(x, z);
@@ -367,4 +381,22 @@ public class ChunkProviderServer implements IChunkProvider {
@@ -367,4 +384,22 @@ public class ChunkProviderServer implements IChunkProvider {
public boolean e(int i, int j) {
return this.chunks.containsKey(LongHash.toLong(i, j)); // CraftBukkit
}
@ -174,10 +178,40 @@ index 63e118d..721bcae 100644
{
i += server.getChunkAt( x, z ).entityCount.get( oClass );
}
diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java
index b356aa6..f996c53 100644
--- a/src/main/java/net/minecraft/server/World.java
+++ b/src/main/java/net/minecraft/server/World.java
@@ -157,9 +157,15 @@ public abstract class World implements IBlockAccess {
}
public Chunk getChunkIfLoaded(int x, int z) {
- return ((ChunkProviderServer) this.chunkProvider).getChunkIfLoaded(x, z);
+ return ((ChunkProviderServer) this.chunkProvider).getLoadedChunkAtWithoutMarkingActive(x, z); // Paper - This is added by CB, and will not mark as active. Simply an alias
}
+ // Paper start
+ public Chunk getChunkIfActive(int x, int z) {
+ return ((ChunkProviderServer) this.chunkProvider).getChunkIfActive(x, z);
+ }
+ // Paper end
+
protected World(IDataManager idatamanager, WorldData worlddata, WorldProvider worldprovider, MethodProfiler methodprofiler, boolean flag, ChunkGenerator gen, org.bukkit.World.Environment env) {
this.spigotConfig = new org.spigotmc.SpigotWorldConfig( worlddata.getName() ); // Spigot
this.paperConfig = new com.destroystokyo.paper.PaperWorldConfig(worlddata.getName(), this.spigotConfig); // Paper
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
index be311cd..66d3e94 100644
index be311cd..c0c642e 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -206,7 +206,7 @@ public class CraftWorld implements World {
}
// Paper start - Don't create a chunk just to unload it
- net.minecraft.server.Chunk chunk = world.getChunkProviderServer().getChunkIfLoaded(x, z);
+ net.minecraft.server.Chunk chunk = world.getChunkProviderServer().getChunkIfActive(x, z); // Paper
if (chunk == null) {
return false;
}
@@ -232,7 +232,7 @@ public class CraftWorld implements World {
continue;
}
@ -227,6 +261,19 @@ index 482af17..a1a6d5a 100644
if (neighbor != null) {
neighbor.setNeighborLoaded(-x, -z);
chunk.setNeighborLoaded(x, z);
diff --git a/src/main/java/org/spigotmc/ActivationRange.java b/src/main/java/org/spigotmc/ActivationRange.java
index daed1db..ba60f1a 100644
--- a/src/main/java/org/spigotmc/ActivationRange.java
+++ b/src/main/java/org/spigotmc/ActivationRange.java
@@ -250,7 +250,7 @@ public class ActivationRange
int x = MathHelper.floor( entity.locX );
int z = MathHelper.floor( entity.locZ );
// Make sure not on edge of unloaded chunk
- Chunk chunk = entity.world.getChunkIfLoaded( x >> 4, z >> 4 );
+ Chunk chunk = entity.world.getChunkIfActive( x >> 4, z >> 4 ); // Paper
if ( isActive && !( chunk != null && chunk.areNeighborsLoaded( 1 ) ) )
{
isActive = false;
--
2.7.3
2.7.4

View file

@ -1,4 +1,4 @@
From 0ef28b03fcca5f758ec4080fe0fcf19baf7b1881 Mon Sep 17 00:00:00 2001
From 6a8a879e92e5058a3c510aa0f82596ee13c5ee29 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Fri, 18 Mar 2016 20:16:03 -0400
Subject: [PATCH] Add World Util Methods
@ -6,7 +6,7 @@ Subject: [PATCH] Add World Util Methods
Methods that can be used for other patches to help improve logic.
diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java
index b356aa6..dafe3b2 100644
index f996c53..d8bd36c 100644
--- a/src/main/java/net/minecraft/server/World.java
+++ b/src/main/java/net/minecraft/server/World.java
@@ -156,6 +156,12 @@ public abstract class World implements IBlockAccess {
@ -15,14 +15,14 @@ index b356aa6..dafe3b2 100644
+ // Paper start
+ public Chunk getChunkIfLoaded(BlockPosition blockposition) {
+ return ((ChunkProviderServer) this.chunkProvider).getChunkIfLoaded(blockposition.getX() >> 4, blockposition.getZ() >> 4);
+ return this.chunkProvider.getLoadedChunkAt(blockposition.getX() >> 4, blockposition.getZ() >> 4);
+ }
+ // Paper end
+
public Chunk getChunkIfLoaded(int x, int z) {
return ((ChunkProviderServer) this.chunkProvider).getChunkIfLoaded(x, z);
return ((ChunkProviderServer) this.chunkProvider).getLoadedChunkAtWithoutMarkingActive(x, z); // Paper - This is added by CB, and will not mark as active. Simply an alias
}
@@ -628,6 +634,41 @@ public abstract class World implements IBlockAccess {
@@ -634,6 +640,41 @@ public abstract class World implements IBlockAccess {
}
}
@ -64,7 +64,7 @@ index b356aa6..dafe3b2 100644
public int getLightLevel(BlockPosition blockposition) {
return this.c(blockposition, true);
}
@@ -742,6 +783,27 @@ public abstract class World implements IBlockAccess {
@@ -748,6 +789,27 @@ public abstract class World implements IBlockAccess {
return this.worldProvider.n()[this.getLightLevel(blockposition)];
}
@ -93,5 +93,5 @@ index b356aa6..dafe3b2 100644
// CraftBukkit start - tree generation
if (captureTreeGeneration) {
--
2.7.3
2.7.4