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;
|
2012-01-12 23:10:13 +01:00
|
|
|
import java.util.Iterator;
|
2011-01-29 22:50:29 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
2011-01-11 09:25:13 +01:00
|
|
|
|
|
|
|
// CraftBukkit start
|
2011-06-12 00:02:58 +02:00
|
|
|
import java.util.Random;
|
2012-07-22 08:18:00 +02:00
|
|
|
|
|
|
|
import org.bukkit.Server;
|
2012-09-11 12:51:09 +02:00
|
|
|
import org.bukkit.craftbukkit.util.LongHash;
|
2012-08-18 01:53:59 +02:00
|
|
|
import org.bukkit.craftbukkit.util.LongHashSet;
|
|
|
|
import org.bukkit.craftbukkit.util.LongObjectHashMap;
|
2011-02-19 16:55:47 +01:00
|
|
|
import org.bukkit.event.world.ChunkUnloadEvent;
|
2011-01-11 09:25:13 +01:00
|
|
|
// 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
|
2012-08-18 01:53:59 +02:00
|
|
|
public LongHashSet unloadQueue = new LongHashSet();
|
2011-04-20 19:05:14 +02:00
|
|
|
public Chunk emptyChunk;
|
|
|
|
public IChunkProvider chunkProvider; // CraftBukkit
|
2011-05-26 14:48:22 +02:00
|
|
|
private IChunkLoader e;
|
2012-08-06 23:46:12 +02:00
|
|
|
public boolean forceChunkLoad = false; // true -> false
|
2012-08-18 01:53:59 +02:00
|
|
|
public LongObjectHashMap<Chunk> chunks = new LongObjectHashMap<Chunk>();
|
2011-04-20 19:05:14 +02:00
|
|
|
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) {
|
2012-03-01 11:49:23 +01:00
|
|
|
this.emptyChunk = new EmptyChunk(worldserver, 0, 0);
|
2011-04-20 19:05:14 +02:00
|
|
|
this.world = worldserver;
|
2011-05-26 14:48:22 +02:00
|
|
|
this.e = ichunkloader;
|
2011-04-20 19:05:14 +02:00
|
|
|
this.chunkProvider = ichunkprovider;
|
2011-01-04 16:54:41 +01:00
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public boolean isChunkLoaded(int i, int j) {
|
2012-09-11 12:51:09 +02:00
|
|
|
return this.chunks.containsKey(LongHash.toLong(i, j)); // CraftBukkit
|
2011-01-04 16:54:41 +01:00
|
|
|
}
|
2011-02-19 16:55:47 +01:00
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public void queueUnload(int i, int j) {
|
2012-07-29 09:33:13 +02:00
|
|
|
if (this.world.worldProvider.e()) {
|
2011-11-20 09:01:14 +01:00
|
|
|
ChunkCoordinates chunkcoordinates = this.world.getSpawn();
|
|
|
|
int k = i * 16 + 8 - chunkcoordinates.x;
|
|
|
|
int l = j * 16 + 8 - chunkcoordinates.z;
|
|
|
|
short short1 = 128;
|
|
|
|
|
2012-08-13 01:40:49 +02:00
|
|
|
// CraftBukkit start
|
|
|
|
if (k < -short1 || k > short1 || l < -short1 || l > short1 || !(this.world.keepSpawnInMemory)) { // Added 'this.world.keepSpawnInMemory'
|
|
|
|
this.unloadQueue.add(i, j);
|
|
|
|
|
2012-09-11 12:51:09 +02:00
|
|
|
Chunk c = this.chunks.get(LongHash.toLong(i, j));
|
2012-08-13 01:40:49 +02:00
|
|
|
if (c != null) {
|
|
|
|
c.mustSave = true;
|
|
|
|
}
|
2011-11-20 09:01:14 +01:00
|
|
|
}
|
2012-08-13 01:40:49 +02:00
|
|
|
// CraftBukkit end
|
2011-11-20 09:01:14 +01:00
|
|
|
} else {
|
2012-08-13 01:40:49 +02:00
|
|
|
// CraftBukkit start
|
|
|
|
this.unloadQueue.add(i, j);
|
|
|
|
|
2012-09-11 12:51:09 +02:00
|
|
|
Chunk c = this.chunks.get(LongHash.toLong(i, j));
|
2012-08-13 01:40:49 +02:00
|
|
|
if (c != null) {
|
|
|
|
c.mustSave = true;
|
|
|
|
}
|
|
|
|
// CraftBukkit end
|
2011-01-04 16:54:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public void a() {
|
2012-08-12 00:34:22 +02:00
|
|
|
Iterator iterator = this.chunks.values().iterator(); // CraftBukkit
|
2012-01-12 23:10:13 +01:00
|
|
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
Chunk chunk = (Chunk) iterator.next();
|
|
|
|
|
|
|
|
this.queueUnload(chunk.x, chunk.z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public Chunk getChunkAt(int i, int j) {
|
2011-02-07 09:43:51 +01:00
|
|
|
// CraftBukkit start
|
2011-04-20 19:05:14 +02:00
|
|
|
this.unloadQueue.remove(i, j);
|
2012-09-11 12:51:09 +02:00
|
|
|
Chunk chunk = (Chunk) this.chunks.get(LongHash.toLong(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) {
|
2011-04-20 19:05:14 +02:00
|
|
|
chunk = this.loadChunk(i, j);
|
2011-01-04 16:54:41 +01:00
|
|
|
if (chunk == null) {
|
2011-04-20 19:05:14 +02:00
|
|
|
if (this.chunkProvider == null) {
|
|
|
|
chunk = this.emptyChunk;
|
2011-01-04 16:54:41 +01:00
|
|
|
} else {
|
2011-04-20 19:05:14 +02:00
|
|
|
chunk = this.chunkProvider.getOrCreateChunk(i, j);
|
2011-01-04 16:54:41 +01:00
|
|
|
}
|
2011-06-27 00:25:01 +02:00
|
|
|
newChunk = true; // CraftBukkit
|
2011-01-04 16:54:41 +01:00
|
|
|
}
|
2011-01-29 22:50:29 +01:00
|
|
|
|
2012-09-11 12:51:09 +02:00
|
|
|
this.chunks.put(LongHash.toLong(i, j), chunk); // CraftBukkit
|
2011-01-04 16:54:41 +01:00
|
|
|
if (chunk != null) {
|
2011-04-20 19:05:14 +02:00
|
|
|
chunk.addEntities();
|
2011-01-04 16:54:41 +01:00
|
|
|
}
|
|
|
|
|
2011-01-11 09:25:13 +01:00
|
|
|
// CraftBukkit start
|
2012-07-22 08:18:00 +02:00
|
|
|
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.
|
|
|
|
*/
|
2012-07-22 08:18:00 +02:00
|
|
|
server.getPluginManager().callEvent(new org.bukkit.event.world.ChunkLoadEvent(chunk.bukkitChunk, newChunk));
|
2011-01-04 16:54:41 +01:00
|
|
|
}
|
2011-01-11 09:25:13 +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;
|
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public Chunk getOrCreateChunk(int i, int j) {
|
2011-03-31 22:40:00 +02:00
|
|
|
// CraftBukkit start
|
2012-09-11 12:51:09 +02:00
|
|
|
Chunk chunk = (Chunk) this.chunks.get(LongHash.toLong(i, j));
|
2011-04-20 19:05:14 +02:00
|
|
|
|
2011-06-27 00:25:01 +02:00
|
|
|
chunk = chunk == null ? (!this.world.isLoading && !this.forceChunkLoad ? this.emptyChunk : this.getChunkAt(i, j)) : chunk;
|
2011-04-20 19:05:14 +02:00
|
|
|
if (chunk == this.emptyChunk) return chunk;
|
|
|
|
if (i != chunk.x || j != chunk.z) {
|
2011-09-17 15:52:30 +02:00
|
|
|
MinecraftServer.log.severe("Chunk (" + chunk.x + ", " + chunk.z + ") stored at (" + i + ", " + j + ") in world '" + world.getWorld().getName() + "'");
|
|
|
|
MinecraftServer.log.severe(chunk.getClass().getName());
|
2011-04-20 19:05:14 +02:00
|
|
|
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) {
|
2012-03-01 11:49:23 +01:00
|
|
|
chunk.n = this.world.getTime();
|
2012-10-25 05:53:23 +02:00
|
|
|
if (this.chunkProvider != null) {
|
|
|
|
this.chunkProvider.recreateStructures(i, j);
|
|
|
|
}
|
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 {
|
2012-03-01 11:49:23 +01:00
|
|
|
chunk.n = 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();
|
2012-07-29 09:33:13 +02:00
|
|
|
// CraftBukkit start - remove extra exception
|
2011-01-29 22:50:29 +01:00
|
|
|
}
|
2012-07-29 09:33:13 +02:00
|
|
|
// } catch (ExceptionWorldConflict exceptionworldconflict) {
|
|
|
|
// exceptionworldconflict.printStackTrace();
|
|
|
|
// }
|
|
|
|
// CraftBukkit end
|
2011-01-04 16:54:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public void getChunkAt(IChunkProvider ichunkprovider, int i, int j) {
|
|
|
|
Chunk chunk = this.getOrCreateChunk(i, j);
|
2011-01-04 16:54:41 +01:00
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
if (!chunk.done) {
|
|
|
|
chunk.done = true;
|
|
|
|
if (this.chunkProvider != null) {
|
|
|
|
this.chunkProvider.getChunkAt(ichunkprovider, i, j);
|
2011-06-06 15:52:02 +02:00
|
|
|
|
2011-06-12 00:02:58 +02:00
|
|
|
// CraftBukkit start
|
2011-06-27 00:25:01 +02:00
|
|
|
BlockSand.instaFall = true;
|
2011-06-06 15:52:02 +02:00
|
|
|
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());
|
2011-06-27 00:25:01 +02:00
|
|
|
|
|
|
|
org.bukkit.World world = this.world.getWorld();
|
2011-06-12 00:47:19 +02:00
|
|
|
if (world != null) {
|
2012-07-22 08:18:00 +02:00
|
|
|
for (org.bukkit.generator.BlockPopulator populator : world.getPopulators()) {
|
2011-06-12 00:47:19 +02:00
|
|
|
populator.populate(world, random, chunk.bukkitChunk);
|
|
|
|
}
|
2011-06-06 15:52:02 +02:00
|
|
|
}
|
2011-06-27 00:25:01 +02:00
|
|
|
BlockSand.instaFall = false;
|
2012-07-22 08:18:00 +02:00
|
|
|
this.world.getServer().getPluginManager().callEvent(new org.bukkit.event.world.ChunkPopulateEvent(chunk.bukkitChunk));
|
2011-06-12 00:02:58 +02:00
|
|
|
// CraftBukkit end
|
2011-06-06 15:52:02 +02:00
|
|
|
|
2012-03-01 11:49:23 +01:00
|
|
|
chunk.e();
|
2011-01-04 16:54:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public boolean saveChunks(boolean flag, IProgressUpdate iprogressupdate) {
|
2011-01-04 16:54:41 +01:00
|
|
|
int i = 0;
|
2012-08-12 00:34:22 +02:00
|
|
|
Iterator iterator = this.chunks.values().iterator(); // CraftBukkit
|
2011-01-04 16:54:41 +01:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
while (iterator.hasNext()) {
|
|
|
|
Chunk chunk = (Chunk) iterator.next();
|
2011-01-04 16:54:41 +01:00
|
|
|
|
2012-03-01 11:49:23 +01:00
|
|
|
if (flag) {
|
2011-04-20 19:05:14 +02:00
|
|
|
this.saveChunkNOP(chunk);
|
2011-01-04 16:54:41 +01:00
|
|
|
}
|
2011-01-29 22:50:29 +01:00
|
|
|
|
|
|
|
if (chunk.a(flag)) {
|
2011-04-20 19:05:14 +02:00
|
|
|
this.saveChunk(chunk);
|
2012-03-01 11:49:23 +01:00
|
|
|
chunk.l = 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;
|
|
|
|
}
|
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
public boolean unloadChunks() {
|
2011-09-24 23:03:31 +02:00
|
|
|
if (!this.world.savingDisabled) {
|
2011-02-11 10:27:57 +01:00
|
|
|
// CraftBukkit start
|
2012-07-22 08:18:00 +02:00
|
|
|
Server server = this.world.getServer();
|
2012-10-25 05:53:23 +02:00
|
|
|
for (int i = 0; i < 100 && !this.unloadQueue.isEmpty(); i++) {
|
2011-04-20 19:05:14 +02:00
|
|
|
long chunkcoordinates = this.unloadQueue.popFirst();
|
|
|
|
Chunk chunk = this.chunks.get(chunkcoordinates);
|
2011-02-20 17:40:40 +01:00
|
|
|
if (chunk == null) continue;
|
|
|
|
|
2011-03-26 22:32:56 +01:00
|
|
|
ChunkUnloadEvent event = new ChunkUnloadEvent(chunk.bukkitChunk);
|
2011-02-23 13:56:36 +01:00
|
|
|
server.getPluginManager().callEvent(event);
|
|
|
|
if (!event.isCancelled()) {
|
2011-04-20 19:05:14 +02:00
|
|
|
chunk.removeEntities();
|
|
|
|
this.saveChunk(chunk);
|
|
|
|
this.saveChunkNOP(chunk);
|
2011-05-14 16:29:42 +02:00
|
|
|
// this.unloadQueue.remove(integer);
|
2011-04-20 19:05:14 +02:00
|
|
|
this.chunks.remove(chunkcoordinates); // CraftBukkit
|
2011-02-19 16:55:47 +01:00
|
|
|
}
|
2011-01-04 16:54:41 +01:00
|
|
|
}
|
2011-02-11 10:27:57 +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
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
return this.chunkProvider.unloadChunks();
|
2011-01-04 16:54:41 +01:00
|
|
|
}
|
|
|
|
|
2011-06-27 00:25:01 +02:00
|
|
|
public boolean canSave() {
|
2011-09-24 23:03:31 +02:00
|
|
|
return !this.world.savingDisabled;
|
2011-01-04 16:54:41 +01:00
|
|
|
}
|
2011-11-20 09:01:14 +01:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public String getName() {
|
2012-03-01 11:49:23 +01:00
|
|
|
return "ServerChunkCache: " + this.chunks.values().size() + " Drop: " + this.unloadQueue.size(); // CraftBukkit
|
2012-01-12 23:10:13 +01:00
|
|
|
}
|
|
|
|
|
2012-01-12 16:27:39 +01:00
|
|
|
public List getMobsFor(EnumCreatureType enumcreaturetype, int i, int j, int k) {
|
|
|
|
return this.chunkProvider.getMobsFor(enumcreaturetype, i, j, k);
|
2011-11-20 09:01:14 +01:00
|
|
|
}
|
|
|
|
|
2012-01-12 16:27:39 +01:00
|
|
|
public ChunkPosition findNearestMapFeature(World world, String s, int i, int j, int k) {
|
|
|
|
return this.chunkProvider.findNearestMapFeature(world, s, i, j, k);
|
2011-11-20 09:01:14 +01:00
|
|
|
}
|
2012-07-29 09:33:13 +02:00
|
|
|
|
|
|
|
public int getLoadedChunks() {
|
|
|
|
return this.chunks.values().size(); // CraftBukkit
|
|
|
|
}
|
2012-10-25 05:53:23 +02:00
|
|
|
|
|
|
|
public void recreateStructures(int i, int j) {}
|
2011-01-04 16:54:41 +01:00
|
|
|
}
|