PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
BookEditPacket.php
1<?php
2
3/*
4 * This file is part of BedrockProtocol.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
6 *
7 * BedrockProtocol is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13declare(strict_types=1);
14
15namespace pocketmine\network\mcpe\protocol;
16
18
20 public const NETWORK_ID = ProtocolInfo::BOOK_EDIT_PACKET;
21
22 public const TYPE_REPLACE_PAGE = 0;
23 public const TYPE_ADD_PAGE = 1;
24 public const TYPE_DELETE_PAGE = 2;
25 public const TYPE_SWAP_PAGES = 3;
26 public const TYPE_SIGN_BOOK = 4;
27
28 public int $type;
29 public int $inventorySlot;
30 public int $pageNumber;
31 public int $secondaryPageNumber;
32 public string $text;
33 public string $photoName;
34 public string $title;
35 public string $author;
36 public string $xuid;
37
38 protected function decodePayload(PacketSerializer $in) : void{
39 $this->type = $in->getByte();
40 $this->inventorySlot = $in->getByte();
41
42 switch($this->type){
43 case self::TYPE_REPLACE_PAGE:
44 case self::TYPE_ADD_PAGE:
45 $this->pageNumber = $in->getByte();
46 $this->text = $in->getString();
47 $this->photoName = $in->getString();
48 break;
49 case self::TYPE_DELETE_PAGE:
50 $this->pageNumber = $in->getByte();
51 break;
52 case self::TYPE_SWAP_PAGES:
53 $this->pageNumber = $in->getByte();
54 $this->secondaryPageNumber = $in->getByte();
55 break;
56 case self::TYPE_SIGN_BOOK:
57 $this->title = $in->getString();
58 $this->author = $in->getString();
59 $this->xuid = $in->getString();
60 break;
61 default:
62 throw new PacketDecodeException("Unknown book edit type $this->type!");
63 }
64 }
65
66 protected function encodePayload(PacketSerializer $out) : void{
67 $out->putByte($this->type);
68 $out->putByte($this->inventorySlot);
69
70 switch($this->type){
71 case self::TYPE_REPLACE_PAGE:
72 case self::TYPE_ADD_PAGE:
73 $out->putByte($this->pageNumber);
74 $out->putString($this->text);
75 $out->putString($this->photoName);
76 break;
77 case self::TYPE_DELETE_PAGE:
78 $out->putByte($this->pageNumber);
79 break;
80 case self::TYPE_SWAP_PAGES:
81 $out->putByte($this->pageNumber);
82 $out->putByte($this->secondaryPageNumber);
83 break;
84 case self::TYPE_SIGN_BOOK:
85 $out->putString($this->title);
86 $out->putString($this->author);
87 $out->putString($this->xuid);
88 break;
89 default:
90 throw new \InvalidArgumentException("Unknown book edit type $this->type!");
91 }
92 }
93
94 public function handle(PacketHandlerInterface $handler) : bool{
95 return $handler->handleBookEdit($this);
96 }
97}
handle(PacketHandlerInterface $handler)