Search in sources :

Example 6 with NbtOutputStream

use of net.glowstone.util.nbt.NbtOutputStream in project Glowstone by GlowstoneMC.

the class NbtStructureDataService method writeStructuresData.

@Override
public void writeStructuresData(Map<Integer, GlowStructure> structures) {
    for (GlowStructure structure : structures.values()) {
        if (structure.isDirty()) {
            CompoundTag data;
            CompoundTag features;
            CompoundTag feature = new CompoundTag();
            CompoundTag inputRoot;
            StructureStore<GlowStructure> store = StructureStorage.saveStructure(structure, feature);
            File structureFile = new File(structureDir, store.getId() + ".dat");
            if (structureFile.exists()) {
                try (NbtInputStream in = new NbtInputStream(new FileInputStream(structureFile))) {
                    inputRoot = in.readCompound();
                    data = // NON-NLS
                    inputRoot.tryGetCompound("data").orElseGet(CompoundTag::new);
                } catch (IOException e) {
                    ConsoleMessages.Error.Structure.LOAD_FAILED.log(e, structureFile);
                    data = new CompoundTag();
                }
                features = // NON-NLS
                data.tryGetCompound("Features").orElseGet(CompoundTag::new);
            } else {
                data = new CompoundTag();
                features = new CompoundTag();
            }
            String key = "[" + structure.getChunkX() + "," + structure.getChunkZ() + "]";
            features.putCompound(key, feature);
            // NON-NLS
            data.putCompound("Features", features);
            CompoundTag root = new CompoundTag();
            // NON-NLS
            root.putCompound("data", data);
            try (NbtOutputStream nbtOut = new NbtOutputStream(new FileOutputStream(structureFile))) {
                nbtOut.writeTag(root);
            } catch (IOException e) {
                ConsoleMessages.Error.Structure.SAVE_FAILED.log(e, structureFile);
            }
            structure.setDirty(false);
        }
    }
}
Also used : GlowStructure(net.glowstone.generator.structures.GlowStructure) NbtOutputStream(net.glowstone.util.nbt.NbtOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) NbtInputStream(net.glowstone.util.nbt.NbtInputStream) CompoundTag(net.glowstone.util.nbt.CompoundTag) FileInputStream(java.io.FileInputStream)

Example 7 with NbtOutputStream

use of net.glowstone.util.nbt.NbtOutputStream in project Glowstone by GlowstoneMC.

the class AnvilChunkIoService method write.

