PocketMine-MP 5.39.2 git-7ad037014ecec1ddc7a9bba215beb03e659ea931
Loading...
Searching...
No Matches
DataStoreChange.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\types;
16
17use pmmp\encoding\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\VarInt;
22
26final class DataStoreChange extends DataStore {
27 public const ID = DataStoreType::CHANGE;
28
29 public function __construct(
30 private string $name,
31 private string $property,
32 private int $updateCount,
33 private DataStoreValue $data
34 ){}
35
36 public function getTypeId() : int{
37 return self::ID;
38 }
39
40 public function getName() : string{ return $this->name; }
41
42 public function getProperty() : string{ return $this->property; }
43
44 public function getUpdateCount() : int{ return $this->updateCount; }
45
46 public function getData() : DataStoreValue{ return $this->data; }
47
48 public static function read(ByteBufferReader $in) : self{
49 $name = CommonTypes::getString($in);
50 $property = CommonTypes::getString($in);
51 $updateCount = VarInt::readUnsignedInt($in);
52
53 $data = match(VarInt::readUnsignedInt($in)){
54 DataStoreValueType::DOUBLE => DoubleDataStoreValue::read($in),
55 DataStoreValueType::BOOL => BoolDataStoreValue::read($in),
56 DataStoreValueType::STRING => StringDataStoreValue::read($in),
57 default => throw new PacketDecodeException("Unknown DataStoreValueType"),
58 };
59
60 return new self(
61 $name,
62 $property,
63 $updateCount,
64 $data,
65 );
66 }
67
68 public function write(ByteBufferWriter $out) : void{
69 CommonTypes::putString($out, $this->name);
70 CommonTypes::putString($out, $this->property);
71 VarInt::writeUnsignedInt($out, $this->updateCount);
72 VarInt::writeUnsignedInt($out, $this->data->getTypeId());
73 $this->data->write($out);
74 }
75}