Search in sources :

Example 11 with NBTInputStream

use of org.jnbt.NBTInputStream in project WorldPainter by Captain-Chaos.

the class JavaChunkStore method getChunk.

/**
 * Load a chunk. Returns <code>null</code> if the chunk is outside the
 * WorldPainter world boundaries.
 *
 * @param x The X coordinate in the Minecraft coordinate system of the chunk
 *     to load.
 * @param z The Z coordinate in the Minecraft coordinate system of the chunk
 *     to load.
 * @return The specified chunk, or <code>null</code> if the coordinates are
 *     outside the WorldPainter world boundaries.
 */
@Override
public Chunk getChunk(int x, int z) {
    // long start = System.currentTimeMillis();
    try {
        RegionFile regionFile = getRegionFile(new Point(x >> 5, z >> 5));
        if (regionFile == null) {
            return null;
        }
        InputStream chunkIn = regionFile.getChunkDataInputStream(x & 31, z & 31);
        if (chunkIn != null) {
            // chunksLoaded++;
            try (NBTInputStream in = new NBTInputStream(chunkIn)) {
                CompoundTag tag = (CompoundTag) in.readTag();
                // timeSpentLoading += System.currentTimeMillis() - start;
                boolean readOnly = honourReadOnlyChunks && dimension.getBitLayerValueAt(ReadOnly.INSTANCE, x << 4, z << 4);
                return platform.equals(DefaultPlugin.JAVA_MCREGION) ? new ChunkImpl(tag, maxHeight, readOnly) : new ChunkImpl2(tag, maxHeight, readOnly);
            }
        } else {
            // timeSpentLoading += System.currentTimeMillis() - start;
            return null;
        }
    } catch (IOException e) {
        throw new RuntimeException("I/O error loading chunk", e);
    }
}
Also used : NBTInputStream(org.jnbt.NBTInputStream) InputStream(java.io.InputStream) NBTInputStream(org.jnbt.NBTInputStream) IOException(java.io.IOException) CompoundTag(org.jnbt.CompoundTag)

Example 12 with NBTInputStream

use of org.jnbt.NBTInputStream in project WorldPainter by Captain-Chaos.

the class Statistics method main.

public static void main(String[] args) throws IOException {
    File levelDatFile = new File(args[0]);
    Level level = Level.load(levelDatFile);
    if ((level.getVersion() != SUPPORTED_VERSION_1) && (level.getVersion() != SUPPORTED_VERSION_2)) {
        throw new UnsupportedOperationException("Level format version " + level.getVersion() + " not supported");
    }
    int maxHeight = level.getMaxHeight();
    int maxY = maxHeight - 1;
    File worldDir = levelDatFile.getParentFile();
    File regionDir = new File(worldDir, "region");
    int version = level.getVersion();
    final Pattern regionFilePattern = (version == SUPPORTED_VERSION_1) ? Pattern.compile("r\\.-?\\d+\\.-?\\d+\\.mcr") : Pattern.compile("r\\.-?\\d+\\.-?\\d+\\.mca");
    File[] regionFiles = regionDir.listFiles((dir, name) -> {
        return regionFilePattern.matcher(name).matches();
    });
    int[][] blockTypeCounts = new int[maxHeight >> 4][4096];
    int[][] blockTypeTotals = new int[maxHeight >> 4][4096];
    // int totalBlockCount = 0, totalBlocksPerLevel = 0;
    System.out.println("Scanning " + worldDir);
    System.out.print('|');
    for (int i = 0; i < regionFiles.length - 2; i++) {
        System.out.print('-');
    }
    System.out.println('|');
    for (File file : regionFiles) {
        RegionFile regionFile = new RegionFile(file);
        try {
            for (int x = 0; x < 32; x++) {
                for (int z = 0; z < 32; z++) {
                    if (regionFile.containsChunk(x, z)) {
                        // totalBlocksPerLevel += 256;
                        // System.out.println("Chunk " + x + ", " + z);
                        // System.out.print('.');
                        Tag tag;
                        try (NBTInputStream in = new NBTInputStream(regionFile.getChunkDataInputStream(x, z))) {
                            tag = in.readTag();
                        }
                        Chunk chunk = (version == SUPPORTED_VERSION_1) ? new ChunkImpl((CompoundTag) tag, maxHeight) : new ChunkImpl2((CompoundTag) tag, maxHeight);
                        for (int xx = 0; xx < 16; xx++) {
                            for (int zz = 0; zz < 16; zz++) {
                                for (int y = maxY; y >= 0; y--) {
                                    int blockType = chunk.getBlockType(xx, y, zz);
                                    int dataValue = chunk.getDataValue(xx, y, zz);
                                    blockTypeCounts[y >> 4][(blockType << 4) | dataValue]++;
                                    blockTypeTotals[y >> 4][(blockType << 4) | dataValue]++;
                                // totalBlockCount++;
                                }
                            }
                        }
                    }
                }
            }
        // System.out.println();
        } finally {
            regionFile.close();
        }
        System.out.print('#');
    }
    System.out.println();
    System.out.println("\tGranite\tDiorite\tAndesite");
    for (int y = 0; y < maxHeight >> 4; y++) {
        int stoneLikeTotal = blockTypeTotals[y][Material.STONE.index] + blockTypeTotals[y][Material.GRANITE.index] + blockTypeTotals[y][Material.DIORITE.index] + blockTypeTotals[y][Material.ANDESITE.index];
        // System.out.println("Total stonelike blocks: " + stoneLikeTotal);
        System.out.print(y + "\t");
        System.out.printf("%6.2f‰\t", ((float) blockTypeTotals[y][Material.GRANITE.index] / stoneLikeTotal * 1000));
        System.out.printf("%6.2f‰\t", ((float) blockTypeTotals[y][Material.DIORITE.index] / stoneLikeTotal * 1000));
        System.out.printf("%6.2f‰%n", ((float) blockTypeTotals[y][Material.ANDESITE.index] / stoneLikeTotal * 1000));
    }
}
Also used : Pattern(java.util.regex.Pattern) NBTInputStream(org.jnbt.NBTInputStream) CompoundTag(org.jnbt.CompoundTag) Tag(org.jnbt.Tag) File(java.io.File) CompoundTag(org.jnbt.CompoundTag)

