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

241 lines
7.2 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.Server;
2011-01-04 16:54:41 +01:00
import org.bukkit.craftbukkit.CraftChunk;
import org.bukkit.craftbukkit.CraftServer;
2011-03-01 19:19:50 +01:00
import org.bukkit.craftbukkit.util.LongHashset;
import org.bukkit.craftbukkit.util.LongHashtable;
2011-01-04 16:54:41 +01:00
import org.bukkit.event.Event.Type;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
// CraftBukkit end
2011-01-04 16:54:41 +01:00
2011-02-20 17:40:40 +01:00
public class ChunkProviderServer implements IChunkProvider {
public LongHashset a = new LongHashset(); // CraftBukkit
2011-03-02 21:27:44 +01:00
public Chunk b; // CraftBukkit
public IChunkProvider c; // CraftBukkit
2011-01-04 16:54:41 +01:00
private IChunkLoader d;
public LongHashtable<Chunk> e = new LongHashtable<Chunk>(); // CraftBukkit
2011-02-23 13:56:36 +01:00
public List f = new ArrayList(); // CraftBukkit
public WorldServer g; // CraftBukkit
2011-01-04 16:54:41 +01:00
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
}
public boolean a(int i, int j) {
2011-02-23 13:56:36 +01:00
return this.e.containsKey(i, j); // CraftBukkit
2011-01-04 16:54:41 +01:00
}
2011-01-04 16:54:41 +01:00
public void c(int i, int j) {
2011-02-23 03:37:56 +01:00
ChunkCoordinates chunkcoordinates = this.g.l();
int k = i * 16 + 8 - chunkcoordinates.a;
int l = j * 16 + 8 - chunkcoordinates.c;
2011-01-29 22:50:29 +01:00
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-03-18 22:57:22 +01:00
server.getPluginManager().callEvent(new ChunkLoadEvent(Type.CHUNK_LOAD, 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-02-24 09:51:00 +01:00
chunk = chunk == null ? (this.g.r ? this.d(i, j) : this.b) : chunk;
if (chunk == this.b) return chunk;
if (i != chunk.j || j != chunk.k) {
2011-02-24 09:51:00 +01:00
MinecraftServer.a.info("Chunk (" + chunk.j + ", " + chunk.k +") stored at (" + i + ", " + j + ")");
MinecraftServer.a.info(chunk.getClass().getName());
Throwable x = new Throwable();
x.fillInStackTrace();
x.printStackTrace();
}
return chunk;
2011-01-04 16:54:41 +01:00
}
2011-02-23 13:56:36 +01:00
public Chunk e(int i, int j) { // CraftBukkit - private->public
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) {
2011-02-23 03:37:56 +01:00
chunk.r = this.g.k();
2011-01-29 22:50:29 +01:00
}
return chunk;
} catch (Exception exception) {
exception.printStackTrace();
return null;
2011-01-04 16:54:41 +01:00
}
}
}
2011-02-23 13:56:36 +01:00
public void a(Chunk chunk) { // CraftBukkit - private->public
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
}
}
2011-02-23 13:56:36 +01:00
public void b(Chunk chunk) { // CraftBukkit - private->public
2011-01-29 22:50:29 +01:00
if (this.d != null) {
try {
2011-02-23 03:37:56 +01:00
chunk.r = this.g.k();
2011-01-29 22:50:29 +01:00
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-02-23 03:37:56 +01:00
if (!this.g.w) {
// CraftBukkit start
Server server = g.getServer();
2011-03-02 09:24:22 +01:00
for (int i = 0; i < 50 && !this.a.isEmpty(); i++) {
2011-02-11 08:01:32 +01:00
long chunkcoordinates = this.a.popFirst();
Chunk chunk = e.get(chunkcoordinates);
2011-02-20 17:40:40 +01:00
if (chunk == null) continue;
2011-03-18 22:57:22 +01:00
ChunkUnloadEvent event = new ChunkUnloadEvent(Type.CHUNK_UNLOAD, chunk.bukkitChunk);
2011-02-23 13:56:36 +01:00
server.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
g.getWorld().preserveChunk( (CraftChunk) chunk.bukkitChunk );
chunk.e();
this.b(chunk);
this.a(chunk);
this.e.remove(chunkcoordinates);
this.f.remove(chunk);
}
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 (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-02-23 03:37:56 +01:00
return !this.g.w;
2011-01-04 16:54:41 +01:00
}
}