Search in sources :

Example 1 with GlowChunkSnapshot

use of net.glowstone.chunk.GlowChunkSnapshot 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

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)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 UnknownEntityTypeException (net.glowstone.io.entity.UnknownEntityTypeException)1 CompoundTag (net.glowstone.util.nbt.CompoundTag)1 NbtOutputStream (net.glowstone.util.nbt.NbtOutputStream)1 Chunk (org.bukkit.Chunk)1 Location (org.bukkit.Location)1