Search in sources :

Example 1 with NBTInputStream

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

the class RespawnPlayer method respawnPlayer.

public static void respawnPlayer(File levelDatFile) throws IOException {
    CompoundTag outerTag;
    try (NBTInputStream in = new NBTInputStream(new GZIPInputStream(new FileInputStream(levelDatFile)))) {
        outerTag = (CompoundTag) in.readTag();
    }
    CompoundTag dataTag = (CompoundTag) outerTag.getTag(TAG_DATA);
    int spawnX = ((IntTag) dataTag.getTag(TAG_SPAWN_X)).getValue();
    int spawnY = ((IntTag) dataTag.getTag(TAG_SPAWN_Y)).getValue();
    int spawnZ = ((IntTag) dataTag.getTag(TAG_SPAWN_Z)).getValue();
    CompoundTag playerTag = (CompoundTag) dataTag.getTag(TAG_PLAYER);
    playerTag.setTag(TAG_DEATH_TIME, new ShortTag(TAG_DEATH_TIME, (short) 0));
    playerTag.setTag(TAG_HEALTH, new ShortTag(TAG_HEALTH, (short) 20));
    List<Tag> motionList = new ArrayList<>(3);
    motionList.add(new DoubleTag(null, 0));
    motionList.add(new DoubleTag(null, 0));
    motionList.add(new DoubleTag(null, 0));
    playerTag.setTag(TAG_MOTION, new ListTag(TAG_MOTION, DoubleTag.class, motionList));
    List<Tag> posList = new ArrayList<>(3);
    posList.add(new DoubleTag(null, spawnX + 0.5));
    posList.add(new DoubleTag(null, spawnY + 3));
    posList.add(new DoubleTag(null, spawnZ + 0.5));
    playerTag.setTag(TAG_POS, new ListTag(TAG_POS, DoubleTag.class, posList));
    try (NBTOutputStream out = new NBTOutputStream(new GZIPOutputStream(new FileOutputStream(levelDatFile)))) {
        out.writeTag(outerTag);
    }
}
Also used : ArrayList(java.util.ArrayList) DoubleTag(org.jnbt.DoubleTag) ListTag(org.jnbt.ListTag) NBTOutputStream(org.jnbt.NBTOutputStream) FileInputStream(java.io.FileInputStream) ShortTag(org.jnbt.ShortTag) GZIPInputStream(java.util.zip.GZIPInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) NBTInputStream(org.jnbt.NBTInputStream) IntTag(org.jnbt.IntTag) DoubleTag(org.jnbt.DoubleTag) ShortTag(org.jnbt.ShortTag) CompoundTag(org.jnbt.CompoundTag) Tag(org.jnbt.Tag) ListTag(org.jnbt.ListTag) CompoundTag(org.jnbt.CompoundTag) IntTag(org.jnbt.IntTag)

Example 2 with NBTInputStream

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

the class DumpChunk method main.

