A full PHP implementation of Minecraft's Named Binary Tag (NBT) format.

Related tags

Files php-nbt
Overview

php-nbt

A full PHP implementation of Minecraft's Named Binary Tag (NBT) format.

In contrast to other implementations, this library provides full support for 64-bit types, including the relatively new TAG_Long_Array.

Additionally, all three flavors (Java Edition, Bedrock Edition/little endian, and Bedrock Edition/VarInt) of the NBT format are supported.

Installation

composer require aternos/nbt

Usage

Reading NBT data

To read existing NBT data, a Reader object is required. This library implements different readers to read NBT data from strings.

//Read uncompressed NBT data
$reader = new \Aternos\Nbt\IO\Reader\StringReader("...nbtData...", \Aternos\Nbt\NbtFormat::BEDROCK_EDITION);

//Read gzip compressed NBT data
$gzipReader = new \Aternos\Nbt\IO\Reader\GZipCompressedStringReader("...compressedNbtData...", \Aternos\Nbt\NbtFormat::JAVA_EDITION);

//Read zlib compressed NBT data
$zlibReader = new \Aternos\Nbt\IO\Reader\ZLibCompressedStringReader("...compressedNbtData...", \Aternos\Nbt\NbtFormat::BEDROCK_EDITION_NETWORK);

Note that the reader object is also used to specify the NBT format flavor. Available are \Aternos\Nbt\NbtFormat::JAVA_EDITION, \Aternos\Nbt\NbtFormat::BEDROCK_EDITION, and \Aternos\Nbt\NbtFormat::BEDROCK_EDITION_NETWORK.

More advanced readers can be created by implementing the \Aternos\Nbt\IO\Reader\Reader interface or by extending the \Aternos\Nbt\IO\Reader\AbstractReader class.

A reader object can be used to load the NBT tag.

$reader = new \Aternos\Nbt\IO\Reader\StringReader("...nbtData...", \Aternos\Nbt\NbtFormat::BEDROCK_EDITION);

$tag = \Aternos\Nbt\Tag\Tag::load($reader);

In theory, any type of NBT tag could be returned, but in reality all NBT files will start with either a compound tag or a list tag.

Manipulating NBT structures

Tag values of type TAG_Byte, TAG_Short, TAG_Int, TAG_Long, TAG_Float, TAG_Double, TAG_String can be accessed via their getValue() and setValue() functions.

$myInt new \Aternos\Nbt\Tag\IntTag();

$myInt->setValue(42);
echo $myInt->getValue(); // 42

Compound tags, list tags, and array tags implement the ArrayAccess, Countable, and Iterator interfaces and can therefore be accessed as arrays.

setValue(42); $myCompound["myFloat"] = (new \Aternos\Nbt\Tag\IntTag())->setValue(42.42); echo count($myCompound); // 2 //Manually setting a list's type is not strictly necessary, //since it's type will be set automatically when the first element is added $myList = (new \Aternos\Nbt\Tag\ListTag())->setContentTag(\Aternos\Nbt\Tag\TagType::TAG_String); $myList[] = (new \Aternos\Nbt\Tag\StringTag())->setValue("Hello"); $myList[] = (new \Aternos\Nbt\Tag\StringTag())->setValue("World"); ">
$myCompound = new \Aternos\Nbt\Tag\CompoundTag();

$myCompound["myInt"] = (new \Aternos\Nbt\Tag\IntTag())->setValue(42);
$myCompound["myFloat"] = (new \Aternos\Nbt\Tag\IntTag())->setValue(42.42);
echo count($myCompound); // 2

//Manually setting a list's type is not strictly necessary,
//since it's type will be set automatically when the first element is added
$myList = (new \Aternos\Nbt\Tag\ListTag())->setContentTag(\Aternos\Nbt\Tag\TagType::TAG_String);

$myList[] = (new \Aternos\Nbt\Tag\StringTag())->setValue("Hello");
$myList[] = (new \Aternos\Nbt\Tag\StringTag())->setValue("World");

Serializing NBT structures

Similar to the reader object to read NBT data, a writer object is required to write NBT data.

//Write uncompressed NBT data
$writer = (new \Aternos\Nbt\IO\Writer\StringWriter())->setFormat(\Aternos\Nbt\NbtFormat::BEDROCK_EDITION);

//Write gzip compressed NBT data
$gzipWriter = (new \Aternos\Nbt\IO\Writer\GZipCompressedStringWriter())->setFormat(\Aternos\Nbt\NbtFormat::JAVA_EDITION);

//Write zlib compressed NBT data
$gzipWriter = (new \Aternos\Nbt\IO\Writer\ZLibCompressedStringWriter())->setFormat(\Aternos\Nbt\NbtFormat::BEDROCK_EDITION_NETWORK);

The NBT flavor used by a writer object can differ from the one used by the reader object that was originally used to read the NBT structure. It is therefore possible to use this library to convert NBT structures between the different formats.

More advanced writers can be created by implementing the \Aternos\Nbt\IO\Writer\Writer interface or by extending the \Aternos\Nbt\IO\Writer\AbstractWriter class.

A writer object can be used to write/serialize an NBT structure.

getStringData()); ">
$writer = (new \Aternos\Nbt\IO\Writer\StringWriter())->setFormat(\Aternos\Nbt\NbtFormat::BEDROCK_EDITION);

$tag->write($writer);
file_put_contents("data.nbt", $writer->getStringData());

