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

257 Zeilen
8.5 KiB
Java

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.HashSet;
import java.util.List;
import java.util.Set;
// CraftBukkit start
2011-06-12 00:02:58 +02:00
import java.util.Random;
2011-01-04 16:54:41 +01:00
import org.bukkit.craftbukkit.CraftChunk;
2011-03-01 19:19:50 +01:00
import org.bukkit.craftbukkit.util.LongHashset;
import org.bukkit.craftbukkit.util.LongHashtable;
import org.bukkit.event.world.ChunkLoadEvent;
2011-06-16 20:33:36 +02:00
import org.bukkit.event.world.ChunkPopulateEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
import org.bukkit.generator.BlockPopulator;
// CraftBukkit end
2011-01-04 16:54:41 +01:00
2011-02-20 17:40:40 +01:00
public class ChunkProviderServer implements IChunkProvider {
2011-03-31 22:40:00 +02:00
// CraftBukkit start
public LongHashset unloadQueue = new LongHashset();
public Chunk emptyChunk;
public IChunkProvider chunkProvider; // CraftBukkit
2011-05-26 14:48:22 +02:00
private IChunkLoader e;
public boolean forceChunkLoad = false;
public LongHashtable<Chunk> chunks = new LongHashtable<Chunk>();
public List chunkList = new ArrayList();
public WorldServer world;
2011-03-31 22:40:00 +02:00
// CraftBukkit end
2011-01-04 16:54:41 +01:00
public ChunkProviderServer(WorldServer worldserver, IChunkLoader ichunkloader, IChunkProvider ichunkprovider) {
worldserver.getClass();
EmptyChunk emptychunk = new EmptyChunk(worldserver, new byte[256 * 128], 0, 0);
this.emptyChunk = emptychunk;
this.world = worldserver;
2011-05-26 14:48:22 +02:00
this.e = ichunkloader;
this.chunkProvider = ichunkprovider;
2011-01-04 16:54:41 +01:00
}
public boolean isChunkLoaded(int i, int j) {
return this.chunks.containsKey(i, j); // CraftBukkit
2011-01-04 16:54:41 +01:00
}
public void queueUnload(int i, int j) {
ChunkCoordinates chunkcoordinates = this.world.getSpawn();
int k = i * 16 + 8 - chunkcoordinates.x;
int l = j * 16 + 8 - chunkcoordinates.z;
2011-01-29 22:50:29 +01:00
short short1 = 128;
2011-01-04 16:54:41 +01:00
if (k < -short1 || k > short1 || l < -short1 || l > short1 || !(this.world.keepSpawnInMemory)) { // CraftBukkit - added 'this.world.keepSpawnInMemory'
this.unloadQueue.add(i, j); // CraftBukkit
2011-01-04 16:54:41 +01:00
}
}
public Chunk getChunkAt(int i, int j) {
2011-02-07 09:43:51 +01:00
// CraftBukkit start
this.unloadQueue.remove(i, j);
Chunk chunk = (Chunk) this.chunks.get(i, j);
2011-06-16 20:33:36 +02:00
boolean newChunk = false;
2011-02-07 09:43:51 +01:00
// CraftBukkit end
2011-01-04 16:54:41 +01:00
if (chunk == null) {
chunk = this.loadChunk(i, j);
2011-01-04 16:54:41 +01:00
if (chunk == null) {
if (this.chunkProvider == null) {
chunk = this.emptyChunk;
2011-01-04 16:54:41 +01:00
} else {
chunk = this.chunkProvider.getOrCreateChunk(i, j);
2011-01-04 16:54:41 +01:00
}
newChunk = true; // CraftBukkit
2011-01-04 16:54:41 +01:00
}
2011-01-29 22:50:29 +01:00
this.chunks.put(i, j, chunk); // CraftBukkit
this.chunkList.add(chunk);
2011-01-04 16:54:41 +01:00
if (chunk != null) {
chunk.loadNOP();
chunk.addEntities();
2011-01-04 16:54:41 +01:00
}
// CraftBukkit start
org.bukkit.Server server = this.world.getServer();
2011-01-04 16:54:41 +01:00
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-06-16 20:33:36 +02:00
server.getPluginManager().callEvent(new ChunkLoadEvent(chunk.bukkitChunk, newChunk));
2011-01-04 16:54:41 +01:00
}
// CraftBukkit end
2011-01-04 16:54:41 +01:00
2011-09-15 02:23:52 +02:00
chunk.a(this, this, i, j);
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 getOrCreateChunk(int i, int j) {
2011-03-31 22:40:00 +02:00
// CraftBukkit start
Chunk chunk = (Chunk) this.chunks.get(i, j);
chunk = chunk == null ? (!this.world.isLoading && !this.forceChunkLoad ? this.emptyChunk : this.getChunkAt(i, j)) : chunk;
if (chunk == this.emptyChunk) return chunk;
if (i != chunk.x || j != chunk.z) {
MinecraftServer.log.severe("Chunk (" + chunk.x + ", " + chunk.z + ") stored at (" + i + ", " + j + ") in world '" + world.getWorld().getName() + "'");
MinecraftServer.log.severe(chunk.getClass().getName());
Throwable ex = new Throwable();
ex.fillInStackTrace();
ex.printStackTrace();
2011-02-24 09:51:00 +01:00
}
return chunk;
2011-03-31 22:40:00 +02:00
// CraftBukkit end
2011-01-04 16:54:41 +01:00
}
2011-05-14 16:29:42 +02:00
public Chunk loadChunk(int i, int j) { // CraftBukkit - private -> public
2011-05-26 14:48:22 +02:00
if (this.e == null) {
2011-01-04 16:54:41 +01:00
return null;
2011-01-29 22:50:29 +01:00
} else {
try {
2011-05-26 14:48:22 +02:00
Chunk chunk = this.e.a(this.world, i, j);
2011-01-04 16:54:41 +01:00
2011-01-29 22:50:29 +01:00
if (chunk != null) {
2011-09-15 02:23:52 +02:00
chunk.t = this.world.getTime();
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-05-14 16:29:42 +02:00
public void saveChunkNOP(Chunk chunk) { // CraftBukkit - private -> public
2011-05-26 14:48:22 +02:00
if (this.e != null) {
2011-01-29 22:50:29 +01:00
try {
2011-05-26 14:48:22 +02:00
this.e.b(this.world, chunk);
2011-01-29 22:50:29 +01:00
} catch (Exception exception) {
exception.printStackTrace();
}
2011-01-04 16:54:41 +01:00
}
}
2011-05-14 16:29:42 +02:00
public void saveChunk(Chunk chunk) { // CraftBukkit - private -> public
2011-05-26 14:48:22 +02:00
if (this.e != null) {
2011-01-29 22:50:29 +01:00
try {
2011-09-15 02:23:52 +02:00
chunk.t = this.world.getTime();
2011-05-26 14:48:22 +02:00
this.e.a(this.world, chunk);
2011-01-29 22:50:29 +01:00
} catch (Exception ioexception) { // CraftBukkit - IOException -> Exception
ioexception.printStackTrace();
}
2011-01-04 16:54:41 +01:00
}
}
public void getChunkAt(IChunkProvider ichunkprovider, int i, int j) {
Chunk chunk = this.getOrCreateChunk(i, j);
2011-01-04 16:54:41 +01:00
if (!chunk.done) {
chunk.done = true;
if (this.chunkProvider != null) {
this.chunkProvider.getChunkAt(ichunkprovider, i, j);
2011-06-12 00:02:58 +02:00
// CraftBukkit start
BlockSand.instaFall = true;
Random random = new Random();
random.setSeed(world.getSeed());
long xRand = random.nextLong() / 2L * 2L + 1L;
long zRand = random.nextLong() / 2L * 2L + 1L;
random.setSeed((long) i * xRand + (long) j * zRand ^ world.getSeed());
org.bukkit.World world = this.world.getWorld();
if (world != null) {
for (BlockPopulator populator : world.getPopulators()) {
populator.populate(world, random, chunk.bukkitChunk);
}
}
BlockSand.instaFall = false;
this.world.getServer().getPluginManager().callEvent(new ChunkPopulateEvent(chunk.bukkitChunk));
2011-06-12 00:02:58 +02:00
// CraftBukkit end
2011-01-04 16:54:41 +01:00
chunk.f();
}
}
}
public boolean saveChunks(boolean flag, IProgressUpdate iprogressupdate) {
2011-01-04 16:54:41 +01:00
int i = 0;
for (int j = 0; j < this.chunkList.size(); ++j) {
Chunk chunk = (Chunk) this.chunkList.get(j);
2011-01-04 16:54:41 +01:00
2011-09-15 02:23:52 +02:00
if (flag && !chunk.r) {
this.saveChunkNOP(chunk);
2011-01-04 16:54:41 +01:00
}
2011-01-29 22:50:29 +01:00
if (chunk.a(flag)) {
this.saveChunk(chunk);
2011-09-15 02:23:52 +02:00
chunk.q = false;
2011-01-29 22:50:29 +01:00
++i;
if (i == 24 && !flag) {
return false;
}
2011-01-04 16:54:41 +01:00
}
}
if (flag) {
2011-05-26 14:48:22 +02:00
if (this.e == null) {
2011-01-04 16:54:41 +01:00
return true;
}
2011-01-29 22:50:29 +01:00
2011-05-26 14:48:22 +02:00
this.e.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 unloadChunks() {
if (!this.world.canSave) {
// CraftBukkit start
org.bukkit.Server server = this.world.getServer();
for (int i = 0; i < 50 && !this.unloadQueue.isEmpty(); i++) {
long chunkcoordinates = this.unloadQueue.popFirst();
Chunk chunk = this.chunks.get(chunkcoordinates);
2011-02-20 17:40:40 +01:00
if (chunk == null) continue;
ChunkUnloadEvent event = new ChunkUnloadEvent(chunk.bukkitChunk);
2011-02-23 13:56:36 +01:00
server.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
2011-05-14 16:29:42 +02:00
this.world.getWorld().preserveChunk((CraftChunk) chunk.bukkitChunk);
chunk.removeEntities();
this.saveChunk(chunk);
this.saveChunkNOP(chunk);
2011-05-14 16:29:42 +02:00
// this.unloadQueue.remove(integer);
this.chunks.remove(chunkcoordinates); // CraftBukkit
this.chunkList.remove(chunk);
}
2011-01-04 16:54:41 +01:00
}
// CraftBukkit end
2011-01-04 16:54:41 +01:00
2011-05-26 14:48:22 +02:00
if (this.e != null) {
this.e.a();
2011-01-04 16:54:41 +01:00
}
}
2011-01-29 22:50:29 +01:00
return this.chunkProvider.unloadChunks();
2011-01-04 16:54:41 +01:00
}
public boolean canSave() {
return !this.world.canSave;
2011-01-04 16:54:41 +01:00
}
}