pocketmine / nbt
PHP library for working with Named Binary Tags
Installs: 511 132
Dependents: 21
Suggesters: 0
Security: 0
Stars: 40
Watchers: 11
Forks: 23
Open Issues: 14
pkg:composer/pocketmine/nbt
Requires
- php: ^7.4 || ^8.0
- php-64bit: *
- pocketmine/binaryutils: ^0.2.0
Requires (Dev)
- phpstan/extension-installer: ^1.0
- phpstan/phpstan: 2.1.27
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^9.5
- dev-stable
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.1
- 1.0.0
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.19
- 0.2.18
- 0.2.17
- 0.2.16
- 0.2.15
- 0.2.14
- 0.2.13
- 0.2.12
- 0.2.11
- 0.2.10
- 0.2.9
- 0.2.8
- 0.2.7
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.0
- 0.0.1
- dev-ext-encoding
- dev-list-tag-phpstan-generics
- dev-dependabot/github_actions/github-actions-61d968ea54
- dev-dependabot/composer/phpstan/phpstan-2.1.13
- dev-lazy-simple-tags
- dev-legacy/0.2
- dev-long-array
This package is auto-updated.
Last update: 2025-09-19 22:44:50 UTC
README
PHP library for working with the NBT (Named Binary Tag) data storage format, as designed by Mojang.
Examples
The library provides two NBT serializers: BigEndianNbtSerializer
(typically suited for Minecraft Java) and LittleEndianNbtSerializer
(typically used by Bedrock for storage).
Note: Bedrock network NBT (which uses varints in some places) is not implemented here. See BedrockProtocol for that.
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.
Reading data
use pocketmine\nbt\LittleEndianNbtSerializer; use pocketmine\nbt\NbtDataException; use pocketmine\nbt\NoSuchTagException; use pocketmine\nbt\UnexpectedTagTypeException; use pocketmine\nbt\tag\StringTag; $serializer = new LittleEndianNbtSerializer(); $optionalStartOffset = 0; $optionalMaxDepth = 0; //unlimited by default $treeRoot = $serializer->read($yourInputBytes, $optionalStartOffset, $optionalMaxDepth); try{ //If you expect a TAG_Compound root (the most common case) $data = $treeRoot->mustGetCompoundTag()); }catch(NbtDataException $e){ var_dump("root isn't a TAG_Compound"); } //For other, less common cases where the root tag isn't a compound var_dump($treeRoot->getTag()); var_dump($treeRoot->getName()); //typically empty try{ var_dump($data->getString("hello")); }catch(UnexpectedTagTypeException $e){ var_dump("not a TAG_String"); }catch(NoSuchTagException $e){ var_dump("no such tag called \"hello\""); } try{ $nestedCompound = $data->getCompoundTag("nestedCompound"); }catch(UnexpectedTagTypeException $e){ var_dump("not a TAG_Compound"); } if($nestedCompound === null){ //For legacy BC reasons, this works differently than the primitive type getters like getString() getInt() etc var_dump("no such nested tag called \"nestedCompound\""); } try{ $nestedList = $data->getListTag("listOfStrings", StringTag::class); }catch(UnexpectedTagTypeException $e){ var_dump("not a list of strings"); } if($nestedList === null){ //For legacy BC reasons, getListTag() returns NULL if the tag doesn't exist var_dump("no such nested tag called \"listOfStrings\""); } var_dump($nestedList->getValue()); //StringTag[]
Writing data
use pocketmine\nbt\LittleEndianNbtSerializer; use pocketmine\nbt\TreeRoot; use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\ListTag; use pocketmine\nbt\tag\StringTag; $compound = CompoundTag::create() ->setByte("byte", 1) ->setInt("int", 2) ->setTag("list", new ListTag([ new StringTag("item1"), new StringTag("item2") ])) ->setTag("compound", CompoundTag::create() ->setByte("nestedByte", 1) ); //empty lists infer their type from the first value added $list = new ListTag(); $list->push(new StringTag("hello")); //list is now ListTag<StringTag> try{ $list->push(new IntTag(1)); }catch(\TypeError $e){ var_dump("can't push an int into a string list"); } $serializer = new LittleEndianNbtSerializer(); $bytes = $serializer->write($treeRoot); //or if you have a Tag instance $bytes = $serializer->write(new TreeRoot($data, "optionalRootName"));