From 0f94cc62f0e7d3d5dc425006e443a1153ac60532 Mon Sep 17 00:00:00 2001 From: Koponya Date: Sat, 28 Jan 2023 21:45:33 +0100 Subject: [PATCH 1/2] Update new NBT format (Int array and Long array) ref: https://minecraft.fandom.com/wiki/NBT_format --- nbt.class.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/nbt.class.php b/nbt.class.php index 4b1d680..aae5de2 100644 --- a/nbt.class.php +++ b/nbt.class.php @@ -31,6 +31,8 @@ class NBT { const TAG_STRING = 8; const TAG_LIST = 9; const TAG_COMPOUND = 10; + const TAG_INT_ARRAY = 11; + const TAG_LONG_ARRAY = 12; public function loadFile($filename, $wrapper = "compress.zlib://") { if(is_string($wrapper) && is_file($filename)) { @@ -148,6 +150,16 @@ public function readType($fp, $tagType) { $tree = array(); while($this->traverseTag($fp, $tree)); return $tree; + case self::TAG_INT_ARRAY: // Int array + $arrayLength = $this->readType($fp, self::TAG_INT); + $array = array(); + for($i = 0; $i < $arrayLength; $i++) $array[] = $this->readType($fp, self::TAG_INT); + return $array; + case self::TAG_LONG_ARRAY: // Long array + $arrayLength = $this->readType($fp, self::TAG_INT); + $array = array(); + for($i = 0; $i < $arrayLength; $i++) $array[] = $this->readType($fp, self::TAG_LONG); + return $array; } } From 79aa348fd2fd105b794144de398e0abffa5d4c32 Mon Sep 17 00:00:00 2001 From: Koponya Date: Tue, 7 Feb 2023 21:04:11 +0100 Subject: [PATCH 2/2] Add write new NBT format IntArray and LongArray --- nbt.class.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/nbt.class.php b/nbt.class.php index aae5de2..9a904d4 100644 --- a/nbt.class.php +++ b/nbt.class.php @@ -199,6 +199,15 @@ public function writeType($fp, $tagType, $value) { foreach($value as $listItem) if(!$this->writeTag($fp, $listItem)) return false; if(!is_int(fwrite($fp, "\0"))) return false; return true; + case self::TAG_INT_ARRAY: // Int array + return $this->writeType($fp, self::TAG_INT, count($value)) && is_int(fwrite($fp, call_user_func_array("pack", array_merge(array("N".count($value)), $value)))); + case self::TAG_LONG_ARRAY: // Long array + $this->writeType($fp, self::TAG_INT, count($value)); + foreach($value as $v) { + if(!$this->writeType($fp, self::TAG_LONG, $v)) + return false; + } + return true; } } }