PocketMine-MP 5.40.1 git-25718e8ccd903d1408fb25666ef84028379bbea6
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
46 public static function create(array $shapes, array $nameMap) : self{
47 $result = new self;
48 $result->shapes = $shapes;
49 $result->nameMap = $nameMap;
50 return $result;
51 }
52
57 public function getShapes() : array{ return $this->shapes; }
58
63 public function getNameMap() : array{ return $this->nameMap; }
64
65 protected function decodePayload(ByteBufferReader $in) : void{
66 $this->shapes = [];
67 for($i = 0, $shapesCount = VarInt::readUnsignedInt($in); $i < $shapesCount; ++$i){
68 $this->shapes[] = SerializableVoxelShape::read($in);
69 }
70
71 $this->nameMap = [];
72 for($i = 0, $namesCount = VarInt::readUnsignedInt($in); $i < $namesCount; ++$i){
73 $name = CommonTypes::getString($in);
74 $id = LE::readUnsignedShort($in);
75 $this->nameMap[$name] = $id;
76 }
77 }
78
79 protected function encodePayload(ByteBufferWriter $out) : void{
80 VarInt::writeUnsignedInt($out, count($this->shapes));
81 foreach($this->shapes as $shape){
82 $shape->write($out);
83 }
84
85 VarInt::writeUnsignedInt($out, count($this->nameMap));
86 foreach($this->nameMap as $name => $id){
87 CommonTypes::putString($out, $name);
88 LE::writeUnsignedShort($out, $id);
89 }
90 }
91
92 public function handle(PacketHandlerInterface $handler) : bool{
93 return $handler->handleVoxelShapes($this);
94 }
95}
static create(array $shapes, array $nameMap)