diff --git a/src/network/mcpe/protocol/types/inventory/ItemStack.php b/src/network/mcpe/protocol/types/inventory/ItemStack.php index 9233086f25..75c89937be 100644 --- a/src/network/mcpe/protocol/types/inventory/ItemStack.php +++ b/src/network/mcpe/protocol/types/inventory/ItemStack.php @@ -24,9 +24,13 @@ declare(strict_types=1); namespace pocketmine\network\mcpe\protocol\types\inventory; use pocketmine\nbt\tag\CompoundTag; +use pocketmine\nbt\TreeRoot; +use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer; use pocketmine\network\mcpe\protocol\types\FixedItemIds; +use function base64_encode; +use function count; -final class ItemStack{ +final class ItemStack implements \JsonSerializable{ /** @var int */ private $id; @@ -110,4 +114,25 @@ final class ItemStack{ ($this->nbt !== null && $itemStack->nbt !== null && $this->nbt->equals($itemStack->nbt)) ); } + + public function jsonSerialize() : array{ + $result = [ + "id" => $this->id, + "meta" => $this->meta, + "count" => $this->count, + ]; + if(count($this->canPlaceOn) > 0){ + $result["canPlaceOn"] = $this->canPlaceOn; + } + if(count($this->canDestroy) > 0){ + $result["canDestroy"] = $this->canDestroy; + } + if($this->shieldBlockingTick !== null){ + $result["shieldBlockingTick"] = $this->shieldBlockingTick; + } + if($this->nbt !== null){ + $result["nbt"] = base64_encode((new NetworkNbtSerializer())->write(new TreeRoot($this->nbt))); + } + return $result; + } }