public static void main(String[] args) throws IOException {
    File levelDatFile = new File(args[0]);
    int chunkX = Integer.parseInt(args[1]);
    int chunkY = Integer.parseInt(args[2]);
    Level level = Level.load(levelDatFile);
    CompoundTag tag;
    try (NBTInputStream in = new NBTInputStream(RegionFileCache.getChunkDataInputStream(levelDatFile.getParentFile(), chunkX, chunkY, level.getVersion()))) {
        tag = (CompoundTag) in.readTag();
    }
    Chunk chunk = (level.getVersion() == SUPPORTED_VERSION_1) ? new ChunkImpl(tag, level.getMaxHeight()) : new ChunkImpl2(tag, level.getMaxHeight());
    System.out.println("Biomes");
    System.out.println("X-->");
    for (int z = 0; z < 16; z++) {
        for (int x = 0; x < 16; x++) {
            System.out.printf("[%3d]", chunk.getBiome(x, z));
        }
        if (z == 0) {
            System.out.print(" Z");
        } else if (z == 1) {
            System.out.print(" |");
        } else if (z == 2) {
            System.out.print(" v");
        }
        System.out.println();
    }
    System.out.println("Blocks:");
    List<TileEntity> tileEntities = chunk.getTileEntities();
    for (int y = 0; y < level.getMaxHeight(); y++) {
        boolean blockFound = false;
        x: for (int x = 0; x < 16; x++) {
            for (int z = 0; z < 16; z++) {
                if (chunk.getBlockType(x, y, z) != 0) {
                    blockFound = true;
                    break x;
                }
            }
        }
        if (!blockFound) {
            break;
        }
        System.out.println("X-->");
        for (int z = 0; z < 16; z++) {
            for (int x = 0; x < 16; x++) {
                int blockType = chunk.getBlockType(x, y, z);
                int data = chunk.getDataValue(x, y, z);
                if (blockType > 0) {
                    if (BLOCKS[blockType].tileEntity) {
                        int count = 0;
                        for (Iterator<TileEntity> i = tileEntities.iterator(); i.hasNext(); ) {
                            TileEntity tileEntity = i.next();
                            if ((tileEntity.getX() == x) && (tileEntity.getY() == y) && (tileEntity.getZ() == z)) {
                                count++;
                                i.remove();
                            }
                        }
                        if (count == 1) {
                            if (data > 0) {
                                System.out.printf("[%3.3s:%2d]", BLOCK_TYPE_NAMES[blockType], data);
                            } else {
                                System.out.printf("[%3.3s:  ]", BLOCK_TYPE_NAMES[blockType]);
                            }
                        } else {
                            System.out.printf("!%3.3s!%2d!", BLOCK_TYPE_NAMES[blockType], count);
                        }
                    } else {
                        if (data > 0) {
                            System.out.printf("[%3.3s:%2d]", BLOCK_TYPE_NAMES[blockType], data);
                        } else {
                            System.out.printf("[%3.3s:  ]", BLOCK_TYPE_NAMES[blockType]);
                        }
                    }
                } else {
                    System.out.print("[   :  ]");
                }
            }
            if (z == 0) {
                System.out.print(" Z");
            } else if (z == 1) {
                System.out.print(" |");
            } else if (z == 2) {
                System.out.print(" v");
            } else if (z == 15) {
                System.out.print(" Y: " + y);
            }
            System.out.println();
        }
    }
    if (!tileEntities.isEmpty()) {
        System.out.println("Unmatched tile entities!");
        for (TileEntity tileEntity : tileEntities) {
            System.out.println(tileEntity.getId() + "@" + tileEntity.getX() + "," + tileEntity.getY() + "," + tileEntity.getZ());
        }
    }
}
Also used : NBTInputStream(org.jnbt.NBTInputStream) File(java.io.File) CompoundTag(org.jnbt.CompoundTag)

Example 3 with NBTInputStream

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

the class JavaWorldMerger method copyAllChunks.