Example 13 with NBTInputStream

use of org.jnbt.NBTInputStream in project WorldPainter by Captain-Chaos.

the class DumpEntities method main.

public static void main(String[] args) throws IOException {
    File worldDir = new File(args[0]);
    File[] regionFiles = new File(worldDir, "region").listFiles();
    for (File file : regionFiles) {
        RegionFile regionFile = new RegionFile(file);
        try {
            for (int x = 0; x < 32; x++) {
                for (int z = 0; z < 32; z++) {
                    if (regionFile.containsChunk(x, z)) {
                        CompoundTag tag;
                        try (NBTInputStream in = new NBTInputStream(regionFile.getChunkDataInputStream(x, z))) {
                            tag = (CompoundTag) in.readTag();
                        }
                        ChunkImpl2 chunk = new ChunkImpl2(tag, 256);
                        /*&& (((Painting) entity).getTileX() == 40) && (((Painting) entity).getTileZ() == 31)*/
                        chunk.getEntities().stream().filter(entity -> (entity instanceof Painting)).forEach(System.out::println);
                    }
                }
            }
        } finally {
            regionFile.close();
        }
    }
}
Also used : NBTInputStream(org.jnbt.NBTInputStream) Painting(org.pepsoft.minecraft.Painting) CompoundTag(org.jnbt.CompoundTag) ChunkImpl2(org.pepsoft.minecraft.ChunkImpl2) IOException(java.io.IOException) File(java.io.File) RegionFile(org.pepsoft.minecraft.RegionFile) RegionFile(org.pepsoft.minecraft.RegionFile) NBTInputStream(org.jnbt.NBTInputStream) File(java.io.File) RegionFile(org.pepsoft.minecraft.RegionFile) CompoundTag(org.jnbt.CompoundTag) ChunkImpl2(org.pepsoft.minecraft.ChunkImpl2) Painting(org.pepsoft.minecraft.Painting)

Example 14 with NBTInputStream

use of org.jnbt.NBTInputStream in project WorldPainter by Captain-Chaos.

the class RegressionIT method verifyJavaDimension.

