2011-02-01 20:26:07 +01:00
|
|
|
package net.minecraft.server;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2012-11-06 13:05:28 +01:00
|
|
|
// CraftBukkit start
|
2012-11-30 09:49:19 +01:00
|
|
|
import java.util.Collections;
|
2012-11-06 13:05:28 +01:00
|
|
|
import java.util.Queue;
|
2012-11-30 09:49:19 +01:00
|
|
|
import java.util.LinkedList;
|
2012-11-06 13:05:28 +01:00
|
|
|
// CraftBukkit end
|
2012-08-05 05:12:16 +02:00
|
|
|
|
2012-12-20 05:03:52 +01:00
|
|
|
public class PlayerChunkMap {
|
2011-02-01 20:26:07 +01:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
private final WorldServer world;
|
|
|
|
private final List managedPlayers = new ArrayList();
|
|
|
|
private final LongHashMap c = new LongHashMap();
|
2012-08-05 05:12:16 +02:00
|
|
|
private final Queue d = new java.util.concurrent.ConcurrentLinkedQueue(); // CraftBukkit ArrayList -> ConcurrentLinkedQueue
|
2013-07-01 13:03:00 +02:00
|
|
|
private final Queue e = new java.util.concurrent.ConcurrentLinkedQueue(); // CraftBukkit ArrayList -> ConcurrentLinkedQueue
|
|
|
|
private final int f;
|
|
|
|
private long g;
|
|
|
|
private final int[][] h = new int[][] { { 1, 0}, { 0, 1}, { -1, 0}, { 0, -1}};
|
2012-03-08 07:58:30 +01:00
|
|
|
private boolean wasNotEmpty; // CraftBukkit
|
2011-05-26 14:48:22 +02:00
|
|
|
|
2012-12-20 05:03:52 +01:00
|
|
|
public PlayerChunkMap(WorldServer worldserver, int i) {
|
2012-07-29 09:33:13 +02:00
|
|
|
if (i > 15) {
|
2011-08-15 20:27:02 +02:00
|
|
|
throw new IllegalArgumentException("Too big view radius!");
|
2012-07-29 09:33:13 +02:00
|
|
|
} else if (i < 3) {
|
2011-08-15 20:27:02 +02:00
|
|
|
throw new IllegalArgumentException("Too small view radius!");
|
|
|
|
} else {
|
2013-07-01 13:03:00 +02:00
|
|
|
this.f = i;
|
2012-07-29 09:33:13 +02:00
|
|
|
this.world = worldserver;
|
2011-08-15 20:27:02 +02:00
|
|
|
}
|
2011-05-26 14:48:22 +02:00
|
|
|
}
|
2011-02-23 13:56:36 +01:00
|
|
|
|
2011-05-26 14:48:22 +02:00
|
|
|
public WorldServer a() {
|
2012-07-29 09:33:13 +02:00
|
|
|
return this.world;
|
2011-02-01 20:26:07 +01:00
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public void flush() {
|
2013-07-01 13:03:00 +02:00
|
|
|
long i = this.world.getTime();
|
|
|
|
int j;
|
|
|
|
PlayerChunk playerchunk;
|
|
|
|
|
|
|
|
if (i - this.g > 8000L) {
|
|
|
|
this.g = i;
|
|
|
|
|
|
|
|
// CraftBukkit start - Use iterator
|
|
|
|
java.util.Iterator iterator = this.e.iterator();
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
playerchunk = (PlayerChunk) iterator.next();
|
|
|
|
playerchunk.b();
|
|
|
|
playerchunk.a();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
java.util.Iterator iterator = this.d.iterator();
|
2012-07-29 09:33:13 +02:00
|
|
|
|
2013-07-01 13:03:00 +02:00
|
|
|
while (iterator.hasNext()) {
|
|
|
|
playerchunk = (PlayerChunk) iterator.next();
|
|
|
|
playerchunk.b();
|
|
|
|
iterator.remove();
|
|
|
|
// CraftBukkit end
|
|
|
|
}
|
2011-02-01 20:26:07 +01:00
|
|
|
}
|
|
|
|
|
2013-03-25 05:22:32 +01:00
|
|
|
// this.d.clear(); // CraftBukkit - Removals are already covered
|
2012-01-12 23:10:13 +01:00
|
|
|
if (this.managedPlayers.isEmpty()) {
|
2013-03-25 05:22:32 +01:00
|
|
|
if (!wasNotEmpty) return; // CraftBukkit - Only do unload when we go from non-empty to empty
|
2012-07-29 09:33:13 +02:00
|
|
|
WorldProvider worldprovider = this.world.worldProvider;
|
2012-01-12 23:10:13 +01:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
if (!worldprovider.e()) {
|
|
|
|
this.world.chunkProviderServer.a();
|
2012-01-12 23:10:13 +01:00
|
|
|
}
|
2012-03-08 07:58:30 +01:00
|
|
|
// CraftBukkit start
|
|
|
|
wasNotEmpty = false;
|
|
|
|
} else {
|
|
|
|
wasNotEmpty = true;
|
2012-01-12 23:10:13 +01:00
|
|
|
}
|
2012-03-08 07:58:30 +01:00
|
|
|
// CraftBukkit end
|
2011-02-01 20:26:07 +01:00
|
|
|
}
|
|
|
|
|
2012-12-20 05:03:52 +01:00
|
|
|
private PlayerChunk a(int i, int j, boolean flag) {
|
2011-02-01 20:26:07 +01:00
|
|
|
long k = (long) i + 2147483647L | (long) j + 2147483647L << 32;
|
2012-12-20 05:03:52 +01:00
|
|
|
PlayerChunk playerchunk = (PlayerChunk) this.c.getEntry(k);
|
2011-02-01 20:26:07 +01:00
|
|
|
|
2012-12-20 05:03:52 +01:00
|
|
|
if (playerchunk == null && flag) {
|
|
|
|
playerchunk = new PlayerChunk(this, i, j);
|
|
|
|
this.c.put(k, playerchunk);
|
2013-07-01 13:03:00 +02:00
|
|
|
this.e.add(playerchunk);
|
2011-02-01 20:26:07 +01:00
|
|
|
}
|
|
|
|
|
2012-12-20 05:03:52 +01:00
|
|
|
return playerchunk;
|
2011-02-01 20:26:07 +01:00
|
|
|
}
|
2012-08-13 01:40:49 +02:00
|
|
|
// CraftBukkit start
|
|
|
|
public final boolean isChunkInUse(int x, int z) {
|
2012-12-20 05:03:52 +01:00
|
|
|
PlayerChunk pi = a(x, z, false);
|
2012-08-13 01:40:49 +02:00
|
|
|
if (pi != null) {
|
2012-12-20 05:03:52 +01:00
|
|
|
return (PlayerChunk.b(pi).size() > 0);
|
2012-08-13 01:40:49 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// CraftBukkit end
|
2011-02-01 20:26:07 +01:00
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public void flagDirty(int i, int j, int k) {
|
2011-02-01 20:26:07 +01:00
|
|
|
int l = i >> 4;
|
|
|
|
int i1 = k >> 4;
|
2012-12-20 05:03:52 +01:00
|
|
|
PlayerChunk playerchunk = this.a(l, i1, false);
|
2011-02-01 20:26:07 +01:00
|
|
|
|
2012-12-20 05:03:52 +01:00
|
|
|
if (playerchunk != null) {
|
|
|
|
playerchunk.a(i & 15, j, k & 15);
|
2011-02-01 20:26:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public void addPlayer(EntityPlayer entityplayer) {
|
2011-02-01 20:26:07 +01:00
|
|
|
int i = (int) entityplayer.locX >> 4;
|
|
|
|
int j = (int) entityplayer.locZ >> 4;
|
|
|
|
|
|
|
|
entityplayer.d = entityplayer.locX;
|
|
|
|
entityplayer.e = entityplayer.locZ;
|
2012-07-29 09:33:13 +02:00
|
|
|
|
2013-03-25 05:22:32 +01:00
|
|
|
// CraftBukkit start - Load nearby chunks first
|
2012-11-30 09:49:19 +01:00
|
|
|
List<ChunkCoordIntPair> chunkList = new LinkedList<ChunkCoordIntPair>();
|
2013-07-01 13:03:00 +02:00
|
|
|
for (int k = i - this.f; k <= i + this.f; ++k) {
|
|
|
|
for (int l = j - this.f; l <= j + this.f; ++l) {
|
2012-11-30 09:49:19 +01:00
|
|
|
chunkList.add(new ChunkCoordIntPair(k, l));
|
2012-07-29 09:33:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-30 09:49:19 +01:00
|
|
|
Collections.sort(chunkList, new ChunkCoordComparator(entityplayer));
|
|
|
|
for (ChunkCoordIntPair pair : chunkList) {
|
|
|
|
this.a(pair.x, pair.z, true).a(entityplayer);
|
|
|
|
}
|
|
|
|
// CraftBukkit end
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
this.managedPlayers.add(entityplayer);
|
|
|
|
this.b(entityplayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void b(EntityPlayer entityplayer) {
|
|
|
|
ArrayList arraylist = new ArrayList(entityplayer.chunkCoordIntPairQueue);
|
|
|
|
int i = 0;
|
2013-07-01 13:03:00 +02:00
|
|
|
int j = this.f;
|
2012-07-29 09:33:13 +02:00
|
|
|
int k = (int) entityplayer.locX >> 4;
|
|
|
|
int l = (int) entityplayer.locZ >> 4;
|
2011-02-23 03:37:56 +01:00
|
|
|
int i1 = 0;
|
2011-05-26 14:48:22 +02:00
|
|
|
int j1 = 0;
|
2012-12-20 05:03:52 +01:00
|
|
|
ChunkCoordIntPair chunkcoordintpair = PlayerChunk.a(this.a(k, l, true));
|
2011-02-01 20:26:07 +01:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
entityplayer.chunkCoordIntPairQueue.clear();
|
|
|
|
if (arraylist.contains(chunkcoordintpair)) {
|
|
|
|
entityplayer.chunkCoordIntPairQueue.add(chunkcoordintpair);
|
|
|
|
}
|
2011-02-01 21:40:52 +01:00
|
|
|
|
2011-05-26 14:48:22 +02:00
|
|
|
int k1;
|
2011-02-01 21:40:52 +01:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
for (k1 = 1; k1 <= j * 2; ++k1) {
|
2011-05-26 14:48:22 +02:00
|
|
|
for (int l1 = 0; l1 < 2; ++l1) {
|
2013-07-01 13:03:00 +02:00
|
|
|
int[] aint = this.h[i++ % 4];
|
2011-02-01 21:40:52 +01:00
|
|
|
|
2011-05-26 14:48:22 +02:00
|
|
|
for (int i2 = 0; i2 < k1; ++i2) {
|
|
|
|
i1 += aint[0];
|
|
|
|
j1 += aint[1];
|
2012-12-20 05:03:52 +01:00
|
|
|
chunkcoordintpair = PlayerChunk.a(this.a(k + i1, l + j1, true));
|
2012-07-29 09:33:13 +02:00
|
|
|
if (arraylist.contains(chunkcoordintpair)) {
|
|
|
|
entityplayer.chunkCoordIntPairQueue.add(chunkcoordintpair);
|
|
|
|
}
|
2011-02-01 21:40:52 +01:00
|
|
|
}
|
2011-02-01 20:26:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
i %= 4;
|
2011-02-23 03:37:56 +01:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
for (k1 = 0; k1 < j * 2; ++k1) {
|
2013-07-01 13:03:00 +02:00
|
|
|
i1 += this.h[i][0];
|
|
|
|
j1 += this.h[i][1];
|
2012-12-20 05:03:52 +01:00
|
|
|
chunkcoordintpair = PlayerChunk.a(this.a(k + i1, l + j1, true));
|
2012-07-29 09:33:13 +02:00
|
|
|
if (arraylist.contains(chunkcoordintpair)) {
|
|
|
|
entityplayer.chunkCoordIntPairQueue.add(chunkcoordintpair);
|
|
|
|
}
|
2011-02-01 21:40:52 +01:00
|
|
|
}
|
2011-02-01 20:26:07 +01:00
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public void removePlayer(EntityPlayer entityplayer) {
|
2011-02-01 20:26:07 +01:00
|
|
|
int i = (int) entityplayer.d >> 4;
|
|
|
|
int j = (int) entityplayer.e >> 4;
|
|
|
|
|
2013-07-01 13:03:00 +02:00
|
|
|
for (int k = i - this.f; k <= i + this.f; ++k) {
|
|
|
|
for (int l = j - this.f; l <= j + this.f; ++l) {
|
2012-12-20 05:03:52 +01:00
|
|
|
PlayerChunk playerchunk = this.a(k, l, false);
|
2011-02-01 20:26:07 +01:00
|
|
|
|
2012-12-20 05:03:52 +01:00
|
|
|
if (playerchunk != null) {
|
|
|
|
playerchunk.b(entityplayer);
|
2011-02-01 20:26:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-27 00:25:01 +02:00
|
|
|
this.managedPlayers.remove(entityplayer);
|
2011-02-01 20:26:07 +01:00
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
private boolean a(int i, int j, int k, int l, int i1) {
|
|
|
|
int j1 = i - k;
|
|
|
|
int k1 = j - l;
|
2011-02-01 20:26:07 +01:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
return j1 >= -i1 && j1 <= i1 ? k1 >= -i1 && k1 <= i1 : false;
|
2011-02-01 20:26:07 +01:00
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public void movePlayer(EntityPlayer entityplayer) {
|
2011-02-01 20:26:07 +01:00
|
|
|
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;
|
2013-07-01 13:03:00 +02:00
|
|
|
int i1 = this.f;
|
2012-07-29 09:33:13 +02:00
|
|
|
int j1 = i - k;
|
|
|
|
int k1 = j - l;
|
2012-11-30 09:49:19 +01:00
|
|
|
List<ChunkCoordIntPair> chunksToLoad = new LinkedList<ChunkCoordIntPair>(); // CraftBukkit
|
2012-07-29 09:33:13 +02:00
|
|
|
|
|
|
|
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)) {
|
2012-11-30 09:49:19 +01:00
|
|
|
chunksToLoad.add(new ChunkCoordIntPair(l1, i2)); // CraftBukkit
|
2011-02-01 20:26:07 +01:00
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
if (!this.a(l1 - j1, i2 - k1, i, j, i1)) {
|
2012-12-20 05:03:52 +01:00
|
|
|
PlayerChunk playerchunk = this.a(l1 - j1, i2 - k1, false);
|
2011-02-01 20:26:07 +01:00
|
|
|
|
2012-12-20 05:03:52 +01:00
|
|
|
if (playerchunk != null) {
|
|
|
|
playerchunk.b(entityplayer);
|
2011-02-01 20:26:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
this.b(entityplayer);
|
2011-02-01 20:26:07 +01:00
|
|
|
entityplayer.d = entityplayer.locX;
|
|
|
|
entityplayer.e = entityplayer.locZ;
|
2011-06-08 23:00:30 +02:00
|
|
|
|
|
|
|
// CraftBukkit start - send nearest chunks first
|
2012-11-30 09:49:19 +01:00
|
|
|
Collections.sort(chunksToLoad, new ChunkCoordComparator(entityplayer));
|
|
|
|
for (ChunkCoordIntPair pair : chunksToLoad) {
|
|
|
|
this.a(pair.x, pair.z, true).a(entityplayer);
|
|
|
|
}
|
2011-06-27 00:25:01 +02:00
|
|
|
|
2012-11-30 09:49:19 +01:00
|
|
|
if (i1 > 1 || i1 < -1 || j1 > 1 || j1 < -1) {
|
|
|
|
Collections.sort(entityplayer.chunkCoordIntPairQueue, new ChunkCoordComparator(entityplayer));
|
2011-06-08 23:00:30 +02:00
|
|
|
}
|
|
|
|
// CraftBukkit end
|
2011-02-01 20:26:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public boolean a(EntityPlayer entityplayer, int i, int j) {
|
2012-12-20 05:03:52 +01:00
|
|
|
PlayerChunk playerchunk = this.a(i, j, false);
|
2012-07-29 09:33:13 +02:00
|
|
|
|
2012-12-20 05:03:52 +01:00
|
|
|
return playerchunk == null ? false : PlayerChunk.b(playerchunk).contains(entityplayer) && !entityplayer.chunkCoordIntPairQueue.contains(PlayerChunk.a(playerchunk));
|
2011-02-01 20:26:07 +01:00
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public static int getFurthestViewableBlock(int i) {
|
|
|
|
return i * 16 - 16;
|
2011-02-01 20:26:07 +01:00
|
|
|
}
|
|
|
|
|
2012-12-20 05:03:52 +01:00
|
|
|
static WorldServer a(PlayerChunkMap playerchunkmap) {
|
|
|
|
return playerchunkmap.world;
|
2012-07-29 09:33:13 +02:00
|
|
|
}
|
|
|
|
|
2012-12-20 05:03:52 +01:00
|
|
|
static LongHashMap b(PlayerChunkMap playerchunkmap) {
|
|
|
|
return playerchunkmap.c;
|
2011-02-01 20:26:07 +01:00
|
|
|
}
|
2012-07-29 09:33:13 +02:00
|
|
|
|
2012-12-20 05:03:52 +01:00
|
|
|
static Queue c(PlayerChunkMap playermanager) { // CraftBukkit List -> Queue
|
2013-07-01 13:03:00 +02:00
|
|
|
return playermanager.e;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Queue d(PlayerChunkMap playermanager) { // CraftBukkit List -> Queue
|
2012-07-29 09:33:13 +02:00
|
|
|
return playermanager.d;
|
|
|
|
}
|
2012-11-30 09:49:19 +01:00
|
|
|
|
2013-03-25 05:22:32 +01:00
|
|
|
// CraftBukkit start - Sorter to load nearby chunks first
|
2012-11-30 09:49:19 +01:00
|
|
|
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
|
2011-02-01 20:26:07 +01:00
|
|
|
}
|