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