Search in sources :

Example 26 with DynmapBlockState

use of org.dynmap.renderer.DynmapBlockState in project dynmap by webbukkit.

the class BukkitVersionHelperSpigot116_3 method initializeBlockStates.

/**
 * Initialize block states (org.dynmap.blockstate.DynmapBlockState)
 */
@Override
public void initializeBlockStates() {
    dataToState = new IdentityHashMap<IBlockData, DynmapBlockState>();
    HashMap<String, DynmapBlockState> lastBlockState = new HashMap<String, DynmapBlockState>();
    int cnt = Block.REGISTRY_ID.a();
    DynmapBlockState.Builder bld = new DynmapBlockState.Builder();
    // Loop through block data states
    for (int i = 0; i < cnt; i++) {
        IBlockData bd = Block.getByCombinedId(i);
        Block b = bd.getBlock();
        String bname = IRegistry.BLOCK.getKey(bd.getBlock()).toString();
        // See if we have seen this one
        DynmapBlockState lastbs = lastBlockState.get(bname);
        int idx = 0;
        if (lastbs != null) {
            // Yes
            // Get number of states so far, since this is next
            idx = lastbs.getStateCount();
        }
        // Build state name
        String sb = "";
        String fname = bd.toString();
        int off1 = fname.indexOf('[');
        if (off1 >= 0) {
            int off2 = fname.indexOf(']');
            sb = fname.substring(off1 + 1, off2);
        }
        Material mat = bd.getMaterial();
        // getLightBlock
        int lightAtten = b.f(bd, BlockAccessAir.INSTANCE, BlockPosition.ZERO);
        // Log.info("statename=" + bname + "[" + sb + "], lightAtten=" + lightAtten);
        // Fill in base attributes
        bld.setBaseState(lastbs).setStateIndex(idx).setBlockName(bname).setStateName(sb).setMaterial(mat.toString()).setAttenuatesLight(lightAtten);
        if (mat.isSolid()) {
            bld.setSolid();
        }
        if (mat == Material.AIR) {
            bld.setAir();
        }
        if ((bd.getBlock() instanceof BlockRotatable) && (bd.getMaterial() == Material.WOOD)) {
            bld.setLog();
        }
        if (mat == Material.LEAVES) {
            bld.setLeaves();
        }
        if ((!bd.getFluid().isEmpty()) && ((bd.getBlock() instanceof BlockFluids) == false)) {
            // Test if fluid type for block is not empty
            bld.setWaterlogged();
        }
        // Build state
        DynmapBlockState dbs = bld.build();
        dataToState.put(bd, dbs);
        lastBlockState.put(bname, (lastbs == null) ? dbs : lastbs);
        Log.verboseinfo("blk=" + bname + ", idx=" + idx + ", state=" + sb + ", waterlogged=" + dbs.isWaterlogged());
    }
}
Also used : BlockRotatable(net.minecraft.server.v1_16_R2.BlockRotatable) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) DynmapBlockState(org.dynmap.renderer.DynmapBlockState) Material(net.minecraft.server.v1_16_R2.Material) BukkitMaterial(org.dynmap.bukkit.helper.BukkitMaterial) IBlockData(net.minecraft.server.v1_16_R2.IBlockData) Block(net.minecraft.server.v1_16_R2.Block) BlockFluids(net.minecraft.server.v1_16_R2.BlockFluids)

Example 27 with DynmapBlockState

use of org.dynmap.renderer.DynmapBlockState in project dynmap by webbukkit.

the class OBJExport method processExport.

/**
 * Process export
 *
 * @param sender - command sender: use for feedback messages
 * @return true if successful, false if not
 */
