PocketMine-MP 5.44.4 git-6a7cc02e9dff59b69241aa0bcffdb9903ce86beb
Loading...
Searching...
No Matches
DataStoreUpdate.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\ddui;
16
17use pmmp\encoding\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\LE;
20use pmmp\encoding\VarInt;
28use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait;
29
34final class DataStoreUpdate implements DataStoreOperation{
35 use GetTypeIdFromConstTrait;
36
37 public const ID = DataStoreOperationType::UPDATE;
38
39 public function __construct(
40 private string $name,
41 private string $property,
42 private string $path,
43 private DataStoreUpdateValue $data,
44 private int $updateCount,
45 private int $pathUpdateCount,
46 ){}
47
48 public function getName() : string{ return $this->name; }
49
50 public function getProperty() : string{ return $this->property; }
51
52 public function getPath() : string{ return $this->path; }
53
54 public function getData() : DataStoreUpdateValue{ return $this->data; }
55
56 public function getUpdateCount() : int{ return $this->updateCount; }
57
58 public function getPathUpdateCount() : int{ return $this->pathUpdateCount; }
59
60 public static function read(ByteBufferReader $in) : self{
61 $name = CommonTypes::getString($in);
62 $property = CommonTypes::getString($in);
63 $path = CommonTypes::getString($in);
64
65 $data = match(VarInt::readUnsignedInt($in)){
66 DataStoreUpdateValueType::DOUBLE => DoubleDataStoreUpdateValue::read($in),
67 DataStoreUpdateValueType::BOOL => BoolDataStoreUpdateValue::read($in),
68 DataStoreUpdateValueType::STRING => StringDataStoreUpdateValue::read($in),
69 default => throw new PacketDecodeException("Unknown DataStoreValueType"),
70 };
71
72 $updateCount = LE::readUnsignedInt($in);
73 $pathUpdateCount = LE::readUnsignedInt($in);
74
75 return new self(
76 $name,
77 $property,
78 $path,
79 $data,
80 $updateCount,
81 $pathUpdateCount,
82 );
83 }
84
85 public function write(ByteBufferWriter $out) : void{
86 CommonTypes::putString($out, $this->name);
87 CommonTypes::putString($out, $this->property);
88 CommonTypes::putString($out, $this->path);
89 VarInt::writeUnsignedInt($out, $this->data->getTypeId());
90 $this->data->write($out);
91 LE::writeUnsignedInt($out, $this->updateCount);
92 LE::writeUnsignedInt($out, $this->pathUpdateCount);
93 }
94}