"Sputnik" help  
Sputnik Help
NbtCompoundWrite(<nbtCompound>, [<byteBuffer>])

Description

Writes an NBT compound to a binary string or optionally writes it to a ByteBuffer class.

Parameters

<nbtCompound> The NBT compound to write to a binary string or ByteBuffer.
<byteBuffer> Optional ByteBuffer class instance to which the NBT compound is written. If not provided, the NBT compound is written to a binary string.

Return Value

Success: Returns a binary string representation of the NBT compound if no ByteBuffer instance is provided. If a ByteBuffer instance is provided, the function returns true upon successful write to the ByteBuffer.
Failure: Returns null if the provided nbtCompound parameter is not a valid NBT compound or if there is an error during the write operation.

Remarks

The NbtCompoundWrite function allows you to write an NBT compound to a binary string or to a ByteBuffer class. It verifies that the nbtCompound parameter is a valid NBT compound and either writes the compound to a binary string or directly to a ByteBuffer instance if provided.

If a ByteBuffer instance is provided, the NBT compound is written to the ByteBuffer using its write methods. If no ByteBuffer instance is provided, the function internally creates a ByteBuffer and writes the NBT compound to it.

Related

Example

Example of usage:

my $nbt = NbtNew(@NbtCompound);
NbtCompoundSet($nbt, "Cat", "Meow");
NbtCompoundSet($nbt, "Dog", "Woof");

my $data = NbtCompoundWrite($nbt);
say BinaryExpand($data);

my $nbtRead = NbtCompoundRead($data);
say $nbtRead;
foreach(my $k => my $v in $nbtRead)
    say $k . " -> " . $v;

// PRINTS
// 00 | 4E 42 54 21 0A 00 00 00 03 43 61 74 00 00 00 04 NBT!.....Cat....
// 01 | 4D 65 6F 77 0A 00 00 00 03 44 6F 67 00 00 00 04 Meow.....Dog....
// 02 | 57 6F 6F 66 00 -- -- -- -- -- -- -- -- -- -- -- Woof.
// {NBTTag;Compound::[Cat:Meow,Dog:Woof]}
// Cat -> Meow
// Dog -> Woof

Example of using a ByteBuffer class:

my $nbt1 = NbtNew(@NbtCompound);
NbtCompoundSet($nbt1, "Cat1", "Meow1");
NbtCompoundSet($nbt1, "Dog1", "Woof1");

my $nbt2 = NbtNew(@NbtCompound);
NbtCompoundSet($nbt2, "Cat2", "Meow2");
NbtCompoundSet($nbt2, "Dog2", "Woof2");

my $buf = new ByteBuffer();

NbtCompoundWrite($nbt1, $buf);
NbtCompoundWrite($nbt2, $buf);

$buf->Rewind();

my $nbtRead1 = NbtCompoundRead($buf);
my $nbtRead2 = NbtCompoundRead($buf);

say $nbtRead1;
foreach(my $k => my $v in $nbtRead1)
    say $k . " -> " . $v;
say $nbtRead2;
foreach(my $k => my $v in $nbtRead2)
    say $k . " -> " . $v;

// PRINTS
// {NBTTag;Compound::[Cat1:Meow1,Dog1:Woof1]}
// Cat1 -> Meow1
// Dog1 -> Woof1
// {NBTTag;Compound::[Cat2:Meow2,Dog2:Woof2]}
// Cat2 -> Meow2
// Dog2 -> Woof2

 


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org