PocketMine-MP 5.44.3 git-327e8a1690982f1ac3634944d705ebad5d91f4ad
Loading...
Searching...
No Matches
NetworkInventoryAction.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;
16
17use pmmp\encoding\Byte;
18use pmmp\encoding\ByteBufferReader;
19use pmmp\encoding\ByteBufferWriter;
20use pmmp\encoding\DataDecodeException;
21use pmmp\encoding\VarInt;
24
26 public const SOURCE_CONTAINER = 0;
27
28 public const SOURCE_GLOBAL = 1;
29 public const SOURCE_WORLD = 2; //drop/pickup item entity
30 public const SOURCE_CREATIVE = 3;
31 public const SOURCE_TODO = 99999;
32
42 public const SOURCE_TYPE_CRAFTING_RESULT = -4;
43 public const SOURCE_TYPE_CRAFTING_USE_INGREDIENT = -5;
44
45 public const SOURCE_TYPE_ANVIL_RESULT = -12;
46 public const SOURCE_TYPE_ANVIL_OUTPUT = -13;
47
48 public const SOURCE_TYPE_ENCHANT_OUTPUT = -17;
49
50 public const SOURCE_TYPE_TRADING_INPUT_1 = -20;
51 public const SOURCE_TYPE_TRADING_INPUT_2 = -21;
52 public const SOURCE_TYPE_TRADING_USE_INPUTS = -22;
53 public const SOURCE_TYPE_TRADING_OUTPUT = -23;
54
55 public const SOURCE_TYPE_BEACON = -24;
56
57 public const ACTION_MAGIC_SLOT_CREATIVE_DELETE_ITEM = 0;
58 public const ACTION_MAGIC_SLOT_CREATIVE_CREATE_ITEM = 1;
59
60 public const ACTION_MAGIC_SLOT_DROP_ITEM = 0;
61 public const ACTION_MAGIC_SLOT_PICKUP_ITEM = 1;
62
63 public int $sourceType;
64 public ?int $windowId = null;
65 public ?int $sourceFlags = null;
66 public int $inventorySlot;
67 public ItemStackWrapper $oldItem;
68 public ItemStackWrapper $newItem;
69
76 public function readAuthInput(ByteBufferReader $in) : NetworkInventoryAction{
77 $this->sourceType = VarInt::readUnsignedInt($in);
78
79 switch($this->sourceType){
80 case self::SOURCE_CONTAINER:
81 $this->windowId = VarInt::readSignedInt($in);
82 break;
83 case self::SOURCE_WORLD:
84 $this->sourceFlags = VarInt::readUnsignedInt($in);
85 break;
86 case self::SOURCE_CREATIVE:
87 break;
88 case self::SOURCE_TODO:
89 $this->windowId = VarInt::readSignedInt($in);
90 break;
91 default:
92 throw new PacketDecodeException("Unknown inventory action source type $this->sourceType");
93 }
94
95 $this->inventorySlot = VarInt::readUnsignedInt($in);
96 $this->oldItem = CommonTypes::getItemStackWrapper($in);
97 $this->newItem = CommonTypes::getItemStackWrapper($in);
98
99 return $this;
100 }
101
102 public function writeAuthInput(ByteBufferWriter $out) : void{
103 VarInt::writeUnsignedInt($out, $this->sourceType);
104
105 switch($this->sourceType){
106 case self::SOURCE_CONTAINER:
107 if($this->windowId === null){
108 throw new \LogicException("WindowID must be set for SOURCE_CONTAINER");
109 }
110 VarInt::writeSignedInt($out, $this->windowId);
111 break;
112 case self::SOURCE_WORLD:
113 if($this->sourceFlags === null){
114 throw new \LogicException("SourceFlags must be set for SOURCE_WORLD");
115 }
116 VarInt::writeUnsignedInt($out, $this->sourceFlags);
117 break;
118 case self::SOURCE_CREATIVE:
119 break;
120 case self::SOURCE_TODO:
121 if($this->windowId === null){
122 throw new \LogicException("WindowID must be set for SOURCE_TODO");
123 }
124 VarInt::writeSignedInt($out, $this->windowId);
125 break;
126 default:
127 throw new \InvalidArgumentException("Unknown inventory action source type $this->sourceType");
128 }
129
130 VarInt::writeUnsignedInt($out, $this->inventorySlot);
131 CommonTypes::putItemStackWrapper($out, $this->oldItem);
132 CommonTypes::putItemStackWrapper($out, $this->newItem);
133 }
134
141 public function readTransaction(ByteBufferReader $in) : NetworkInventoryAction{
142 $this->sourceType = VarInt::readUnsignedInt($in);
143
144 if(Byte::readUnsigned($in) !== 1){
145 throw new PacketDecodeException("Inconsistent optional state for windowId");
146 }
147 $this->windowId = CommonTypes::readOptional($in, Byte::readSigned(...));
148
149 if(Byte::readUnsigned($in) !== 1){
150 throw new PacketDecodeException("Inconsistent optional state for sourceFlags");
151 }
152 $this->sourceFlags = CommonTypes::readOptional($in, VarInt::readUnsignedInt(...));
153
154 $this->inventorySlot = VarInt::readUnsignedInt($in);
155 $this->oldItem = CommonTypes::getNetworkItemStackDescriptor($in);
156 $this->newItem = CommonTypes::getNetworkItemStackDescriptor($in);
157
158 return $this;
159 }
160
164 public function writeTransaction(ByteBufferWriter $out) : void{
165 VarInt::writeUnsignedInt($out, $this->sourceType);
166
167 Byte::writeUnsigned($out, 1);
168 CommonTypes::writeOptional($out, $this->windowId, Byte::writeSigned(...));
169
170 Byte::writeUnsigned($out, 1);
171 CommonTypes::writeOptional($out, $this->sourceFlags, VarInt::writeUnsignedInt(...));
172
173 VarInt::writeUnsignedInt($out, $this->inventorySlot);
174 CommonTypes::putNetworkItemStackDescriptor($out, $this->oldItem);
175 CommonTypes::putNetworkItemStackDescriptor($out, $this->newItem);
176 }
177}