Skip to content

Library made to easily serialize/deserialize NBT into text/byte

License

Notifications You must be signed in to change notification settings

MinesharpMC/Minesharp.Nbt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minesharp.Nbt

Using static methods

public static void Main(string[] args)
{
    var tag = new CompoundTag
    {
        ["MyShort"] = new ShortTag(456),
        ["MyInt"] = new IntTag(4),
        ["MyDouble"] = new DoubleTag(57.5),
        ["MyCompound"] = new CompoundTag
        {
            ["MyByte"] = new ByteTag(1),
            ["MyLong"] = new LongTag(48747),
            ["MyFloat"] = new FloatTag(49.84f)
        },
        ["MyList"] = new ListTag
        {
            new StringTag("Named"),
            new StringTag("Binary"),
            new StringTag("Tag")
        },
        ["MyByteArray"] = new ByteArrayTag { 1, 2, 3 },
        ["MyIntArray"] = new IntArrayTag { 1, 2, 3 },
        ["MyLongArray"] = new LongArrayTag { 1, 2, 3 }
    };

    var compressedBytes = NamedBinaryTagConverter.GetBytes(tag, true);
    var compressed = NamedBinaryTagConverter.FromBytes(compressedBytes, true);
    
    var uncompressedBytes = NamedBinaryTagConverter.GetBytes(tag);
    var uncompressed = NamedBinaryTagConverter.FromBytes(uncompressedBytes);
    
    var serialized = NamedBinaryTagConverter.GetString(tag);
}

Using streams

public static void Main(string[] args)
{
    var tag = new CompoundTag
    {
        ["MyShort"] = new ShortTag(456),
        ["MyInt"] = new IntTag(4),
        ["MyDouble"] = new DoubleTag(57.5),
        ["MyCompound"] = new CompoundTag
        {
            ["MyByte"] = new ByteTag(1),
            ["MyLong"] = new LongTag(48747),
            ["MyFloat"] = new FloatTag(49.84f)
        },
        ["MyList"] = new ListTag
        {
            new StringTag("Named"),
            new StringTag("Binary"),
            new StringTag("Tag")
        },
        ["MyByteArray"] = new ByteArrayTag { 1, 2, 3 },
        ["MyIntArray"] = new IntArrayTag { 1, 2, 3 },
        ["MyLongArray"] = new LongArrayTag { 1, 2, 3 }
    };

    // Without compression
    using (var file = File.Create("example.nbt"))
    using (var writer = new TagWriter(file))
    {
        writer.Write(tag);
    }
    
    // With compression
    using (var file = File.Create("example.nbt"))
    using (var writer = new CompressedTagWriter(file))
    {
        writer.Write(tag);
    }

    // Without compression
    using (var file = File.OpenRead("example.nbt"))
    using (var reader = new TagReader(file))
    {
        tag = reader.ReadTag();
    }
    
    // With compression
    using (var file = File.OpenRead("example.nbt"))
    using (var reader = new CompressedTagReader(file))
    {
        tag = reader.ReadTag();
    }
}

About

Library made to easily serialize/deserialize NBT into text/byte

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages