PocketMine-MP 5.42.1 git-443151900a14fe9dc682cbf786fa441be1294fab
Loading...
Searching...
No Matches
VoxelShapesPacket.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\LE;
20use pmmp\encoding\VarInt;
23use function count;
24
26 public const NETWORK_ID = ProtocolInfo::VOXEL_SHAPES_PACKET;
27
32 private array $shapes;
37 private array $nameMap;
38
39 private int $customShapeCount;
40
48 public static function create(array $shapes, array $nameMap, int $customShapeCount) : self{
49 $result = new self;
50 $result->shapes = $shapes;
51 $result->nameMap = $nameMap;
52 $result->customShapeCount = $customShapeCount;
53 return $result;
54 }
55
60 public function getShapes() : array{ return $this->shapes; }
61
66 public function getNameMap() : array{ return $this->nameMap; }
67
68 public function getCustomShapeCount() : int{ return $this->customShapeCount; }
69
70 protected function decodePayload(ByteBufferReader $in) : void{
71 $this->shapes = [];
72 for($i = 0, $shapesCount = VarInt::readUnsignedInt($in); $i < $shapesCount; ++$i){
73 $this->shapes[] = SerializableVoxelShape::read($in);
74 }
75
76 $this->nameMap = [];
77 for($i = 0, $namesCount = VarInt::readUnsignedInt($in); $i < $namesCount; ++$i){
78 $name = CommonTypes::getString($in);
79 $id = LE::readUnsignedShort($in);
80 $this->nameMap[$name] = $id;
81 }
82
83 $this->customShapeCount = LE::readUnsignedShort($in);
84 }
85
86 protected function encodePayload(ByteBufferWriter $out) : void{
87 VarInt::writeUnsignedInt($out, count($this->shapes));
88 foreach($this->shapes as $shape){
89 $shape->write($out);
90 }
91
92 VarInt::writeUnsignedInt($out, count($this->nameMap));
93 foreach($this->nameMap as $name => $id){
94 CommonTypes::putString($out, $name);
95 LE::writeUnsignedShort($out, $id);
96 }
97
98 LE::writeUnsignedShort($out, $this->customShapeCount);
99 }
100
101 public function handle(PacketHandlerInterface $handler) : bool{
102 return $handler->handleVoxelShapes($this);
103 }
104}
static create(array $shapes, array $nameMap, int $customShapeCount)