From 17518195d1f9c56d62efd153033174663b8ac00a Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 24 Aug 2017 12:02:03 +0100 Subject: [PATCH] Be more smart about json-serializing items Don't include nbt_hex if we don't have a NBT tag Don't include damage unless it's non-zero Don't include count unless it's non-1 --- src/pocketmine/item/Item.php | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/pocketmine/item/Item.php b/src/pocketmine/item/Item.php index a1d5695f8..9f3a058f6 100644 --- a/src/pocketmine/item/Item.php +++ b/src/pocketmine/item/Item.php @@ -859,12 +859,23 @@ class Item implements ItemIds, \JsonSerializable{ * @return array */ final public function jsonSerialize(){ - return [ - "id" => $this->getId(), - "damage" => $this->getDamage(), - "count" => $this->getCount(), - "nbt_hex" => bin2hex($this->getCompoundTag()) + $data = [ + "id" => $this->getId() ]; + + if($this->getDamage() !== 0){ + $data["damage"] = $this->getDamage(); + } + + if($this->getCount() !== 1){ + $data["count"] = $this->getCount(); + } + + if($this->hasCompoundTag()){ + $data["nbt_hex"] = bin2hex($this->getCompoundTag()); + } + + return $data; } /** @@ -876,9 +887,9 @@ class Item implements ItemIds, \JsonSerializable{ final public static function jsonDeserialize(array $data) : Item{ return ItemFactory::get( (int) $data["id"], - (int) $data["damage"], - (int) $data["count"], - (string) ($data["nbt"] ?? hex2bin($data["nbt_hex"])) //`nbt` key might contain old raw data + (int) ($data["damage"] ?? 0), + (int) ($data["count"] ?? 1), + (string) ($data["nbt"] ?? (isset($data["nbt_hex"]) ? hex2bin($data["nbt_hex"]) : "")) //`nbt` key might contain old raw data ); }