2021-07-26 01:49:36 +02:00
|
|
|
package org.dynmap.forge_1_17_1;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.dynmap.DynmapChunk;
|
|
|
|
import org.dynmap.Log;
|
2021-12-07 07:01:07 +01:00
|
|
|
import org.dynmap.common.chunk.GenericChunk;
|
|
|
|
import org.dynmap.common.chunk.GenericChunkCache;
|
|
|
|
import org.dynmap.common.chunk.GenericMapChunkCache;
|
2021-07-26 01:49:36 +02:00
|
|
|
|
|
|
|
import net.minecraft.nbt.CompoundTag;
|
|
|
|
import net.minecraft.server.level.ServerChunkCache;
|
|
|
|
import net.minecraft.server.level.ServerLevel;
|
|
|
|
import net.minecraft.world.level.ChunkPos;
|
|
|
|
import net.minecraft.world.level.chunk.ChunkAccess;
|
|
|
|
import net.minecraft.world.level.chunk.ChunkStatus;
|
|
|
|
import net.minecraft.world.level.chunk.storage.ChunkSerializer;
|
|
|
|
|
|
|
|
/**
|
2021-07-27 04:15:20 +02:00
|
|
|
* Container for managing chunks - dependent upon using chunk snapshots, since
|
|
|
|
* rendering is off server thread
|
2021-07-26 01:49:36 +02:00
|
|
|
*/
|
2021-12-07 07:01:07 +01:00
|
|
|
public class ForgeMapChunkCache extends GenericMapChunkCache {
|
2021-07-27 04:15:20 +02:00
|
|
|
private ServerLevel w;
|
|
|
|
private ServerChunkCache cps;
|
|
|
|
/**
|
2021-12-07 07:01:07 +01:00
|
|
|
* Construct empty cache
|
2021-07-27 04:15:20 +02:00
|
|
|
*/
|
2021-12-07 07:01:07 +01:00
|
|
|
public ForgeMapChunkCache(GenericChunkCache cc) {
|
|
|
|
super(cc);
|
2021-07-27 04:15:20 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 07:01:07 +01:00
|
|
|
// Load generic chunk from existing and already loaded chunk
|
|
|
|
protected GenericChunk getLoadedChunk(DynmapChunk chunk) {
|
|
|
|
GenericChunk gc = null;
|
|
|
|
ChunkAccess ch = cps.getChunk(chunk.x, chunk.z, ChunkStatus.FULL, false);
|
|
|
|
if (ch != null) {
|
|
|
|
CompoundTag nbt = ChunkSerializer.write(w, ch);
|
2021-12-16 19:32:10 +01:00
|
|
|
if (nbt != null) {
|
2021-12-07 07:01:07 +01:00
|
|
|
gc = parseChunkFromNBT(new NBT.NBTCompound(nbt));
|
2021-07-27 04:15:20 +02:00
|
|
|
}
|
|
|
|
}
|
2021-12-07 07:01:07 +01:00
|
|
|
return gc;
|
2021-07-27 04:15:20 +02:00
|
|
|
}
|
2021-12-07 07:01:07 +01:00
|
|
|
// Load generic chunk from unloaded chunk
|
|
|
|
protected GenericChunk loadChunk(DynmapChunk chunk) {
|
|
|
|
GenericChunk gc = null;
|
|
|
|
CompoundTag nbt = readChunk(chunk.x, chunk.z);
|
|
|
|
// If read was good
|
2021-12-16 19:32:10 +01:00
|
|
|
if (nbt != null) {
|
2021-12-07 07:01:07 +01:00
|
|
|
gc = parseChunkFromNBT(new NBT.NBTCompound(nbt));
|
2021-07-27 04:15:20 +02:00
|
|
|
}
|
2021-12-07 07:01:07 +01:00
|
|
|
return gc;
|
2021-07-27 04:15:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setChunks(ForgeWorld dw, List<DynmapChunk> chunks) {
|
|
|
|
this.w = dw.getWorld();
|
|
|
|
if (dw.isLoaded()) {
|
|
|
|
/* Check if world's provider is ServerChunkProvider */
|
|
|
|
cps = this.w.getChunkSource();
|
|
|
|
}
|
2021-12-07 07:01:07 +01:00
|
|
|
super.setChunks(dw, chunks);
|
2021-07-27 04:15:20 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 07:01:07 +01:00
|
|
|
private CompoundTag readChunk(int x, int z) {
|
2021-07-27 04:15:20 +02:00
|
|
|
try {
|
2021-12-16 19:32:10 +01:00
|
|
|
return cps.chunkMap.readChunk(new ChunkPos(x, z));
|
2021-07-27 04:15:20 +02:00
|
|
|
} catch (Exception exc) {
|
|
|
|
Log.severe(String.format("Error reading chunk: %s,%d,%d", dw.getName(), x, z), exc);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2021-07-26 01:49:36 +02:00
|
|
|
}
|