Usage
composer require stilling/snbt-parserparse() returns native PHP values. Every integer type becomes an int, and every floating-point type becomes a float:
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]]To keep the NBT types, use parseTyped() or parseCompound(). parseTyped() returns a tag for any SNBT value. parseCompound() returns a CompoundTag, whose getters return PHP values and check the tag type:
$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"); // falseTags are immutable. with() and without() return a changed copy, and toSnbt() writes the tag with its NBT types:
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"}'UUIDs
Minecraft stores a UUID as an array of four integers. getUuid() reads that array as a UUID string. IntArrayTag::fromUuid() makes the array from a UUID string, and uuidToSnbt() writes a UUID string as SNBT:
use Stilling\SNBTParser\Tag\IntArrayTag;
$player = SNBTParser::parseCompound('{UUID: [I; 110787060, 1156138790, -1514210135, 238594805]}');
$player->getUuid("UUID");
// "069a79f4-44e9-4726-a5be-fca90e38aaf5"
$player->with("UUID", IntArrayTag::fromUuid("853c80ef-3c37-49fd-aa49-938b674adae6"))->toSnbt();
// "{UUID:[I;-2059632401,1010256381,-1438018677,1732958950]}"
SNBTParser::uuidToSnbt("069a79f4-44e9-4726-a5be-fca90e38aaf5");
// "[I;110787060,1156138790,-1514210135,238594805]"Details
- Three output formats: compact, spaced, and pretty on several lines.
- A parse error gives the byte offset, the line and the column of the problem.
- Requires PHP 8.3 or later.
data get to a running server and get the full response, also when it is longer than 4096 bytes.