PocketMine-MP 5.17.1 git-df4ada81e5d74a14046f27cf44a37dcee69d657e
ItemStackRequest.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\stackrequest;
16
20use function count;
21
22final class ItemStackRequest{
28 public function __construct(
29 private int $requestId,
30 private array $actions,
31 private array $filterStrings,
32 private int $filterStringCause
33 ){}
34
35 public function getRequestId() : int{ return $this->requestId; }
36
38 public function getActions() : array{ return $this->actions; }
39
44 public function getFilterStrings() : array{ return $this->filterStrings; }
45
46 public function getFilterStringCause() : int{ return $this->filterStringCause; }
47
52 private static function readAction(PacketSerializer $in, int $typeId) : ItemStackRequestAction{
53 return match($typeId){
54 TakeStackRequestAction::ID => TakeStackRequestAction::read($in),
55 PlaceStackRequestAction::ID => PlaceStackRequestAction::read($in),
56 SwapStackRequestAction::ID => SwapStackRequestAction::read($in),
57 DropStackRequestAction::ID => DropStackRequestAction::read($in),
58 DestroyStackRequestAction::ID => DestroyStackRequestAction::read($in),
59 CraftingConsumeInputStackRequestAction::ID => CraftingConsumeInputStackRequestAction::read($in),
60 CraftingCreateSpecificResultStackRequestAction::ID => CraftingCreateSpecificResultStackRequestAction::read($in),
61 PlaceIntoBundleStackRequestAction::ID => PlaceIntoBundleStackRequestAction::read($in),
62 TakeFromBundleStackRequestAction::ID => TakeFromBundleStackRequestAction::read($in),
63 LabTableCombineStackRequestAction::ID => LabTableCombineStackRequestAction::read($in),
64 BeaconPaymentStackRequestAction::ID => BeaconPaymentStackRequestAction::read($in),
65 MineBlockStackRequestAction::ID => MineBlockStackRequestAction::read($in),
66 CraftRecipeStackRequestAction::ID => CraftRecipeStackRequestAction::read($in),
67 CraftRecipeAutoStackRequestAction::ID => CraftRecipeAutoStackRequestAction::read($in),
68 CreativeCreateStackRequestAction::ID => CreativeCreateStackRequestAction::read($in),
69 CraftRecipeOptionalStackRequestAction::ID => CraftRecipeOptionalStackRequestAction::read($in),
70 GrindstoneStackRequestAction::ID => GrindstoneStackRequestAction::read($in),
71 LoomStackRequestAction::ID => LoomStackRequestAction::read($in),
72 DeprecatedCraftingNonImplementedStackRequestAction::ID => DeprecatedCraftingNonImplementedStackRequestAction::read($in),
73 DeprecatedCraftingResultsStackRequestAction::ID => DeprecatedCraftingResultsStackRequestAction::read($in),
74 default => throw new PacketDecodeException("Unhandled item stack request action type $typeId"),
75 };
76 }
77
78 public static function read(PacketSerializer $in) : self{
79 $requestId = $in->readItemStackRequestId();
80 $actions = [];
81 for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){
82 $typeId = $in->getByte();
83 $actions[] = self::readAction($in, $typeId);
84 }
85 $filterStrings = [];
86 for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){
87 $filterStrings[] = $in->getString();
88 }
89 $filterStringCause = $in->getLInt();
90 return new self($requestId, $actions, $filterStrings, $filterStringCause);
91 }
92
93 public function write(PacketSerializer $out) : void{
94 $out->writeItemStackRequestId($this->requestId);
95 $out->putUnsignedVarInt(count($this->actions));
96 foreach($this->actions as $action){
97 $out->putByte($action->getTypeId());
98 $action->write($out);
99 }
100 $out->putUnsignedVarInt(count($this->filterStrings));
101 foreach($this->filterStrings as $string){
102 $out->putString($string);
103 }
104 $out->putLInt($this->filterStringCause);
105 }
106}
__construct(private int $requestId, private array $actions, private array $filterStrings, private int $filterStringCause)