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);
}
}
Aggregations