Skip to content

Instantly share code, notes, and snippets.

@Steellgold
Created July 10, 2022 21:06
Show Gist options
  • Save Steellgold/1524c7268882ca456f1321f568c5ef69 to your computer and use it in GitHub Desktop.
Save Steellgold/1524c7268882ca456f1321f568c5ef69 to your computer and use it in GitHub Desktop.
Serialize and unserialize contents inventory (PocketMine-MP 4)
<?php
namespace steellgold\utils;
use pocketmine\item\Item;
use pocketmine\nbt\BigEndianNbtSerializer;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\TreeRoot;
class Serializer {
private BigEndianNbtSerializer $nbtSerializer;
public function __construct() {
$this->nbtSerializer = new BigEndianNbtSerializer();
}
public function read(string $data) : array {
$contents = [];
$inventoryTag = $this->nbtSerializer->read(zlib_decode($data))->mustGetCompoundTag()->getListTag("Inventory");
/** @var CompoundTag $tag */
foreach($inventoryTag as $tag){
$contents[$tag->getByte("Slot")] = Item::nbtDeserialize($tag);
}
return $contents;
}
public function write(array $items) : string|bool {
$contents = [];
/** @var Item[] $items */
foreach($items as $slot => $item){
$contents[] = $item->nbtSerialize($slot);
}
return zlib_encode($this->nbtSerializer->write(new TreeRoot(CompoundTag::create()
->setTag("Inventory", new ListTag($contents, NBT::TAG_Compound))
)), ZLIB_ENCODING_GZIP);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment