PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
CreativeContentPacket.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\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\VarInt;
22use function count;
23
25 public const NETWORK_ID = ProtocolInfo::CREATIVE_CONTENT_PACKET;
26
27 public const CATEGORY_CONSTRUCTION = 1;
28 public const CATEGORY_NATURE = 2;
29 public const CATEGORY_EQUIPMENT = 3;
30 public const CATEGORY_ITEMS = 4;
31
33 private array $groups;
35 private array $items;
36
42 public static function create(array $groups, array $items) : self{
43 $result = new self;
44 $result->groups = $groups;
45 $result->items = $items;
46 return $result;
47 }
48
50 public function getGroups() : array{ return $this->groups; }
51
53 public function getItems() : array{ return $this->items; }
54
55 protected function decodePayload(ByteBufferReader $in) : void{
56 $this->groups = [];
57 for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){
58 $this->groups[] = CreativeGroupEntry::read($in);
59 }
60
61 $this->items = [];
62 for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){
63 $this->items[] = CreativeItemEntry::read($in);
64 }
65 }
66
67 protected function encodePayload(ByteBufferWriter $out) : void{
68 VarInt::writeUnsignedInt($out, count($this->groups));
69 foreach($this->groups as $entry){
70 $entry->write($out);
71 }
72
73 VarInt::writeUnsignedInt($out, count($this->items));
74 foreach($this->items as $entry){
75 $entry->write($out);
76 }
77 }
78
79 public function handle(PacketHandlerInterface $handler) : bool{
80 return $handler->handleCreativeContent($this);
81 }
82}