dynmap/forge-1.16.5/src/main/java/org/dynmap/forge_1_16_5/ForgeMapChunkCache.java

81 lines
2.4 KiB
Java

package org.dynmap.forge_1_16_5;
import java.util.List;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.chunk.AbstractChunkProvider;
import net.minecraft.world.chunk.ChunkStatus;
import net.minecraft.world.chunk.IChunk;
import net.minecraft.world.chunk.storage.ChunkSerializer;
import net.minecraft.world.server.ServerChunkProvider;
import net.minecraft.world.server.ServerWorld;
import org.dynmap.DynmapChunk;
import org.dynmap.Log;
import org.dynmap.common.chunk.GenericChunk;
import org.dynmap.common.chunk.GenericChunkCache;
import org.dynmap.common.chunk.GenericMapChunkCache;
/**
* Container for managing chunks - dependent upon using chunk snapshots, since rendering is off server thread
*/
public class ForgeMapChunkCache extends GenericMapChunkCache {
private ServerWorld w;
private ServerChunkProvider cps;
/**
* Construct empty cache
*/
public ForgeMapChunkCache(GenericChunkCache cc) {
super(cc);
}
// Load generic chunk from existing and already loaded chunk
protected GenericChunk getLoadedChunk(DynmapChunk chunk) {
GenericChunk gc = null;
IChunk ch = cps.getChunk(chunk.x, chunk.z, ChunkStatus.FULL, false);
if (ch != null) {
CompoundNBT nbt = ChunkSerializer.write(w, ch);
if (nbt != null) {
gc = parseChunkFromNBT(new NBT.NBTCompound(nbt));
}
}
return gc;
}
// Load generic chunk from unloaded chunk
protected GenericChunk loadChunk(DynmapChunk chunk) {
GenericChunk gc = null;
CompoundNBT nbt = readChunk(chunk.x, chunk.z);
// If read was good
if (nbt != null) {
gc = parseChunkFromNBT(new NBT.NBTCompound(nbt));
}
return gc;
}
public void setChunks(ForgeWorld dw, List<DynmapChunk> chunks) {
this.w = (ServerWorld) dw.getWorld();
if (dw.isLoaded()) {
/* Check if world's provider is ServerChunkProvider */
AbstractChunkProvider cp = this.w.getChunkProvider();
if (cp instanceof ServerChunkProvider) {
cps = (ServerChunkProvider)cp;
}
else {
Log.severe("Error: world " + dw.getName() + " has unsupported chunk provider");
}
}
super.setChunks(dw, chunks);
}
private CompoundNBT readChunk(int x, int z) {
try {
return cps.chunkManager.readChunk(new ChunkPos(x, z));
} catch (Exception exc) {
Log.severe(String.format("Error reading chunk: %s,%d,%d", dw.getName(), x, z), exc);
return null;
}
}
}