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

324 lines
9.4 KiB
Java
Raw Normal View History

2011-01-04 16:54:41 +01:00
package net.minecraft.server;
import java.io.IOException;
2011-01-29 22:50:29 +01:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
// CraftBukkit start
import org.bukkit.Location;
2011-01-04 16:54:41 +01:00
import org.bukkit.craftbukkit.CraftChunk;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.entity.Player;
2011-01-04 16:54:41 +01:00
import org.bukkit.event.Event.Type;
import org.bukkit.event.world.ChunkLoadEvent;
// CraftBukkit end
2011-01-04 16:54:41 +01:00
public class ChunkProviderServer implements IChunkProvider {
2011-01-04 16:54:41 +01:00
2011-02-07 09:43:51 +01:00
private LongHashset a = new LongHashset(); // CraftBukkit
2011-01-04 16:54:41 +01:00
private Chunk b;
private IChunkProvider c;
private IChunkLoader d;
2011-02-07 09:43:51 +01:00
private LongHashtable<Chunk> e = new LongHashtable<Chunk>(); // CraftBukkit
2011-01-29 22:50:29 +01:00
private List f = new ArrayList();
2011-01-04 16:54:41 +01:00
private WorldServer g;
public ChunkProviderServer(WorldServer worldserver, IChunkLoader ichunkloader, IChunkProvider ichunkprovider) {
2011-01-29 22:50:29 +01:00
this.b = new EmptyChunk(worldserver, new byte['\u8000'], 0, 0);
this.g = worldserver;
this.d = ichunkloader;
this.c = ichunkprovider;
2011-01-04 16:54:41 +01:00
}
2011-02-04 21:11:57 +01:00
// CraftBukkit start
public org.bukkit.Chunk[] getLoadedChunks() {
Object[] chunks = e.values().toArray();
org.bukkit.Chunk[] craftChunks = new CraftChunk[chunks.length];
for(int cnt =0;cnt<chunks.length;cnt++) {
craftChunks[cnt] = ((Chunk)chunks[cnt]).bukkitChunk;
}
return craftChunks;
}
2011-01-04 16:54:41 +01:00
public boolean a(int i, int j) {
2011-02-07 06:56:07 +01:00
return this.e.containsKey(i, j);
2011-01-04 16:54:41 +01:00
}
public void saveChunk(int x, int z) {
Chunk chunk = this.b(x, z);
chunk.e();
this.b(chunk);
this.a(chunk);
}
public boolean unloadChunk(int x, int z, boolean save, boolean safe) {
if (safe && isChunkInUse(x, z))
return false;
Chunk chunk = this.b(x, z);
if (save)
saveChunk(x, z);
this.a.remove(x, z);
this.e.remove(x, z);
this.f.remove(chunk);
return true;
}
public boolean unloadChunkRequest(int x, int z, boolean safe) {
if (safe && isChunkInUse(x, z))
return false;
c(x, z);
return true;
}
public boolean isChunkInUse(int x, int z) {
CraftServer server = g.getServer();
Player[] players = server.getOnlinePlayers();
for (Player player : players) {
Location loc = player.getLocation();
if (loc.getWorld() != g.getWorld()) {
continue;
}
// If the chunk is within 256 blocks of a player, refuse to accept the unload request
// This is larger than the distance of loaded chunks that actually surround a player
// The player is the center of a 21x21 chunk grid, so the edge is 10 chunks (160 blocks) away from the player
if (Math.abs(loc.getBlockX() - (x << 4)) <= 256 && Math.abs(loc.getBlockZ() - (z << 4)) <= 256) {
return false;
}
}
return true;
}
public Chunk loadChunk(int x, int z, boolean generate) {
if (generate) {
// Use the default variant of loadChunk when generate == true.
return d(x, z);
}
this.a.remove(x, z);
Chunk chunk = (Chunk) this.e.get(x, z);
if (chunk == null) {
chunk = this.e(x, z);
if (chunk != null) {
this.e.put(x, z, chunk);
this.f.add(chunk);
chunk.c();
chunk.d();
if (!chunk.n && this.a(x + 1, z + 1) && this.a(x, z + 1) && this.a(x + 1, z)) {
this.a(this, x, z);
}
if (this.a(x - 1, z) && !this.b(x - 1, z).n && this.a(x - 1, z + 1) && this.a(x, z + 1) && this.a(x - 1, z)) {
this.a(this, x - 1, z);
}
if (this.a(x, z - 1) && !this.b(x, z - 1).n && this.a(x + 1, z - 1) && this.a(x, z - 1) && this.a(x + 1, z)) {
this.a(this, x, z - 1);
}
if (this.a(x - 1, z - 1) && !this.b(x - 1, z - 1).n && this.a(x - 1, z - 1) && this.a(x, z - 1) && this.a(x - 1, z)) {
this.a(this, x - 1, z - 1);
}
}
}
return chunk;
}
// CraftBukkit end
2011-01-04 16:54:41 +01:00
public void c(int i, int j) {
2011-01-29 22:50:29 +01:00
int k = i * 16 + 8 - this.g.spawnX;
int l = j * 16 + 8 - this.g.spawnZ;
short short1 = 128;
2011-01-04 16:54:41 +01:00
2011-01-29 22:50:29 +01:00
if (k < -short1 || k > short1 || l < -short1 || l > short1) {
2011-02-07 09:43:51 +01:00
this.a.add(i, j); // CraftBukkit
2011-01-04 16:54:41 +01:00
}
}
public Chunk d(int i, int j) {
2011-02-07 09:43:51 +01:00
// CraftBukkit start
2011-02-07 06:56:07 +01:00
this.a.remove(i, j);
Chunk chunk = (Chunk) this.e.get(i, j);
2011-02-07 09:43:51 +01:00
// CraftBukkit end
2011-01-04 16:54:41 +01:00
if (chunk == null) {
2011-01-29 22:50:29 +01:00
chunk = this.e(i, j);
2011-01-04 16:54:41 +01:00
if (chunk == null) {
2011-01-29 22:50:29 +01:00
if (this.c == null) {
chunk = this.b;
2011-01-04 16:54:41 +01:00
} else {
2011-01-29 22:50:29 +01:00
chunk = this.c.b(i, j);
2011-01-04 16:54:41 +01:00
}
}
2011-01-29 22:50:29 +01:00
2011-02-07 09:43:51 +01:00
this.e.put(i, j, chunk); // CraftBukkit
2011-01-29 22:50:29 +01:00
this.f.add(chunk);
2011-01-04 16:54:41 +01:00
if (chunk != null) {
2011-01-14 14:31:10 +01:00
chunk.c();
2011-01-04 16:54:41 +01:00
chunk.d();
}
// CraftBukkit start
2011-01-04 16:54:41 +01:00
CraftServer server = g.getServer();
if (server != null) {
/*
* If it's a new world, the first few chunks are generated inside
* the World constructor. We can't reliably alter that, so we have
* no way of creating a CraftWorld/CraftServer at that point.
*/
2011-02-01 23:49:28 +01:00
server.getPluginManager().callEvent(new ChunkLoadEvent(Type.CHUNK_LOADED, chunk.bukkitChunk));
2011-01-04 16:54:41 +01:00
}
// CraftBukkit end
2011-01-04 16:54:41 +01:00
2011-01-29 22:50:29 +01:00
if (!chunk.n && this.a(i + 1, j + 1) && this.a(i, j + 1) && this.a(i + 1, j)) {
this.a(this, i, j);
2011-01-04 16:54:41 +01:00
}
2011-01-29 22:50:29 +01:00
if (this.a(i - 1, j) && !this.b(i - 1, j).n && this.a(i - 1, j + 1) && this.a(i, j + 1) && this.a(i - 1, j)) {
this.a(this, i - 1, j);
2011-01-04 16:54:41 +01:00
}
2011-01-29 22:50:29 +01:00
if (this.a(i, j - 1) && !this.b(i, j - 1).n && this.a(i + 1, j - 1) && this.a(i, j - 1) && this.a(i + 1, j)) {
this.a(this, i, j - 1);
2011-01-04 16:54:41 +01:00
}
2011-01-29 22:50:29 +01:00
if (this.a(i - 1, j - 1) && !this.b(i - 1, j - 1).n && this.a(i - 1, j - 1) && this.a(i, j - 1) && this.a(i - 1, j)) {
this.a(this, i - 1, j - 1);
2011-01-04 16:54:41 +01:00
}
}
2011-01-29 22:50:29 +01:00
2011-01-04 16:54:41 +01:00
return chunk;
}
public Chunk b(int i, int j) {
2011-02-07 09:43:51 +01:00
Chunk chunk = (Chunk) this.e.get(i, j); // CraftBukkit
2011-01-04 16:54:41 +01:00
2011-01-29 22:50:29 +01:00
return chunk == null ? (this.g.x ? this.d(i, j) : this.b) : chunk;
2011-01-04 16:54:41 +01:00
}
private Chunk e(int i, int j) {
2011-01-29 22:50:29 +01:00
if (this.d == null) {
2011-01-04 16:54:41 +01:00
return null;
2011-01-29 22:50:29 +01:00
} else {
try {
Chunk chunk = this.d.a(this.g, i, j);
2011-01-04 16:54:41 +01:00
2011-01-29 22:50:29 +01:00
if (chunk != null) {
chunk.r = this.g.e;
}
return chunk;
} catch (Exception exception) {
exception.printStackTrace();
return null;
2011-01-04 16:54:41 +01:00
}
}
}
private void a(Chunk chunk) {
2011-01-29 22:50:29 +01:00
if (this.d != null) {
try {
this.d.b(this.g, chunk);
} catch (Exception exception) {
exception.printStackTrace();
}
2011-01-04 16:54:41 +01:00
}
}
private void b(Chunk chunk) {
2011-01-29 22:50:29 +01:00
if (this.d != null) {
try {
chunk.r = this.g.e;
this.d.a(this.g, chunk);
} catch (Exception ioexception) { // CraftBukkit - IOException -> Exception
ioexception.printStackTrace();
}
2011-01-04 16:54:41 +01:00
}
}
public void a(IChunkProvider ichunkprovider, int i, int j) {
2011-01-29 22:50:29 +01:00
Chunk chunk = this.b(i, j);
2011-01-04 16:54:41 +01:00
if (!chunk.n) {
chunk.n = true;
2011-01-29 22:50:29 +01:00
if (this.c != null) {
this.c.a(ichunkprovider, i, j);
2011-01-04 16:54:41 +01:00
chunk.f();
}
}
}
public boolean a(boolean flag, IProgressUpdate iprogressupdate) {
int i = 0;
2011-01-29 22:50:29 +01:00
for (int j = 0; j < this.f.size(); ++j) {
Chunk chunk = (Chunk) this.f.get(j);
2011-01-04 16:54:41 +01:00
if (flag && !chunk.p) {
2011-01-29 22:50:29 +01:00
this.a(chunk);
2011-01-04 16:54:41 +01:00
}
2011-01-29 22:50:29 +01:00
if (chunk.a(flag)) {
this.b(chunk);
chunk.o = false;
++i;
if (i == 24 && !flag) {
return false;
}
2011-01-04 16:54:41 +01:00
}
}
if (flag) {
2011-01-29 22:50:29 +01:00
if (this.d == null) {
2011-01-04 16:54:41 +01:00
return true;
}
2011-01-29 22:50:29 +01:00
this.d.b();
2011-01-04 16:54:41 +01:00
}
2011-01-29 22:50:29 +01:00
2011-01-04 16:54:41 +01:00
return true;
}
public boolean a() {
2011-01-29 22:50:29 +01:00
if (!this.g.C) {
for (int i = 0; i < 100; ++i) {
if (!this.a.isEmpty()) {
2011-02-07 09:43:51 +01:00
// CraftBukkit start
2011-02-07 06:56:07 +01:00
long chunkcoordinates = this.a.popFirst();
Chunk chunk = e.get(chunkcoordinates);
2011-02-07 09:43:51 +01:00
if (chunk == null) continue;
// CraftBukkit end
2011-01-04 16:54:41 +01:00
chunk.e();
2011-01-29 22:50:29 +01:00
this.b(chunk);
this.a(chunk);
2011-02-07 09:43:51 +01:00
// this.a.remove(chunkcoordinates); // CraftBukkit
2011-01-29 22:50:29 +01:00
this.e.remove(chunkcoordinates);
this.f.remove(chunk);
2011-01-04 16:54:41 +01:00
}
}
2011-01-29 22:50:29 +01:00
if (this.d != null) {
this.d.a();
2011-01-04 16:54:41 +01:00
}
}
2011-01-29 22:50:29 +01:00
return this.c.a();
2011-01-04 16:54:41 +01:00
}
public boolean b() {
2011-01-29 22:50:29 +01:00
return !this.g.C;
2011-01-04 16:54:41 +01:00
}
}