@Override
public void write(GlowChunk chunk) throws IOException {
    int x = chunk.getX();
    int z = chunk.getZ();
    RegionFile region = cache.getRegionFile(x, z);
    int regionX = x & REGION_SIZE - 1;
    int regionZ = z & REGION_SIZE - 1;
    CompoundTag levelTags = new CompoundTag();
    // core properties
    // NON-NLS
    levelTags.putInt("xPos", chunk.getX());
    // NON-NLS
    levelTags.putInt("zPos", chunk.getZ());
    // NON-NLS
    levelTags.putLong("LastUpdate", 0);
    // NON-NLS
    levelTags.putLong("InhabitedTime", chunk.getInhabitedTime());
    // NON-NLS
    levelTags.putBool("TerrainPopulated", chunk.isPopulated());
    // chunk sections
    List<CompoundTag> sectionTags = new ArrayList<>();
    GlowChunkSnapshot snapshot = chunk.getChunkSnapshot(true, true, false);
    ChunkSection[] sections = snapshot.getRawSections();
    for (byte i = 0; i < sections.length; ++i) {
        ChunkSection sec = sections[i];
        if (sec == null) {
            continue;
        }
        CompoundTag sectionTag = new CompoundTag();
        // NON-NLS
        sectionTag.putByte("Y", i);
        sec.recount();
        sec.writeToNbt(sectionTag);
        sectionTags.add(sectionTag);
    }
    // NON-NLS
    levelTags.putCompoundList("Sections", sectionTags);
    // height map and biomes
    // NON-NLS
    levelTags.putIntArray("HeightMap", snapshot.getRawHeightmap());
    // NON-NLS
    levelTags.putByteArray("Biomes", snapshot.getRawBiomes());
    // Save Slime Chunk
    // NON-NLS
    levelTags.putByte("isSlimeChunk", snapshot.isSlimeChunk() ? 1 : 0);
    // entities
    List<CompoundTag> entities = new ArrayList<>();
    for (GlowEntity entity : chunk.getRawEntities()) {
        if (!entity.shouldSave()) {
            continue;
        }
        // passengers will be saved as part of the vehicle
        if (entity.isInsideVehicle()) {
            continue;
        }
        try {
            CompoundTag tag = new CompoundTag();
            EntityStorage.save(entity, tag);
            entities.add(tag);
        } catch (Exception e) {
            ConsoleMessages.Warn.Entity.SAVE_FAILED.log(e, entity, chunk);
        }
    }
    levelTags.putCompoundList("Entities", entities);
    // block entities
    List<CompoundTag> blockEntities = new ArrayList<>();
    for (BlockEntity entity : chunk.getRawBlockEntities()) {
        try {
            CompoundTag tag = new CompoundTag();
            entity.saveNbt(tag);
            blockEntities.add(tag);
        } catch (Exception ex) {
            ConsoleMessages.Error.BlockEntity.SAVE_FAILED.log(ex, entity.getBlock());
        }
    }
    levelTags.putCompoundList("TileEntities", blockEntities);
    List<CompoundTag> tileTicks = new ArrayList<>();
    for (Location location : chunk.getWorld().getTickMap()) {
        Chunk locationChunk = location.getChunk();
        if (locationChunk.getX() == chunk.getX() && locationChunk.getZ() == chunk.getZ()) {
            int tileX = location.getBlockX();
            int tileY = location.getBlockY();
            int tileZ = location.getBlockZ();
            String type = ItemIds.getName(location.getBlock().getType());
            CompoundTag tag = new CompoundTag();
            tag.putInt("x", tileX);
            tag.putInt("y", tileY);
            tag.putInt("z", tileZ);
            tag.putString("i", type);
            tileTicks.add(tag);
        }
    }
    levelTags.putCompoundList("TileTicks", tileTicks);
    CompoundTag levelOut = new CompoundTag();
    levelOut.putCompound("Level", levelTags);
    try (NbtOutputStream nbt = new NbtOutputStream(region.getChunkDataOutputStream(regionX, regionZ), false)) {
        nbt.writeTag(levelOut);
    }
}
Also used : NbtOutputStream(net.glowstone.util.nbt.NbtOutputStream) ArrayList(java.util.ArrayList) GlowChunk(net.glowstone.chunk.GlowChunk) Chunk(org.bukkit.Chunk) UnknownEntityTypeException(net.glowstone.io.entity.UnknownEntityTypeException) IOException(java.io.IOException) GlowEntity(net.glowstone.entity.GlowEntity) ChunkSection(net.glowstone.chunk.ChunkSection) CompoundTag(net.glowstone.util.nbt.CompoundTag) GlowChunkSnapshot(net.glowstone.chunk.GlowChunkSnapshot) BlockEntity(net.glowstone.block.entity.BlockEntity) Location(org.bukkit.Location)

Aggregations

NbtOutputStream (net.glowstone.util.nbt.NbtOutputStream)6 IOException (java.io.IOException)5 CompoundTag (net.glowstone.util.nbt.CompoundTag)5 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Location (org.bukkit.Location)2 DataOutputStream (java.io.DataOutputStream)1 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 BlockEntity (net.glowstone.block.entity.BlockEntity)1 ChunkSection (net.glowstone.chunk.ChunkSection)1 GlowChunk (net.glowstone.chunk.GlowChunk)1 GlowChunkSnapshot (net.glowstone.chunk.GlowChunkSnapshot)1 GlowEntity (net.glowstone.entity.GlowEntity)1 GlowStructure (net.glowstone.generator.structures.GlowStructure)1 UnknownEntityTypeException (net.glowstone.io.entity.UnknownEntityTypeException)1 NBTOutputStream (net.glowstone.util.nbt.NBTOutputStream)1 NbtInputStream (net.glowstone.util.nbt.NbtInputStream)1