Paper/src/main/java/net/minecraft/server/PlayerManager.java

253 Zeilen
8.3 KiB
Java

package net.minecraft.server;
import java.util.ArrayList;
import java.util.List;
2012-11-06 13:05:28 +01:00
// CraftBukkit start
import java.util.Queue;
import java.util.Iterator;
// CraftBukkit end
public class PlayerManager {
2012-07-29 09:33:13 +02:00
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
2012-07-29 09:33:13 +02:00
private final int e;
private final int[][] f = new int[][] { { 1, 0}, { 0, 1}, { -1, 0}, { 0, -1}};
private boolean wasNotEmpty; // CraftBukkit
2011-05-26 14:48:22 +02:00
2012-07-29 09:33:13 +02:00
public PlayerManager(WorldServer worldserver, int i) {
if (i > 15) {
throw new IllegalArgumentException("Too big view radius!");
2012-07-29 09:33:13 +02:00
} else if (i < 3) {
throw new IllegalArgumentException("Too small view radius!");
} else {
this.e = i;
2012-07-29 09:33:13 +02:00
this.world = worldserver;
}
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;
}
public void flush() {
2012-11-06 13:05:28 +01:00
// CraftBukkit start - use iterator
2012-07-29 09:33:13 +02:00
Iterator iterator = this.d.iterator();
while (iterator.hasNext()) {
PlayerInstance playerinstance = (PlayerInstance) iterator.next();
playerinstance.a();
2012-11-06 13:05:28 +01:00
iterator.remove();
}
2012-11-06 13:05:28 +01:00
// CraftBukkit end
// this.d.clear(); // CraftBukkit - removals are already covered
2012-01-12 23:10:13 +01:00
if (this.managedPlayers.isEmpty()) {
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
}
// CraftBukkit start
wasNotEmpty = false;
} else {
wasNotEmpty = true;
2012-01-12 23:10:13 +01:00
}
// CraftBukkit end
}
2011-02-23 03:37:56 +01:00
private PlayerInstance a(int i, int j, boolean flag) {
long k = (long) i + 2147483647L | (long) j + 2147483647L << 32;
2012-07-29 09:33:13 +02:00
PlayerInstance playerinstance = (PlayerInstance) this.c.getEntry(k);
if (playerinstance == null && flag) {
2011-02-07 00:47:44 +01:00
playerinstance = new PlayerInstance(this, i, j);
2012-07-29 09:33:13 +02:00
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;
2011-02-23 03:37:56 +01:00
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;
2012-07-29 09:33:13 +02:00
for (int k = i - this.e; k <= i + this.e; ++k) {
for (int l = j - this.e; l <= j + this.e; ++l) {
this.a(k, l, true).a(entityplayer);
}
}
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;
2011-02-23 03:37:56 +01:00
int i1 = 0;
2011-05-26 14:48:22 +02:00
int j1 = 0;
2012-07-29 09:33:13 +02:00
ChunkCoordIntPair chunkcoordintpair = PlayerInstance.a(this.a(k, l, true));
2012-07-29 09:33:13 +02:00
entityplayer.chunkCoordIntPairQueue.clear();
if (arraylist.contains(chunkcoordintpair)) {
entityplayer.chunkCoordIntPairQueue.add(chunkcoordintpair);
}
2011-05-26 14:48:22 +02:00
int k1;
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) {
2012-07-29 09:33:13 +02:00
int[] aint = this.f[i++ % 4];
2011-05-26 14:48:22 +02:00
for (int i2 = 0; i2 < k1; ++i2) {
i1 += aint[0];
j1 += aint[1];
2012-07-29 09:33:13 +02:00
chunkcoordintpair = PlayerInstance.a(this.a(k + i1, l + j1, true));
if (arraylist.contains(chunkcoordintpair)) {
entityplayer.chunkCoordIntPairQueue.add(chunkcoordintpair);
}
}
}
}
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) {
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;
2012-07-29 09:33:13 +02:00
for (int k = i - this.e; k <= i + this.e; ++k) {
for (int l = j - this.e; l <= j + this.e; ++l) {
2011-02-23 03:37:56 +01:00
PlayerInstance playerinstance = this.a(k, l, false);
if (playerinstance != null) {
playerinstance.b(entityplayer);
}
}
}
this.managedPlayers.remove(entityplayer);
}
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;
2012-07-29 09:33:13 +02:00
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;
2012-07-29 09:33:13 +02:00
int i1 = this.e;
int j1 = i - k;
int k1 = j - l;
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);
}
2012-07-29 09:33:13 +02:00
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);
}
}
}
}
2012-07-29 09:33:13 +02:00
this.b(entityplayer);
entityplayer.d = entityplayer.locX;
entityplayer.e = entityplayer.locZ;
2011-06-08 23:00:30 +02:00
// CraftBukkit start - send nearest chunks first
if (i1 > 1 || i1 < -1 || j1 > 1 || j1 < -1) {
final int x = i;
final int z = j;
List<ChunkCoordIntPair> chunksToSend = entityplayer.chunkCoordIntPairQueue;
java.util.Collections.sort(chunksToSend, new java.util.Comparator<ChunkCoordIntPair>() {
2011-06-08 23:00:30 +02:00
public int compare(ChunkCoordIntPair a, ChunkCoordIntPair b) {
2011-06-12 00:02:58 +02:00
return Math.max(Math.abs(a.x - x), Math.abs(a.z - z)) - Math.max(Math.abs(b.x - x), Math.abs(b.z - z));
2011-06-08 23:00:30 +02:00
}
});
}
// CraftBukkit end
}
}
}
2012-07-29 09:33:13 +02:00
public boolean a(EntityPlayer entityplayer, int i, int j) {
PlayerInstance playerinstance = this.a(i, j, false);
2012-08-25 02:51:51 +02:00
return playerinstance == null ? false : PlayerInstance.b(playerinstance).contains(entityplayer) && !entityplayer.chunkCoordIntPairQueue.contains(PlayerInstance.a(playerinstance));
}
2012-07-29 09:33:13 +02:00
public static int getFurthestViewableBlock(int i) {
return i * 16 - 16;
}
2012-07-29 09:33:13 +02:00
static WorldServer a(PlayerManager playermanager) {
return playermanager.world;
}
static LongHashMap b(PlayerManager playermanager) {
return playermanager.c;
}
2012-07-29 09:33:13 +02:00
static Queue c(PlayerManager playermanager) { // CraftBukkit List -> Queue
2012-07-29 09:33:13 +02:00
return playermanager.d;
}
}