public boolean processExport(DynmapCommandSender sender) {
    boolean good = false;
    try {
        // Open ZIP file destination
        zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(destZipFile)));
        List<DynmapChunk> requiredChunks = new ArrayList<DynmapChunk>();
        int mincx = (minX >> 4);
        int maxcx = (maxX + 15) >> 4;
        int mincz = (minZ >> 4);
        int maxcz = (maxZ + 15) >> 4;
        boolean[] edgebits = new boolean[6];
        startExportedFile(basename + ".obj");
        // Add material library
        addStringToExportedFile("mtllib " + basename + ".mtl\n");
        // Loop through - do 8x8 chunks at a time (plus 1 border each way)
        for (int cx = mincx; cx <= maxcx; cx += 4) {
            for (int cz = mincz; cz <= maxcz; cz += 4) {
                // Build chunk cache for block of chunks
                requiredChunks.clear();
                for (int i = -1; i < 5; i++) {
                    for (int j = -1; j < 5; j++) {
                        if (((cx + i) <= maxcx) && ((cz + j) <= maxcz) && ((cx + i) >= mincx) && ((cz + j) >= mincz)) {
                            requiredChunks.add(new DynmapChunk(cx + i, cz + j));
                        }
                    }
                }
                // Get the chunk buffer
                MapChunkCache cache = core.getServer().createMapChunkCache(world, requiredChunks, true, false, true, false);
                if (cache == null) {
                    throw new IOException("Error loading chunk cache");
                }
                MapIterator iter = cache.getIterator(minX, minY, minZ);
                for (int x = cx * 16; (x < (cx * 16 + 64)) && (x <= maxX); x++) {
                    if (x < minX)
                        x = minX;
                    edgebits[BlockStep.X_PLUS.ordinal()] = (x == minX);
                    edgebits[BlockStep.X_MINUS.ordinal()] = (x == maxX);
                    for (int z = cz * 16; (z < (cz * 16 + 64)) && (z <= maxZ); z++) {
                        if (z < minZ)
                            z = minZ;
                        edgebits[BlockStep.Z_PLUS.ordinal()] = (z == minZ);
                        edgebits[BlockStep.Z_MINUS.ordinal()] = (z == maxZ);
                        iter.initialize(x, minY, z);
                        updateGroup(GROUP_CHUNK, "chunk" + (x >> 4) + "_" + (z >> 4));
                        // Do first (bottom)
                        edgebits[BlockStep.Y_MINUS.ordinal()] = true;
                        edgebits[BlockStep.Y_PLUS.ordinal()] = false;
                        DynmapBlockState blk = iter.getBlockType();
                        if (blk.isNotAir()) {
                            // Not air
                            handleBlock(blk, iter, edgebits);
                        }
                        // Do middle
                        edgebits[BlockStep.Y_MINUS.ordinal()] = false;
                        for (int y = minY + 1; y < maxY; y++) {
                            iter.setY(y);
                            blk = iter.getBlockType();
                            if (blk.isNotAir()) {
                                // Not air
                                handleBlock(blk, iter, edgebits);
                            }
                        }
                        // Do top
                        edgebits[BlockStep.Y_PLUS.ordinal()] = true;
                        iter.setY(maxY);
                        blk = iter.getBlockType();
                        if (blk.isNotAir()) {
                            // Not air
                            handleBlock(blk, iter, edgebits);
                        }
                    }
                }
                // Output faces by texture
                String grp = "";
                for (String material : facesByTexture.keySet()) {
                    List<Face> faces = facesByTexture.get(material);
                    // Record material use
                    matIDs.add(material);
                    addStringToExportedFile(String.format("usemtl %s\n", material));
                    for (Face face : faces) {
                        if ((face.groupLine != null) && (!face.groupLine.equals(grp))) {
                            grp = face.groupLine;
                            addStringToExportedFile(grp);
                        }
                        addStringToExportedFile(face.faceLine);
                    }
                }
                // Clear face table
                facesByTexture.clear();
                // Clean up vertices we've moved past
                vertices.resetSet(minX, minY, minZ, cx * 16 + 64, maxY, cz * 16 + 64);
            }
        }
        finishExportedFile();
        // If shader provided, add shader content to ZIP
        if (shader != null) {
            sender.sendMessage("Adding textures from shader " + shader.getName());
            shader.exportAsMaterialLibrary(sender, this);
            sender.sendMessage("Texture export completed");
        }
        // And close the ZIP
        zos.finish();
        zos.close();
        zos = null;
        good = true;
        sender.sendMessage("Export completed - " + destZipFile.getPath());
    } catch (IOException iox) {
        sender.sendMessage("Export failed: " + iox.getMessage());
    } finally {
        if (zos != null) {
            try {
                zos.close();
            } catch (IOException e) {
            }
            zos = null;
            destZipFile.delete();
        }
    }
    return good;
}
Also used : MapChunkCache(org.dynmap.utils.MapChunkCache) DynmapBlockState(org.dynmap.renderer.DynmapBlockState) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MapIterator(org.dynmap.utils.MapIterator) DynmapChunk(org.dynmap.DynmapChunk) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 28 with DynmapBlockState