private String copyAllChunks(MinecraftWorld minecraftWorld, File oldRegionDir, Dimension dimension, Point regionCoords, ProgressReceiver progressReceiver) throws IOException, ProgressReceiver.OperationCancelled {
    if (progressReceiver != null) {
        progressReceiver.setMessage("Copying chunks unchanged");
    }
    int lowestChunkX = regionCoords.x << 5;
    int highestChunkX = (regionCoords.x << 5) + 31;
    int lowestChunkY = regionCoords.y << 5;
    int highestChunkY = (regionCoords.y << 5) + 31;
    Platform platform = dimension.getWorld().getPlatform();
    int maxHeight = dimension.getMaxHeight();
    Map<Point, RegionFile> regionFiles = new HashMap<>();
    Set<Point> damagedRegions = new HashSet<>();
    StringBuilder reportBuilder = new StringBuilder();
    try {
        int chunkNo = 0;
        for (int chunkX = lowestChunkX; chunkX <= highestChunkX; chunkX++) {
            for (int chunkY = lowestChunkY; chunkY <= highestChunkY; chunkY++) {
                chunkNo++;
                if (progressReceiver != null) {
                    progressReceiver.setProgress((float) chunkNo / 1024);
                }
                int regionX = chunkX >> 5;
                int regionY = chunkY >> 5;
                Point coords = new Point(regionX, regionY);
                if (damagedRegions.contains(coords)) {
                    // reported and logged earlier
                    continue;
                }
                RegionFile regionFile = regionFiles.get(coords);
                if (regionFile == null) {
                    File file = new File(oldRegionDir, "r." + regionX + "." + regionY + (platform.equals(DefaultPlugin.JAVA_ANVIL) ? ".mca" : ".mcr"));
                    try {
                        regionFile = new RegionFile(file);
                        regionFiles.put(coords, regionFile);
                    } catch (IOException e) {
                        reportBuilder.append("I/O error while opening region file " + file + " (message: \"" + e.getMessage() + "\"); skipping region" + EOL);
                        logger.error("I/O error while opening region file " + file + "; skipping region", e);
                        damagedRegions.add(coords);
                        continue;
                    }
                }
                int chunkXInRegion = chunkX & 0x1f;
                int chunkYInRegion = chunkY & 0x1f;
                if (regionFile.containsChunk(chunkXInRegion, chunkYInRegion)) {
                    Tag tag;
                    try {
                        InputStream chunkData = regionFile.getChunkDataInputStream(chunkXInRegion, chunkYInRegion);
                        if (chunkData == null) {
                            // This should never happen, since we checked
                            // with isChunkPresent(), but in practice it
                            // does. Perhaps corrupted data?
                            reportBuilder.append("Missing chunk data for chunk " + chunkXInRegion + ", " + chunkYInRegion + " in " + regionFile + "; skipping chunk" + EOL);
                            logger.warn("Missing chunk data for chunk " + chunkXInRegion + ", " + chunkYInRegion + " in " + regionFile + "; skipping chunk");
                            continue;
                        }
                        try (NBTInputStream in = new NBTInputStream(chunkData)) {
                            tag = in.readTag();
                        }
                    } catch (IOException e) {
                        reportBuilder.append("I/O error while reading chunk " + chunkXInRegion + ", " + chunkYInRegion + " from file " + regionFile + " (message: \"" + e.getMessage() + "\"); skipping chunk" + EOL);
                        logger.error("I/O error while reading chunk " + chunkXInRegion + ", " + chunkYInRegion + " from file " + regionFile + "; skipping chunk", e);
                        continue;
                    } catch (IllegalArgumentException e) {
                        reportBuilder.append("Illegal argument exception while reading chunk " + chunkXInRegion + ", " + chunkYInRegion + " from file " + regionFile + " (message: \"" + e.getMessage() + "\"); skipping chunk" + EOL);
                        logger.error("Illegal argument exception while reading chunk " + chunkXInRegion + ", " + chunkYInRegion + " from file " + regionFile + "; skipping chunk", e);
                        continue;
                    }
                    Chunk existingChunk = platform.equals(DefaultPlugin.JAVA_MCREGION) ? new ChunkImpl((CompoundTag) tag, maxHeight) : new ChunkImpl2((CompoundTag) tag, maxHeight);
                    minecraftWorld.addChunk(existingChunk);
                }
            }
        }
    } finally {
        for (RegionFile regionFile : regionFiles.values()) {
            regionFile.close();
        }
    }
    if (progressReceiver != null) {
        progressReceiver.setProgress(1.0f);
    }
    return reportBuilder.length() != 0 ? reportBuilder.toString() : null;
}
Also used : NBTInputStream(org.jnbt.NBTInputStream) NBTInputStream(org.jnbt.NBTInputStream) CompoundTag(org.jnbt.CompoundTag) Tag(org.jnbt.Tag) CompoundTag(org.jnbt.CompoundTag)

