PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
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
18use function count;
19
21
22 public const RESULT_OK = 0;
23 public const RESULT_ERROR = 1;
24 //TODO: there are a ton more possible result types but we don't need them yet and they are wayyyyyy too many for me
25 //to waste my time on right now...
26
30 public function __construct(
31 private int $result,
32 private int $requestId,
33 private array $containerInfos = []
34 ){
35 if($this->result !== self::RESULT_OK && count($this->containerInfos) !== 0){
36 throw new \InvalidArgumentException("Container infos must be empty if rejecting the request");
37 }
38 }
39
40 public function getResult() : int{ return $this->result; }
41
42 public function getRequestId() : int{ return $this->requestId; }
43
45 public function getContainerInfos() : array{ return $this->containerInfos; }
46
47 public static function read(PacketSerializer $in) : self{
48 $result = $in->getByte();
49 $requestId = $in->readItemStackRequestId();
50 $containerInfos = [];
51 if($result === self::RESULT_OK){
52 for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){
53 $containerInfos[] = ItemStackResponseContainerInfo::read($in);
54 }
55 }
56 return new self($result, $requestId, $containerInfos);
57 }
58
59 public function write(PacketSerializer $out) : void{
60 $out->putByte($this->result);
61 $out->writeItemStackRequestId($this->requestId);
62 if($this->result === self::RESULT_OK){
63 $out->putUnsignedVarInt(count($this->containerInfos));
64 foreach($this->containerInfos as $containerInfo){
65 $containerInfo->write($out);
66 }
67 }
68 }
69}
__construct(private int $result, private int $requestId, private array $containerInfos=[])