Search in sources :

Example 1 with Tag

use of org.jnbt.Tag 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 Tag

use of org.jnbt.Tag 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 3 with Tag

use of org.jnbt.Tag 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 4 with Tag

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

the class AbstractNBTItem method setDoubleList.

protected final void setDoubleList(String name, double[] values) {
    List<Tag> list = new ArrayList<>(values.length);
    for (double value : values) {
        list.add(new DoubleTag(null, value));
    }
    tag.setTag(name, new ListTag(name, DoubleTag.class, list));
}
Also used : ArrayList(java.util.ArrayList) IntTag(org.jnbt.IntTag) DoubleTag(org.jnbt.DoubleTag) ByteTag(org.jnbt.ByteTag) ShortTag(org.jnbt.ShortTag) FloatTag(org.jnbt.FloatTag) CompoundTag(org.jnbt.CompoundTag) LongTag(org.jnbt.LongTag) IntArrayTag(org.jnbt.IntArrayTag) StringTag(org.jnbt.StringTag) Tag(org.jnbt.Tag) ListTag(org.jnbt.ListTag) ByteArrayTag(org.jnbt.ByteArrayTag) DoubleTag(org.jnbt.DoubleTag) ListTag(org.jnbt.ListTag)

Example 5 with Tag

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

the class AbstractNBTItem method setFloatList.

protected final void setFloatList(String name, float[] values) {
    List<Tag> list = new ArrayList<>(values.length);
    for (float value : values) {
        list.add(new FloatTag(null, value));
    }
    tag.setTag(name, new ListTag(name, FloatTag.class, list));
}
Also used : FloatTag(org.jnbt.FloatTag) ArrayList(java.util.ArrayList) IntTag(org.jnbt.IntTag) DoubleTag(org.jnbt.DoubleTag) ByteTag(org.jnbt.ByteTag) ShortTag(org.jnbt.ShortTag) FloatTag(org.jnbt.FloatTag) CompoundTag(org.jnbt.CompoundTag) LongTag(org.jnbt.LongTag) IntArrayTag(org.jnbt.IntArrayTag) StringTag(org.jnbt.StringTag) Tag(org.jnbt.Tag) ListTag(org.jnbt.ListTag) ByteArrayTag(org.jnbt.ByteArrayTag) ListTag(org.jnbt.ListTag)

Aggregations

Tag (org.jnbt.Tag)10 CompoundTag (org.jnbt.CompoundTag)9 NBTInputStream (org.jnbt.NBTInputStream)8 File (java.io.File)3 ArrayList (java.util.ArrayList)3 Pattern (java.util.regex.Pattern)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 DoubleTag (org.jnbt.DoubleTag)3 IntTag (org.jnbt.IntTag)3 ListTag (org.jnbt.ListTag)3 ShortTag (org.jnbt.ShortTag)3 ByteArrayTag (org.jnbt.ByteArrayTag)2 ByteTag (org.jnbt.ByteTag)2 FloatTag (org.jnbt.FloatTag)2 IntArrayTag (org.jnbt.IntArrayTag)2 LongTag (org.jnbt.LongTag)2 StringTag (org.jnbt.StringTag)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1