PocketMine-MP 5.44.4 git-bc94a0da0c87abe7eb99d229a9ec672b3c405d11
Loading...
Searching...
No Matches
InventoryTransactionPacket.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;
16
17use pmmp\encoding\Byte;
18use pmmp\encoding\ByteBufferReader;
19use pmmp\encoding\ByteBufferWriter;
20use pmmp\encoding\VarInt;
29use function count;
30
35 public const NETWORK_ID = ProtocolInfo::INVENTORY_TRANSACTION_PACKET;
36
37 public const TYPE_NORMAL = 0;
38 public const TYPE_MISMATCH = 1;
39 public const TYPE_USE_ITEM = 2;
40 public const TYPE_USE_ITEM_ON_ENTITY = 3;
41 public const TYPE_RELEASE_ITEM = 4;
42
43 public int $requestId;
45 public ?array $requestChangedSlots;
46 public TransactionData $trData;
47
52 public static function create(int $requestId, ?array $requestChangedSlots, TransactionData $trData) : self{
53 $result = new self;
54 $result->requestId = $requestId;
55 $result->requestChangedSlots = $requestChangedSlots;
56 $result->trData = $trData;
57 return $result;
58 }
59
60 protected function decodePayload(ByteBufferReader $in) : void{
61 $this->requestId = CommonTypes::readLegacyItemStackRequestId($in);
62
63 $this->requestChangedSlots = CommonTypes::readOptional($in, static function(ByteBufferReader $in) : array{
64 $result = [];
65 for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){
66 $result[] = InventoryTransactionChangedSlotsHack::read($in);
67 }
68 return $result;
69 });
70
71 if(Byte::readUnsigned($in) !== 1){
72 throw new PacketDecodeException("Dummy optional bool for transactionType should always be 1");
73 }
74 $transactionType = VarInt::readUnsignedInt($in);
75 if(Byte::readUnsigned($in) !== 1){
76 throw new PacketDecodeException("Dummy optional bool for trData should always be 1");
77 }
78 $this->trData = match($transactionType) {
79 NormalTransactionData::ID => new NormalTransactionData(),
80 MismatchTransactionData::ID => new MismatchTransactionData(),
81 UseItemTransactionData::ID => new UseItemTransactionData(),
82 UseItemOnEntityTransactionData::ID => new UseItemOnEntityTransactionData(),
83 ReleaseItemTransactionData::ID => new ReleaseItemTransactionData(),
84 default => throw new PacketDecodeException("Unknown transaction type $transactionType"),
85 };
86 $this->trData->decodeTransaction($in);
87 }
88
89 protected function encodePayload(ByteBufferWriter $out) : void{
90 CommonTypes::writeLegacyItemStackRequestId($out, $this->requestId);
91
92 CommonTypes::writeOptional($out, $this->requestChangedSlots, static function(ByteBufferWriter $out, array $value) : void{
93 VarInt::writeUnsignedInt($out, count($value));
94 foreach($value as $changedSlots){
95 $changedSlots->write($out);
96 }
97 });
98
99 Byte::writeUnsigned($out, 1);
100 VarInt::writeUnsignedInt($out, $this->trData->getTypeId());
101 Byte::writeUnsigned($out, 1);
102 $this->trData->encodeTransaction($out);
103 }
104
105 public function handle(PacketHandlerInterface $handler) : bool{
106 return $handler->handleInventoryTransaction($this);
107 }
108}
static create(int $requestId, ?array $requestChangedSlots, TransactionData $trData)