PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
ItemStackResponse.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\inventory\stackresponse;
16
17use pmmp\encoding\Byte;
18use pmmp\encoding\ByteBufferReader;
19use pmmp\encoding\ByteBufferWriter;
20use pmmp\encoding\VarInt;
22use function count;
23
25
26 public const RESULT_OK = 0;
27 public const RESULT_ERROR = 1;
28 //TODO: there are a ton more possible result types but we don't need them yet and they are wayyyyyy too many for me
29 //to waste my time on right now...
30
34 public function __construct(
35 private int $result,
36 private int $requestId,
37 private array $containerInfos = []
38 ){
39 if($this->result !== self::RESULT_OK && count($this->containerInfos) !== 0){
40 throw new \InvalidArgumentException("Container infos must be empty if rejecting the request");
41 }
42 }
43
44 public function getResult() : int{ return $this->result; }
45
46 public function getRequestId() : int{ return $this->requestId; }
47
49 public function getContainerInfos() : array{ return $this->containerInfos; }
50
51 public static function read(ByteBufferReader $in) : self{
52 $result = Byte::readUnsigned($in);
53 $requestId = CommonTypes::readItemStackRequestId($in);
54 $containerInfos = [];
55 if($result === self::RESULT_OK){
56 for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){
57 $containerInfos[] = ItemStackResponseContainerInfo::read($in);
58 }
59 }
60 return new self($result, $requestId, $containerInfos);
61 }
62
63 public function write(ByteBufferWriter $out) : void{
64 Byte::writeUnsigned($out, $this->result);
65 CommonTypes::writeItemStackRequestId($out, $this->requestId);
66 if($this->result === self::RESULT_OK){
67 VarInt::writeUnsignedInt($out, count($this->containerInfos));
68 foreach($this->containerInfos as $containerInfo){
69 $containerInfo->write($out);
70 }
71 }
72 }
73}
__construct(private int $result, private int $requestId, private array $containerInfos=[])