Skip to content

Commit 51b8d6a

Browse files
committed
Add usage examples to README
1 parent 70aa971 commit 51b8d6a

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,102 @@
22
![CI](https://github.com/pmmp/NBT/workflows/CI/badge.svg)
33

44
PHP library for working with the NBT (Named Binary Tag) data storage format, as designed by Mojang.
5+
6+
## Examples
7+
8+
The library provides two NBT serializers: `BigEndianNbtSerializer` (typically suited for Minecraft Java) and `LittleEndianNbtSerializer` (typically used by Bedrock for storage).
9+
10+
Note: Bedrock network NBT (which uses varints in some places) is not implemented here. See [BedrockProtocol](https://github.com/pmmp/BedrockProtocol) for that.
11+
12+
Note: `TAG_LongArray` is not supported because Bedrock doesn't support it, so it's not clear how NBT trees intended for serialization by Bedrock would be restricted from using it.
13+
14+
### Reading data
15+
```php
16+
use pocketmine\nbt\LittleEndianNbtSerializer;
17+
use pocketmine\nbt\NbtDataException;
18+
use pocketmine\nbt\NoSuchTagException;
19+
use pocketmine\nbt\UnexpectedTagTypeException;
20+
use pocketmine\nbt\tag\StringTag;
21+
22+
$serializer = new LittleEndianNbtSerializer();
23+
$optionalStartOffset = 0;
24+
$optionalMaxDepth = 0; //unlimited by default
25+
$treeRoot = $serializer->read($yourInputBytes, $optionalStartOffset, $optionalMaxDepth);
26+
27+
try{
28+
//If you expect a TAG_Compound root (the most common case)
29+
$data = $treeRoot->mustGetCompoundTag());
30+
}catch(NbtDataException $e){
31+
var_dump("root isn't a TAG_Compound");
32+
}
33+
//For other, less common cases where the root tag isn't a compound
34+
var_dump($treeRoot->getTag());
35+
36+
var_dump($treeRoot->getName()); //typically empty
37+
38+
try{
39+
var_dump($data->getString("hello"));
40+
}catch(UnexpectedTagTypeException $e){
41+
var_dump("not a TAG_String");
42+
}catch(NoSuchTagException $e){
43+
var_dump("no such tag called \"hello\"");
44+
}
45+
46+
try{
47+
$nestedCompound = $data->getCompoundTag("nestedCompound");
48+
}catch(UnexpectedTagTypeException $e){
49+
var_dump("not a TAG_Compound");
50+
}
51+
if($nestedCompound === null){
52+
//For legacy BC reasons, this works differently than the primitive type getters like getString() getInt() etc
53+
var_dump("no such nested tag called \"nestedCompound\"");
54+
}
55+
56+
try{
57+
$nestedList = $data->getListTag("listOfStrings", StringTag::class);
58+
}catch(UnexpectedTagTypeException $e){
59+
var_dump("not a list of strings");
60+
}
61+
if($nestedList === null){
62+
//For legacy BC reasons, getListTag() returns NULL if the tag doesn't exist
63+
var_dump("no such nested tag called \"listOfStrings\"");
64+
}
65+
var_dump($nestedList->getValue()); //StringTag[]
66+
```
67+
68+
### Writing data
69+
70+
```php
71+
use pocketmine\nbt\LittleEndianNbtSerializer;
72+
use pocketmine\nbt\TreeRoot;
73+
use pocketmine\nbt\tag\CompoundTag;
74+
use pocketmine\nbt\tag\IntTag;
75+
use pocketmine\nbt\tag\ListTag;
76+
use pocketmine\nbt\tag\StringTag;
77+
78+
$compound = CompoundTag::create()
79+
->setByte("byte", 1)
80+
->setInt("int", 2)
81+
->setTag("list", new ListTag([
82+
new StringTag("item1"),
83+
new StringTag("item2")
84+
]))
85+
->setTag("compound", CompoundTag::create()
86+
->setByte("nestedByte", 1)
87+
);
88+
89+
//empty lists infer their type from the first value added
90+
$list = new ListTag();
91+
$list->push(new StringTag("hello")); //list is now ListTag<StringTag>
92+
try{
93+
$list->push(new IntTag(1));
94+
}catch(\TypeError $e){
95+
var_dump("can't push an int into a string list");
96+
}
97+
98+
$serializer = new LittleEndianNbtSerializer();
99+
$bytes = $serializer->write($treeRoot);
100+
101+
//or if you have a Tag instance
102+
$bytes = $serializer->write(new TreeRoot($data, "optionalRootName"));
103+
```

0 commit comments

Comments
 (0)