24143ef6a1
When a player triggers a chunk load via walking around or teleporting there is no need to stop everything and get this chunk on the main thread. The client is used to having to wait some time for this chunk and the server doesn't immediately do anything with it except send it to the player. At the same time chunk loading is the last major source of file IO that still runs on the main thread. These two facts make it possible to offload chunks loaded for this reason to another thread. However, not all parts of chunk loading can happen off the main thread. For this we use the new AsynchronousExecutor system to split chunk loading in to three pieces. The first is loading data from disk, decompressing it, and parsing it in to an NBT structure. The second piece is creating entities and tile entities in the chunk and adding them to the world, this is still done on the main thread. The third piece is informing everyone who requested a chunk load that the load is finished. For this we register callbacks and then run them on the main thread once the previous two stages are finished. There are still cases where a chunk is needed immediately and these will still trigger chunk loading entirely on the main thread. The most obvious case is plugins using the API to request a chunk load. We also must load the chunk immediately when something in the world tries to access it. In these cases we ignore any possibly pending or in progress chunk loading that is happening asynchronously as we will have the chunk loaded by the time they are finished. The hope is that overall this system will result in less CPU time and pauses due to blocking file IO on the main thread thus giving more consistent performance. Testing so far has shown that this also speeds up chunk loading client side although some of this is likely to be because we are sending less chunks at once for the client to process. Thanks for @ammaraskar for help with the implementation of this feature.
304 Zeilen
9.8 KiB
Java
304 Zeilen
9.8 KiB
Java
package net.minecraft.server;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
// CraftBukkit start
|
|
import java.util.Collections;
|
|
import java.util.Queue;
|
|
import java.util.Iterator;
|
|
import java.util.LinkedList;
|
|
// CraftBukkit end
|
|
|
|
public class PlayerManager {
|
|
|
|
private final WorldServer world;
|
|
private final List managedPlayers = new ArrayList();
|
|
private final LongHashMap c = new LongHashMap();
|
|
private final Queue d = new java.util.concurrent.ConcurrentLinkedQueue(); // CraftBukkit ArrayList -> ConcurrentLinkedQueue
|
|
private final int e;
|
|
private final int[][] f = new int[][] { { 1, 0}, { 0, 1}, { -1, 0}, { 0, -1}};
|
|
private boolean wasNotEmpty; // CraftBukkit
|
|
|
|
public PlayerManager(WorldServer worldserver, int i) {
|
|
if (i > 15) {
|
|
throw new IllegalArgumentException("Too big view radius!");
|
|
} else if (i < 3) {
|
|
throw new IllegalArgumentException("Too small view radius!");
|
|
} else {
|
|
this.e = i;
|
|
this.world = worldserver;
|
|
}
|
|
}
|
|
|
|
public WorldServer a() {
|
|
return this.world;
|
|
}
|
|
|
|
public void flush() {
|
|
// CraftBukkit start - use iterator
|
|
Iterator iterator = this.d.iterator();
|
|
|
|
while (iterator.hasNext()) {
|
|
PlayerInstance playerinstance = (PlayerInstance) iterator.next();
|
|
playerinstance.a();
|
|
iterator.remove();
|
|
}
|
|
// CraftBukkit end
|
|
|
|
// this.d.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.a();
|
|
}
|
|
// CraftBukkit start
|
|
wasNotEmpty = false;
|
|
} else {
|
|
wasNotEmpty = true;
|
|
}
|
|
// CraftBukkit end
|
|
}
|
|
|
|
private PlayerInstance a(int i, int j, boolean flag) {
|
|
long k = (long) i + 2147483647L | (long) j + 2147483647L << 32;
|
|
PlayerInstance playerinstance = (PlayerInstance) this.c.getEntry(k);
|
|
|
|
if (playerinstance == null && flag) {
|
|
playerinstance = new PlayerInstance(this, i, j);
|
|
this.c.put(k, playerinstance);
|
|
}
|
|
|
|
return playerinstance;
|
|
}
|
|
// CraftBukkit start
|
|
public final boolean isChunkInUse(int x, int z) {
|
|
PlayerInstance pi = a(x, z, false);
|
|
if (pi != null) {
|
|
return (PlayerInstance.b(pi).size() > 0);
|
|
}
|
|
return false;
|
|
}
|
|
// CraftBukkit end
|
|
|
|
public void flagDirty(int i, int j, int k) {
|
|
int l = i >> 4;
|
|
int i1 = k >> 4;
|
|
PlayerInstance playerinstance = this.a(l, i1, false);
|
|
|
|
if (playerinstance != null) {
|
|
playerinstance.a(i & 15, j, k & 15);
|
|
}
|
|
}
|
|
|
|
public void addPlayer(EntityPlayer entityplayer) {
|
|
int i = (int) entityplayer.locX >> 4;
|
|
int j = (int) entityplayer.locZ >> 4;
|
|
|
|
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.e; k <= i + this.e; ++k) {
|
|
for (int l = j - this.e; l <= j + this.e; ++l) {
|
|
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);
|
|
}
|
|
|
|
public void b(EntityPlayer entityplayer) {
|
|
ArrayList arraylist = new ArrayList(entityplayer.chunkCoordIntPairQueue);
|
|
int i = 0;
|
|
int j = this.e;
|
|
int k = (int) entityplayer.locX >> 4;
|
|
int l = (int) entityplayer.locZ >> 4;
|
|
int i1 = 0;
|
|
int j1 = 0;
|
|
ChunkCoordIntPair chunkcoordintpair = PlayerInstance.a(this.a(k, l, true));
|
|
|
|
entityplayer.chunkCoordIntPairQueue.clear();
|
|
if (arraylist.contains(chunkcoordintpair)) {
|
|
entityplayer.chunkCoordIntPairQueue.add(chunkcoordintpair);
|
|
}
|
|
|
|
int k1;
|
|
|
|
for (k1 = 1; k1 <= j * 2; ++k1) {
|
|
for (int l1 = 0; l1 < 2; ++l1) {
|
|
int[] aint = this.f[i++ % 4];
|
|
|
|
for (int i2 = 0; i2 < k1; ++i2) {
|
|
i1 += aint[0];
|
|
j1 += aint[1];
|
|
chunkcoordintpair = PlayerInstance.a(this.a(k + i1, l + j1, true));
|
|
if (arraylist.contains(chunkcoordintpair)) {
|
|
entityplayer.chunkCoordIntPairQueue.add(chunkcoordintpair);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
i %= 4;
|
|
|
|
for (k1 = 0; k1 < j * 2; ++k1) {
|
|
i1 += this.f[i][0];
|
|
j1 += this.f[i][1];
|
|
chunkcoordintpair = PlayerInstance.a(this.a(k + i1, l + j1, true));
|
|
if (arraylist.contains(chunkcoordintpair)) {
|
|
entityplayer.chunkCoordIntPairQueue.add(chunkcoordintpair);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void removePlayer(EntityPlayer entityplayer) {
|
|
int i = (int) entityplayer.d >> 4;
|
|
int j = (int) entityplayer.e >> 4;
|
|
|
|
for (int k = i - this.e; k <= i + this.e; ++k) {
|
|
for (int l = j - this.e; l <= j + this.e; ++l) {
|
|
PlayerInstance playerinstance = this.a(k, l, false);
|
|
|
|
if (playerinstance != null) {
|
|
playerinstance.b(entityplayer);
|
|
}
|
|
}
|
|
}
|
|
|
|
this.managedPlayers.remove(entityplayer);
|
|
}
|
|
|
|
private boolean a(int i, int j, int k, int l, int i1) {
|
|
int j1 = i - k;
|
|
int k1 = j - l;
|
|
|
|
return j1 >= -i1 && j1 <= i1 ? k1 >= -i1 && k1 <= i1 : false;
|
|
}
|
|
|
|
public void movePlayer(EntityPlayer entityplayer) {
|
|
int i = (int) entityplayer.locX >> 4;
|
|
int j = (int) entityplayer.locZ >> 4;
|
|
double d0 = entityplayer.d - entityplayer.locX;
|
|
double d1 = entityplayer.e - entityplayer.locZ;
|
|
double d2 = d0 * d0 + d1 * d1;
|
|
|
|
if (d2 >= 64.0D) {
|
|
int k = (int) entityplayer.d >> 4;
|
|
int l = (int) entityplayer.e >> 4;
|
|
int i1 = this.e;
|
|
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)) {
|
|
chunksToLoad.add(new ChunkCoordIntPair(l1, i2)); // CraftBukkit
|
|
}
|
|
|
|
if (!this.a(l1 - j1, i2 - k1, i, j, i1)) {
|
|
PlayerInstance playerinstance = this.a(l1 - j1, i2 - k1, false);
|
|
|
|
if (playerinstance != null) {
|
|
playerinstance.b(entityplayer);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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 (i1 > 1 || i1 < -1 || j1 > 1 || j1 < -1) {
|
|
Collections.sort(entityplayer.chunkCoordIntPairQueue, new ChunkCoordComparator(entityplayer));
|
|
}
|
|
// CraftBukkit end
|
|
}
|
|
}
|
|
}
|
|
|
|
public boolean a(EntityPlayer entityplayer, int i, int j) {
|
|
PlayerInstance playerinstance = this.a(i, j, false);
|
|
|
|
return playerinstance == null ? false : PlayerInstance.b(playerinstance).contains(entityplayer) && !entityplayer.chunkCoordIntPairQueue.contains(PlayerInstance.a(playerinstance));
|
|
}
|
|
|
|
public static int getFurthestViewableBlock(int i) {
|
|
return i * 16 - 16;
|
|
}
|
|
|
|
static WorldServer a(PlayerManager playermanager) {
|
|
return playermanager.world;
|
|
}
|
|
|
|
static LongHashMap b(PlayerManager playermanager) {
|
|
return playermanager.c;
|
|
}
|
|
|
|
static Queue c(PlayerManager playermanager) { // CraftBukkit List -> Queue
|
|
return playermanager.d;
|
|
}
|
|
|
|
// 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
|
|
}
|