/// <summary> /// Writes an NBT tag to a stream. /// </summary> /// <param name="stream">Stream to save to.</param> /// <param name="name">Name of the root tag.</param> /// <param name="compression">The compression to use.</param> /// <param name="encoding">The encoding to use for strings. Uses Java's weird MUTF8 encoding by default.</param> /// <param name="endianness">The endianness to use. PC = Big, Pocket Edition = Little.</param> /// <param name="ensureCompound">If true, throws an exception if the root tag isn't a compound.</param> public void WriteTo(Stream stream, string name, NbtCompression compression, Encoding encoding = null, Endianness endianness = Endianness.Big, bool ensureCompound = true) { if (name == null) { throw new ArgumentNullException("name"); } if (ensureCompound && (Type != TagType.Compound)) { throw new NotSupportedException("Root tag is not a compound"); } encoding = encoding ?? MUTF8Encoding.Instance; using (stream = compression.Compress(stream)) { var writer = new NBTStreamWriter(stream, encoding, endianness); writer.Write(this, name); } }