pocketmine/nbt
Composer 安装命令:
composer require pocketmine/nbt
包简介
PHP library for working with Named Binary Tags
README 文档
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"));
pocketmine/nbt 适用场景与选型建议
pocketmine/nbt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 600.35k 次下载、GitHub Stars 达 42, 最近一次更新时间为 2020 年 01 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 pocketmine/nbt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pocketmine/nbt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 600.35k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 42
- 点击次数: 20
- 依赖项目数: 21
- 推荐数: 0
其他信息
- 授权协议: LGPL-3.0
- 更新时间: 2020-01-28