ItemStack: added jsonSerialize

This commit is contained in:
Dylan K. Taylor 2020-11-09 19:05:00 +00:00
parent 1d02829d6f
commit 2bcb398db9

View File

@ -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;
}
}