cartograph/minecraft-nbt
Composer 安装命令:
composer require cartograph/minecraft-nbt
包简介
Read, write, and manipulate Minecraft's NBT (Named Binary Tag) format; both binary and SNBT.
README 文档
README
Minecraft NBT
A pure-PHP library for reading, writing, and manipulating Minecraft's NBT (Named Binary Tag) format, in both binary (file and network) form and SNBT.
Features
- Binary NBT: read and write the file format (named root) and the network format (unnamed root, Minecraft 1.20.2+)
- Compression: auto-detects GZip and Zlib on read; choose any of GZip, Zlib, or none on write
- SNBT: parse and render stringified NBT, with optional pretty-printing and configurable quote style
- 1.21.5 SNBT extensions: typed-array prefixes (
[B;...],[I;...],[L;...]), signed/unsigned suffixes (42ub,100sl, ...), hex (0xFF) and binary (0b1010) literals, underscore digit separators, trailing commas - All 13 tag types: Byte, Short, Int, Long, Float, Double, ByteArray, String, List, Compound, IntArray, LongArray, plus End for empty lists
- Type-safe construction: element-type validation on List and array tags, range-checked numeric tags
- PHPStan level max: full generic annotations on
Tag<TValue>,CompoundTag,ListTag, and theNbtfacade
Requirements
- PHP 8.5 or newer
- ext-zlib (compiled in by default on most distributions)
Installation
Install via Composer:
composer require cartograph/minecraft-nbt
Quick start
Build a tag tree and write it to a file:
use Cartograph\NBT\Nbt; $tag = Nbt::compound([ 'name' => Nbt::string('Bananrama'), 'level' => Nbt::int(42), ]); Nbt::writeFile($tag, 'player.nbt');
That's a complete, runnable example, with no fixtures and no setup. The output is a GZip-compressed binary NBT file with a TAG_Compound root containing one string and one int child.
To read it back:
$tag = Nbt::readFile('player.nbt'); echo $tag->get('name')->value(); // "Bananrama" echo $tag->get('level')->value(); // 42
Concepts
NBT is a tree of typed tags. Every tree has a CompoundTag at its root,
which is an ordered map of named children. Children can be other compounds
(nesting), ListTags (homogeneous, ordered sequences of tags), or any of
the leaf types: Byte, Short, Int, Long, Float, Double,
String, ByteArray, IntArray, or LongArray.
The same tree can be serialised two ways:
- Binary NBT, the compact wire format used by Minecraft itself, with optional GZip or Zlib compression
- SNBT, a human-readable text representation, similar to JSON with
type suffixes (
42b,1.5f,[L;1L,2L,3L])
This library reads, writes, parses, and renders both.
Usage
Read a binary file
readFile auto-detects GZip and Zlib compression from the file's leading
bytes; uncompressed files work too.
use Cartograph\NBT\Nbt; $level = Nbt::readFile('level.dat'); echo $level->get('Data')->get('LevelName')->value();
Write a binary file
GZip is the default. Pass a different Compression to override.
use Cartograph\NBT\Binary\Compression; use Cartograph\NBT\Nbt; Nbt::writeFile($tag, 'data.nbt'); // GZip Nbt::writeFile($tag, 'data.zlib.nbt', '', Compression::Zlib); Nbt::writeFile($tag, 'data.raw.nbt', '', Compression::None);
The third argument is the root tag's name (most files use an empty string).
Parse SNBT
parseSnbt accepts the full SNBT grammar including 1.21.5 extensions:
typed-array prefixes, signed/unsigned suffixes, hex/binary literals,
underscore digit separators, and trailing commas.
use Cartograph\NBT\Nbt; $tag = Nbt::parseSnbt('{name: "Bananrama", level: 42b, scores: [1, 2, 3]}'); echo $tag->get('name')->value(); // "Bananrama" echo $tag->get('level')->value(); // 42 (ByteTag) echo $tag->get('scores')->get(1)->value(); // 2 (IntTag)
Render SNBT
Two modes via SnbtOptions: compact (default) and pretty.
use Cartograph\NBT\Nbt; use Cartograph\NBT\Snbt\SnbtOptions; echo Nbt::renderSnbt($tag); // {name:Bananrama,level:42b,scores:[1,2,3,],} echo Nbt::renderSnbt($tag, new SnbtOptions(prettyPrint: true)); // { // name:Bananrama, // level:42b, // scores:[ // 1, // 2, // 3, // ], // }
SnbtOptions also lets you force-quote keys (quoteKeys: true), pick a
preferred quote style (quoteStyle: QuoteStyle::Single), and choose the
indentation unit.
Network format (Minecraft 1.20.2+)
The network protocol uses an unnamed root compound. Nbt::readNetworkBinary
and Nbt::writeNetworkBinary handle it.
use Cartograph\NBT\Nbt; $bytes = Nbt::writeNetworkBinary($tag); $tag = Nbt::readNetworkBinary($bytes);
Class overview
All tag classes live in the Cartograph\NBT\Tags namespace.
| Class | Holds | Notes |
|---|---|---|
ByteTag |
int |
-128 to 127 |
ShortTag |
int |
-32 768 to 32 767 |
IntTag |
int |
signed 32-bit |
LongTag |
int |
signed 64-bit (full PHP int range) |
FloatTag |
float |
IEEE 754 single-precision |
DoubleTag |
float |
IEEE 754 double-precision |
StringTag |
string |
UTF-8, max 65 535 bytes |
ByteArrayTag |
int[] |
each element in ByteTag range |
IntArrayTag |
int[] |
each element in IntTag range |
LongArrayTag |
int[] |
(PHP int always fits) |
CompoundTag |
array<string, Tag<*>> |
ordered map of named children |
ListTag |
list<Tag<*>> |
homogeneous; element type declared at construction |
The Nbt facade has a factory method for each (Nbt::byte(),
Nbt::compound(), ...) that returns the corresponding tag instance.
Compatibility
This library targets the Java Edition NBT format. All 13 tag types (including TAG_End for empty lists) are supported.
- File format: fully supported, all Minecraft Java versions
- Network format: Minecraft Java 1.20.2 and newer (unnamed root compound)
- Compression: GZip, Zlib, and uncompressed
- SNBT: classic syntax plus the 1.21.5 grammar extensions
Bedrock Edition's NBT variant (little-endian, varint-prefixed) is not supported.
Contributing
Bug reports, feature requests, and pull requests are welcome at
github.com/cartographgg/minecraft-nbt.
See CONTRIBUTING.md for development setup and the
required checks (tests, static analysis, code style, and mutation
testing). Each check has a Composer script: composer test,
composer static, composer style, and composer mutation.
License
Released under the MIT License. © Cartograph contributors.
Maintained as part of Cartograph, a Minecraft server directory and monitoring platform. The library is self-contained and has no Cartograph-specific behaviour; use it anywhere you need to work with NBT data in PHP.
cartograph/minecraft-nbt 适用场景与选型建议
cartograph/minecraft-nbt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 251 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「parser」 「minecraft」 「NBT」 「codec」 「snbt」 「named-binary-tag」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cartograph/minecraft-nbt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cartograph/minecraft-nbt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cartograph/minecraft-nbt 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
An MT940 bank statement parser for PHP
Laravel integration for the jsonapi.org parser
Allow users to use the head of a Minecraft skin as avatar.
Parser/Writer for the NBT file format
A modern HTML DOM parser for PHP using DOMDocument and Symfony CssSelector.
统计信息
- 总下载量: 251
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 44
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-05-07
