PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
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
17use pmmp\encoding\Byte;
18use pmmp\encoding\ByteBufferReader;
19use pmmp\encoding\ByteBufferWriter;
21
23 public const NETWORK_ID = ProtocolInfo::BOOK_EDIT_PACKET;
24
25 public const TYPE_REPLACE_PAGE = 0;
26 public const TYPE_ADD_PAGE = 1;
27 public const TYPE_DELETE_PAGE = 2;
28 public const TYPE_SWAP_PAGES = 3;
29 public const TYPE_SIGN_BOOK = 4;
30
31 public int $type;
32 public int $inventorySlot;
33 public int $pageNumber;
34 public int $secondaryPageNumber;
35 public string $text;
36 public string $photoName;
37 public string $title;
38 public string $author;
39 public string $xuid;
40
41 protected function decodePayload(ByteBufferReader $in) : void{
42 $this->type = Byte::readUnsigned($in);
43 $this->inventorySlot = Byte::readUnsigned($in);
44
45 switch($this->type){
46 case self::TYPE_REPLACE_PAGE:
47 case self::TYPE_ADD_PAGE:
48 $this->pageNumber = Byte::readUnsigned($in);
49 $this->text = CommonTypes::getString($in);
50 $this->photoName = CommonTypes::getString($in);
51 break;
52 case self::TYPE_DELETE_PAGE:
53 $this->pageNumber = Byte::readUnsigned($in);
54 break;
55 case self::TYPE_SWAP_PAGES:
56 $this->pageNumber = Byte::readUnsigned($in);
57 $this->secondaryPageNumber = Byte::readUnsigned($in);
58 break;
59 case self::TYPE_SIGN_BOOK:
60 $this->title = CommonTypes::getString($in);
61 $this->author = CommonTypes::getString($in);
62 $this->xuid = CommonTypes::getString($in);
63 break;
64 default:
65 throw new PacketDecodeException("Unknown book edit type $this->type!");
66 }
67 }
68
69 protected function encodePayload(ByteBufferWriter $out) : void{
70 Byte::writeUnsigned($out, $this->type);
71 Byte::writeUnsigned($out, $this->inventorySlot);
72
73 switch($this->type){
74 case self::TYPE_REPLACE_PAGE:
75 case self::TYPE_ADD_PAGE:
76 Byte::writeUnsigned($out, $this->pageNumber);
77 CommonTypes::putString($out, $this->text);
78 CommonTypes::putString($out, $this->photoName);
79 break;
80 case self::TYPE_DELETE_PAGE:
81 Byte::writeUnsigned($out, $this->pageNumber);
82 break;
83 case self::TYPE_SWAP_PAGES:
84 Byte::writeUnsigned($out, $this->pageNumber);
85 Byte::writeUnsigned($out, $this->secondaryPageNumber);
86 break;
87 case self::TYPE_SIGN_BOOK:
88 CommonTypes::putString($out, $this->title);
89 CommonTypes::putString($out, $this->author);
90 CommonTypes::putString($out, $this->xuid);
91 break;
92 default:
93 throw new \InvalidArgumentException("Unknown book edit type $this->type!");
94 }
95 }
96
97 public function handle(PacketHandlerInterface $handler) : bool{
98 return $handler->handleBookEdit($this);
99 }
100}
handle(PacketHandlerInterface $handler)