use of org.dynmap.renderer.DynmapBlockState 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)

Example 29 with DynmapBlockState

use of org.dynmap.renderer.DynmapBlockState in project dynmap by webbukkit.

the class HDBlockStateTextureMap method remapTexture.

// Copy textures from source block ID to destination
public static void remapTexture(String dest, String src) {
    DynmapBlockState dblk = DynmapBlockState.getBaseStateByName(dest);
    DynmapBlockState sblk = DynmapBlockState.getBaseStateByName(src);
    int scnt = sblk.getStateCount();
    for (int i = 0; i < dblk.getStateCount(); i++) {
        int didx = dblk.getState(i).globalStateIndex;
        int sidx = sblk.getState(i % scnt).globalStateIndex;
        texmaps[didx] = new HDBlockStateTextureMap(texmaps[sidx], null);
    }
}
Also used : DynmapBlockState(org.dynmap.renderer.DynmapBlockState)

Example 30 with DynmapBlockState

use of org.dynmap.renderer.DynmapBlockState in project dynmap by webbukkit.

the class ForgeMapChunkCache method prepChunkSnapshot.

// Prep snapshot and add to cache
private SnapshotRec prepChunkSnapshot(DynmapChunk chunk, CompoundNBT nbt) {
    ChunkSnapshot ss = new ChunkSnapshot(nbt, dw.worldheight);
    DynIntHashMap tileData = new DynIntHashMap();
    ListNBT tiles = nbt.getList("TileEntities", 10);
    if (tiles == null)
        tiles = new ListNBT();
    /* Get tile entity data */
    List<Object> vals = new ArrayList<Object>();
    for (int tid = 0; tid < tiles.size(); tid++) {
        CompoundNBT tc = tiles.getCompound(tid);
        int tx = tc.getInt("x");
        int ty = tc.getInt("y");
        int tz = tc.getInt("z");
        int cx = tx & 0xF;
        int cz = tz & 0xF;
        DynmapBlockState blk = ss.getBlockType(cx, ty, cz);
        String[] te_fields = HDBlockModels.getTileEntityFieldsNeeded(blk);
        if (te_fields != null) {
            vals.clear();
            for (String id : te_fields) {
                INBT v = tc.get(id);
                /* Get field */
                if (v != null) {
                    Object val = getNBTValue(v);
                    if (val != null) {
                        vals.add(id);
                        vals.add(val);
                    }
                }
            }
            if (vals.size() > 0) {
                Object[] vlist = vals.toArray(new Object[vals.size()]);
                tileData.put(getIndexInChunk(cx, ty, cz), vlist);
            }
        }
    }
    SnapshotRec ssr = new SnapshotRec();
    ssr.ss = ss;
    ssr.tileData = tileData;
    DynmapPlugin.plugin.sscache.putSnapshot(dw.getName(), chunk.x, chunk.z, ssr, blockdata, biome, biomeraw, highesty);
    return ssr;
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT) DynmapBlockState(org.dynmap.renderer.DynmapBlockState) ArrayList(java.util.ArrayList) DynIntHashMap(org.dynmap.utils.DynIntHashMap) SnapshotRec(org.dynmap.forge_1_15_2.SnapshotCache.SnapshotRec) ListNBT(net.minecraft.nbt.ListNBT) INBT(net.minecraft.nbt.INBT)

Aggregations

DynmapBlockState (org.dynmap.renderer.DynmapBlockState)62 HashMap (java.util.HashMap)12 ArrayList (java.util.ArrayList)10 IdentityHashMap (java.util.IdentityHashMap)10 Block (net.minecraft.block.Block)10 GsonBuilder (com.google.gson.GsonBuilder)9 BlockState (net.minecraft.block.BlockState)9 BitSet (java.util.BitSet)7 RequiredArgumentBuilder (com.mojang.brigadier.builder.RequiredArgumentBuilder)6 File (java.io.File)6 FluidBlock (net.minecraft.block.FluidBlock)6 Material (net.minecraft.block.Material)6 Identifier (net.minecraft.util.Identifier)6 Block (net.minecraft.world.level.block.Block)6 RenderPatch (org.dynmap.renderer.RenderPatch)6 JsonParseException (com.google.gson.JsonParseException)5 IOException (java.io.IOException)5 CancellationException (java.util.concurrent.CancellationException)5 ExecutionException (java.util.concurrent.ExecutionException)5 ZipFile (java.util.zip.ZipFile)5