2021-07-08 03:18:35 +02:00
|
|
|
package org.dynmap.fabric_1_17_1;
|
|
|
|
|
|
|
|
import net.minecraft.nbt.*;
|
|
|
|
import net.minecraft.server.world.ServerChunkManager;
|
|
|
|
import net.minecraft.server.world.ServerWorld;
|
|
|
|
import net.minecraft.server.world.ThreadedAnvilChunkStorage;
|
|
|
|
import net.minecraft.util.math.ChunkPos;
|
|
|
|
import net.minecraft.world.ChunkSerializer;
|
|
|
|
import net.minecraft.world.World;
|
2022-11-18 12:18:19 +01:00
|
|
|
import net.minecraft.world.biome.Biome;
|
|
|
|
import net.minecraft.world.biome.BiomeEffects;
|
2021-07-08 03:18:35 +02:00
|
|
|
import net.minecraft.world.chunk.ChunkManager;
|
|
|
|
import net.minecraft.world.chunk.ChunkStatus;
|
|
|
|
import org.dynmap.DynmapChunk;
|
|
|
|
import org.dynmap.Log;
|
2022-11-18 12:18:19 +01:00
|
|
|
import org.dynmap.common.BiomeMap;
|
2021-12-07 07:01:07 +01:00
|
|
|
import org.dynmap.common.chunk.GenericChunk;
|
|
|
|
import org.dynmap.common.chunk.GenericMapChunkCache;
|
2021-07-08 03:18:35 +02:00
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
2021-12-07 07:01:07 +01:00
|
|
|
public class FabricMapChunkCache extends GenericMapChunkCache {
|
2021-07-08 03:18:35 +02:00
|
|
|
private World w;
|
|
|
|
private ServerChunkManager cps;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct empty cache
|
|
|
|
*/
|
|
|
|
public FabricMapChunkCache(DynmapPlugin plugin) {
|
2021-12-07 07:01:07 +01:00
|
|
|
super(plugin.sscache);
|
2021-07-08 03:18:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setChunks(FabricWorld dw, List<DynmapChunk> chunks) {
|
|
|
|
this.w = dw.getWorld();
|
|
|
|
if (dw.isLoaded()) {
|
|
|
|
/* Check if world's provider is ServerChunkManager */
|
|
|
|
ChunkManager cp = this.w.getChunkManager();
|
|
|
|
|
|
|
|
if (cp instanceof ServerChunkManager) {
|
|
|
|
cps = (ServerChunkManager) cp;
|
|
|
|
} else {
|
|
|
|
Log.severe("Error: world " + dw.getName() + " has unsupported chunk provider");
|
|
|
|
}
|
2021-12-07 07:01:07 +01:00
|
|
|
}
|
|
|
|
super.setChunks(dw, chunks);
|
2021-07-08 03:18:35 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 07:01:07 +01:00
|
|
|
private NbtCompound readChunk(int x, int z) {
|
2021-07-08 03:18:35 +02:00
|
|
|
try {
|
|
|
|
ThreadedAnvilChunkStorage acl = cps.threadedAnvilChunkStorage;
|
|
|
|
|
|
|
|
ChunkPos coord = new ChunkPos(x, z);
|
2021-12-16 19:32:10 +01:00
|
|
|
return acl.getNbt(coord);
|
2021-07-08 03:18:35 +02:00
|
|
|
} catch (Exception exc) {
|
|
|
|
Log.severe(String.format("Error reading chunk: %s,%d,%d", dw.getName(), x, z), exc);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
if (cps.isChunkLoaded(chunk.x, chunk.z)) {
|
|
|
|
NbtCompound nbt = null;
|
|
|
|
try {
|
|
|
|
nbt = ChunkSerializer.serialize((ServerWorld) w, cps.getWorldChunk(chunk.x, chunk.z, false));
|
|
|
|
} catch (NullPointerException e) {
|
|
|
|
// TODO: find out why this is happening and why it only seems to happen since 1.16.2
|
|
|
|
Log.severe("ChunkSerializer.serialize threw a NullPointerException", e);
|
|
|
|
}
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load generic chunk from unloaded chunk
|
|
|
|
protected GenericChunk loadChunk(DynmapChunk chunk) {
|
|
|
|
GenericChunk gc = null;
|
|
|
|
NbtCompound 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));
|
|
|
|
}
|
|
|
|
return gc;
|
|
|
|
}
|
2022-11-18 12:18:19 +01:00
|
|
|
@Override
|
|
|
|
public int getFoliageColor(BiomeMap bm, int[] colormap, int x, int z) {
|
|
|
|
return bm.<Biome>getBiomeObject().map(Biome::getEffects).flatMap(BiomeEffects::getFoliageColor).orElse(colormap[bm.biomeLookup()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getGrassColor(BiomeMap bm, int[] colormap, int x, int z) {
|
|
|
|
BiomeEffects effects = bm.<Biome>getBiomeObject().map(Biome::getEffects).orElse(null);
|
|
|
|
if (effects == null) return colormap[bm.biomeLookup()];
|
|
|
|
return effects.getGrassColorModifier().getModifiedGrassColor(x, z, effects.getGrassColor().orElse(colormap[bm.biomeLookup()]));
|
|
|
|
}
|
2021-07-08 03:18:35 +02:00
|
|
|
}
|