PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
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\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\DataDecodeException;
20use pmmp\encoding\VarInt;
23
25 public const SOURCE_CONTAINER = 0;
26
27 public const SOURCE_WORLD = 2; //drop/pickup item entity
28 public const SOURCE_CREATIVE = 3;
29 public const SOURCE_TODO = 99999;
30
40 public const SOURCE_TYPE_CRAFTING_RESULT = -4;
41 public const SOURCE_TYPE_CRAFTING_USE_INGREDIENT = -5;
42
43 public const SOURCE_TYPE_ANVIL_RESULT = -12;
44 public const SOURCE_TYPE_ANVIL_OUTPUT = -13;
45
46 public const SOURCE_TYPE_ENCHANT_OUTPUT = -17;
47
48 public const SOURCE_TYPE_TRADING_INPUT_1 = -20;
49 public const SOURCE_TYPE_TRADING_INPUT_2 = -21;
50 public const SOURCE_TYPE_TRADING_USE_INPUTS = -22;
51 public const SOURCE_TYPE_TRADING_OUTPUT = -23;
52
53 public const SOURCE_TYPE_BEACON = -24;
54
55 public const ACTION_MAGIC_SLOT_CREATIVE_DELETE_ITEM = 0;
56 public const ACTION_MAGIC_SLOT_CREATIVE_CREATE_ITEM = 1;
57
58 public const ACTION_MAGIC_SLOT_DROP_ITEM = 0;
59 public const ACTION_MAGIC_SLOT_PICKUP_ITEM = 1;
60
61 public int $sourceType;
62 public int $windowId;
63 public int $sourceFlags = 0;
64 public int $inventorySlot;
65 public ItemStackWrapper $oldItem;
66 public ItemStackWrapper $newItem;
67
74 public function read(ByteBufferReader $in) : NetworkInventoryAction{
75 $this->sourceType = VarInt::readUnsignedInt($in);
76
77 switch($this->sourceType){
78 case self::SOURCE_CONTAINER:
79 $this->windowId = VarInt::readSignedInt($in);
80 break;
81 case self::SOURCE_WORLD:
82 $this->sourceFlags = VarInt::readUnsignedInt($in);
83 break;
84 case self::SOURCE_CREATIVE:
85 break;
86 case self::SOURCE_TODO:
87 $this->windowId = VarInt::readSignedInt($in);
88 break;
89 default:
90 throw new PacketDecodeException("Unknown inventory action source type $this->sourceType");
91 }
92
93 $this->inventorySlot = VarInt::readUnsignedInt($in);
94 $this->oldItem = CommonTypes::getItemStackWrapper($in);
95 $this->newItem = CommonTypes::getItemStackWrapper($in);
96
97 return $this;
98 }
99
103 public function write(ByteBufferWriter $out) : void{
104 VarInt::writeUnsignedInt($out, $this->sourceType);
105
106 switch($this->sourceType){
107 case self::SOURCE_CONTAINER:
108 VarInt::writeSignedInt($out, $this->windowId);
109 break;
110 case self::SOURCE_WORLD:
111 VarInt::writeUnsignedInt($out, $this->sourceFlags);
112 break;
113 case self::SOURCE_CREATIVE:
114 break;
115 case self::SOURCE_TODO:
116 VarInt::writeSignedInt($out, $this->windowId);
117 break;
118 default:
119 throw new \InvalidArgumentException("Unknown inventory action source type $this->sourceType");
120 }
121
122 VarInt::writeUnsignedInt($out, $this->inventorySlot);
123 CommonTypes::putItemStackWrapper($out, $this->oldItem);
124 CommonTypes::putItemStackWrapper($out, $this->newItem);
125 }
126}