Fix handling of JEIDS, shift to 1.13 style state for chunk ss

This commit is contained in:
Mike Primm 2018-09-06 17:56:21 -05:00
parent 77c4d50d29
commit 032ab5bcba
5 changed files with 320 additions and 336 deletions

View file

@ -13,8 +13,7 @@ import net.minecraft.nbt.NBTTagList;
public class ChunkSnapshot
{
private final int x, z;
private final short[][] blockids; /* Block IDs, by section */
private final byte[][] blockdata;
private final int[][] blockidx; /* Block state index, by section */
private final byte[][] skylight;
private final byte[][] emitlight;
private final boolean[] empty;
@ -26,7 +25,7 @@ public class ChunkSnapshot
private static final int BLOCKS_PER_SECTION = 16 * 16 * 16;
private static final int COLUMNS_PER_CHUNK = 16 * 16;
private static final short[] emptyIDs = new short[BLOCKS_PER_SECTION];
private static final int[] emptyIdx = new int[BLOCKS_PER_SECTION];
private static final byte[] emptyData = new byte[BLOCKS_PER_SECTION / 2];
private static final byte[] fullData = new byte[BLOCKS_PER_SECTION / 2];
@ -52,8 +51,7 @@ public class ChunkSnapshot
this.biome = new int[COLUMNS_PER_CHUNK];
this.sectionCnt = worldheight / 16;
/* Allocate arrays indexed by section */
this.blockids = new short[this.sectionCnt][];
this.blockdata = new byte[this.sectionCnt][];
this.blockidx = new int[this.sectionCnt][];
this.skylight = new byte[this.sectionCnt][];
this.emitlight = new byte[this.sectionCnt][];
this.empty = new boolean[this.sectionCnt];
@ -62,8 +60,7 @@ public class ChunkSnapshot
for (int i = 0; i < this.sectionCnt; i++)
{
this.empty[i] = true;
this.blockids[i] = emptyIDs;
this.blockdata[i] = emptyData;
this.blockidx[i] = emptyIdx;
this.emitlight[i] = emptyData;
this.skylight[i] = fullData;
}
@ -87,16 +84,14 @@ public class ChunkSnapshot
this.inhabitedTicks = 0;
}
/* Allocate arrays indexed by section */
this.blockids = new short[this.sectionCnt][];
this.blockdata = new byte[this.sectionCnt][];
this.blockidx = new int[this.sectionCnt][];
this.skylight = new byte[this.sectionCnt][];
this.emitlight = new byte[this.sectionCnt][];
this.empty = new boolean[this.sectionCnt];
/* Fill with empty data */
for (int i = 0; i < this.sectionCnt; i++) {
this.empty[i] = true;
this.blockids[i] = emptyIDs;
this.blockdata[i] = emptyData;
this.blockidx[i] = emptyIdx;
this.emitlight[i] = emptyData;
this.skylight[i] = fullData;
}
@ -109,18 +104,28 @@ public class ChunkSnapshot
Log.info("Section " + (int) secnum + " above world height " + worldheight);
continue;
}
short[] blkids = new short[BLOCKS_PER_SECTION];
this.blockids[secnum] = blkids;
int[] blkidxs = new int[BLOCKS_PER_SECTION];
this.blockidx[secnum] = blkidxs;
// JEI format
if (sec.hasKey("Palette", 11)) {
byte[] bd = new byte[BLOCKS_PER_SECTION / 2];
int[] p = sec.getIntArray("Palette");
this.blockdata[secnum] = bd;
int len = BLOCKS_PER_SECTION;
if(len > p.length) len = p.length;
for(int j = 0; j < len; j++) {
blkids[j] = (short)((p[j] >> 4) & 0xFFFF);
bd[j / 2] |= (p[j] & 0xF) << (4 * (j&1));
// Palette is list of state values, where Blocks=bit 11-4 of index, Data=bit 3-0
byte[] msb_bytes = sec.getByteArray("Blocks");
int mlen = msb_bytes.length / 2;
byte[] lsb_bytes = sec.getByteArray("Data");
int llen = BLOCKS_PER_SECTION / 2;
if (llen > lsb_bytes.length) llen = lsb_bytes.length;
for(int j = 0; j < llen; j++) {
int idx = lsb_bytes[j] & 0xF;
int idx2 = (lsb_bytes[j] & 0xF0) >>> 4;
if (j < mlen) {
idx += (255 & msb_bytes[2*j]) << 4;
idx2 += (255 & msb_bytes[2*j+1]) << 4;
}
// Get even block id
blkidxs[2*j] = (idx < p.length) ? p[idx] : 0;
// Get odd block id
blkidxs[2*j+1] = (idx2 < p.length) ? p[idx2] : 0;
}
}
else {
@ -128,7 +133,7 @@ public class ChunkSnapshot
int len = BLOCKS_PER_SECTION;
if(len > lsb_bytes.length) len = lsb_bytes.length;
for(int j = 0; j < len; j++) {
blkids[j] = (short)(0xFF & lsb_bytes[j]);
blkidxs[j] = (0xFF & lsb_bytes[j]) << 4;
}
if (sec.hasKey("Add", 7)) { /* If additional data, add it */
byte[] msb = sec.getByteArray("Add");
@ -139,8 +144,8 @@ public class ChunkSnapshot
if (b == 0) {
continue;
}
blkids[j << 1] |= (b & 0x0F) << 8;
blkids[(j << 1) + 1] |= (b & 0xF0) << 4;
blkidxs[j << 1] |= (b & 0x0F) << 12;
blkidxs[(j << 1) + 1] |= (b & 0xF0) << 8;
}
}
if (sec.hasKey("Add2", 7)) { /* If additional data (NEID), add it */
@ -152,11 +157,19 @@ public class ChunkSnapshot
if (b == 0) {
continue;
}
blkids[j << 1] |= (b & 0x0F) << 12;
blkids[(j << 1) + 1] |= (b & 0xF0) << 8;
blkidxs[j << 1] |= (b & 0x0F) << 16;
blkidxs[(j << 1) + 1] |= (b & 0xF0) << 12;
}
}
this.blockdata[secnum] = sec.getByteArray("Data");
byte[] bd = sec.getByteArray("Data");
for (int j = 0; j < bd.length; j++) {
int b = bd[j] & 0xFF;
if (b == 0) {
continue;
}
blkidxs[j << 1] |= b & 0x0F;
blkidxs[(j << 1) + 1] |= (b & 0xF0) >> 4;
}
}
this.emitlight[secnum] = sec.getByteArray("BlockLight");
if (sec.hasKey("SkyLight")) {
@ -198,21 +211,18 @@ public class ChunkSnapshot
public int getBlockTypeId(int x, int y, int z)
{
return blockids[y >> 4][((y & 0xF) << 8) | (z << 4) | x];
return blockidx[y >> 4][((y & 0xF) << 8) | (z << 4) | x] >> 4;
}
public int getBlockData(int x, int y, int z)
{
int off = ((y & 0xF) << 7) | (z << 3) | (x >> 1);
return (blockdata[y >> 4][off] >> ((x & 1) << 2)) & 0xF;
return blockidx[y >> 4][((y & 0xF) << 8) | (z << 4) | x] & 0xF;
}
public DynmapBlockState getBlockType(int x, int y, int z)
{
int id = blockids[y >> 4][((y & 0xF) << 8) | (z << 4) | x];
int off = ((y & 0xF) << 7) | (z << 3) | (x >> 1);
int dat = (blockdata[y >> 4][off] >> ((x & 1) << 2)) & 0xF;
return DynmapPlugin.stateByID[(id << 4) + dat];
int id = blockidx[y >> 4][((y & 0xF) << 8) | (z << 4) | x];
return DynmapPlugin.stateByID[id];
}
public int getBlockSkyLight(int x, int y, int z)