PocketMine-MP 5.39.2 git-7ad037014ecec1ddc7a9bba215beb03e659ea931
Loading...
Searching...
No Matches
ClientboundDataStorePacket.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;
25use function count;
26
28 public const NETWORK_ID = ProtocolInfo::CLIENTBOUND_DATA_STORE_PACKET;
29
34 public array $values = [];
35
41 public static function create(array $values) : self{
42 $result = new self;
43 $result->values = $values;
44 return $result;
45 }
46
47 protected function decodePayload(ByteBufferReader $in) : void{
48 $this->values = [];
49 for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){
50 $this->values[] = match(VarInt::readUnsignedInt($in)){
51 DataStoreType::UPDATE => DataStoreUpdate::read($in),
52 DataStoreType::CHANGE => DataStoreChange::read($in),
53 DataStoreType::REMOVAL => DataStoreRemoval::read($in),
54 default => throw new PacketDecodeException("Unknown DataStore type"),
55 };
56 }
57 }
58
59 protected function encodePayload(ByteBufferWriter $out) : void{
60 VarInt::writeUnsignedInt($out, count($this->values));
61 foreach($this->values as $value){
62 VarInt::writeUnsignedInt($out, $value->getTypeId());
63 $value->write($out);
64 }
65 }
66
67 public function handle(PacketHandlerInterface $handler) : bool{
68 return $handler->handleClientboundDataStore($this);
69 }
70}