stilling / snbt-parser
Parse and write Minecraft SNBT (stringified NBT) in PHP, as native PHP values or typed tags.
Requires
- php: ^8.3
Requires (Dev)
- pestphp/pest: ^4.0 || ^5.0
- phpstan/phpstan: ^2.0
Suggests
- ext-intl: Needed to parse the \N{name} string escape
Provides
None
Conflicts
None
Replaces
None
README
Read Minecraft SNBT (stringified NBT) into PHP values or typed tags, change it, and write it back to SNBT. The parser reads the NBT data that commands such as data get return, and the SNBT syntax that Java Edition 1.21.5 added.
composer require stilling/snbt-parser
The package requires PHP 8.3 or later.
Quick start
Read SNBT as PHP values
use Stilling\SNBTParser\SNBTParser; SNBTParser::parse('{id: "minecraft:shield", Slot: 3b, count: 1, Pos: [1.5d, 64.0d, -2.5d]}'); // ["id" => "minecraft:shield", "Slot" => 3, "count" => 1, "Pos" => [1.5, 64.0, -2.5]]
parse() returns native PHP values. Every integer type (b, s, i, l) becomes an int, and every floating-point type (f, d) becomes a float. To keep the NBT types, use parseTyped() or parseCompound().
Read typed values
parseCompound() returns a CompoundTag. Its getters return PHP values and check the tag type:
use Stilling\SNBTParser\SNBTParser; $player = SNBTParser::parseCompound('{Health: 20.0f, OnGround: 1b, Pos: [-78.5d, 65.0d, -19.5d], abilities: {flying: 0b}}'); $player->getFloat("Health"); // 20.0 $player->getBool("OnGround"); // true $player->getList("Pos")->getFloat(1); // 65.0 $player->getCompound("abilities")->getBool("flying"); // false
Change a tag and write SNBT
Tags are immutable. with() and without() return a changed copy. toSnbt() writes the tag with its NBT types:
use Stilling\SNBTParser\SNBTParser; use Stilling\SNBTParser\Tag\ByteTag; $item = SNBTParser::parseCompound('{Slot: 3b, id: "minecraft:shield", count: 1}'); $item->with("Slot", new ByteTag(4))->without("count")->toSnbt(); // '{Slot:4b,id:"minecraft:shield"}'
Documentation
- Typed tags - tag classes, typed getters, building and changing tags, UUIDs
- Writing SNBT - output formats, and how
toSnbt()writes keys, numbers and strings - Supported syntax - the SNBT that the parser reads: numbers, strings and operations
- Errors - exceptions and error positions
See also
stilling/minecraft-rcon is a Minecraft RCON client that handles multi-packet responses. Use it to run commands such as data get ... on a server, and give the output to this parser.