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

105 lines
3.0 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);
init();
}
private boolean isLitChunk(CompoundNBT nbt) {
if ((nbt != null) && nbt.contains("Level")) {
nbt = nbt.getCompound("Level");
}
if (nbt != null) {
String stat = nbt.getString("Status");
ChunkStatus cs = ChunkStatus.byName(stat);
if ((stat != null) && cs.isAtLeast(ChunkStatus.LIGHT)) { // ChunkStatus.LIGHT
return true;
}
}
return false;
}
// 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 (isLitChunk(nbt)) {
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 (isLitChunk(nbt)) {
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 {
CompoundNBT rslt = cps.chunkManager.readChunk(new ChunkPos(x, z));
if (rslt != null) {
if (rslt.contains("Level")) {
rslt = rslt.getCompound("Level");
}
}
if (!isLitChunk(rslt)) {
rslt = null;
}
return rslt;
} catch (Exception exc) {
Log.severe(String.format("Error reading chunk: %s,%d,%d", dw.getName(), x, z), exc);
return null;
}
}
}