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);
}