2015-05-25 12:37:24 +02:00
|
|
|
--- a/net/minecraft/server/ChunkProviderServer.java
|
|
|
|
+++ b/net/minecraft/server/ChunkProviderServer.java
|
2016-02-29 22:32:46 +01:00
|
|
|
@@ -1,24 +1,31 @@
|
|
|
|
package net.minecraft.server;
|
|
|
|
|
|
|
|
-import com.google.common.collect.Lists;
|
|
|
|
import java.io.IOException;
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.Collections;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
-import java.util.Set;
|
|
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
+
|
2014-11-25 22:32:16 +01:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
|
|
|
|
+// CraftBukkit start
|
|
|
|
+
|
|
|
|
+import org.bukkit.Server;
|
|
|
|
+import org.bukkit.craftbukkit.chunkio.ChunkIOExecutor;
|
|
|
|
+import org.bukkit.craftbukkit.util.LongHash;
|
|
|
|
+import org.bukkit.craftbukkit.util.LongHashSet;
|
|
|
|
+import org.bukkit.craftbukkit.util.LongObjectHashMap;
|
|
|
|
+import org.bukkit.event.world.ChunkUnloadEvent;
|
|
|
|
+// CraftBukkit end
|
|
|
|
+
|
|
|
|
public class ChunkProviderServer implements IChunkProvider {
|
|
|
|
|
2016-02-29 22:32:46 +01:00
|
|
|
private static final Logger a = LogManager.getLogger();
|
|
|
|
- private final Set<Long> unloadQueue = Collections.newSetFromMap(new ConcurrentHashMap());
|
|
|
|
- private final ChunkGenerator chunkGenerator;
|
|
|
|
+ public final LongHashSet unloadQueue = new LongHashSet(); // CraftBukkit - LongHashSet
|
|
|
|
+ public final ChunkGenerator chunkGenerator; // CraftBukkit - public
|
|
|
|
private final IChunkLoader chunkLoader;
|
|
|
|
- private final LongHashMap<Chunk> chunks = new LongHashMap();
|
|
|
|
- private final List<Chunk> chunkList = Lists.newArrayList();
|
|
|
|
+ public LongObjectHashMap<Chunk> chunks = new LongObjectHashMap<Chunk>(); // CraftBukkit
|
|
|
|
+ // private final LongHashMap<Chunk> chunks = new LongHashMap();
|
|
|
|
+ // private final List<Chunk> chunkList = Lists.newArrayList();
|
|
|
|
public final WorldServer world;
|
2014-11-25 22:32:16 +01:00
|
|
|
|
2016-02-29 22:32:46 +01:00
|
|
|
public ChunkProviderServer(WorldServer worldserver, IChunkLoader ichunkloader, ChunkGenerator chunkgenerator) {
|
|
|
|
@@ -27,19 +34,33 @@
|
|
|
|
this.chunkGenerator = chunkgenerator;
|
2014-11-25 22:32:16 +01:00
|
|
|
}
|
|
|
|
|
2015-02-26 23:41:06 +01:00
|
|
|
- public List<Chunk> a() {
|
2014-11-25 22:32:16 +01:00
|
|
|
- return this.chunkList;
|
2016-02-29 22:32:46 +01:00
|
|
|
+ public boolean isChunkLoaded(int i, int j) {
|
|
|
|
+ return this.chunks.containsKey(LongHash.toLong(i, j)); // CraftBukkit
|
|
|
|
+ }
|
|
|
|
+
|
2014-11-25 22:32:16 +01:00
|
|
|
+ // CraftBukkit start - Change return type to Collection and return the values of our chunk map
|
|
|
|
+ public java.util.Collection a() {
|
|
|
|
+ // return this.chunkList;
|
|
|
|
+ return this.chunks.values();
|
|
|
|
+ // CraftBukkit end
|
|
|
|
}
|
|
|
|
|
|
|
|
public void queueUnload(int i, int j) {
|
2016-02-29 22:32:46 +01:00
|
|
|
if (this.world.worldProvider.c(i, j)) {
|
2014-11-25 22:32:16 +01:00
|
|
|
- this.unloadQueue.add(Long.valueOf(ChunkCoordIntPair.a(i, j)));
|
|
|
|
+ // CraftBukkit start
|
|
|
|
+ this.unloadQueue.add(i, j);
|
2015-02-26 23:41:06 +01:00
|
|
|
+
|
2014-11-25 22:32:16 +01:00
|
|
|
+ Chunk c = chunks.get(LongHash.toLong(i, j));
|
|
|
|
+ if (c != null) {
|
|
|
|
+ c.mustSave = true;
|
|
|
|
+ }
|
|
|
|
+ // CraftBukkit end
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void b() {
|
|
|
|
- Iterator iterator = this.chunkList.iterator();
|
2016-02-29 22:32:46 +01:00
|
|
|
+ Iterator iterator = this.chunks.values().iterator(); // CraftBukkit
|
2014-11-25 22:32:16 +01:00
|
|
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
Chunk chunk = (Chunk) iterator.next();
|
2016-03-05 08:50:38 +01:00
|
|
|
@@ -49,11 +70,20 @@
|
2015-02-26 23:41:06 +01:00
|
|
|
|
|
|
|
}
|
2014-11-25 22:32:16 +01:00
|
|
|
|
|
|
|
+ // CraftBukkit start - Add async variant, provide compatibility
|
2016-03-05 08:50:38 +01:00
|
|
|
+ public Chunk getOrCreateChunkFast(int x, int z) {
|
|
|
|
+ Chunk chunk = chunks.get(LongHash.toLong(x, z));
|
|
|
|
+ return (chunk == null) ? getChunkAt(x, z) : chunk;
|
|
|
|
+ }
|
|
|
|
+
|
2014-11-25 22:32:16 +01:00
|
|
|
+ public Chunk getChunkIfLoaded(int x, int z) {
|
|
|
|
+ return chunks.get(LongHash.toLong(x, z));
|
2015-02-26 23:41:06 +01:00
|
|
|
+ }
|
|
|
|
+
|
2016-02-29 22:32:46 +01:00
|
|
|
public Chunk getLoadedChunkAt(int i, int j) {
|
2014-11-25 22:32:16 +01:00
|
|
|
- long k = ChunkCoordIntPair.a(i, j);
|
2016-02-29 22:32:46 +01:00
|
|
|
- Chunk chunk = (Chunk) this.chunks.getEntry(k);
|
|
|
|
+ Chunk chunk = chunks.get(LongHash.toLong(i, j)); // CraftBukkit
|
|
|
|
|
|
|
|
- this.unloadQueue.remove(Long.valueOf(k));
|
|
|
|
+ this.unloadQueue.remove(i, j); // CraftBukkit
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
|
2016-04-20 12:00:35 +02:00
|
|
|
@@ -61,20 +91,71 @@
|
2016-02-29 22:32:46 +01:00
|
|
|
Chunk chunk = this.getLoadedChunkAt(i, j);
|
|
|
|
|
|
|
|
if (chunk == null) {
|
|
|
|
- chunk = this.loadChunk(i, j);
|
|
|
|
+ // CraftBukkit start
|
|
|
|
+ ChunkRegionLoader loader = null;
|
|
|
|
+
|
|
|
|
+ if (this.chunkLoader instanceof ChunkRegionLoader) {
|
|
|
|
+ loader = (ChunkRegionLoader) this.chunkLoader;
|
|
|
|
+ }
|
|
|
|
+ if (loader != null && loader.chunkExists(world, i, j)) {
|
|
|
|
+ chunk = ChunkIOExecutor.syncChunkLoad(world, loader, this, i, j);
|
|
|
|
+ }
|
|
|
|
+ /* chunk = this.loadChunk(i, j);
|
|
|
|
if (chunk != null) {
|
|
|
|
this.chunks.put(ChunkCoordIntPair.a(i, j), chunk);
|
|
|
|
this.chunkList.add(chunk);
|
|
|
|
chunk.addEntities();
|
|
|
|
chunk.loadNearby(this, this.chunkGenerator);
|
|
|
|
}
|
|
|
|
+ */
|
|
|
|
+ // CraftBukkit end
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Chunk getChunkAt(int i, int j) {
|
2016-03-02 22:27:49 +01:00
|
|
|
- Chunk chunk = this.getOrLoadChunkAt(i, j);
|
2014-11-25 22:32:16 +01:00
|
|
|
+ return getChunkAt(i, j, null);
|
|
|
|
+ }
|
2015-02-26 23:41:06 +01:00
|
|
|
+
|
2014-11-25 22:32:16 +01:00
|
|
|
+ public Chunk getChunkAt(int i, int j, Runnable runnable) {
|
2016-04-20 12:00:35 +02:00
|
|
|
+ return getChunkAt(i, j, runnable, true);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Chunk getChunkAt(int i, int j, Runnable runnable, boolean generate) {
|
2014-11-25 22:32:16 +01:00
|
|
|
+ unloadQueue.remove(i, j);
|
|
|
|
+ Chunk chunk = chunks.get(LongHash.toLong(i, j));
|
|
|
|
+ ChunkRegionLoader loader = null;
|
2015-02-26 23:41:06 +01:00
|
|
|
+
|
2014-11-25 22:32:16 +01:00
|
|
|
+ if (this.chunkLoader instanceof ChunkRegionLoader) {
|
|
|
|
+ loader = (ChunkRegionLoader) this.chunkLoader;
|
2016-02-29 22:32:46 +01:00
|
|
|
+
|
2014-11-25 22:32:16 +01:00
|
|
|
+ }
|
|
|
|
+ // We can only use the queue for already generated chunks
|
|
|
|
+ if (chunk == null && loader != null && loader.chunkExists(world, i, j)) {
|
|
|
|
+ if (runnable != null) {
|
|
|
|
+ ChunkIOExecutor.queueChunkLoad(world, loader, this, i, j, runnable);
|
|
|
|
+ return null;
|
|
|
|
+ } else {
|
|
|
|
+ chunk = ChunkIOExecutor.syncChunkLoad(world, loader, this, i, j);
|
|
|
|
+ }
|
2016-04-20 12:00:35 +02:00
|
|
|
+ } else if (chunk == null && generate) {
|
2014-11-25 22:32:16 +01:00
|
|
|
+ chunk = originalGetChunkAt(i, j);
|
|
|
|
+ }
|
2015-02-26 23:41:06 +01:00
|
|
|
+
|
2014-11-25 22:32:16 +01:00
|
|
|
+ // If we didn't load the chunk async and have a callback run it now
|
|
|
|
+ if (runnable != null) {
|
|
|
|
+ runnable.run();
|
|
|
|
+ }
|
2015-02-26 23:41:06 +01:00
|
|
|
+
|
2014-11-25 22:32:16 +01:00
|
|
|
+ return chunk;
|
|
|
|
+ }
|
2016-02-29 22:32:46 +01:00
|
|
|
+
|
2014-11-25 22:32:16 +01:00
|
|
|
+ public Chunk originalGetChunkAt(int i, int j) {
|
2016-03-02 22:27:49 +01:00
|
|
|
+ this.unloadQueue.remove(i, j);
|
|
|
|
+ Chunk chunk = this.chunks.get(LongHash.toLong(i, j));
|
2014-11-25 22:32:16 +01:00
|
|
|
+ boolean newChunk = false;
|
|
|
|
+ // CraftBukkit end
|
|
|
|
|
|
|
|
if (chunk == null) {
|
2016-02-29 22:32:46 +01:00
|
|
|
long k = ChunkCoordIntPair.a(i, j);
|
2016-04-20 12:00:35 +02:00
|
|
|
@@ -92,11 +173,38 @@
|
2016-02-29 22:32:46 +01:00
|
|
|
crashreportsystemdetails.a("Generator", (Object) this.chunkGenerator);
|
|
|
|
throw new ReportedException(crashreport);
|
2014-11-25 22:32:16 +01:00
|
|
|
}
|
|
|
|
+ newChunk = true; // CraftBukkit
|
|
|
|
}
|
|
|
|
|
|
|
|
- this.chunks.put(k, chunk);
|
|
|
|
- this.chunkList.add(chunk);
|
2016-02-29 22:32:46 +01:00
|
|
|
+ this.chunks.put(LongHash.toLong(i, j), chunk); // CraftBukkit
|
2014-11-25 22:32:16 +01:00
|
|
|
chunk.addEntities();
|
2016-02-29 22:32:46 +01:00
|
|
|
+
|
2014-11-25 22:32:16 +01:00
|
|
|
+ // CraftBukkit start
|
|
|
|
+ Server server = world.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.
|
|
|
|
+ */
|
|
|
|
+ server.getPluginManager().callEvent(new org.bukkit.event.world.ChunkLoadEvent(chunk.bukkitChunk, newChunk));
|
|
|
|
+ }
|
2015-02-26 23:41:06 +01:00
|
|
|
+
|
2014-11-25 22:32:16 +01:00
|
|
|
+ // Update neighbor counts
|
|
|
|
+ for (int x = -2; x < 3; x++) {
|
|
|
|
+ for (int z = -2; z < 3; z++) {
|
|
|
|
+ if (x == 0 && z == 0) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Chunk neighbor = this.getChunkIfLoaded(chunk.locX + x, chunk.locZ + z);
|
|
|
|
+ if (neighbor != null) {
|
|
|
|
+ neighbor.setNeighborLoaded(-x, -z);
|
|
|
|
+ chunk.setNeighborLoaded(x, z);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // CraftBukkit end
|
2016-02-29 22:32:46 +01:00
|
|
|
chunk.loadNearby(this, this.chunkGenerator);
|
2014-11-25 22:32:16 +01:00
|
|
|
}
|
|
|
|
|
2016-04-20 12:00:35 +02:00
|
|
|
@@ -142,10 +250,12 @@
|
2015-02-26 23:41:06 +01:00
|
|
|
|
2016-02-29 22:32:46 +01:00
|
|
|
public boolean a(boolean flag) {
|
2014-11-25 22:32:16 +01:00
|
|
|
int i = 0;
|
2015-02-26 23:41:06 +01:00
|
|
|
- ArrayList arraylist = Lists.newArrayList(this.chunkList);
|
|
|
|
|
|
|
|
- for (int j = 0; j < arraylist.size(); ++j) {
|
|
|
|
- Chunk chunk = (Chunk) arraylist.get(j);
|
2014-11-25 22:32:16 +01:00
|
|
|
+ // CraftBukkit start
|
|
|
|
+ Iterator iterator = this.chunks.values().iterator();
|
|
|
|
+ while (iterator.hasNext()) {
|
|
|
|
+ Chunk chunk = (Chunk) iterator.next();
|
|
|
|
+ // CraftBukkit end
|
|
|
|
|
|
|
|
if (flag) {
|
|
|
|
this.saveChunkNOP(chunk);
|
2016-04-20 12:00:35 +02:00
|
|
|
@@ -170,22 +280,43 @@
|
2014-11-25 22:32:16 +01:00
|
|
|
|
|
|
|
public boolean unloadChunks() {
|
|
|
|
if (!this.world.savingDisabled) {
|
|
|
|
- for (int i = 0; i < 100; ++i) {
|
|
|
|
- if (!this.unloadQueue.isEmpty()) {
|
|
|
|
- Long olong = (Long) this.unloadQueue.iterator().next();
|
|
|
|
- Chunk chunk = (Chunk) this.chunks.getEntry(olong.longValue());
|
|
|
|
+ // CraftBukkit start
|
|
|
|
+ Server server = this.world.getServer();
|
|
|
|
+ for (int i = 0; i < 100 && !this.unloadQueue.isEmpty(); ++i) {
|
|
|
|
+ long chunkcoordinates = this.unloadQueue.popFirst();
|
|
|
|
+ Chunk chunk = this.chunks.get(chunkcoordinates);
|
|
|
|
+ if (chunk == null) continue;
|
|
|
|
+
|
|
|
|
+ ChunkUnloadEvent event = new ChunkUnloadEvent(chunk.bukkitChunk);
|
|
|
|
+ server.getPluginManager().callEvent(event);
|
|
|
|
+ if (!event.isCancelled()) {
|
2015-02-26 23:41:06 +01:00
|
|
|
|
2014-11-25 22:32:16 +01:00
|
|
|
if (chunk != null) {
|
|
|
|
chunk.removeEntities();
|
|
|
|
this.saveChunk(chunk);
|
|
|
|
this.saveChunkNOP(chunk);
|
|
|
|
- this.chunks.remove(olong.longValue());
|
|
|
|
- this.chunkList.remove(chunk);
|
|
|
|
+ this.chunks.remove(chunkcoordinates); // CraftBukkit
|
|
|
|
}
|
|
|
|
|
|
|
|
- this.unloadQueue.remove(olong);
|
|
|
|
+ // this.unloadQueue.remove(olong);
|
2015-02-26 23:41:06 +01:00
|
|
|
+
|
2014-11-25 22:32:16 +01:00
|
|
|
+ // Update neighbor counts
|
|
|
|
+ for (int x = -2; x < 3; x++) {
|
|
|
|
+ for (int z = -2; z < 3; z++) {
|
|
|
|
+ if (x == 0 && z == 0) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Chunk neighbor = this.getChunkIfLoaded(chunk.locX + x, chunk.locZ + z);
|
|
|
|
+ if (neighbor != null) {
|
|
|
|
+ neighbor.setNeighborUnloaded(-x, -z);
|
|
|
|
+ chunk.setNeighborUnloaded(x, z);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
}
|
2015-02-26 23:41:06 +01:00
|
|
|
}
|
2014-11-25 22:32:16 +01:00
|
|
|
+ // CraftBukkit end
|
|
|
|
|
2016-02-29 22:32:46 +01:00
|
|
|
this.chunkLoader.a();
|
|
|
|
}
|
2016-04-20 12:00:35 +02:00
|
|
|
@@ -198,7 +329,8 @@
|
2014-11-25 22:32:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
- return "ServerChunkCache: " + this.chunks.count() + " Drop: " + this.unloadQueue.size();
|
|
|
|
+ // CraftBukkit - this.chunks.count() -> .size()
|
|
|
|
+ return "ServerChunkCache: " + this.chunks.size() + " Drop: " + this.unloadQueue.size();
|
|
|
|
}
|
|
|
|
|
2016-02-29 22:32:46 +01:00
|
|
|
public List<BiomeBase.BiomeMeta> a(EnumCreatureType enumcreaturetype, BlockPosition blockposition) {
|
2016-04-20 12:00:35 +02:00
|
|
|
@@ -210,10 +342,11 @@
|
2014-11-25 22:32:16 +01:00
|
|
|
}
|
|
|
|
|
2016-02-29 22:32:46 +01:00
|
|
|
public int g() {
|
2014-11-25 22:32:16 +01:00
|
|
|
- return this.chunks.count();
|
|
|
|
+ // CraftBukkit - this.chunks.count() -> this.chunks.size()
|
|
|
|
+ return this.chunks.size();
|
|
|
|
}
|
|
|
|
|
2016-02-29 22:32:46 +01:00
|
|
|
public boolean e(int i, int j) {
|
|
|
|
- return this.chunks.contains(ChunkCoordIntPair.a(i, j));
|
|
|
|
+ return this.chunks.containsKey(LongHash.toLong(i, j)); // CraftBukkit
|
|
|
|
}
|
|
|
|
}
|