Bedrock Edition level.dat

While the Bedrock Edition level.dat file is an uncompressed NBT file, its NBT data is prepended by two 32-bit little endian integers.

The first one seems to be the version of the Bedrock Edition Storage Tool, which is also stored in the StorageVersion tag of the NBT structure.

The second number is the size of the file's NBT structure (not including the two prepending integers).

A Bedrock Edition level.dat file could be read like this:

$data = file_get_contents("level.dat");

$version = unpack("V", $data)[1];
$dataLength = unpack("V", $data, 4)[1];

if($dataLength !== strlen($data) - 8) {
    throw new Exception("Invalid level.dat data length");
}
$tag = \Aternos\Nbt\Tag\Tag::load(new \Aternos\Nbt\IO\Reader\StringReader(substr($data, 8), \Aternos\Nbt\NbtFormat::BEDROCK_EDITION));
You might also like...
Watch changes in the file system using PHP
Watch changes in the file system using PHP

Watch changes in the file system using PHP This package allows you to react to all kinds of changes in the file system. Here's how you can run code wh

PHP stream wrapper for Flysystem v2

Flystream enables you to use core PHP filesystem functions to interact with Flysystem filesystems by registering them as custom protocols.

File manager module for the Lumen PHP framework.

Lumen File Manager File manager module for the Lumen PHP framework. Please note that this module is still under active development. NOTE: Branch 5.1 i

This small PHP package assists in the loading and parsing of VTT files.

VTT Transcriptions This small PHP package assists in the loading and parsing of VTT files. Usage use Laracasts\Transcriptions\Transcription; $transcr

一个轻量级的 PHP 文件编辑器

AdminX 一个轻量级的 PHP 文件编辑器 安装 只需要前往 Release 页面下载最新版本的 adminx.php 放在你的主机文件里即可 若需要实时更新的 Develop Version 可以前往源代码找到 adminx.php 即可 配置 所有可配置的选项均在 adminx.php 的前

A php sharex uploader with discord embed function/twitter card support

Sharex Uploader Simple Sharex Uploader with Discord embed function Download replace your data and upload to your sevrer

Upload File Library For PHP ( Simple & Easy User )

Upload File Library For Backend/ServerSide PHP ( Simple & Easy For Use ), Support Multiple Upload

Gotipath Storage is a file storage library for PHP. It provides one interface to interact with FTP/SFTP.

Gotipath Storage is a file storage library for PHP. It provides one interface to interact with FTP/SFTP. When you use this package, you're protected from vendor lock-in, That mean you can connect to any FTP/SFTP storage. Also it's comes with base URL option to connect Gotipath CDN.

Releases(v1.7.0)
Owner
Aternos
Minecraft servers. Free. Forever.
Aternos
An object oriented PHP driver for FFMpeg binary

php-ffmpeg An Object-Oriented library to convert video/audio files with FFmpeg / AVConv. Check another amazing repo: PHP FFMpeg extras, you will find

null 4.4k Jan 2, 2023
ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It's widely used to build languages, tools, and frameworks. From a grammar, ANTLR generates a parser that can build parse trees and also generates a listener interface (or visitor) that makes it easy to respond to the recognition of phrases of interest.

Antlr Project 13.6k Jan 6, 2023
🛬🧾 A PHP8 TacView ACMI file format parser

A PHP8 TacView ACMI file format parser This package offers parsing support for TacView ACMI flight recordings version 2.1, it follows the standard des

Ignacio Muñoz Fernandez 1 Jan 18, 2022
A lightweight file manager with full ShareX, Screencloud support and more

XBackBone is a simple, self-hosted, lightweight PHP file manager that support the instant sharing tool ShareX and *NIX systems. It supports uploading

Sergio Brighenti 751 Jan 8, 2023
CSV data manipulation made easy in PHP

CSV Csv is a simple library to ease CSV parsing, writing and filtering in PHP. The goal of the library is to be powerful while remaining lightweight,

The League of Extraordinary Packages 3k Jan 4, 2023
PHP library that provides a filesystem abstraction layer − will be a feast for your files!

Gaufrette Gaufrette provides a filesystem abstraction layer. Why use Gaufrette? Imagine you have to manage a lot of medias in a PHP project. Lets see

KNP Labs 2.4k Jan 7, 2023
PHP-based anti-virus anti-trojan anti-malware solution.

What is phpMussel? An ideal solution for shared hosting environments, where it's often not possible to utilise or install conventional anti-virus prot

null 384 Dec 13, 2022
PHP runtime & extensions header files for PhpStorm

phpstorm-stubs STUBS are normal, syntactically correct PHP files that contain function & class signatures, constant definitions, etc. for all built-in

JetBrains 1.2k Dec 25, 2022
PHP Phar Stream Wrapper

Based on Sam Thomas' findings concerning insecure deserialization in combination with obfuscation strategies allowing to hide Phar files inside valid image resources, the TYPO3 project decided back then to introduce a PharStreamWrapper to intercept invocations of the phar:// stream in PHP and only allow usage for defined locations in the file system.

TYPO3 GitHub Department 55 Dec 7, 2022
A PHP library to deal with all those media services around, parsing their URLs and displaying their audios/videos.

MediaEmbed A utility library that generates HTML embed tags for audio or video located on a given URL. It also parses and validates given media URLs.

Mark Sch. 165 Nov 10, 2022