Search in sources :

Example 1 with DataBitsPacked

use of org.dynmap.utils.DataBitsPacked in project dynmap by webbukkit.

the class GenericMapChunkCache method parseChunkFromNBT.

public GenericChunk parseChunkFromNBT(GenericNBTCompound orignbt) {
    GenericNBTCompound nbt = orignbt;
    if ((nbt != null) && nbt.contains("Level", GenericNBTCompound.TAG_COMPOUND)) {
        nbt = nbt.getCompound("Level");
    }
    if (nbt == null)
        return null;
    String status = nbt.getString("Status");
    int version = orignbt.getInt("DataVersion");
    boolean lit = nbt.getBoolean("isLightOn");
    boolean hasLitState = false;
    if (status != null) {
        for (int i = 0; i < litStates.length; i++) {
            if (status.equals(litStates[i])) {
                hasLitState = true;
            }
        }
    }
    // pessimistic: only has light if we see it, due to WB and other flawed chunk generation hasLitState;	// Assume good light in a lit state
    boolean hasLight = false;
    // Start generic chunk builder
    GenericChunk.Builder bld = new GenericChunk.Builder(dw.minY, dw.worldheight);
    int x = nbt.getInt("xPos");
    int z = nbt.getInt("zPos");
    // Set chunk info
    bld.coords(x, z).chunkStatus(status).dataVersion(version);
    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
    // By section, then YZX list
    List<BiomeMap[]> old3d = null;
    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);
    // And process sections
    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 ex) {
                // 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 {
                // Set palette
                sbld.xyzBlockStatePalette(palette);
                for (int j = 0; j < 4096; j++) {
                    int v = db != null ? db.get(j) : dbp.getAt(j);
                    sbld.xyzBlockStateInPalette(j & 0xF, (j & 0xF00) >> 8, (j & 0xF0) >> 4, (short) v);
                }
            }
        } else if (sec.contains("block_states", GenericNBTCompound.TAG_COMPOUND)) {
            // 1.18
            GenericNBTCompound block_states = sec.getCompound("block_states");
            // If we've got palette, process non-empty section
            if (block_states.contains("palette", GenericNBTCompound.TAG_LIST)) {
                // Handle zero bit palette (all same)
                long[] statelist = block_states.contains("data", GenericNBTCompound.TAG_LONG_ARRAY) ? block_states.getLongArray("data") : new long[4096 / 64];
                GenericNBTList plist = block_states.getList("palette", GenericNBTCompound.TAG_COMPOUND);
                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 {
                    // Set palette
                    sbld.xyzBlockStatePalette(palette);
                    for (int j = 0; j < 4096; j++) {
                        int v = db != null ? db.get(j) : dbp.getAt(j);
                        sbld.xyzBlockStateInPalette(j & 0xF, (j & 0xF00) >> 8, (j & 0xF0) >> 4, (short) v);
                    }
                }
            }
        }
        if (sec.contains("BlockLight")) {
            sbld.emittedLight(sec.getByteArray("BlockLight"));
        }
        if (sec.contains("SkyLight")) {
            sbld.skyLight(sec.getByteArray("SkyLight"));
            hasLight = true;
        }
        // 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();
    }
    // Assume skylight is only trustworthy in a lit state
    if ((!hasLitState) || (!lit)) {
        hasLight = false;
    }
    // If no light, do simple generate
    if (!hasLight) {
        // Log.info(String.format("generateSky(%d,%d)", x, z));
        bld.generateSky();
    }
    return bld.build();
}
Also used : DynmapBlockState(org.dynmap.renderer.DynmapBlockState) BiomeMap(org.dynmap.common.BiomeMap) DataBitsPacked(org.dynmap.utils.DataBitsPacked)

Aggregations

BiomeMap (org.dynmap.common.BiomeMap)1 DynmapBlockState (org.dynmap.renderer.DynmapBlockState)1 DataBitsPacked (org.dynmap.utils.DataBitsPacked)1