Skip to content

Instantly share code, notes, and snippets.

@aikar
Last active October 19, 2018 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aikar/e5fa2d1d9dc7e3f23d97f2bc347b977d to your computer and use it in GitHub Desktop.
Save aikar/e5fa2d1d9dc7e3f23d97f2bc347b977d to your computer and use it in GitHub Desktop.
package net.minecraft.server;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class ChunkMap extends Long2ObjectOpenHashMap<Chunk> {
private static final Logger a = LogManager.getLogger();
public ChunkMap(int i) {
super(i);
}
public Chunk a(long i, Chunk chunk) {
org.spigotmc.AsyncCatcher.catchOp("Async Chunk put"); // Paper
chunk.world.timings.syncChunkLoadPostTimer.startTiming(); // Paper
lastChunkByPos = chunk; // Paper
// Paper start
Chunk chunk1;
synchronized (this) {
// synchronize so any async gets are safe
chunk1 = (Chunk) super.put(i, chunk);
}
if (chunk1 == null) { // Paper - we should never be overwriting chunks
// Paper end
ChunkCoordIntPair chunkcoordintpair = new ChunkCoordIntPair(i);
for (int j = chunkcoordintpair.x - 1; j <= chunkcoordintpair.x + 1; ++j) {
for (int k = chunkcoordintpair.z - 1; k <= chunkcoordintpair.z + 1; ++k) {
if (j != chunkcoordintpair.x || k != chunkcoordintpair.z) {
long l = ChunkCoordIntPair.a(j, k);
Chunk chunk2 = (Chunk) super.get(l); // Paper - use super to avoid polluting last access cache
if (chunk2 != null) {
chunk.H();
chunk2.H();
}
}
}
}
// CraftBukkit start
// 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 = super.get(ChunkCoordIntPair.a(chunkcoordintpair.x + x, chunkcoordintpair.z + z)); // Paper - use super to avoid polluting last access cache
if (neighbor != null) {
neighbor.setNeighborLoaded(-x, -z);
chunk.setNeighborLoaded(x, z);
}
}
// Paper start
} } else {
a.error("Overwrote existing chunk! (" + chunk.world.getWorld().getName() + ":" + chunk.locX+"," + chunk.locZ + ")", new IllegalStateException());
}
// Paper end
// Paper start - if this is a spare chunk (not part of any players view distance), go ahead and queue it for unload.
if (!((WorldServer)chunk.world).getPlayerChunkMap().isChunkInUse(chunk.locX, chunk.locZ)) {
if (chunk.world.paperConfig.delayChunkUnloadsBy > 0) {
chunk.scheduledForUnload = System.currentTimeMillis();
} else {
((WorldServer) chunk.world).getChunkProviderServer().unload(chunk);
}
}
// Paper end
chunk.world.timings.syncChunkLoadPostTimer.stopTiming(); // Paper
// CraftBukkit end
return chunk1;
}
public Chunk a(Long olong, Chunk chunk) {
return MCUtil.ensureMain("Chunk Put", () -> this.a(olong.longValue(), chunk)); // Paper
}
public Chunk a(long i) {
// Paper start
org.spigotmc.AsyncCatcher.catchOp("Async Chunk remove");
Chunk chunk;
synchronized (this) {
// synchronize so any async gets are safe
chunk = super.remove(i);
}
if (chunk != null) { // Paper - don't decrement if we didn't remove anything
// Paper end
ChunkCoordIntPair chunkcoordintpair = new ChunkCoordIntPair(i);
for (int j = chunkcoordintpair.x - 1; j <= chunkcoordintpair.x + 1; ++j) {
for (int k = chunkcoordintpair.z - 1; k <= chunkcoordintpair.z + 1; ++k) {
if (j != chunkcoordintpair.x || k != chunkcoordintpair.z) {
Chunk chunk1 = (Chunk) super.get(ChunkCoordIntPair.a(j, k)); // Paper - use super to avoid polluting last access cache
if (chunk1 != null) {
chunk1.I();
}
}
}
}
// Paper start
} // close if (chunk != null)
if (this.lastChunkByPos != null && i == this.lastChunkByPos.chunkKey) {
this.lastChunkByPos = null;
}
return chunk;
}
private Chunk lastChunkByPos = null;
@Override
public Chunk get(long l) {
if (MCUtil.isMainThread()) {
if (this.lastChunkByPos != null && l == this.lastChunkByPos.chunkKey) {
return this.lastChunkByPos;
}
final Chunk chunk = super.get(l);
return chunk != null ? (lastChunkByPos = chunk) : null;
} else {
synchronized (this) {
// synchronize and try again to be sure
return super.get(l);
}
}
}
// Paper end
public Chunk a(Object object) {
return MCUtil.ensureMain("Chunk Remove", () -> this.a(((Long) object).longValue())); // Paper
}
public void putAll(Map<? extends Long, ? extends Chunk> map) {
throw new RuntimeException("Not yet implemented");
}
public boolean remove(Object object, Object object1) {
throw new RuntimeException("Not yet implemented");
}
// CraftBukkit start - decompile errors
public Chunk remove(long i) {
return MCUtil.ensureMain("Chunk Remove", () -> this.a(i)); // Paper
}
public Chunk put(long i, Chunk object) {
return MCUtil.ensureMain("Chunk Put", () -> this.a(i, (Chunk) object)); // Paper
}
public Chunk remove(Object object) {
return this.a(object);
}
public Chunk put(Long olong, Chunk object) {
return this.a(olong, (Chunk) object);
}
public Object put(Object object, Chunk object1) {
return this.a((Long) object, (Chunk) object1);
}
// CraftBukkit end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment