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