Example 4 with NBTInputStream

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

the class NBTFileNode method loadChildren.

@Override
protected Node[] loadChildren() {
    try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
        in.mark(2);
        Tag tag;
        if ((in.read() == 0x1f) && (in.read() == 0x8b)) {
            // Gzip signature
            in.reset();
            tag = new NBTInputStream(new GZIPInputStream(in)).readTag();
        } else {
            tag = new NBTInputStream(in).readTag();
        }
        return new Node[] { new TagNode(tag) };
    } catch (IOException e) {
        throw new RuntimeException("I/O error while reading level.dat file", e);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) NBTInputStream(org.jnbt.NBTInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) Node(org.pepsoft.worldpainter.mapexplorer.Node) NBTInputStream(org.jnbt.NBTInputStream) Tag(org.jnbt.Tag)

Example 5 with NBTInputStream

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

the class MinecraftMapTileProvider method paintTile.

@Override
public boolean paintTile(Image tileImage, int x, int y, int dx, int dy) {
    final BufferedImage image = renderBufferRef.get();
    int scale = MathUtils.pow(2, -zoom);
    final int chunkX1 = x * 8 * scale, chunkY1 = y * 8 * scale;
    final int chunkX2 = chunkX1 + 8 * scale - 1, chunkY2 = chunkY1 + 8 * scale - 1;
    int previousRegionX = Integer.MIN_VALUE, previousRegionY = Integer.MIN_VALUE;
    RegionFile previousRegion = null;
    final int step = Math.max(scale / 16, 1);
    for (int chunkX = chunkX1; chunkX <= chunkX2; chunkX += step) {
        for (int chunkY = chunkY1; chunkY <= chunkY2; chunkY += step) {
            try {
                int regionX = chunkX >> 5, regionY = chunkY >> 5;
                RegionFile region;
                if ((regionX != previousRegionX) || (regionY != previousRegionY)) {
                    region = getRegionFile(regionX, regionY);
                    previousRegion = region;
                    previousRegionX = regionX;
                    previousRegionY = regionY;
                } else {
                    region = previousRegion;
                }
                if (region == null) {
                    continue;
                }
                DataInputStream dataIn = region.getChunkDataInputStream(chunkX & 0x1f, chunkY & 0x1f);
                if (dataIn != null) {
                    Chunk chunk;
                    try (NBTInputStream in = new NBTInputStream(dataIn)) {
                        chunk = (version == Constants.SUPPORTED_VERSION_2) ? new ChunkImpl2((CompoundTag) in.readTag(), maxHeight) : new ChunkImpl((CompoundTag) in.readTag(), maxHeight);
                    }
                    for (int blockX = 0; blockX < 16; blockX += scale) {
                        for (int blockY = 0; blockY < 16; blockY += scale) {
                            image.setRGB((((chunkX - chunkX1) << 4) | blockX) / scale, (((chunkY - chunkY1) << 4) | blockY) / scale, 0xff000000 | getColour(chunk, blockX, blockY));
                        }
                    }
                } else {
                    for (int blockX = 0; blockX < 16; blockX += scale) {
                        for (int blockY = 0; blockY < 16; blockY += scale) {
                            image.setRGB((((chunkX - chunkX1) << 4) | blockX) / scale, (((chunkY - chunkY1) << 4) | blockY) / scale, 0);
                        }
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException("I/O error while reading chunk data", e);
            }
        }
    }
    Graphics2D g2 = (Graphics2D) tileImage.getGraphics();
    try {
        g2.setComposite(AlphaComposite.Src);
        g2.drawImage(image, dx, dy, null);
    } finally {
        g2.dispose();
    }
    return true;
}
Also used : NBTInputStream(org.jnbt.NBTInputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) BufferedImage(java.awt.image.BufferedImage)

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