--- ../work/decompile-8eb82bde//net/minecraft/server/PlayerChunkMap.java 2014-11-28 17:43:43.321707430 +0000 +++ src/main/java/net/minecraft/server/PlayerChunkMap.java 2014-11-28 17:38:19.000000000 +0000 @@ -7,17 +7,24 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +// CraftBukkit start +import java.util.Collections; +import java.util.Queue; +import java.util.LinkedList; +// CraftBukkit end + public class PlayerChunkMap { private static final Logger a = LogManager.getLogger(); private final WorldServer world; private final List managedPlayers = Lists.newArrayList(); private final LongHashMap d = new LongHashMap(); - private final List e = Lists.newArrayList(); - private final List f = Lists.newArrayList(); + private final Queue e = new java.util.concurrent.ConcurrentLinkedQueue(); // CraftBukkit ArrayList -> ConcurrentLinkedQueue + private final Queue f = new java.util.concurrent.ConcurrentLinkedQueue(); // CraftBukkit ArrayList -> ConcurrentLinkedQueue private int g; private long h; private final int[][] i = new int[][] { { 1, 0}, { 0, 1}, { -1, 0}, { 0, -1}}; + private boolean wasNotEmpty; // CraftBukkit - add field public PlayerChunkMap(WorldServer worldserver) { this.world = worldserver; @@ -35,28 +42,39 @@ if (i - this.h > 8000L) { this.h = i; - - for (j = 0; j < this.f.size(); ++j) { - playerchunk = (PlayerChunk) this.f.get(j); + + // CraftBukkit start - Use iterator + java.util.Iterator iterator = this.f.iterator(); + while (iterator.hasNext()) { + playerchunk = (PlayerChunk) iterator.next(); playerchunk.b(); playerchunk.a(); } } else { - for (j = 0; j < this.e.size(); ++j) { - playerchunk = (PlayerChunk) this.e.get(j); + java.util.Iterator iterator = this.e.iterator(); + + while (iterator.hasNext()) { + playerchunk = (PlayerChunk) iterator.next(); playerchunk.b(); + iterator.remove(); + // CraftBukkit end } } - this.e.clear(); + // this.e.clear(); // CraftBukkit - Removals are already covered if (this.managedPlayers.isEmpty()) { + if (!wasNotEmpty) return; // CraftBukkit - Only do unload when we go from non-empty to empty WorldProvider worldprovider = this.world.worldProvider; if (!worldprovider.e()) { this.world.chunkProviderServer.b(); } + // CraftBukkit start + wasNotEmpty = false; + } else { + wasNotEmpty = true; } - + // CraftBukkit end } public boolean a(int i, int j) { @@ -77,6 +95,16 @@ return playerchunk; } + + // CraftBukkit start - add method + public final boolean isChunkInUse(int x, int z) { + PlayerChunk pi = a(x, z, false); + if (pi != null) { + return (PlayerChunk.b(pi).size() > 0); + } + return false; + } + // CraftBukkit end public void flagDirty(BlockPosition blockposition) { int i = blockposition.getX() >> 4; @@ -95,13 +123,22 @@ entityplayer.d = entityplayer.locX; entityplayer.e = entityplayer.locZ; + + // CraftBukkit start - Load nearby chunks first + List<ChunkCoordIntPair> chunkList = new LinkedList<ChunkCoordIntPair>(); for (int k = i - this.g; k <= i + this.g; ++k) { for (int l = j - this.g; l <= j + this.g; ++l) { - this.a(k, l, true).a(entityplayer); + chunkList.add(new ChunkCoordIntPair(k, l)); } } - + + Collections.sort(chunkList, new ChunkCoordComparator(entityplayer)); + for (ChunkCoordIntPair pair : chunkList) { + this.a(pair.x, pair.z, true).a(entityplayer); + } + // CraftBukkit end + this.managedPlayers.add(entityplayer); this.b(entityplayer); } @@ -188,12 +225,13 @@ int i1 = this.g; int j1 = i - k; int k1 = j - l; + List<ChunkCoordIntPair> chunksToLoad = new LinkedList<ChunkCoordIntPair>(); // CraftBukkit if (j1 != 0 || k1 != 0) { for (int l1 = i - i1; l1 <= i + i1; ++l1) { for (int i2 = j - i1; i2 <= j + i1; ++i2) { if (!this.a(l1, i2, k, l, i1)) { - this.a(l1, i2, true).a(entityplayer); + chunksToLoad.add(new ChunkCoordIntPair(l1, i2)); // CraftBukkit } if (!this.a(l1 - j1, i2 - k1, i, j, i1)) { @@ -209,6 +247,17 @@ this.b(entityplayer); entityplayer.d = entityplayer.locX; entityplayer.e = entityplayer.locZ; + + // CraftBukkit start - send nearest chunks first + Collections.sort(chunksToLoad, new ChunkCoordComparator(entityplayer)); + for (ChunkCoordIntPair pair : chunksToLoad) { + this.a(pair.x, pair.z, true).a(entityplayer); + } + + if (j1 > 1 || j1 < -1 || k1 > 1 || k1 < -1) { + Collections.sort(entityplayer.chunkCoordIntPairQueue, new ChunkCoordComparator(entityplayer)); + } + // CraftBukkit end } } } @@ -274,11 +323,54 @@ return playerchunkmap.d; } - static List c(PlayerChunkMap playerchunkmap) { + static Queue c(PlayerChunkMap playerchunkmap) { // CraftBukkit List -> Queue return playerchunkmap.f; } - static List d(PlayerChunkMap playerchunkmap) { + static Queue d(PlayerChunkMap playerchunkmap) { // CraftBukkit List -> Queue return playerchunkmap.e; } + + // CraftBukkit start - Sorter to load nearby chunks first + private static class ChunkCoordComparator implements java.util.Comparator<ChunkCoordIntPair> { + private int x; + private int z; + + public ChunkCoordComparator (EntityPlayer entityplayer) { + x = (int) entityplayer.locX >> 4; + z = (int) entityplayer.locZ >> 4; + } + + public int compare(ChunkCoordIntPair a, ChunkCoordIntPair b) { + if (a.equals(b)) { + return 0; + } + + // Subtract current position to set center point + int ax = a.x - this.x; + int az = a.z - this.z; + int bx = b.x - this.x; + int bz = b.z - this.z; + + int result = ((ax - bx) * (ax + bx)) + ((az - bz) * (az + bz)); + if (result != 0) { + return result; + } + + if (ax < 0) { + if (bx < 0) { + return bz - az; + } else { + return -1; + } + } else { + if (bx < 0) { + return 1; + } else { + return az - bz; + } + } + } + } + // CraftBukkit end }