protected void verifyJavaDimension(File worldDir, Dimension dimension, int... expectedBlocks) throws IOException {
    World2 world = dimension.getWorld();
    logger.info("Verifying dimension {} of map {}", dimension.getName(), world.getName());
    boolean checkBounds;
    Rectangle expectedBounds = null;
    if (!dimension.containsOneOf(NotPresent.INSTANCE)) {
        checkBounds = true;
        int lowestTileX, highestTileX, lowestTileY, highestTileY;
        if (dimension.getWorld().getTilesToExport() != null) {
            lowestTileX = Integer.MAX_VALUE;
            highestTileX = Integer.MIN_VALUE;
            lowestTileY = Integer.MAX_VALUE;
            highestTileY = Integer.MIN_VALUE;
            for (Point tile : dimension.getWorld().getTilesToExport()) {
                if (tile.x < lowestTileX) {
                    lowestTileX = tile.x;
                }
                if (tile.x > highestTileX) {
                    highestTileX = tile.x;
                }
                if (tile.y < lowestTileY) {
                    lowestTileY = tile.y;
                }
                if (tile.y > highestTileY) {
                    highestTileY = tile.y;
                }
            }
        } else {
            lowestTileX = dimension.getLowestX();
            highestTileX = dimension.getHighestX();
            lowestTileY = dimension.getLowestY();
            highestTileY = dimension.getHighestY();
        }
        int expectedLowestChunkX = lowestTileX << 3;
        int expectedHighestChunkX = ((highestTileX + 1) << 3) - 1;
        int expectedLowestChunkZ = lowestTileY << 3;
        int expectedHighestChunkZ = ((highestTileY + 1) << 3) - 1;
        expectedBounds = new Rectangle(expectedLowestChunkX, expectedLowestChunkZ, expectedHighestChunkX - expectedLowestChunkX + 1, expectedHighestChunkZ - expectedLowestChunkZ + 1);
    } else {
        checkBounds = false;
        logger.warn("Skipping bounds check for dimension which contains the NotReady layer");
    }
    File regionDir;
    switch(dimension.getDim()) {
        case DIM_NORMAL:
            regionDir = new File(worldDir, "region");
            break;
        case DIM_NETHER:
            regionDir = new File(worldDir, "DIM-1/region");
            break;
        case DIM_END:
            regionDir = new File(worldDir, "DIM1/region");
            break;
        default:
            throw new IllegalArgumentException();
    }
    Platform platform = world.getPlatform();
    int maxHeight = dimension.getMaxHeight();
    Pattern regionFilePattern = platform.equals(DefaultPlugin.JAVA_MCREGION) ? Pattern.compile("r\\.(-?\\d+)\\.(-?\\d+)\\.mcr") : Pattern.compile("r\\.(-?\\d+)\\.(-?\\d+)\\.mca");
    int lowestChunkX = Integer.MAX_VALUE, highestChunkX = Integer.MIN_VALUE;
    int lowestChunkZ = Integer.MAX_VALUE, highestChunkZ = Integer.MIN_VALUE;
    BitSet blockTypes = new BitSet(256);
    for (File file : regionDir.listFiles()) {
        Matcher matcher = regionFilePattern.matcher(file.getName());
        if (matcher.matches()) {
            int regionX = Integer.parseInt(matcher.group(1));
            int regionZ = Integer.parseInt(matcher.group(2));
            try (RegionFile regionFile = new RegionFile(file, true)) {
                for (int chunkX = 0; chunkX < 32; chunkX++) {
                    for (int chunkZ = 0; chunkZ < 32; chunkZ++) {
                        if (regionFile.containsChunk(chunkX, chunkZ)) {
                            int absChunkX = (regionX << 5) + chunkX;
                            int absChunkZ = (regionZ << 5) + chunkZ;
                            if (absChunkX < lowestChunkX) {
                                lowestChunkX = absChunkX;
                            }
                            if (absChunkX > highestChunkX) {
                                highestChunkX = absChunkX;
                            }
                            if (absChunkZ < lowestChunkZ) {
                                lowestChunkZ = absChunkZ;
                            }
                            if (absChunkZ > highestChunkZ) {
                                highestChunkZ = absChunkZ;
                            }
                            Chunk chunk;
                            try (NBTInputStream in = new NBTInputStream(regionFile.getChunkDataInputStream(chunkX, chunkZ))) {
                                Tag tag = in.readTag();
                                chunk = platform.equals(DefaultPlugin.JAVA_MCREGION) ? new ChunkImpl((CompoundTag) tag, maxHeight, true) : new ChunkImpl2((CompoundTag) tag, maxHeight, true);
                            }
                            // all block types present
                            for (int x = 0; x < 16; x++) {
                                for (int y = 0; y < maxHeight; y++) {
                                    for (int z = 0; z < 16; z++) {
                                        blockTypes.set(chunk.getBlockType(x, y, z));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (checkBounds) {
        assertEquals(expectedBounds, new Rectangle(lowestChunkX, lowestChunkZ, highestChunkX - lowestChunkX + 1, highestChunkZ - lowestChunkZ + 1));
    }
    // types and layers used
    for (int expectedBlock : expectedBlocks) {
        assertTrue("expected block type " + Block.BLOCKS[expectedBlock].name + " missing", blockTypes.get(expectedBlock));
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) BitSet(java.util.BitSet) NBTInputStream(org.jnbt.NBTInputStream) CompoundTag(org.jnbt.CompoundTag) Tag(org.jnbt.Tag) File(java.io.File)

Aggregations

NBTInputStream (org.jnbt.NBTInputStream)14 CompoundTag (org.jnbt.CompoundTag)12 Tag (org.jnbt.Tag)8 File (java.io.File)5 IOException (java.io.IOException)4 Pattern (java.util.regex.Pattern)4 GZIPInputStream (java.util.zip.GZIPInputStream)4 InputStream (java.io.InputStream)2 NBTOutputStream (org.jnbt.NBTOutputStream)2 BufferedImage (java.awt.image.BufferedImage)1 DataInputStream (java.io.DataInputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 Matcher (java.util.regex.Matcher)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 DoubleTag (org.jnbt.DoubleTag)1 IntTag (org.jnbt.IntTag)1 ListTag (org.jnbt.ListTag)1