2021-02-08 05:22:35 +01:00
|
|
|
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;
|
2021-12-08 01:49:51 +01:00
|
|
|
import net.minecraft.world.chunk.IChunk;
|
2021-02-08 05:22:35 +01:00
|
|
|
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;
|
2021-12-08 01:49:51 +01:00
|
|
|
import org.dynmap.common.chunk.GenericChunk;
|
|
|
|
import org.dynmap.common.chunk.GenericChunkCache;
|
|
|
|
import org.dynmap.common.chunk.GenericMapChunkCache;
|
2021-02-08 05:22:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Container for managing chunks - dependent upon using chunk snapshots, since rendering is off server thread
|
|
|
|
*/
|
2021-12-08 01:49:51 +01:00
|
|
|
public class ForgeMapChunkCache extends GenericMapChunkCache {
|
|
|
|
private ServerWorld w;
|
2021-02-08 05:22:35 +01:00
|
|
|
private ServerChunkProvider cps;
|
2021-12-08 01:49:51 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
2021-02-08 05:22:35 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-08 01:49:51 +01:00
|
|
|
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));
|
2021-02-08 05:22:35 +01:00
|
|
|
}
|
2021-12-08 01:49:51 +01:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2021-02-08 05:22:35 +01:00
|
|
|
|
2021-12-08 01:49:51 +01:00
|
|
|
public void setChunks(ForgeWorld dw, List<DynmapChunk> chunks) {
|
|
|
|
this.w = (ServerWorld) dw.getWorld();
|
|
|
|
if (dw.isLoaded()) {
|
2021-02-08 05:22:35 +01:00
|
|
|
/* Check if world's provider is ServerChunkProvider */
|
|
|
|
AbstractChunkProvider cp = this.w.getChunkProvider();
|
|
|
|
|
2021-12-08 01:49:51 +01:00
|
|
|
if (cp instanceof ServerChunkProvider) {
|
2021-02-08 05:22:35 +01:00
|
|
|
cps = (ServerChunkProvider)cp;
|
|
|
|
}
|
2021-12-08 01:49:51 +01:00
|
|
|
else {
|
2021-02-08 05:22:35 +01:00
|
|
|
Log.severe("Error: world " + dw.getName() + " has unsupported chunk provider");
|
|
|
|
}
|
2021-12-08 01:49:51 +01:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2021-02-08 05:22:35 +01:00
|
|
|
}
|