Migrate Spigot, Forge, Fabric 1.17.1 to generic chunk handling

This commit is contained in:
Mike Primm 2021-12-07 00:01:07 -06:00
parent 4db7eeab95
commit d42921beb5
24 changed files with 1172 additions and 4688 deletions

View File

@ -0,0 +1,5 @@
package org.dynmap.common.chunk;
public interface GenericBitStorage {
public int get(int idx);
}

View File

@ -16,6 +16,7 @@ import org.dynmap.utils.DynIntHashMap;
import org.dynmap.utils.MapChunkCache;
import org.dynmap.utils.MapIterator;
import org.dynmap.utils.BlockStep;
import org.dynmap.utils.DataBitsPacked;
import org.dynmap.utils.VisibilityLimit;
/**
@ -41,10 +42,6 @@ public abstract class GenericMapChunkCache extends MapChunkCache {
private static final BlockStep unstep[] = { BlockStep.X_MINUS, BlockStep.Y_MINUS, BlockStep.Z_MINUS,
BlockStep.X_PLUS, BlockStep.Y_PLUS, BlockStep.Z_PLUS };
private static final int getIndexInChunk(int cx, int cy, int cz) {
return (cy << 8) | (cz << 4) | cx;
}
/**
* Iterator for traversing map chunk cache (base is for non-snapshot)
*/
@ -55,13 +52,8 @@ public abstract class GenericMapChunkCache extends MapChunkCache {
private DynmapBlockState blk;
private final int worldheight;
private final int ymin;
private final int x_base;
private final int z_base;
OurMapIterator(int x0, int y0, int z0) {
x_base = x_min << 4;
z_base = z_min << 4;
initialize(x0, y0, z0);
worldheight = dw.worldheight;
ymin = dw.minY;
@ -600,8 +592,6 @@ public abstract class GenericMapChunkCache extends MapChunkCache {
}
private static boolean didError = false;
private boolean isChunkVisible(DynmapChunk chunk) {
boolean vis = true;
if (visible_limits != null) {
@ -906,4 +896,192 @@ public abstract class GenericMapChunkCache extends MapChunkCache {
return true;
}
public GenericChunk parseChunkFromNBT(GenericNBTCompound nbt) {
if ((nbt != null) && nbt.contains("Level")) {
nbt = nbt.getCompound("Level");
}
if (nbt == null) return null;
// Start generic chunk builder
GenericChunk.Builder bld = new GenericChunk.Builder(dw.minY, dw.worldheight);
bld.coords(nbt.getInt("xPos"), nbt.getInt("zPos"));
if (nbt.contains("InhabitedTime")) {
bld.inhabitedTicks(nbt.getLong("InhabitedTime"));
}
// Check for 2D or old 3D biome data from chunk level: need these when we build old sections
List<BiomeMap[]> old3d = null; // By section, then YZX list
BiomeMap[] old2d = null;
if (nbt.contains("Biomes")) {
int[] bb = nbt.getIntArray("Biomes");
if (bb != null) {
// If v1.15+ format
if (bb.length > 256) {
old3d = new ArrayList<BiomeMap[]>();
// Get 4 x 4 x 4 list for each section
for (int sect = 0; sect < (bb.length / 64); sect++) {
BiomeMap smap[] = new BiomeMap[64];
for (int i = 0; i < 64; i++) {
smap[i] = BiomeMap.byBiomeID(bb[sect*64 + i]);
}
old3d.add(smap);
}
}
else { // Else, older chunks
old2d = new BiomeMap[256];
for (int i = 0; i < bb.length; i++) {
old2d[i] = BiomeMap.byBiomeID(bb[i]);
}
}
}
}
// Start section builder
GenericChunkSection.Builder sbld = new GenericChunkSection.Builder();
/* Get sections */
GenericNBTList sect = nbt.contains("sections") ? nbt.getList("sections", 10) : nbt.getList("Sections", 10);
for (int i = 0; i < sect.size(); i++) {
GenericNBTCompound sec = sect.getCompound(i);
int secnum = sec.getByte("Y");
DynmapBlockState[] palette = null;
// If we've got palette and block states list, process non-empty section
if (sec.contains("Palette", 9) && sec.contains("BlockStates", 12)) {
GenericNBTList plist = sec.getList("Palette", 10);
long[] statelist = sec.getLongArray("BlockStates");
palette = new DynmapBlockState[plist.size()];
for (int pi = 0; pi < plist.size(); pi++) {
GenericNBTCompound tc = plist.getCompound(pi);
String pname = tc.getString("Name");
if (tc.contains("Properties")) {
StringBuilder statestr = new StringBuilder();
GenericNBTCompound prop = tc.getCompound("Properties");
for (String pid : prop.getAllKeys()) {
if (statestr.length() > 0) statestr.append(',');
statestr.append(pid).append('=').append(prop.getAsString(pid));
}
palette[pi] = DynmapBlockState.getStateByNameAndState(pname, statestr.toString());
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.getBaseStateByName(pname);
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.AIR;
}
}
int recsperblock = (4096 + statelist.length - 1) / statelist.length;
int bitsperblock = 64 / recsperblock;
GenericBitStorage db = null;
DataBitsPacked dbp = null;
try {
db = nbt.makeBitStorage(bitsperblock, 4096, statelist);
} catch (Exception x) { // Handle legacy encoded
bitsperblock = (statelist.length * 64) / 4096;
dbp = new DataBitsPacked(bitsperblock, 4096, statelist);
}
if (bitsperblock > 8) { // Not palette
for (int j = 0; j < 4096; j++) {
int v = (dbp != null) ? dbp.getAt(j) : db.get(j);
sbld.xyzBlockState(j & 0xF, (j & 0xF00) >> 8, (j & 0xF0) >> 4, DynmapBlockState.getStateByGlobalIndex(v));
}
}
else {
for (int j = 0; j < 4096; j++) {
int v = (dbp != null) ? dbp.getAt(j) : db.get(j);
DynmapBlockState bs = (v < palette.length) ? palette[v] : DynmapBlockState.AIR;
sbld.xyzBlockState(j & 0xF, (j & 0xF00) >> 8, (j & 0xF0) >> 4, bs);
}
}
}
else if (sec.contains("block_states")) { // 1.18
GenericNBTCompound block_states = sec.getCompound("block_states");
// If we've block state data, process non-empty section
if (block_states.contains("data", 12)) {
long[] statelist = block_states.getLongArray("data");
GenericNBTList plist = block_states.getList("palette", 10);
palette = new DynmapBlockState[plist.size()];
for (int pi = 0; pi < plist.size(); pi++) {
GenericNBTCompound tc = plist.getCompound(pi);
String pname = tc.getString("Name");
if (tc.contains("Properties")) {
StringBuilder statestr = new StringBuilder();
GenericNBTCompound prop = tc.getCompound("Properties");
for (String pid : prop.getAllKeys()) {
if (statestr.length() > 0) statestr.append(',');
statestr.append(pid).append('=').append(prop.getAsString(pid));
}
palette[pi] = DynmapBlockState.getStateByNameAndState(pname, statestr.toString());
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.getBaseStateByName(pname);
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.AIR;
}
}
GenericBitStorage db = null;
DataBitsPacked dbp = null;
int bitsperblock = (statelist.length * 64) / 4096;
int expectedStatelistLength = (4096 + (64 / bitsperblock) - 1) / (64 / bitsperblock);
if (statelist.length == expectedStatelistLength) {
db = nbt.makeBitStorage(bitsperblock, 4096, statelist);
}
else {
bitsperblock = (statelist.length * 64) / 4096;
dbp = new DataBitsPacked(bitsperblock, 4096, statelist);
}
if (bitsperblock > 8) { // Not palette
for (int j = 0; j < 4096; j++) {
int v = db != null ? db.get(j) : dbp.getAt(j);
sbld.xyzBlockState(j & 0xF, (j & 0xF00) >> 8, (j & 0xF0) >> 4, DynmapBlockState.getStateByGlobalIndex(v));
}
}
else {
for (int j = 0; j < 4096; j++) {
int v = db != null ? db.get(j) : dbp.getAt(j);
DynmapBlockState bs = (v < palette.length) ? palette[v] : DynmapBlockState.AIR;
sbld.xyzBlockState(j & 0xF, (j & 0xF00) >> 8, (j & 0xF0) >> 4, bs);
}
}
}
}
if (sec.contains("BlockLight")) {
sbld.emittedLight(sec.getByteArray("BlockLight"));
}
if (sec.contains("SkyLight")) {
sbld.skyLight(sec.getByteArray("SkyLight"));
}
// If section biome palette
if (sec.contains("biomes")) {
GenericNBTCompound nbtbiomes = sec.getCompound("biomes");
long[] bdataPacked = nbtbiomes.getLongArray("data");
GenericNBTList bpalette = nbtbiomes.getList("palette", 8);
GenericBitStorage bdata = null;
if (bdataPacked.length > 0)
bdata = nbt.makeBitStorage(bdataPacked.length, 64, bdataPacked);
for (int j = 0; j < 64; j++) {
int b = bdata != null ? bdata.get(j) : 0;
sbld.xyzBiome(j & 0x3, (j & 0x30) >> 4, (j & 0xC) >> 2, BiomeMap.byBiomeResourceLocation(bpalette.getString(b)));
}
}
else { // Else, apply legacy biomes
if (old3d != null) {
BiomeMap m[] = old3d.get((secnum > 0) ? ((secnum < old3d.size()) ? secnum : old3d.size()-1) : 0);
if (m != null) {
for (int j = 0; j < 64; j++) {
sbld.xyzBiome(j & 0x3, (j & 0x30) >> 4, (j & 0xC) >> 2, m[j]);
}
}
}
else if (old2d != null) {
for (int j = 0; j < 256; j++) {
sbld.xzBiome(j & 0xF, (j & 0xF0) >> 4, old2d[j]);
}
}
}
// Finish and add section
bld.addSection(secnum, sbld.build());
sbld.reset();
}
return bld.build();
}
}

View File

@ -0,0 +1,41 @@
package org.dynmap.common.chunk;
import java.util.Set;
// Generic interface for accessing an NBT Composite object
public interface GenericNBTCompound {
public final byte TAG_END = 0;
public final byte TAG_BYTE = 1;
public final byte TAG_SHORT = 2;
public final byte TAG_INT = 3;
public final byte TAG_LONG = 4;
public final byte TAG_FLOAT = 5;
public final byte TAG_DOUBLE = 6;
public final byte TAG_BYTE_ARRAY = 7;
public final byte TAG_STRING = 8;
public final byte TAG_LIST = 9;
public final byte TAG_COMPOUND = 10;
public final byte TAG_INT_ARRAY = 11;
public final byte TAG_LONG_ARRAY = 12;
public final byte TAG_ANY_NUMERIC = 99;
public Set<String> getAllKeys();
public boolean contains(String s);
public boolean contains(String s, int i);
public byte getByte(String s);
public short getShort(String s);
public int getInt(String s);
public long getLong(String s);
public float getFloat(String s);
public double getDouble(String s);
public String getString(String s);
public byte[] getByteArray(String s);
public int[] getIntArray(String s);
public long[] getLongArray(String s);
public GenericNBTCompound getCompound(String s);
public GenericNBTList getList(String s, int i);
public boolean getBoolean(String s);
public String getAsString(String s); /// get(s).getAsString()
// Factory for bit storage
public GenericBitStorage makeBitStorage(int bits, int count, long[] data);
}

View File

@ -0,0 +1,10 @@
package org.dynmap.common.chunk;
import java.util.Set;
// Generic interface for accessing an NBT Composite object
public interface GenericNBTList {
public int size();
public String getString(int idx);
public GenericNBTCompound getCompound(int idx);
}

View File

@ -166,7 +166,7 @@ public class BukkitVersionHelperSpigot117 extends BukkitVersionHelper {
*/
@Override
public MapChunkCache getChunkCache(BukkitWorld dw, List<DynmapChunk> chunks) {
MapChunkCache117 c = new MapChunkCache117();
MapChunkCache117 c = new MapChunkCache117(gencache);
c.setChunks(dw, chunks);
return c;
}
@ -406,4 +406,8 @@ public class BukkitVersionHelperSpigot117 extends BukkitVersionHelper {
CraftWorld cw = (CraftWorld) w;
return cw.getMinHeight();
}
@Override
public boolean useGenericCache() {
return true;
}
}

View File

@ -1,513 +1,90 @@
package org.dynmap.bukkit.helper.v117;
import org.bukkit.ChunkSnapshot;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.craftbukkit.v1_17_R1.CraftWorld;
import org.dynmap.DynmapChunk;
import org.dynmap.DynmapCore;
import org.dynmap.bukkit.helper.AbstractMapChunkCache;
import org.dynmap.bukkit.helper.BukkitVersionHelper;
import org.dynmap.bukkit.helper.SnapshotCache;
import org.dynmap.bukkit.helper.SnapshotCache.SnapshotRec;
import org.dynmap.renderer.DynmapBlockState;
import org.dynmap.utils.DynIntHashMap;
import org.dynmap.utils.VisibilityLimit;
import org.dynmap.bukkit.helper.BukkitWorld;
import org.dynmap.common.chunk.GenericChunk;
import org.dynmap.common.chunk.GenericChunkCache;
import org.dynmap.common.chunk.GenericMapChunkCache;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.DataBits;
import net.minecraft.util.datafix.DataBitsPacked;
import net.minecraft.world.level.ChunkCoordIntPair;
import net.minecraft.world.level.chunk.Chunk;
import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.level.chunk.storage.ChunkRegionLoader;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* Container for managing chunks - dependent upon using chunk snapshots, since rendering is off server thread
*/
public class MapChunkCache117 extends AbstractMapChunkCache {
public static class NBTSnapshot implements Snapshot {
private static interface Section {
public DynmapBlockState getBlockType(int x, int y, int z);
public int getBlockSkyLight(int x, int y, int z);
public int getBlockEmittedLight(int x, int y, int z);
public boolean isEmpty();
}
private final int x, z;
private final Section[] section;
private final int sectionOffset;
private final int[] hmap; // Height map
private final int[] biome;
private final Object[] biomebase;
private final long captureFulltime;
private final int sectionCnt;
private final long inhabitedTicks;
private static final int BLOCKS_PER_SECTION = 16 * 16 * 16;
private static final int COLUMNS_PER_CHUNK = 16 * 16;
private static final int V1_15_BIOME_PER_CHUNK = 4 * 4 * 64;
private static final byte[] emptyData = new byte[BLOCKS_PER_SECTION / 2];
private static final byte[] fullData = new byte[BLOCKS_PER_SECTION / 2];
static
{
Arrays.fill(fullData, (byte)0xFF);
}
private static byte[] dataCopy(byte[] v) {
if (Arrays.equals(v, emptyData))
return emptyData;
else if (Arrays.equals(v, fullData))
return fullData;
else
return v.clone();
}
private static class EmptySection implements Section {
@Override
public DynmapBlockState getBlockType(int x, int y, int z) {
return DynmapBlockState.AIR;
}
@Override
public int getBlockSkyLight(int x, int y, int z) {
return 15;
}
@Override
public int getBlockEmittedLight(int x, int y, int z) {
return 0;
}
@Override
public boolean isEmpty() {
return true;
}
}
private static final EmptySection empty_section = new EmptySection();
private static class StdSection implements Section {
DynmapBlockState[] states;
byte[] skylight;
byte[] emitlight;
public StdSection() {
states = new DynmapBlockState[BLOCKS_PER_SECTION];
Arrays.fill(states, DynmapBlockState.AIR);
skylight = emptyData;
emitlight = emptyData;
}
@Override
public DynmapBlockState getBlockType(int x, int y, int z) {
return states[((y & 0xF) << 8) | (z << 4) | x];
}
@Override
public int getBlockSkyLight(int x, int y, int z) {
int off = ((y & 0xF) << 7) | (z << 3) | (x >> 1);
return (skylight[off] >> (4 * (x & 1))) & 0xF;
}
@Override
public int getBlockEmittedLight(int x, int y, int z)
{
int off = ((y & 0xF) << 7) | (z << 3) | (x >> 1);
return (emitlight[off] >> (4 * (x & 1))) & 0xF;
}
@Override
public boolean isEmpty() {
return false;
}
}
/**
* Construct empty chunk snapshot
*
* @param x
* @param z
*/
public NBTSnapshot(int worldheight, int x, int z, long captime, long inhabitedTime)
{
this.x = x;
this.z = z;
this.captureFulltime = captime;
this.biome = new int[COLUMNS_PER_CHUNK];
this.biomebase = new Object[COLUMNS_PER_CHUNK];
this.sectionCnt = worldheight / 16;
/* Allocate arrays indexed by section */
this.section = new Section[this.sectionCnt+1];
this.sectionOffset = 0;
/* Fill with empty data */
for (int i = 0; i <= this.sectionCnt; i++) {
this.section[i] = empty_section;
}
/* Create empty height map */
this.hmap = new int[16 * 16];
this.inhabitedTicks = inhabitedTime;
}
public NBTSnapshot(NBTTagCompound nbt, int worldheight) {
this.x = nbt.getInt("xPos");
this.z = nbt.getInt("zPos");
this.captureFulltime = 0;
this.hmap = nbt.getIntArray("HeightMap");
this.sectionCnt = worldheight / 16;
if (nbt.hasKey("InhabitedTime")) {
this.inhabitedTicks = nbt.getLong("InhabitedTime");
}
else {
this.inhabitedTicks = 0;
}
/* Allocate arrays indexed by section */
LinkedList<Section> sections = new LinkedList<Section>();
int sectoff = 0; // Default to zero
int sectcnt = 0;
/* Fill with empty data */
for (int i = 0; i <= this.sectionCnt; i++) {
sections.add(empty_section);
sectcnt++;
}
/* Get sections */
NBTTagList sect = nbt.getList("Sections", 10);
for (int i = 0; i < sect.size(); i++) {
NBTTagCompound sec = sect.getCompound(i);
int secnum = sec.getByte("Y");
// Beyond end - extend up
while (secnum >= (sectcnt - sectoff)) {
sections.addLast(empty_section); // Pad with empty
sectcnt++;
}
// Negative - see if we need to extend sectionOffset
while ((secnum + sectoff) < 0) {
sections.addFirst(empty_section); // Pad with empty
sectoff++;
sectcnt++;
}
//System.out.println("section(" + secnum + ")=" + sec.asString());
// Create normal section to initialize
StdSection cursect = new StdSection();
sections.set(secnum + sectoff, cursect);
DynmapBlockState[] states = cursect.states;
DynmapBlockState[] palette = null;
// If we've got palette and block states list, process non-empty section
if (sec.hasKeyOfType("Palette", 9) && sec.hasKeyOfType("BlockStates", 12)) {
NBTTagList plist = sec.getList("Palette", 10);
long[] statelist = sec.getLongArray("BlockStates");
palette = new DynmapBlockState[plist.size()];
for (int pi = 0; pi < plist.size(); pi++) {
NBTTagCompound tc = plist.getCompound(pi);
String pname = tc.getString("Name");
if (tc.hasKey("Properties")) {
StringBuilder statestr = new StringBuilder();
NBTTagCompound prop = tc.getCompound("Properties");
for (String pid : prop.getKeys()) {
if (statestr.length() > 0) statestr.append(',');
statestr.append(pid).append('=').append(prop.get(pid).asString());
}
palette[pi] = DynmapBlockState.getStateByNameAndState(pname, statestr.toString());
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.getBaseStateByName(pname);
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.AIR;
}
}
int recsperblock = (4096 + statelist.length - 1) / statelist.length;
int bitsperblock = 64 / recsperblock;
DataBits db = null;
DataBitsPacked dbp = null;
try {
db = new DataBits(bitsperblock, 4096, statelist);
} catch (Exception x) { // Handle legacy encoded
bitsperblock = (statelist.length * 64) / 4096;
dbp = new DataBitsPacked(bitsperblock, 4096, statelist);
}
if (bitsperblock > 8) { // Not palette
for (int j = 0; j < 4096; j++) {
int v = (db != null) ? db.a(j) : dbp.a(j);
states[j] = DynmapBlockState.getStateByGlobalIndex(v);
}
}
else {
for (int j = 0; j < 4096; j++) {
int v = (db != null) ? db.a(j) : dbp.a(j);
states[j] = (v < palette.length) ? palette[v] : DynmapBlockState.AIR;
}
}
}
if (sec.hasKey("BlockLight")) {
cursect.emitlight = dataCopy(sec.getByteArray("BlockLight"));
}
if (sec.hasKey("SkyLight")) {
cursect.skylight = dataCopy(sec.getByteArray("SkyLight"));
}
}
/* Get biome data */
this.biome = new int[COLUMNS_PER_CHUNK];
this.biomebase = new Object[COLUMNS_PER_CHUNK];
Object[] bbl = BukkitVersionHelper.helper.getBiomeBaseList();
if (nbt.hasKey("Biomes")) {
int[] bb = nbt.getIntArray("Biomes");
if (bb != null) {
// If v1.15+ format
if (bb.length > COLUMNS_PER_CHUNK) {
// For now, just pad the grid with the first 16
for (int i = 0; i < COLUMNS_PER_CHUNK; i++) {
int off = ((i >> 4) & 0xC) + ((i >> 2) & 0x3);
int bv = bb[off + 64]; // Offset to y=64
if (bv < 0) bv = 0;
this.biome[i] = bv;
this.biomebase[i] = bbl[bv];
}
}
else { // Else, older chunks
for (int i = 0; i < bb.length; i++) {
int bv = bb[i];
if (bv < 0) bv = 0;
this.biome[i] = bv;
this.biomebase[i] = bbl[bv];
}
}
}
}
// Finalize sections array
this.section = sections.toArray(new Section[sections.size()]);
this.sectionOffset = sectoff;
}
public int getX()
{
return x;
}
public int getZ()
{
return z;
}
public DynmapBlockState getBlockType(int x, int y, int z)
{
int idx = (y >> 4) + sectionOffset;
if ((idx < 0) || (idx >= section.length)) return DynmapBlockState.AIR;
return section[idx].getBlockType(x, y, z);
}
public int getBlockSkyLight(int x, int y, int z)
{
int idx = (y >> 4) + sectionOffset;
if ((idx < 0) || (idx >= section.length)) return 15;
return section[idx].getBlockSkyLight(x, y, z);
}
public int getBlockEmittedLight(int x, int y, int z)
{
int idx = (y >> 4) + sectionOffset;
if ((idx < 0) || (idx >= section.length)) return 0;
return section[idx].getBlockEmittedLight(x, y, z);
}
public int getHighestBlockYAt(int x, int z)
{
return hmap[z << 4 | x];
}
public final long getCaptureFullTime()
{
return captureFulltime;
}
public boolean isSectionEmpty(int sy)
{
int idx = sy + sectionOffset;
if ((idx < 0) || (idx >= section.length)) return true;
return section[idx].isEmpty();
}
public long getInhabitedTicks() {
return inhabitedTicks;
}
@Override
public Biome getBiome(int x, int z) {
return AbstractMapChunkCache.getBiomeByID(biome[z << 4 | x]);
}
@Override
public Object[] getBiomeBaseFromSnapshot() {
return this.biomebase;
}
public class MapChunkCache117 extends GenericMapChunkCache {
private World w;
/**
* Construct empty cache
*/
public MapChunkCache117(GenericChunkCache cc) {
super(cc);
init();
}
private NBTTagCompound fetchLoadedChunkNBT(World w, int x, int z) {
private boolean isLitChunk(NBTTagCompound nbt) {
if ((nbt != null) && nbt.hasKey("Level")) {
nbt = nbt.getCompound("Level");
}
if (nbt != null) {
String stat = nbt.getString("Status");
ChunkStatus cs = ChunkStatus.a(stat);
if ((stat != null) && cs.b(ChunkStatus.l)) { // ChunkStatus.LIGHT
return true;
}
}
return false;
}
// Load generic chunk from existing and already loaded chunk
protected GenericChunk getLoadedChunk(DynmapChunk chunk) {
CraftWorld cw = (CraftWorld) w;
NBTTagCompound nbt = null;
if (cw.isChunkLoaded(x, z)) {
Chunk c = cw.getHandle().getChunkAt(x, z);
GenericChunk gc = null;
if (cw.isChunkLoaded(chunk.x, chunk.z)) {
Chunk c = cw.getHandle().getChunkAt(chunk.x, chunk.z);
if ((c != null) && c.h) { // c.loaded
nbt = ChunkRegionLoader.saveChunk(cw.getHandle(), c);
}
}
if (nbt != null) {
nbt = nbt.getCompound("Level");
if (!isLitChunk(nbt)) {
nbt = null;
}
if (nbt != null) {
String stat = nbt.getString("Status");
ChunkStatus cs = ChunkStatus.a(stat);
if ((stat == null) || (!cs.b(ChunkStatus.j))) { // ChunkStatus.LIGHT
nbt = null;
}
gc = parseChunkFromNBT(new NBT.NBTCompound(nbt));
}
}
return nbt;
}
private NBTTagCompound loadChunkNBT(World w, int x, int z) {
return gc;
}
// Load generic chunk from unloaded chunk
protected GenericChunk loadChunk(DynmapChunk chunk) {
CraftWorld cw = (CraftWorld) w;
NBTTagCompound nbt = null;
ChunkCoordIntPair cc = new ChunkCoordIntPair(x, z);
ChunkCoordIntPair cc = new ChunkCoordIntPair(chunk.x, chunk.z);
GenericChunk gc = null;
try {
nbt = cw.getHandle().getChunkProvider().a.read(cc); // playerChunkMap
} catch (IOException iox) {
}
if (!isLitChunk(nbt)) {
nbt = null;
}
if (nbt != null) {
nbt = nbt.getCompound("Level");
if (nbt != null) {
String stat = nbt.getString("Status");
if ((stat == null) || (stat.equals("full") == false)) {
nbt = null;
if ((stat == null) || stat.equals("") && DynmapCore.migrateChunks()) {
Chunk c = cw.getHandle().getChunkAt(x, z);
if (c != null) {
nbt = fetchLoadedChunkNBT(w, x, z);
cw.getHandle().unloadChunk(c);
}
}
}
}
gc = parseChunkFromNBT(new NBT.NBTCompound(nbt));
}
return nbt;
}
@Override
public Snapshot wrapChunkSnapshot(ChunkSnapshot css) {
// TODO Auto-generated method stub
return null;
}
// Load chunk snapshots
@Override
public int loadChunks(int max_to_load) {
if(dw.isLoaded() == false)
return 0;
int cnt = 0;
if(iterator == null)
iterator = chunks.listIterator();
return gc;
}
DynmapCore.setIgnoreChunkLoads(true);
// Load the required chunks.
while((cnt < max_to_load) && iterator.hasNext()) {
long startTime = System.nanoTime();
DynmapChunk chunk = iterator.next();
boolean vis = true;
if(visible_limits != null) {
vis = false;
for(VisibilityLimit limit : visible_limits) {
if (limit.doIntersectChunk(chunk.x, chunk.z)) {
vis = true;
break;
}
}
}
if(vis && (hidden_limits != null)) {
for(VisibilityLimit limit : hidden_limits) {
if (limit.doIntersectChunk(chunk.x, chunk.z)) {
vis = false;
break;
}
}
}
/* Check if cached chunk snapshot found */
Snapshot ss = null;
long inhabited_ticks = 0;
DynIntHashMap tileData = null;
int idx = (chunk.x-x_min) + (chunk.z - z_min)*x_dim;
SnapshotRec ssr = SnapshotCache.sscache.getSnapshot(dw.getName(), chunk.x, chunk.z, blockdata, biome, biomeraw, highesty);
if(ssr != null) {
inhabited_ticks = ssr.inhabitedTicks;
if(!vis) {
if(hidestyle == HiddenChunkStyle.FILL_STONE_PLAIN)
ss = STONE;
else if(hidestyle == HiddenChunkStyle.FILL_OCEAN)
ss = OCEAN;
else
ss = EMPTY;
}
else {
ss = ssr.ss;
}
snaparray[idx] = ss;
snaptile[idx] = ssr.tileData;
inhabitedTicks[idx] = inhabited_ticks;
endChunkLoad(startTime, ChunkStats.CACHED_SNAPSHOT_HIT);
continue;
}
// Fetch NTB for chunk if loaded
NBTTagCompound nbt = fetchLoadedChunkNBT(w, chunk.x, chunk.z);
boolean did_load = false;
if (nbt == null) {
// Load NTB for chunk, if it exists
nbt = loadChunkNBT(w, chunk.x, chunk.z);
did_load = true;
}
if (nbt != null) {
NBTSnapshot nss = new NBTSnapshot(nbt, w.getMaxHeight());
ss = nss;
inhabited_ticks = nss.getInhabitedTicks();
if(!vis) {
if(hidestyle == HiddenChunkStyle.FILL_STONE_PLAIN)
ss = STONE;
else if(hidestyle == HiddenChunkStyle.FILL_OCEAN)
ss = OCEAN;
else
ss = EMPTY;
}
}
else {
ss = EMPTY;
}
ssr = new SnapshotRec();
ssr.ss = ss;
ssr.inhabitedTicks = inhabited_ticks;
ssr.tileData = tileData;
SnapshotCache.sscache.putSnapshot(dw.getName(), chunk.x, chunk.z, ssr, blockdata, biome, biomeraw, highesty);
snaparray[idx] = ss;
snaptile[idx] = ssr.tileData;
inhabitedTicks[idx] = inhabited_ticks;
if (nbt == null)
endChunkLoad(startTime, ChunkStats.UNGENERATED_CHUNKS);
else if (did_load)
endChunkLoad(startTime, ChunkStats.UNLOADED_CHUNKS);
else
endChunkLoad(startTime, ChunkStats.LOADED_CHUNKS);
cnt++;
}
DynmapCore.setIgnoreChunkLoads(false);
if(iterator.hasNext() == false) { /* If we're done */
isempty = true;
/* Fill missing chunks with empty dummy chunk */
for(int i = 0; i < snaparray.length; i++) {
if(snaparray[i] == null)
snaparray[i] = EMPTY;
else if(snaparray[i] != EMPTY)
isempty = false;
}
}
return cnt;
}
public void setChunks(BukkitWorld dw, List<DynmapChunk> chunks) {
this.w = dw.getWorld();
super.setChunks(dw, chunks);
}
}

View File

@ -0,0 +1,120 @@
package org.dynmap.bukkit.helper.v117;
import org.dynmap.common.chunk.GenericBitStorage;
import org.dynmap.common.chunk.GenericNBTCompound;
import org.dynmap.common.chunk.GenericNBTList;
import java.util.Set;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.DataBits;
public class NBT {
public static class NBTCompound implements GenericNBTCompound {
private final NBTTagCompound obj;
public NBTCompound(NBTTagCompound t) {
this.obj = t;
}
@Override
public Set<String> getAllKeys() {
return obj.getKeys();
}
@Override
public boolean contains(String s) {
return obj.hasKey(s);
}
@Override
public boolean contains(String s, int i) {
return obj.hasKeyOfType(s, i);
}
@Override
public byte getByte(String s) {
return obj.getByte(s);
}
@Override
public short getShort(String s) {
return obj.getShort(s);
}
@Override
public int getInt(String s) {
return obj.getInt(s);
}
@Override
public long getLong(String s) {
return obj.getLong(s);
}
@Override
public float getFloat(String s) {
return obj.getFloat(s);
}
@Override
public double getDouble(String s) {
return obj.getDouble(s);
}
@Override
public String getString(String s) {
return obj.getString(s);
}
@Override
public byte[] getByteArray(String s) {
return obj.getByteArray(s);
}
@Override
public int[] getIntArray(String s) {
return obj.getIntArray(s);
}
@Override
public long[] getLongArray(String s) {
return obj.getLongArray(s);
}
@Override
public GenericNBTCompound getCompound(String s) {
return new NBTCompound(obj.getCompound(s));
}
@Override
public GenericNBTList getList(String s, int i) {
return new NBTList(obj.getList(s, i));
}
@Override
public boolean getBoolean(String s) {
return obj.getBoolean(s);
}
@Override
public String getAsString(String s) {
return obj.get(s).asString();
}
@Override
public GenericBitStorage makeBitStorage(int bits, int count, long[] data) {
return new OurBitStorage(bits, count, data);
}
}
public static class NBTList implements GenericNBTList {
private final NBTTagList obj;
public NBTList(NBTTagList t) {
obj = t;
}
@Override
public int size() {
return obj.size();
}
@Override
public String getString(int idx) {
return obj.getString(idx);
}
@Override
public GenericNBTCompound getCompound(int idx) {
return new NBTCompound(obj.getCompound(idx));
}
}
public static class OurBitStorage implements GenericBitStorage {
private final DataBits bs;
public OurBitStorage(int bits, int count, long[] data) {
bs = new DataBits(bits, count, data);
}
@Override
public int get(int idx) {
return bs.a(idx);
}
}
}

View File

@ -50,271 +50,60 @@ public class MapChunkCache118 extends GenericMapChunkCache {
init();
}
private GenericChunk parseChunkFromNBT(NBTTagCompound nbt) {
private boolean isLitChunk(NBTTagCompound nbt) {
if ((nbt != null) && nbt.e("Level")) {
nbt = nbt.p("Level");
}
if (nbt == null) return null;
// Start generic chunk builder
GenericChunk.Builder bld = new GenericChunk.Builder(dw.minY, dw.worldheight);
int cx = nbt.h("xPos");
int cz = nbt.h("zPos");
bld.coords(cx, cz);
if (nbt.e("InhabitedTime")) {
bld.inhabitedTicks(nbt.i("InhabitedTime"));
}
// Check for 2D or old 3D biome data from chunk level: need these when we build old sections
List<BiomeMap[]> old3d = null; // By section, then YZX list
BiomeMap[] old2d = null;
if (nbt.e("Biomes")) {
int[] bb = nbt.n("Biomes");
if (bb != null) {
// If v1.15+ format
if (bb.length > 256) {
old3d = new ArrayList<BiomeMap[]>();
// Get 4 x 4 x 4 list for each section
for (int sect = 0; sect < (bb.length / 64); sect++) {
BiomeMap smap[] = new BiomeMap[64];
for (int i = 0; i < 64; i++) {
smap[i] = BiomeMap.byBiomeID(bb[sect*64 + i]);
}
old3d.add(smap);
}
}
else { // Else, older chunks
old2d = new BiomeMap[256];
for (int i = 0; i < bb.length; i++) {
old2d[i] = BiomeMap.byBiomeID(bb[i]);
}
}
nbt = nbt.p("Level");
}
if (nbt != null) {
String stat = nbt.l("Status");
ChunkStatus cs = ChunkStatus.a(stat);
if ((stat != null) && cs.b(ChunkStatus.l)) { // ChunkStatus.LIGHT
return true;
}
}
// Start section builder
GenericChunkSection.Builder sbld = new GenericChunkSection.Builder();
/* Get sections */
NBTTagList sect = nbt.e("sections") ? nbt.c("sections", 10) : nbt.c("Sections", 10);
for (int i = 0; i < sect.size(); i++) {
NBTTagCompound sec = sect.a(i);
int secnum = sec.h("Y");
DynmapBlockState[] palette = null;
// If we've got palette and block states list, process non-empty section
if (sec.b("Palette", 9) && sec.b("BlockStates", 12)) {
NBTTagList plist = sec.c("Palette", 10);
long[] statelist = sec.o("BlockStates");
palette = new DynmapBlockState[plist.size()];
for (int pi = 0; pi < plist.size(); pi++) {
NBTTagCompound tc = plist.a(pi);
String pname = tc.l("Name");
if (tc.e("Properties")) {
StringBuilder statestr = new StringBuilder();
NBTTagCompound prop = tc.p("Properties");
for (String pid : prop.d()) {
if (statestr.length() > 0) statestr.append(',');
statestr.append(pid).append('=').append(prop.c(pid).e_());
}
palette[pi] = DynmapBlockState.getStateByNameAndState(pname, statestr.toString());
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.getBaseStateByName(pname);
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.AIR;
}
}
int recsperblock = (4096 + statelist.length - 1) / statelist.length;
int bitsperblock = 64 / recsperblock;
DataBits db = null;
DataBitsPacked dbp = null;
try {
db = new SimpleBitStorage(bitsperblock, 4096, statelist);
} catch (Exception x) { // Handle legacy encoded
bitsperblock = (statelist.length * 64) / 4096;
dbp = new DataBitsPacked(bitsperblock, 4096, statelist);
}
if (bitsperblock > 8) { // Not palette
for (int j = 0; j < 4096; j++) {
int v = (dbp != null) ? dbp.getAt(j) : db.a(j);
sbld.xyzBlockState(j & 0xF, (j & 0xF00) >> 8, (j & 0xF0) >> 4, DynmapBlockState.getStateByGlobalIndex(v));
}
}
else {
for (int j = 0; j < 4096; j++) {
int v = (dbp != null) ? dbp.getAt(j) : db.a(j);
DynmapBlockState bs = (v < palette.length) ? palette[v] : DynmapBlockState.AIR;
sbld.xyzBlockState(j & 0xF, (j & 0xF00) >> 8, (j & 0xF0) >> 4, bs);
}
}
}
else if (sec.e("block_states")) { // 1.18
NBTTagCompound block_states = sec.p("block_states");
// If we've block state data, process non-empty section
if (block_states.b("data", 12)) {
long[] statelist = block_states.o("data");
NBTTagList plist = block_states.c("palette", 10);
palette = new DynmapBlockState[plist.size()];
for (int pi = 0; pi < plist.size(); pi++) {
NBTTagCompound tc = plist.a(pi);
String pname = tc.l("Name");
if (tc.e("Properties")) {
StringBuilder statestr = new StringBuilder();
NBTTagCompound prop = tc.p("Properties");
for (String pid : prop.d()) {
if (statestr.length() > 0) statestr.append(',');
statestr.append(pid).append('=').append(prop.c(pid).e_());
}
palette[pi] = DynmapBlockState.getStateByNameAndState(pname, statestr.toString());
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.getBaseStateByName(pname);
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.AIR;
}
}
SimpleBitStorage db = null;
DataBitsPacked dbp = null;
int bitsperblock = (statelist.length * 64) / 4096;
int expectedStatelistLength = (4096 + (64 / bitsperblock) - 1) / (64 / bitsperblock);
if (statelist.length == expectedStatelistLength) {
db = new SimpleBitStorage(bitsperblock, 4096, statelist);
}
else {
bitsperblock = (statelist.length * 64) / 4096;
dbp = new DataBitsPacked(bitsperblock, 4096, statelist);
}
if (bitsperblock > 8) { // Not palette
for (int j = 0; j < 4096; j++) {
int v = db != null ? db.a(j) : dbp.getAt(j);
sbld.xyzBlockState(j & 0xF, (j & 0xF00) >> 8, (j & 0xF0) >> 4, DynmapBlockState.getStateByGlobalIndex(v));
}
}
else {
for (int j = 0; j < 4096; j++) {
int v = db != null ? db.a(j) : dbp.getAt(j);
DynmapBlockState bs = (v < palette.length) ? palette[v] : DynmapBlockState.AIR;
sbld.xyzBlockState(j & 0xF, (j & 0xF00) >> 8, (j & 0xF0) >> 4, bs);
}
}
}
}
if (sec.e("BlockLight")) {
sbld.emittedLight(sec.m("BlockLight"));
}
if (sec.e("SkyLight")) {
sbld.skyLight(sec.m("SkyLight"));
}
// If section biome palette
if (sec.e("biomes")) {
NBTTagCompound nbtbiomes = sec.p("biomes");
long[] bdataPacked = nbtbiomes.o("data");
NBTTagList bpalette = nbtbiomes.c("palette", 8);
SimpleBitStorage bdata = null;
if (bdataPacked.length > 0)
bdata = new SimpleBitStorage(bdataPacked.length, 64, bdataPacked);
for (int j = 0; j < 64; j++) {
int b = bdata != null ? bdata.a(j) : 0;
String rl = bpalette.j(b);
BiomeMap bm = BiomeMap.byBiomeResourceLocation(rl);
sbld.xyzBiome(j & 0x3, (j & 0x30) >> 4, (j & 0xC) >> 2, bm);
}
}
else { // Else, apply legacy biomes
if (old3d != null) {
BiomeMap m[] = old3d.get((secnum > 0) ? ((secnum < old3d.size()) ? secnum : old3d.size()-1) : 0);
if (m != null) {
for (int j = 0; j < 64; j++) {
sbld.xyzBiome(j & 0x3, (j & 0x30) >> 4, (j & 0xC) >> 2, m[j]);
}
}
}
else if (old2d != null) {
for (int j = 0; j < 256; j++) {
sbld.xzBiome(j & 0xF, (j & 0xF0) >> 4, old2d[j]);
}
}
}
// Finish and add section
bld.addSection(secnum, sbld.build());
sbld.reset();
}
return bld.build();
return false;
}
// Load generic chunk from existing and already loaded chunk
protected GenericChunk getLoadedChunk(DynmapChunk chunk) {
CraftWorld cw = (CraftWorld) w;
NBTTagCompound nbt = null;
GenericChunk gc = null;
if (cw.isChunkLoaded(chunk.x, chunk.z)) {
Chunk c = cw.getHandle().getChunkIfLoaded(chunk.x, chunk.z);
if ((c != null) && c.o) { // c.loaded
nbt = ChunkRegionLoader.a(cw.getHandle(), c);
}
if (nbt.e("Level")) {
nbt = nbt.p("Level");
}
if (!isLitChunk(nbt)) {
nbt = null;
}
if (nbt != null) {
String stat = nbt.l("Status");
ChunkStatus cs = ChunkStatus.a(stat);
if ((stat == null) || (!cs.b(ChunkStatus.l))) { // ChunkStatus.LIGHT
nbt = null;
}
gc = parseChunkFromNBT(new NBT.NBTCompound(nbt));
}
}
return parseChunkFromNBT(nbt);
return gc;
}
// Load generic chunk from unloaded chunk
protected GenericChunk loadChunk(DynmapChunk chunk) {
CraftWorld cw = (CraftWorld) w;
NBTTagCompound nbt = null;
ChunkCoordIntPair cc = new ChunkCoordIntPair(chunk.x, chunk.z);
GenericChunk gc = null;
try {
nbt = cw.getHandle().k().a.f(cc); // playerChunkMap
} catch (IOException iox) {
}
if (nbt != null) {
// See if we have Level - unwrap this if so
if (nbt.e("Level")) {
nbt = nbt.p("Level");
}
if (nbt != null) {
String stat = nbt.l("Status");
if ((stat == null) || (stat.equals("full") == false)) {
nbt = null;
}
}
if (!isLitChunk(nbt)) {
nbt = null;
}
return parseChunkFromNBT(nbt);
if (nbt != null) {
gc = parseChunkFromNBT(new NBT.NBTCompound(nbt));
}
return gc;
}
public void setChunks(BukkitWorld dw, List<DynmapChunk> chunks) {
this.w = dw.getWorld();
super.setChunks(dw, chunks);
}
private NBTTagCompound fetchLoadedChunkNBT(World w, int x, int z) {
CraftWorld cw = (CraftWorld) w;
NBTTagCompound nbt = null;
if (cw.isChunkLoaded(x, z)) {
Chunk c = cw.getHandle().getChunkIfLoaded(x, z);
if ((c != null) && c.o) { // c.loaded
nbt = ChunkRegionLoader.a(cw.getHandle(), c);
}
}
if (nbt != null) {
if (nbt.e("Level")) {
nbt = nbt.p("Level");
}
if (nbt != null) {
String stat = nbt.l("Status");
ChunkStatus cs = ChunkStatus.a(stat);
if ((stat == null) || (!cs.b(ChunkStatus.l))) { // ChunkStatus.LIGHT
nbt = null;
}
}
}
return nbt;
}
}

View File

@ -0,0 +1,120 @@
package org.dynmap.bukkit.helper.v118;
import org.dynmap.common.chunk.GenericBitStorage;
import org.dynmap.common.chunk.GenericNBTCompound;
import org.dynmap.common.chunk.GenericNBTList;
import java.util.Set;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.SimpleBitStorage;
public class NBT {
public static class NBTCompound implements GenericNBTCompound {
private final NBTTagCompound obj;
public NBTCompound(NBTTagCompound t) {
this.obj = t;
}
@Override
public Set<String> getAllKeys() {
return obj.d();
}
@Override
public boolean contains(String s) {
return obj.e(s);
}
@Override
public boolean contains(String s, int i) {
return obj.b(s, i);
}
@Override
public byte getByte(String s) {
return obj.d(s);
}
@Override
public short getShort(String s) {
return obj.g(s);
}
@Override
public int getInt(String s) {
return obj.h(s);
}
@Override
public long getLong(String s) {
return obj.i(s);
}
@Override
public float getFloat(String s) {
return obj.j(s);
}
@Override
public double getDouble(String s) {
return obj.k(s);
}
@Override
public String getString(String s) {
return obj.l(s);
}
@Override
public byte[] getByteArray(String s) {
return obj.m(s);
}
@Override
public int[] getIntArray(String s) {
return obj.n(s);
}
@Override
public long[] getLongArray(String s) {
return obj.o(s);
}
@Override
public GenericNBTCompound getCompound(String s) {
return new NBTCompound(obj.p(s));
}
@Override
public GenericNBTList getList(String s, int i) {
return new NBTList(obj.c(s, i));
}
@Override
public boolean getBoolean(String s) {
return obj.q(s);
}
@Override
public String getAsString(String s) {
return obj.c(s).e_();
}
@Override
public GenericBitStorage makeBitStorage(int bits, int count, long[] data) {
return new OurBitStorage(bits, count, data);
}
}
public static class NBTList implements GenericNBTList {
private final NBTTagList obj;
public NBTList(NBTTagList t) {
obj = t;
}
@Override
public int size() {
return obj.size();
}
@Override
public String getString(int idx) {
return obj.j(idx);
}
@Override
public GenericNBTCompound getCompound(int idx) {
return new NBTCompound(obj.a(idx));
}
}
public static class OurBitStorage implements GenericBitStorage {
private final SimpleBitStorage bs;
public OurBitStorage(int bits, int count, long[] data) {
bs = new SimpleBitStorage(bits, count, data);
}
@Override
public int get(int idx) {
return bs.a(idx);
}
}
}

View File

@ -1,327 +0,0 @@
package org.dynmap.fabric_1_17_1;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.util.collection.PackedIntegerArray;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.WordPackedArray;
import org.dynmap.Log;
import org.dynmap.renderer.DynmapBlockState;
import java.util.Arrays;
import java.util.LinkedList;
/**
* Represents a static, thread-safe snapshot of chunk of blocks
* Purpose is to allow clean, efficient copy of a chunk data to be made, and then handed off for processing in another thread (e.g. map rendering)
*/
public class ChunkSnapshot {
private static interface Section {
public DynmapBlockState getBlockType(int x, int y, int z);
public int getBlockSkyLight(int x, int y, int z);
public int getBlockEmittedLight(int x, int y, int z);
public boolean isEmpty();
}
private final int x, z;
private final Section[] section;
private final int sectionOffset;
private final int[] hmap; // Height map
private final int[] biome;
private final long captureFulltime;
private final int sectionCnt;
private final long inhabitedTicks;
private static final int BLOCKS_PER_SECTION = 16 * 16 * 16;
private static final int COLUMNS_PER_CHUNK = 16 * 16;
private static final byte[] emptyData = new byte[BLOCKS_PER_SECTION / 2];
private static final byte[] fullData = new byte[BLOCKS_PER_SECTION / 2];
static {
Arrays.fill(fullData, (byte) 0xFF);
}
private static class EmptySection implements Section {
@Override
public DynmapBlockState getBlockType(int x, int y, int z) {
return DynmapBlockState.AIR;
}
@Override
public int getBlockSkyLight(int x, int y, int z) {
return 15;
}
@Override
public int getBlockEmittedLight(int x, int y, int z) {
return 0;
}
@Override
public boolean isEmpty() {
return true;
}
}
private static final EmptySection empty_section = new EmptySection();
private static class StdSection implements Section {
DynmapBlockState[] states;
byte[] skylight;
byte[] emitlight;
public StdSection() {
states = new DynmapBlockState[BLOCKS_PER_SECTION];
Arrays.fill(states, DynmapBlockState.AIR);
skylight = emptyData;
emitlight = emptyData;
}
@Override
public DynmapBlockState getBlockType(int x, int y, int z) {
return states[((y & 0xF) << 8) | (z << 4) | x];
}
@Override
public int getBlockSkyLight(int x, int y, int z) {
int off = ((y & 0xF) << 7) | (z << 3) | (x >> 1);
return (skylight[off] >> (4 * (x & 1))) & 0xF;
}
@Override
public int getBlockEmittedLight(int x, int y, int z) {
int off = ((y & 0xF) << 7) | (z << 3) | (x >> 1);
return (emitlight[off] >> (4 * (x & 1))) & 0xF;
}
@Override
public boolean isEmpty() {
return false;
}
}
/**
* Construct empty chunk snapshot
*
* @param x
* @param z
*/
public ChunkSnapshot(int worldheight, int x, int z, long captime, long inhabitedTime) {
this.x = x;
this.z = z;
this.captureFulltime = captime;
this.biome = new int[COLUMNS_PER_CHUNK];
this.sectionCnt = worldheight / 16;
/* Allocate arrays indexed by section */
this.section = new Section[this.sectionCnt+1];
this.sectionOffset = 0;
/* Fill with empty data */
for (int i = 0; i <= this.sectionCnt; i++) {
this.section[i] = empty_section;
}
/* Create empty height map */
this.hmap = new int[16 * 16];
this.inhabitedTicks = inhabitedTime;
}
public static class StateListException extends Exception {
private static boolean loggedOnce = false;
public StateListException(int x, int z, int actualLength, int expectedLength, int expectedLegacyLength) {
if (Log.verbose || !loggedOnce) {
loggedOnce = true;
Log.info("Skipping chunk at x=" + x + ",z=" + z + ". Expected statelist of length " + expectedLength + " or " + expectedLegacyLength + " but got " + actualLength + ". This can happen if the chunk was not yet converted to the 1.16 format which can be fixed by visiting the chunk.");
if (!Log.verbose) {
Log.info("You will only see this message once. Turn on verbose logging in the configuration to see all messages.");
}
}
}
}
public ChunkSnapshot(NbtCompound nbt, int worldheight) throws StateListException {
this.x = nbt.getInt("xPos");
this.z = nbt.getInt("zPos");
this.captureFulltime = 0;
this.hmap = nbt.getIntArray("HeightMap");
this.sectionCnt = worldheight / 16;
if (nbt.contains("InhabitedTime")) {
this.inhabitedTicks = nbt.getLong("InhabitedTime");
} else {
this.inhabitedTicks = 0;
}
/* Allocate arrays indexed by section */
LinkedList<Section> sections = new LinkedList<Section>();
int sectoff = 0; // Default to zero
int sectcnt = 0;
/* Fill with empty data */
for (int i = 0; i <= this.sectionCnt; i++) {
sections.add(empty_section);
sectcnt++;
}
/* Get sections */
NbtList sect = nbt.getList("Sections", 10);
for (int i = 0; i < sect.size(); i++) {
NbtCompound sec = sect.getCompound(i);
int secnum = sec.getByte("Y");
// Beyond end - extend up
while (secnum >= (sectcnt - sectoff)) {
sections.addLast(empty_section); // Pad with empty
sectcnt++;
}
// Negative - see if we need to extend sectionOffset
while ((secnum + sectoff) < 0) {
sections.addFirst(empty_section); // Pad with empty
sectoff++;
sectcnt++;
}
//System.out.println("section(" + secnum + ")=" + sec.asString());
// Create normal section to initialize
StdSection cursect = new StdSection();
sections.set(secnum + sectoff, cursect);
DynmapBlockState[] states = cursect.states;
DynmapBlockState[] palette = null;
// If we've got palette and block states list, process non-empty section
if (sec.contains("Palette", 9) && sec.contains("BlockStates", 12)) {
NbtList plist = sec.getList("Palette", 10);
long[] statelist = sec.getLongArray("BlockStates");
palette = new DynmapBlockState[plist.size()];
for (int pi = 0; pi < plist.size(); pi++) {
NbtCompound tc = plist.getCompound(pi);
String pname = tc.getString("Name");
if (tc.contains("Properties")) {
StringBuilder statestr = new StringBuilder();
NbtCompound prop = tc.getCompound("Properties");
for (String pid : prop.getKeys()) {
if (statestr.length() > 0) statestr.append(',');
statestr.append(pid).append('=').append(prop.get(pid).asString());
}
palette[pi] = DynmapBlockState.getStateByNameAndState(pname, statestr.toString());
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.getBaseStateByName(pname);
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.AIR;
}
}
PackedIntegerArray db = null;
WordPackedArray dbp = null;
int bitsperblock = (statelist.length * 64) / 4096;
int expectedStatelistLength = (4096 + (64 / bitsperblock) - 1) / (64 / bitsperblock);
if (statelist.length == expectedStatelistLength) {
db = new PackedIntegerArray(bitsperblock, 4096, statelist);
} else {
int expectedLegacyStatelistLength = MathHelper.roundUpToMultiple(bitsperblock * 4096, 64) / 64;
if (statelist.length == expectedLegacyStatelistLength) {
dbp = new WordPackedArray(bitsperblock, 4096, statelist);
} else {
throw new StateListException(x, z, statelist.length, expectedStatelistLength, expectedLegacyStatelistLength);
}
}
if (bitsperblock > 8) { // Not palette
for (int j = 0; j < 4096; j++) {
int v = db != null ? db.get(j) : dbp.get(j);
states[j] = DynmapBlockState.getStateByGlobalIndex(v);
}
} else {
for (int j = 0; j < 4096; j++) {
int v = db != null ? db.get(j) : dbp.get(j);
states[j] = (v < palette.length) ? palette[v] : DynmapBlockState.AIR;
}
}
}
if (sec.contains("BlockLight")) {
cursect.emitlight = sec.getByteArray("BlockLight");
}
if (sec.contains("SkyLight")) {
cursect.skylight = sec.getByteArray("SkyLight");
}
}
/* Get biome data */
this.biome = new int[COLUMNS_PER_CHUNK];
if (nbt.contains("Biomes")) {
int[] bb = nbt.getIntArray("Biomes");
if (bb != null) {
// If v1.15+ format
if (bb.length > COLUMNS_PER_CHUNK) {
// For now, just pad the grid with the first 16
for (int i = 0; i < COLUMNS_PER_CHUNK; i++) {
int off = ((i >> 4) & 0xC) + ((i >> 2) & 0x3);
int bv = bb[off + 64]; // Offset to y=64
if (bv < 0) bv = 0;
this.biome[i] = bv;
}
} else { // Else, older chunks
for (int i = 0; i < bb.length; i++) {
int bv = bb[i];
if (bv < 0) bv = 0;
this.biome[i] = bv;
}
}
}
}
// Finalize sections array
this.section = sections.toArray(new Section[sections.size()]);
this.sectionOffset = sectoff;
}
public int getX() {
return x;
}
public int getZ() {
return z;
}
public DynmapBlockState getBlockType(int x, int y, int z)
{
int idx = (y >> 4) + sectionOffset;
if ((idx < 0) || (idx >= section.length)) return DynmapBlockState.AIR;
return section[idx].getBlockType(x, y, z);
}
public int getBlockSkyLight(int x, int y, int z)
{
int idx = (y >> 4) + sectionOffset;
if ((idx < 0) || (idx >= section.length)) return 15;
return section[idx].getBlockSkyLight(x, y, z);
}
public int getBlockEmittedLight(int x, int y, int z)
{
int idx = (y >> 4) + sectionOffset;
if ((idx < 0) || (idx >= section.length)) return 0;
return section[idx].getBlockEmittedLight(x, y, z);
}
public int getHighestBlockYAt(int x, int z) {
return hmap[z << 4 | x];
}
public int getBiome(int x, int z) {
return biome[z << 4 | x];
}
public final long getCaptureFullTime() {
return captureFulltime;
}
public boolean isSectionEmpty(int sy)
{
int idx = sy + sectionOffset;
if ((idx < 0) || (idx >= section.length)) return true;
return section[idx].isEmpty();
}
public long getInhabitedTicks() {
return inhabitedTicks;
}
}

View File

@ -39,6 +39,7 @@ import org.dynmap.common.BiomeMap;
import org.dynmap.common.DynmapCommandSender;
import org.dynmap.common.DynmapListenerManager;
import org.dynmap.common.DynmapPlayer;
import org.dynmap.common.chunk.GenericChunkCache;
import org.dynmap.fabric_1_17_1.DynmapPlugin.ChatMessage;
import org.dynmap.fabric_1_17_1.command.DmapCommand;
import org.dynmap.fabric_1_17_1.command.DmarkerCommand;
@ -66,7 +67,7 @@ public class DynmapPlugin {
DynmapCore core;
private PermissionProvider permissions;
private boolean core_enabled;
public SnapshotCache sscache;
public GenericChunkCache sscache;
public PlayerList playerList;
MapManager mapManager;
/**
@ -505,7 +506,7 @@ public class DynmapPlugin {
}
playerList = core.playerList;
sscache = new SnapshotCache(core.getSnapShotCacheSize(), core.useSoftRefInSnapShotCache());
sscache = new GenericChunkCache(core.getSnapShotCacheSize(), core.useSoftRefInSnapShotCache());
/* Get map manager from core */
mapManager = core.getMapManager();

View File

@ -0,0 +1,120 @@
package org.dynmap.fabric_1_17_1;
import org.dynmap.common.chunk.GenericBitStorage;
import org.dynmap.common.chunk.GenericNBTCompound;
import org.dynmap.common.chunk.GenericNBTList;
import java.util.Set;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.util.collection.PackedIntegerArray;
public class NBT {
public static class NBTCompound implements GenericNBTCompound {
private final NbtCompound obj;
public NBTCompound(NbtCompound t) {
this.obj = t;
}
@Override
public Set<String> getAllKeys() {
return obj.getKeys();
}
@Override
public boolean contains(String s) {
return obj.contains(s);
}
@Override
public boolean contains(String s, int i) {
return obj.contains(s, i);
}
@Override
public byte getByte(String s) {
return obj.getByte(s);
}
@Override
public short getShort(String s) {
return obj.getShort(s);
}
@Override
public int getInt(String s) {
return obj.getInt(s);
}
@Override
public long getLong(String s) {
return obj.getLong(s);
}
@Override
public float getFloat(String s) {
return obj.getFloat(s);
}
@Override
public double getDouble(String s) {
return obj.getDouble(s);
}
@Override
public String getString(String s) {
return obj.getString(s);
}
@Override
public byte[] getByteArray(String s) {
return obj.getByteArray(s);
}
@Override
public int[] getIntArray(String s) {
return obj.getIntArray(s);
}
@Override
public long[] getLongArray(String s) {
return obj.getLongArray(s);
}
@Override
public GenericNBTCompound getCompound(String s) {
return new NBTCompound(obj.getCompound(s));
}
@Override
public GenericNBTList getList(String s, int i) {
return new NBTList(obj.getList(s, i));
}
@Override
public boolean getBoolean(String s) {
return obj.getBoolean(s);
}
@Override
public String getAsString(String s) {
return obj.get(s).asString();
}
@Override
public GenericBitStorage makeBitStorage(int bits, int count, long[] data) {
return new OurBitStorage(bits, count, data);
}
}
public static class NBTList implements GenericNBTList {
private final NbtList obj;
public NBTList(NbtList t) {
obj = t;
}
@Override
public int size() {
return obj.size();
}
@Override
public String getString(int idx) {
return obj.getString(idx);
}
@Override
public GenericNBTCompound getCompound(int idx) {
return new NBTCompound(obj.getCompound(idx));
}
}
public static class OurBitStorage implements GenericBitStorage {
private final PackedIntegerArray bs;
public OurBitStorage(int bits, int count, long[] data) {
bs = new PackedIntegerArray(bits, count, data);
}
@Override
public int get(int idx) {
return bs.get(idx);
}
}
}

View File

@ -1,201 +0,0 @@
package org.dynmap.fabric_1_17_1;
import org.dynmap.utils.DynIntHashMap;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class SnapshotCache {
public static class SnapshotRec {
public ChunkSnapshot ss;
public DynIntHashMap tileData;
}
private CacheHashMap snapcache;
private ReferenceQueue<SnapshotRec> refqueue;
private long cache_attempts;
private long cache_success;
private boolean softref;
private static class CacheRec {
Reference<SnapshotRec> ref;
boolean hasbiome;
boolean hasrawbiome;
boolean hasblockdata;
boolean hashighesty;
}
@SuppressWarnings("serial")
public class CacheHashMap extends LinkedHashMap<String, CacheRec> {
private int limit;
private IdentityHashMap<Reference<SnapshotRec>, String> reverselookup;
public CacheHashMap(int lim) {
super(16, (float) 0.75, true);
limit = lim;
reverselookup = new IdentityHashMap<Reference<SnapshotRec>, String>();
}
protected boolean removeEldestEntry(Map.Entry<String, CacheRec> last) {
boolean remove = (size() >= limit);
if (remove && (last != null) && (last.getValue() != null)) {
reverselookup.remove(last.getValue().ref);
}
return remove;
}
}
/**
* Create snapshot cache
*/
public SnapshotCache(int max_size, boolean softref) {
snapcache = new CacheHashMap(max_size);
refqueue = new ReferenceQueue<SnapshotRec>();
this.softref = softref;
}
private String getKey(String w, int cx, int cz) {
return w + ":" + cx + ":" + cz;
}
/**
* Invalidate cached snapshot, if in cache
*/
public void invalidateSnapshot(String w, int x, int y, int z) {
String key = getKey(w, x >> 4, z >> 4);
synchronized (snapcache) {
CacheRec rec = snapcache.remove(key);
if (rec != null) {
snapcache.reverselookup.remove(rec.ref);
rec.ref.clear();
}
}
//processRefQueue();
}
/**
* Invalidate cached snapshot, if in cache
*/
public void invalidateSnapshot(String w, int x0, int y0, int z0, int x1, int y1, int z1) {
for (int xx = (x0 >> 4); xx <= (x1 >> 4); xx++) {
for (int zz = (z0 >> 4); zz <= (z1 >> 4); zz++) {
String key = getKey(w, xx, zz);
synchronized (snapcache) {
CacheRec rec = snapcache.remove(key);
if (rec != null) {
snapcache.reverselookup.remove(rec.ref);
rec.ref.clear();
}
}
}
}
//processRefQueue();
}
/**
* Look for chunk snapshot in cache
*/
public SnapshotRec getSnapshot(String w, int chunkx, int chunkz,
boolean blockdata, boolean biome, boolean biomeraw, boolean highesty) {
String key = getKey(w, chunkx, chunkz);
processRefQueue();
SnapshotRec ss = null;
CacheRec rec;
synchronized (snapcache) {
rec = snapcache.get(key);
if (rec != null) {
ss = rec.ref.get();
if (ss == null) {
snapcache.reverselookup.remove(rec.ref);
snapcache.remove(key);
}
}
}
if (ss != null) {
if ((blockdata && (!rec.hasblockdata)) ||
(biome && (!rec.hasbiome)) ||
(biomeraw && (!rec.hasrawbiome)) ||
(highesty && (!rec.hashighesty))) {
ss = null;
}
}
cache_attempts++;
if (ss != null) cache_success++;
return ss;
}
/**
* Add chunk snapshot to cache
*/
public void putSnapshot(String w, int chunkx, int chunkz, SnapshotRec ss,
boolean blockdata, boolean biome, boolean biomeraw, boolean highesty) {
String key = getKey(w, chunkx, chunkz);
processRefQueue();
CacheRec rec = new CacheRec();
rec.hasblockdata = blockdata;
rec.hasbiome = biome;
rec.hasrawbiome = biomeraw;
rec.hashighesty = highesty;
if (softref)
rec.ref = new SoftReference<SnapshotRec>(ss, refqueue);
else
rec.ref = new WeakReference<SnapshotRec>(ss, refqueue);
synchronized (snapcache) {
CacheRec prevrec = snapcache.put(key, rec);
if (prevrec != null) {
snapcache.reverselookup.remove(prevrec.ref);
}
snapcache.reverselookup.put(rec.ref, key);
}
}
/**
* Process reference queue
*/
private void processRefQueue() {
Reference<? extends SnapshotRec> ref;
while ((ref = refqueue.poll()) != null) {
synchronized (snapcache) {
String k = snapcache.reverselookup.remove(ref);
if (k != null) {
snapcache.remove(k);
}
}
}
}
/**
* Get hit rate (percent)
*/
public double getHitRate() {
if (cache_attempts > 0) {
return (100.0 * cache_success) / (double) cache_attempts;
}
return 0.0;
}
/**
* Reset cache stats
*/
public void resetStats() {
cache_attempts = cache_success = 0;
}
/**
* Cleanup
*/
public void cleanup() {
if (snapcache != null) {
snapcache.clear();
snapcache.reverselookup.clear();
snapcache.reverselookup = null;
snapcache = null;
}
}
}

View File

@ -84,212 +84,24 @@ public class FabricMapChunkCache extends GenericMapChunkCache {
}
}
private GenericChunk parseChunkFromNBT(NbtCompound nbt) {
private boolean isLitChunk(NbtCompound nbt) {
if ((nbt != null) && nbt.contains("Level")) {
nbt = nbt.getCompound("Level");
}
if (nbt == null) return null;
// Start generic chunk builder
GenericChunk.Builder bld = new GenericChunk.Builder(dw.minY, dw.worldheight);
int x = nbt.getInt("xPos");
int z = nbt.getInt("zPos");
bld.coords(x, z);
if (nbt.contains("InhabitedTime")) {
bld.inhabitedTicks(nbt.getLong("InhabitedTime"));
}
// Check for 2D or old 3D biome data from chunk level: need these when we build old sections
List<BiomeMap[]> old3d = null; // By section, then YZX list
BiomeMap[] old2d = null;
if (nbt.contains("Biomes")) {
int[] bb = nbt.getIntArray("Biomes");
if (bb != null) {
// If v1.15+ format
if (bb.length > 256) {
old3d = new ArrayList<BiomeMap[]>();
// Get 4 x 4 x 4 list for each section
for (int sect = 0; sect < (bb.length / 64); sect++) {
BiomeMap smap[] = new BiomeMap[64];
for (int i = 0; i < 64; i++) {
smap[i] = BiomeMap.byBiomeID(bb[sect*64 + i]);
}
old3d.add(smap);
}
}
else { // Else, older chunks
old2d = new BiomeMap[256];
for (int i = 0; i < bb.length; i++) {
old2d[i] = BiomeMap.byBiomeID(bb[i]);
}
}
nbt = nbt.getCompound("Level");
}
if (nbt != null) {
String stat = nbt.getString("Status");
ChunkStatus cs = ChunkStatus.byId(stat);
if ((stat != null) && cs.isAtLeast(ChunkStatus.LIGHT)) { // ChunkStatus.LIGHT
return true;
}
}
// Start section builder
GenericChunkSection.Builder sbld = new GenericChunkSection.Builder();
/* Get sections */
NbtList sect = nbt.contains("sections") ? nbt.getList("sections", 10) : nbt.getList("Sections", 10);
for (int i = 0; i < sect.size(); i++) {
NbtCompound sec = sect.getCompound(i);
int secnum = sec.getByte("Y");
DynmapBlockState[] palette = null;
// If we've got palette and block states list, process non-empty section
if (sec.contains("Palette", 9) && sec.contains("BlockStates", 12)) {
NbtList plist = sec.getList("Palette", 10);
long[] statelist = sec.getLongArray("BlockStates");
palette = new DynmapBlockState[plist.size()];
for (int pi = 0; pi < plist.size(); pi++) {
NbtCompound tc = plist.getCompound(pi);
String pname = tc.getString("Name");
if (tc.contains("Properties")) {
StringBuilder statestr = new StringBuilder();
NbtCompound prop = tc.getCompound("Properties");
for (String pid : prop.getKeys()) {
if (statestr.length() > 0) statestr.append(',');
statestr.append(pid).append('=').append(prop.get(pid).asString());
}
palette[pi] = DynmapBlockState.getStateByNameAndState(pname, statestr.toString());
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.getBaseStateByName(pname);
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.AIR;
}
}
PackedIntegerArray db = null;
WordPackedArray dbp = null;
int bitsperblock = (statelist.length * 64) / 4096;
int expectedStatelistLength = (4096 + (64 / bitsperblock) - 1) / (64 / bitsperblock);
if (statelist.length == expectedStatelistLength) {
db = new PackedIntegerArray(bitsperblock, 4096, statelist);
} else {
int expectedLegacyStatelistLength = MathHelper.roundUpToMultiple(bitsperblock * 4096, 64) / 64;
if (statelist.length == expectedLegacyStatelistLength) {
dbp = new WordPackedArray(bitsperblock, 4096, statelist);
} else {
bitsperblock = (statelist.length * 64) / 4096;
dbp = new WordPackedArray(bitsperblock, 4096, statelist);
}
}
if (bitsperblock > 8) { // Not palette
for (int j = 0; j < 4096; j++) {
int v = db != null ? db.get(j) : dbp.get(j);
sbld.xyzBlockState(j & 0xF, (j & 0xF00) >> 8, (j & 0xF0) >> 4, DynmapBlockState.getStateByGlobalIndex(v));
}
} else {
for (int j = 0; j < 4096; j++) {
int v = db != null ? db.get(j) : dbp.get(j);
DynmapBlockState bs = (v < palette.length) ? palette[v] : DynmapBlockState.AIR;
sbld.xyzBlockState(j & 0xF, (j & 0xF00) >> 8, (j & 0xF0) >> 4, bs);
}
}
}
else if (sec.contains("block_states")) { // 1.18
NbtCompound block_states = sec.getCompound("block_states");
// If we've block state data, process non-empty section
if (block_states.contains("data", 12)) {
long[] statelist = block_states.getLongArray("data");
NbtList plist = block_states.getList("palette", 10);
palette = new DynmapBlockState[plist.size()];
for (int pi = 0; pi < plist.size(); pi++) {
NbtCompound tc = plist.getCompound(pi);
String pname = tc.getString("Name");
if (tc.contains("Properties")) {
StringBuilder statestr = new StringBuilder();
NbtCompound prop = tc.getCompound("Properties");
for (String pid : prop.getKeys()) {
if (statestr.length() > 0) statestr.append(',');
statestr.append(pid).append('=').append(prop.get(pid).asString());
}
palette[pi] = DynmapBlockState.getStateByNameAndState(pname, statestr.toString());
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.getBaseStateByName(pname);
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.AIR;
}
}
PackedIntegerArray db = null;
WordPackedArray dbp = null;
int bitsperblock = (statelist.length * 64) / 4096;
int expectedStatelistLength = (4096 + (64 / bitsperblock) - 1) / (64 / bitsperblock);
if (statelist.length == expectedStatelistLength) {
db = new PackedIntegerArray(bitsperblock, 4096, statelist);
} else {
int expectedLegacyStatelistLength = MathHelper.roundUpToMultiple(bitsperblock * 4096, 64) / 64;
if (statelist.length == expectedLegacyStatelistLength) {
dbp = new WordPackedArray(bitsperblock, 4096, statelist);
} else {
bitsperblock = (statelist.length * 64) / 4096;
dbp = new WordPackedArray(bitsperblock, 4096, statelist);
}
}
if (bitsperblock > 8) { // Not palette
for (int j = 0; j < 4096; j++) {
int v = db != null ? db.get(j) : dbp.get(j);
sbld.xyzBlockState(j & 0xF, (j & 0xF00) >> 8, (j & 0xF0) >> 4, DynmapBlockState.getStateByGlobalIndex(v));
}
} else {
for (int j = 0; j < 4096; j++) {
int v = db != null ? db.get(j) : dbp.get(j);
DynmapBlockState bs = (v < palette.length) ? palette[v] : DynmapBlockState.AIR;
sbld.xyzBlockState(j & 0xF, (j & 0xF00) >> 8, (j & 0xF0) >> 4, bs);
}
}
}
}
if (sec.contains("BlockLight")) {
sbld.emittedLight(sec.getByteArray("BlockLight"));
}
if (sec.contains("SkyLight")) {
sbld.skyLight(sec.getByteArray("SkyLight"));
}
// If section biome palette
if (sec.contains("biomes")) {
NbtCompound nbtbiomes = sec.getCompound("biomes");
long[] bdataPacked = nbtbiomes.getLongArray("data");
NbtList bpalette = nbtbiomes.getList("palette", 8);
PackedIntegerArray bdata = null;
if (bdataPacked.length > 0)
bdata = new PackedIntegerArray(bdataPacked.length, 64, bdataPacked);
for (int j = 0; j < 64; j++) {
int b = bdata != null ? bdata.get(j) : 0;
sbld.xyzBiome(j & 0x3, (j & 0x30) >> 4, (j & 0xC) >> 2, BiomeMap.byBiomeResourceLocation(bpalette.getString(b)));
}
}
else { // Else, apply legacy biomes
if (old3d != null) {
BiomeMap m[] = old3d.get((secnum > 0) ? ((secnum < old3d.size()) ? secnum : old3d.size()-1) : 0);
if (m != null) {
for (int j = 0; j < 64; j++) {
sbld.xyzBiome(j & 0x3, (j & 0x30) >> 4, (j & 0xC) >> 2, m[j]);
}
}
}
else if (old2d != null) {
for (int j = 0; j < 256; j++) {
sbld.xzBiome(j & 0xF, (j & 0xF0) >> 4, old2d[j]);
}
}
}
// Finish and add section
bld.addSection(secnum, sbld.build());
sbld.reset();
}
return bld.build();
return false;
}
// Load generic chunk from existing and already loaded chunk
protected GenericChunk getLoadedChunk(DynmapChunk chunk) {
GenericChunk gc = null;
if (cps.isChunkLoaded(chunk.x, chunk.z)) {
DynIntHashMap tileData;
NbtCompound nbt = null;
try {
nbt = ChunkSerializer.serialize((ServerWorld) w, cps.getWorldChunk(chunk.x, chunk.z, false));
@ -297,8 +109,8 @@ public class FabricMapChunkCache extends GenericMapChunkCache {
// 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);
}
if (nbt != null) {
gc = parseChunkFromNBT(nbt);
if (isLitChunk(nbt)) {
gc = parseChunkFromNBT(new NBT.NBTCompound(nbt));
}
}
return gc;
@ -309,13 +121,8 @@ public class FabricMapChunkCache extends GenericMapChunkCache {
GenericChunk gc = null;
NbtCompound nbt = readChunk(chunk.x, chunk.z);
// If read was good
if (nbt != null) {
if ((nbt != null) && nbt.contains("Level")) {
nbt = nbt.getCompound("Level");
}
if (nbt != null) {
gc = parseChunkFromNBT(nbt);
}
if (isLitChunk(nbt)) {
gc = parseChunkFromNBT(new NBT.NBTCompound(nbt));
}
return gc;
}

View File

@ -0,0 +1,120 @@
package org.dynmap.fabric_1_18;
import org.dynmap.common.chunk.GenericBitStorage;
import org.dynmap.common.chunk.GenericNBTCompound;
import org.dynmap.common.chunk.GenericNBTList;
import java.util.Set;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.util.collection.PackedIntegerArray;
public class NBT {
public static class NBTCompound implements GenericNBTCompound {
private final NbtCompound obj;
public NBTCompound(NbtCompound t) {
this.obj = t;
}
@Override
public Set<String> getAllKeys() {
return obj.getKeys();
}
@Override
public boolean contains(String s) {
return obj.contains(s);
}
@Override
public boolean contains(String s, int i) {
return obj.contains(s, i);
}
@Override
public byte getByte(String s) {
return obj.getByte(s);
}
@Override
public short getShort(String s) {
return obj.getShort(s);
}
@Override
public int getInt(String s) {
return obj.getInt(s);
}
@Override
public long getLong(String s) {
return obj.getLong(s);
}
@Override
public float getFloat(String s) {
return obj.getFloat(s);
}
@Override
public double getDouble(String s) {
return obj.getDouble(s);
}
@Override
public String getString(String s) {
return obj.getString(s);
}
@Override
public byte[] getByteArray(String s) {
return obj.getByteArray(s);
}
@Override
public int[] getIntArray(String s) {
return obj.getIntArray(s);
}
@Override
public long[] getLongArray(String s) {
return obj.getLongArray(s);
}
@Override
public GenericNBTCompound getCompound(String s) {
return new NBTCompound(obj.getCompound(s));
}
@Override
public GenericNBTList getList(String s, int i) {
return new NBTList(obj.getList(s, i));
}
@Override
public boolean getBoolean(String s) {
return obj.getBoolean(s);
}
@Override
public String getAsString(String s) {
return obj.get(s).asString();
}
@Override
public GenericBitStorage makeBitStorage(int bits, int count, long[] data) {
return new OurBitStorage(bits, count, data);
}
}
public static class NBTList implements GenericNBTList {
private final NbtList obj;
public NBTList(NbtList t) {
obj = t;
}
@Override
public int size() {
return obj.size();
}
@Override
public String getString(int idx) {
return obj.getString(idx);
}
@Override
public GenericNBTCompound getCompound(int idx) {
return new NBTCompound(obj.getCompound(idx));
}
}
public static class OurBitStorage implements GenericBitStorage {
private final PackedIntegerArray bs;
public OurBitStorage(int bits, int count, long[] data) {
bs = new PackedIntegerArray(bits, count, data);
}
@Override
public int get(int idx) {
return bs.get(idx);
}
}
}

View File

@ -1,309 +0,0 @@
package org.dynmap.forge_1_17_1;
import java.util.Arrays;
import java.util.LinkedList;
import org.dynmap.renderer.DynmapBlockState;
import org.dynmap.utils.DataBitsPacked;
import org.dynmap.Log;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.util.BitStorage;
/**
* Represents a static, thread-safe snapshot of chunk of blocks
* Purpose is to allow clean, efficient copy of a chunk data to be made, and then handed off for processing in another thread (e.g. map rendering)
*/
public class ChunkSnapshot
{
private static interface Section {
public DynmapBlockState getBlockType(int x, int y, int z);
public int getBlockSkyLight(int x, int y, int z);
public int getBlockEmittedLight(int x, int y, int z);
public boolean isEmpty();
}
private final int x, z;
private final Section[] section; // Section, indexed by (Y/16) + sectionOffset (to handle negatives)
private final int sectionOffset; // Offset - section[N] = section for Y = N-sectionOffset
private final int[] hmap; // Height map
private final int[] biome;
private final long captureFulltime;
private final int sectionCnt;
private final long inhabitedTicks;
private static final int BLOCKS_PER_SECTION = 16 * 16 * 16;
private static final int COLUMNS_PER_CHUNK = 16 * 16;
private static final byte[] emptyData = new byte[BLOCKS_PER_SECTION / 2];
private static final byte[] fullData = new byte[BLOCKS_PER_SECTION / 2];
static
{
Arrays.fill(fullData, (byte)0xFF);
}
private static class EmptySection implements Section {
@Override
public DynmapBlockState getBlockType(int x, int y, int z) {
return DynmapBlockState.AIR;
}
@Override
public int getBlockSkyLight(int x, int y, int z) {
return 15;
}
@Override
public int getBlockEmittedLight(int x, int y, int z) {
return 0;
}
@Override
public boolean isEmpty() {
return true;
}
}
private static final EmptySection empty_section = new EmptySection();
private static class StdSection implements Section {
DynmapBlockState[] states;
byte[] skylight;
byte[] emitlight;
public StdSection() {
states = new DynmapBlockState[BLOCKS_PER_SECTION];
Arrays.fill(states, DynmapBlockState.AIR);
skylight = emptyData;
emitlight = emptyData;
}
@Override
public DynmapBlockState getBlockType(int x, int y, int z) {
return states[((y & 0xF) << 8) | (z << 4) | x];
}
@Override
public int getBlockSkyLight(int x, int y, int z) {
int off = ((y & 0xF) << 7) | (z << 3) | (x >> 1);
return (skylight[off] >> (4 * (x & 1))) & 0xF;
}
@Override
public int getBlockEmittedLight(int x, int y, int z)
{
int off = ((y & 0xF) << 7) | (z << 3) | (x >> 1);
return (emitlight[off] >> (4 * (x & 1))) & 0xF;
}
@Override
public boolean isEmpty() {
return false;
}
}
/**
* Construct empty chunk snapshot
*
* @param x
* @param z
*/
public ChunkSnapshot(int worldheight, int x, int z, long captime, long inhabitedTime)
{
this.x = x;
this.z = z;
this.captureFulltime = captime;
this.biome = new int[COLUMNS_PER_CHUNK];
this.sectionCnt = worldheight / 16;
/* Allocate arrays indexed by section */
this.section = new Section[this.sectionCnt+1];
this.sectionOffset = 0;
/* Fill with empty data */
for (int i = 0; i <= this.sectionCnt; i++) {
this.section[i] = empty_section;
}
/* Create empty height map */
this.hmap = new int[16 * 16];
this.inhabitedTicks = inhabitedTime;
}
public ChunkSnapshot(CompoundTag nbt, int worldheight) {
this.x = nbt.getInt("xPos");
this.z = nbt.getInt("zPos");
this.captureFulltime = 0;
this.hmap = nbt.getIntArray("HeightMap");
this.sectionCnt = worldheight / 16;
if (nbt.contains("InhabitedTime")) {
this.inhabitedTicks = nbt.getLong("InhabitedTime");
}
else {
this.inhabitedTicks = 0;
}
/* Allocate arrays indexed by section */
LinkedList<Section> sections = new LinkedList<Section>();
int sectoff = 0; // Default to zero
int sectcnt = 0;
/* Fill with empty data */
for (int i = 0; i <= this.sectionCnt; i++) {
sections.add(empty_section);
sectcnt++;
}
/* Get sections */
ListTag sect = nbt.getList("Sections", 10);
for (int i = 0; i < sect.size(); i++) {
CompoundTag sec = sect.getCompound(i);
int secnum = sec.getByte("Y");
// Beyond end - extend up
while (secnum >= (sectcnt - sectoff)) {
sections.addLast(empty_section); // Pad with empty
sectcnt++;
}
// Negative - see if we need to extend sectionOffset
while ((secnum + sectoff) < 0) {
sections.addFirst(empty_section); // Pad with empty
sectoff++;
sectcnt++;
}
//System.out.println("section(" + secnum + ")=" + sec.asString());
// Create normal section to initialize
StdSection cursect = new StdSection();
sections.set(secnum + sectoff, cursect);
DynmapBlockState[] states = cursect.states;
DynmapBlockState[] palette = null;
// If we've got palette and block states list, process non-empty section
if (sec.contains("Palette", 9) && sec.contains("BlockStates", 12)) {
ListTag plist = sec.getList("Palette", 10);
long[] statelist = sec.getLongArray("BlockStates");
palette = new DynmapBlockState[plist.size()];
for (int pi = 0; pi < plist.size(); pi++) {
CompoundTag tc = plist.getCompound(pi);
String pname = tc.getString("Name");
if (tc.contains("Properties")) {
StringBuilder statestr = new StringBuilder();
CompoundTag prop = tc.getCompound("Properties");
for (String pid : prop.getAllKeys()) {
if (statestr.length() > 0) statestr.append(',');
statestr.append(pid).append('=').append(prop.get(pid).getAsString());
}
palette[pi] = DynmapBlockState.getStateByNameAndState(pname, statestr.toString());
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.getBaseStateByName(pname);
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.AIR;
}
}
int recsperblock = (4096 + statelist.length - 1) / statelist.length;
int bitsperblock = 64 / recsperblock;
BitStorage db = null;
DataBitsPacked dbp = null;
try {
db = new BitStorage(bitsperblock, 4096, statelist);
} catch (Exception x) { // Handle legacy encoded
bitsperblock = (statelist.length * 64) / 4096;
dbp = new DataBitsPacked(bitsperblock, 4096, statelist);
}
if (bitsperblock > 8) { // Not palette
for (int j = 0; j < 4096; j++) {
int v = (dbp != null) ? dbp.getAt(j) : db.get(j);
states[j] = DynmapBlockState.getStateByGlobalIndex(v);
}
}
else {
for (int j = 0; j < 4096; j++) {
int v = (dbp != null) ? dbp.getAt(j) : db.get(j);
states[j] = (v < palette.length) ? palette[v] : DynmapBlockState.AIR;
}
}
}
if (sec.contains("BlockLight")) {
cursect.emitlight = sec.getByteArray("BlockLight");
}
if (sec.contains("SkyLight")) {
cursect.skylight = sec.getByteArray("SkyLight");
}
}
/* Get biome data */
this.biome = new int[COLUMNS_PER_CHUNK];
if (nbt.contains("Biomes")) {
int[] bb = nbt.getIntArray("Biomes");
if (bb != null) {
// If v1.15+ format
if (bb.length > COLUMNS_PER_CHUNK) {
// For now, just pad the grid with the first 16
for (int i = 0; i < COLUMNS_PER_CHUNK; i++) {
int off = ((i >> 4) & 0xC) + ((i >> 2) & 0x3);
int bv = bb[off + 64]; // Offset to y=64
if (bv < 0) bv = 0;
this.biome[i] = bv;
}
}
else { // Else, older chunks
for (int i = 0; i < bb.length; i++) {
int bv = bb[i];
if (bv < 0) bv = 0;
this.biome[i] = bv;
}
}
}
}
// Finalize sections array
this.section = sections.toArray(new Section[sections.size()]);
this.sectionOffset = sectoff;
}
public int getX()
{
return x;
}
public int getZ()
{
return z;
}
public DynmapBlockState getBlockType(int x, int y, int z)
{
int idx = (y >> 4) + sectionOffset;
if ((idx < 0) || (idx >= section.length)) return DynmapBlockState.AIR;
return section[idx].getBlockType(x, y, z);
}
public int getBlockSkyLight(int x, int y, int z)
{
int idx = (y >> 4) + sectionOffset;
if ((idx < 0) || (idx >= section.length)) return 15;
return section[idx].getBlockSkyLight(x, y, z);
}
public int getBlockEmittedLight(int x, int y, int z)
{
int idx = (y >> 4) + sectionOffset;
if ((idx < 0) || (idx >= section.length)) return 0;
return section[idx].getBlockEmittedLight(x, y, z);
}
public int getHighestBlockYAt(int x, int z)
{
return hmap[z << 4 | x];
}
public int getBiome(int x, int z)
{
return biome[z << 4 | x];
}
public final long getCaptureFullTime()
{
return captureFulltime;
}
public boolean isSectionEmpty(int sy)
{
int idx = sy + sectionOffset;
if ((idx < 0) || (idx >= section.length)) return true;
return section[idx].isEmpty();
}
public long getInhabitedTicks() {
return inhabitedTicks;
}
}

View File

@ -95,6 +95,7 @@ import org.dynmap.common.DynmapCommandSender;
import org.dynmap.common.DynmapPlayer;
import org.dynmap.common.DynmapServerInterface;
import org.dynmap.common.DynmapListenerManager.EventType;
import org.dynmap.common.chunk.GenericChunkCache;
import org.dynmap.forge_1_17_1.DmapCommand;
import org.dynmap.forge_1_17_1.DmarkerCommand;
import org.dynmap.forge_1_17_1.DynmapCommand;
@ -128,7 +129,7 @@ public class DynmapPlugin
private DynmapCore core;
private PermissionProvider permissions;
private boolean core_enabled;
public SnapshotCache sscache;
public GenericChunkCache sscache;
public PlayerList playerList;
private MapManager mapManager;
private static net.minecraft.server.MinecraftServer server;
@ -1527,7 +1528,7 @@ public class DynmapPlugin
}
playerList = core.playerList;
sscache = new SnapshotCache(core.getSnapShotCacheSize(), core.useSoftRefInSnapShotCache());
sscache = new GenericChunkCache(core.getSnapShotCacheSize(), core.useSoftRefInSnapShotCache());
/* Get map manager from core */
mapManager = core.getMapManager();

View File

@ -215,7 +215,7 @@ public class ForgeWorld extends DynmapWorld
public MapChunkCache getChunkCache(List<DynmapChunk> chunks)
{
if(world != null) {
ForgeMapChunkCache c = new ForgeMapChunkCache();
ForgeMapChunkCache c = new ForgeMapChunkCache(DynmapPlugin.plugin.sscache);
c.setChunks(this, chunks);
return c;
}

View File

@ -0,0 +1,120 @@
package org.dynmap.forge_1_17_1;
import org.dynmap.common.chunk.GenericBitStorage;
import org.dynmap.common.chunk.GenericNBTCompound;
import org.dynmap.common.chunk.GenericNBTList;
import java.util.Set;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.util.BitStorage;
public class NBT {
public static class NBTCompound implements GenericNBTCompound {
private final CompoundTag obj;
public NBTCompound(CompoundTag t) {
this.obj = t;
}
@Override
public Set<String> getAllKeys() {
return obj.getAllKeys();
}
@Override
public boolean contains(String s) {
return obj.contains(s);
}
@Override
public boolean contains(String s, int i) {
return obj.contains(s, i);
}
@Override
public byte getByte(String s) {
return obj.getByte(s);
}
@Override
public short getShort(String s) {
return obj.getShort(s);
}
@Override
public int getInt(String s) {
return obj.getInt(s);
}
@Override
public long getLong(String s) {
return obj.getLong(s);
}
@Override
public float getFloat(String s) {
return obj.getFloat(s);
}
@Override
public double getDouble(String s) {
return obj.getDouble(s);
}
@Override
public String getString(String s) {
return obj.getString(s);
}
@Override
public byte[] getByteArray(String s) {
return obj.getByteArray(s);
}
@Override
public int[] getIntArray(String s) {
return obj.getIntArray(s);
}
@Override
public long[] getLongArray(String s) {
return obj.getLongArray(s);
}
@Override
public GenericNBTCompound getCompound(String s) {
return new NBTCompound(obj.getCompound(s));
}
@Override
public GenericNBTList getList(String s, int i) {
return new NBTList(obj.getList(s, i));
}
@Override
public boolean getBoolean(String s) {
return obj.getBoolean(s);
}
@Override
public String getAsString(String s) {
return obj.get(s).getAsString();
}
@Override
public GenericBitStorage makeBitStorage(int bits, int count, long[] data) {
return new OurBitStorage(bits, count, data);
}
}
public static class NBTList implements GenericNBTList {
private final ListTag obj;
public NBTList(ListTag t) {
obj = t;
}
@Override
public int size() {
return obj.size();
}
@Override
public String getString(int idx) {
return obj.getString(idx);
}
@Override
public GenericNBTCompound getCompound(int idx) {
return new NBTCompound(obj.getCompound(idx));
}
}
public static class OurBitStorage implements GenericBitStorage {
private final BitStorage bs;
public OurBitStorage(int bits, int count, long[] data) {
bs = new BitStorage(bits, count, data);
}
@Override
public int get(int idx) {
return bs.get(idx);
}
}
}

View File

@ -1,191 +0,0 @@
package org.dynmap.forge_1_17_1;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.lang.ref.SoftReference;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.dynmap.utils.DynIntHashMap;
public class SnapshotCache {
public static class SnapshotRec {
public ChunkSnapshot ss;
public DynIntHashMap tileData;
};
private CacheHashMap snapcache;
private ReferenceQueue<SnapshotRec> refqueue;
private long cache_attempts;
private long cache_success;
private boolean softref;
private static class CacheRec {
Reference<SnapshotRec> ref;
boolean hasbiome;
boolean hasrawbiome;
boolean hasblockdata;
boolean hashighesty;
}
@SuppressWarnings("serial")
public class CacheHashMap extends LinkedHashMap<String, CacheRec> {
private int limit;
private IdentityHashMap<Reference<SnapshotRec>, String> reverselookup;
public CacheHashMap(int lim) {
super(16, (float)0.75, true);
limit = lim;
reverselookup = new IdentityHashMap<Reference<SnapshotRec>, String>();
}
protected boolean removeEldestEntry(Map.Entry<String, CacheRec> last) {
boolean remove = (size() >= limit);
if(remove && (last != null) && (last.getValue() != null)) {
reverselookup.remove(last.getValue().ref);
}
return remove;
}
}
/**
* Create snapshot cache
*/
public SnapshotCache(int max_size, boolean softref) {
snapcache = new CacheHashMap(max_size);
refqueue = new ReferenceQueue<SnapshotRec>();
this.softref = softref;
}
private String getKey(String w, int cx, int cz) {
return w + ":" + cx + ":" + cz;
}
/**
* Invalidate cached snapshot, if in cache
*/
public void invalidateSnapshot(String w, int x, int y, int z) {
String key = getKey(w, x>>4, z>>4);
synchronized(snapcache) {
CacheRec rec = snapcache.remove(key);
if(rec != null) {
snapcache.reverselookup.remove(rec.ref);
rec.ref.clear();
}
}
//processRefQueue();
}
/**
* Invalidate cached snapshot, if in cache
*/
public void invalidateSnapshot(String w, int x0, int y0, int z0, int x1, int y1, int z1) {
for(int xx = (x0>>4); xx <= (x1>>4); xx++) {
for(int zz = (z0>>4); zz <= (z1>>4); zz++) {
String key = getKey(w, xx, zz);
synchronized(snapcache) {
CacheRec rec = snapcache.remove(key);
if(rec != null) {
snapcache.reverselookup.remove(rec.ref);
rec.ref.clear();
}
}
}
}
//processRefQueue();
}
/**
* Look for chunk snapshot in cache
*/
public SnapshotRec getSnapshot(String w, int chunkx, int chunkz,
boolean blockdata, boolean biome, boolean biomeraw, boolean highesty) {
String key = getKey(w, chunkx, chunkz);
processRefQueue();
SnapshotRec ss = null;
CacheRec rec;
synchronized(snapcache) {
rec = snapcache.get(key);
if(rec != null) {
ss = rec.ref.get();
if(ss == null) {
snapcache.reverselookup.remove(rec.ref);
snapcache.remove(key);
}
}
}
if(ss != null) {
if((blockdata && (!rec.hasblockdata)) ||
(biome && (!rec.hasbiome)) ||
(biomeraw && (!rec.hasrawbiome)) ||
(highesty && (!rec.hashighesty))) {
ss = null;
}
}
cache_attempts++;
if(ss != null) cache_success++;
return ss;
}
/**
* Add chunk snapshot to cache
*/
public void putSnapshot(String w, int chunkx, int chunkz, SnapshotRec ss,
boolean blockdata, boolean biome, boolean biomeraw, boolean highesty) {
String key = getKey(w, chunkx, chunkz);
processRefQueue();
CacheRec rec = new CacheRec();
rec.hasblockdata = blockdata;
rec.hasbiome = biome;
rec.hasrawbiome = biomeraw;
rec.hashighesty = highesty;
if (softref)
rec.ref = new SoftReference<SnapshotRec>(ss, refqueue);
else
rec.ref = new WeakReference<SnapshotRec>(ss, refqueue);
synchronized(snapcache) {
CacheRec prevrec = snapcache.put(key, rec);
if(prevrec != null) {
snapcache.reverselookup.remove(prevrec.ref);
}
snapcache.reverselookup.put(rec.ref, key);
}
}
/**
* Process reference queue
*/
private void processRefQueue() {
Reference<? extends SnapshotRec> ref;
while((ref = refqueue.poll()) != null) {
synchronized(snapcache) {
String k = snapcache.reverselookup.remove(ref);
if(k != null) {
snapcache.remove(k);
}
}
}
}
/**
* Get hit rate (percent)
*/
public double getHitRate() {
if(cache_attempts > 0) {
return (100.0*cache_success)/(double)cache_attempts;
}
return 0.0;
}
/**
* Reset cache stats
*/
public void resetStats() {
cache_attempts = cache_success = 0;
}
/**
* Cleanup
*/
public void cleanup() {
if(snapcache != null) {
snapcache.clear();
snapcache.reverselookup.clear();
snapcache.reverselookup = null;
snapcache = null;
}
}
}

View File

@ -62,205 +62,28 @@ public class ForgeMapChunkCache extends GenericMapChunkCache {
init();
}
private GenericChunk parseChunkFromNBT(CompoundTag nbt) {
private boolean isLitChunk(CompoundTag nbt) {
if ((nbt != null) && nbt.contains("Level")) {
nbt = nbt.getCompound("Level");
}
if (nbt == null) return null;
// Start generic chunk builder
GenericChunk.Builder bld = new GenericChunk.Builder(dw.minY, dw.worldheight);
bld.coords(nbt.getInt("xPos"), nbt.getInt("zPos"));
if (nbt.contains("InhabitedTime")) {
bld.inhabitedTicks(nbt.getLong("InhabitedTime"));
}
// Check for 2D or old 3D biome data from chunk level: need these when we build old sections
List<BiomeMap[]> old3d = null; // By section, then YZX list
BiomeMap[] old2d = null;
if (nbt.contains("Biomes")) {
int[] bb = nbt.getIntArray("Biomes");
if (bb != null) {
// If v1.15+ format
if (bb.length > 256) {
old3d = new ArrayList<BiomeMap[]>();
// Get 4 x 4 x 4 list for each section
for (int sect = 0; sect < (bb.length / 64); sect++) {
BiomeMap smap[] = new BiomeMap[64];
for (int i = 0; i < 64; i++) {
smap[i] = BiomeMap.byBiomeID(bb[sect*64 + i]);
}
old3d.add(smap);
}
}
else { // Else, older chunks
old2d = new BiomeMap[256];
for (int i = 0; i < bb.length; i++) {
old2d[i] = BiomeMap.byBiomeID(bb[i]);
}
}
nbt = nbt.getCompound("Level");
}
if (nbt != null) {
String stat = nbt.getString("Status");
ChunkStatus cs = ChunkStatus.byName(stat);
if ((stat != null) && cs.isOrAfter(ChunkStatus.LIGHT)) { // ChunkStatus.LIGHT
return true;
}
}
// Start section builder
GenericChunkSection.Builder sbld = new GenericChunkSection.Builder();
/* Get sections */
ListTag sect = nbt.contains("sections") ? nbt.getList("sections", 10) : nbt.getList("Sections", 10);
for (int i = 0; i < sect.size(); i++) {
CompoundTag sec = sect.getCompound(i);
int secnum = sec.getByte("Y");
DynmapBlockState[] palette = null;
// If we've got palette and block states list, process non-empty section
if (sec.contains("Palette", 9) && sec.contains("BlockStates", 12)) {
ListTag plist = sec.getList("Palette", 10);
long[] statelist = sec.getLongArray("BlockStates");
palette = new DynmapBlockState[plist.size()];
for (int pi = 0; pi < plist.size(); pi++) {
CompoundTag tc = plist.getCompound(pi);
String pname = tc.getString("Name");
if (tc.contains("Properties")) {
StringBuilder statestr = new StringBuilder();
CompoundTag prop = tc.getCompound("Properties");
for (String pid : prop.getAllKeys()) {
if (statestr.length() > 0) statestr.append(',');
statestr.append(pid).append('=').append(prop.get(pid).getAsString());
}
palette[pi] = DynmapBlockState.getStateByNameAndState(pname, statestr.toString());
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.getBaseStateByName(pname);
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.AIR;
}
}
int recsperblock = (4096 + statelist.length - 1) / statelist.length;
int bitsperblock = 64 / recsperblock;
BitStorage db = null;
DataBitsPacked dbp = null;
try {
db = new SimpleBitStorage(bitsperblock, 4096, statelist);
} catch (Exception x) { // Handle legacy encoded
bitsperblock = (statelist.length * 64) / 4096;
dbp = new DataBitsPacked(bitsperblock, 4096, statelist);
}
if (bitsperblock > 8) { // Not palette
for (int j = 0; j < 4096; j++) {
int v = (dbp != null) ? dbp.getAt(j) : db.get(j);
sbld.xyzBlockState(j & 0xF, (j & 0xF00) >> 8, (j & 0xF0) >> 4, DynmapBlockState.getStateByGlobalIndex(v));
}
}
else {
for (int j = 0; j < 4096; j++) {
int v = (dbp != null) ? dbp.getAt(j) : db.get(j);
DynmapBlockState bs = (v < palette.length) ? palette[v] : DynmapBlockState.AIR;
sbld.xyzBlockState(j & 0xF, (j & 0xF00) >> 8, (j & 0xF0) >> 4, bs);
}
}
}
else if (sec.contains("block_states")) { // 1.18
CompoundTag block_states = sec.getCompound("block_states");
// If we've block state data, process non-empty section
if (block_states.contains("data", 12)) {
long[] statelist = block_states.getLongArray("data");
ListTag plist = block_states.getList("palette", 10);
palette = new DynmapBlockState[plist.size()];
for (int pi = 0; pi < plist.size(); pi++) {
CompoundTag tc = plist.getCompound(pi);
String pname = tc.getString("Name");
if (tc.contains("Properties")) {
StringBuilder statestr = new StringBuilder();
CompoundTag prop = tc.getCompound("Properties");
for (String pid : prop.getAllKeys()) {
if (statestr.length() > 0) statestr.append(',');
statestr.append(pid).append('=').append(prop.get(pid).getAsString());
}
palette[pi] = DynmapBlockState.getStateByNameAndState(pname, statestr.toString());
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.getBaseStateByName(pname);
}
if (palette[pi] == null) {
palette[pi] = DynmapBlockState.AIR;
}
}
SimpleBitStorage db = null;
DataBitsPacked dbp = null;
int bitsperblock = (statelist.length * 64) / 4096;
int expectedStatelistLength = (4096 + (64 / bitsperblock) - 1) / (64 / bitsperblock);
if (statelist.length == expectedStatelistLength) {
db = new SimpleBitStorage(bitsperblock, 4096, statelist);
}
else {
bitsperblock = (statelist.length * 64) / 4096;
dbp = new DataBitsPacked(bitsperblock, 4096, statelist);
}
if (bitsperblock > 8) { // Not palette
for (int j = 0; j < 4096; j++) {
int v = db != null ? db.get(j) : dbp.getAt(j);
sbld.xyzBlockState(j & 0xF, (j & 0xF00) >> 8, (j & 0xF0) >> 4, DynmapBlockState.getStateByGlobalIndex(v));
}
}
else {
for (int j = 0; j < 4096; j++) {
int v = db != null ? db.get(j) : dbp.getAt(j);
DynmapBlockState bs = (v < palette.length) ? palette[v] : DynmapBlockState.AIR;
sbld.xyzBlockState(j & 0xF, (j & 0xF00) >> 8, (j & 0xF0) >> 4, bs);
}
}
}
}
if (sec.contains("BlockLight")) {
sbld.emittedLight(sec.getByteArray("BlockLight"));
}
if (sec.contains("SkyLight")) {
sbld.skyLight(sec.getByteArray("SkyLight"));
}
// If section biome palette
if (sec.contains("biomes")) {
CompoundTag nbtbiomes = sec.getCompound("biomes");
long[] bdataPacked = nbtbiomes.getLongArray("data");
ListTag bpalette = nbtbiomes.getList("palette", 8);
SimpleBitStorage bdata = null;
if (bdataPacked.length > 0)
bdata = new SimpleBitStorage(bdataPacked.length, 64, bdataPacked);
for (int j = 0; j < 64; j++) {
int b = bdata != null ? bdata.get(j) : 0;
sbld.xyzBiome(j & 0x3, (j & 0x30) >> 4, (j & 0xC) >> 2, BiomeMap.byBiomeResourceLocation(bpalette.getString(b)));
}
}
else { // Else, apply legacy biomes
if (old3d != null) {
BiomeMap m[] = old3d.get((secnum > 0) ? ((secnum < old3d.size()) ? secnum : old3d.size()-1) : 0);
if (m != null) {
for (int j = 0; j < 64; j++) {
sbld.xyzBiome(j & 0x3, (j & 0x30) >> 4, (j & 0xC) >> 2, m[j]);
}
}
}
else if (old2d != null) {
for (int j = 0; j < 256; j++) {
sbld.xzBiome(j & 0xF, (j & 0xF0) >> 4, old2d[j]);
}
}
}
// Finish and add section
bld.addSection(secnum, sbld.build());
sbld.reset();
}
return bld.build();
return false;
}
// 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);
if ((nbt != null) && nbt.contains("Level")) {
nbt = nbt.getCompound("Level");
}
if (nbt != null) {
gc = parseChunkFromNBT(nbt);
if (isLitChunk(nbt)) {
gc = parseChunkFromNBT(new NBT.NBTCompound(nbt));
}
}
return gc;
@ -270,13 +93,8 @@ public class ForgeMapChunkCache extends GenericMapChunkCache {
GenericChunk gc = null;
CompoundTag nbt = readChunk(chunk.x, chunk.z);
// If read was good
if (nbt != null) {
if ((nbt != null) && nbt.contains("Level")) {
nbt = nbt.getCompound("Level");
}
if (nbt != null) {
gc = parseChunkFromNBT(nbt);
}
if (isLitChunk(nbt)) {
gc = parseChunkFromNBT(new NBT.NBTCompound(nbt));
}
return gc;
}

View File

@ -0,0 +1,120 @@
package org.dynmap.forge_1_18;
import org.dynmap.common.chunk.GenericBitStorage;
import org.dynmap.common.chunk.GenericNBTCompound;
import org.dynmap.common.chunk.GenericNBTList;
import java.util.Set;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.util.SimpleBitStorage;
public class NBT {
public static class NBTCompound implements GenericNBTCompound {
private final CompoundTag obj;
public NBTCompound(CompoundTag t) {
this.obj = t;
}
@Override
public Set<String> getAllKeys() {
return obj.getAllKeys();
}
@Override
public boolean contains(String s) {
return obj.contains(s);
}
@Override
public boolean contains(String s, int i) {
return obj.contains(s, i);
}
@Override
public byte getByte(String s) {
return obj.getByte(s);
}
@Override
public short getShort(String s) {
return obj.getShort(s);
}
@Override
public int getInt(String s) {
return obj.getInt(s);
}
@Override
public long getLong(String s) {
return obj.getLong(s);
}
@Override
public float getFloat(String s) {
return obj.getFloat(s);
}
@Override
public double getDouble(String s) {
return obj.getDouble(s);
}
@Override
public String getString(String s) {
return obj.getString(s);
}
@Override
public byte[] getByteArray(String s) {
return obj.getByteArray(s);
}
@Override
public int[] getIntArray(String s) {
return obj.getIntArray(s);
}
@Override
public long[] getLongArray(String s) {
return obj.getLongArray(s);
}
@Override
public GenericNBTCompound getCompound(String s) {
return new NBTCompound(obj.getCompound(s));
}
@Override
public GenericNBTList getList(String s, int i) {
return new NBTList(obj.getList(s, i));
}
@Override
public boolean getBoolean(String s) {
return obj.getBoolean(s);
}
@Override
public String getAsString(String s) {
return obj.get(s).getAsString();
}
@Override
public GenericBitStorage makeBitStorage(int bits, int count, long[] data) {
return new OurBitStorage(bits, count, data);
}
}
public static class NBTList implements GenericNBTList {
private final ListTag obj;
public NBTList(ListTag t) {
obj = t;
}
@Override
public int size() {
return obj.size();
}
@Override
public String getString(int idx) {
return obj.getString(idx);
}
@Override
public GenericNBTCompound getCompound(int idx) {
return new NBTCompound(obj.getCompound(idx));
}
}
public static class OurBitStorage implements GenericBitStorage {
private final SimpleBitStorage bs;
public OurBitStorage(int bits, int count, long[] data) {
bs = new SimpleBitStorage(bits, count, data);
}
@Override
public int get(int idx) {
return bs.get(idx);
}
}
}