Added loadChunk, unloadChunk and unloadChunkRequest.

This commit is contained in:
Dinnerbone 2011-02-08 14:26:55 +00:00
parent 6b9b59db4e
commit 3526a66fbe
2 changed files with 114 additions and 2 deletions

View File

@ -9,8 +9,10 @@ import java.util.Map;
import java.util.Set;
// CraftBukkit start
import org.bukkit.Location;
import org.bukkit.craftbukkit.CraftChunk;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Type;
import org.bukkit.event.world.ChunkLoadEvent;
// CraftBukkit end
@ -47,8 +49,94 @@ public class ChunkProviderServer implements IChunkProvider {
public boolean a(int i, int j) {
return this.e.containsKey(i, j);
}
// CraftBukkit end
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
public void c(int i, int j) {
int k = i * 16 + 8 - this.g.spawnX;
int l = j * 16 + 8 - this.g.spawnZ;

View File

@ -75,7 +75,31 @@ public class CraftWorld implements World {
}
public void loadChunk(int x, int z) {
world.A.d(x, z);
loadChunk(x, z, true);
}
public boolean loadChunk(int x, int z, boolean generate) {
return world.A.loadChunk(x, z, generate) != null;
}
public boolean unloadChunk(int x, int z) {
return unloadChunk(x, z, true);
}
public boolean unloadChunk(int x, int z, boolean save) {
return unloadChunk(x, z, save, false);
}
public boolean unloadChunk(int x, int z, boolean save, boolean safe) {
return world.A.unloadChunk(x, z, save, safe);
}
public boolean unloadChunkRequest(int x, int z) {
return unloadChunkRequest(x, z, true);
}
public boolean unloadChunkRequest(int x, int z, boolean safe) {
return world.A.unloadChunkRequest(x, z, safe);
}
public boolean isChunkLoaded(Chunk chunk) {