PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
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\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\VarInt;
28use function count;
29
34 public const NETWORK_ID = ProtocolInfo::INVENTORY_TRANSACTION_PACKET;
35
36 public const TYPE_NORMAL = 0;
37 public const TYPE_MISMATCH = 1;
38 public const TYPE_USE_ITEM = 2;
39 public const TYPE_USE_ITEM_ON_ENTITY = 3;
40 public const TYPE_RELEASE_ITEM = 4;
41
42 public int $requestId;
44 public array $requestChangedSlots;
45 public TransactionData $trData;
46
51 public static function create(int $requestId, array $requestChangedSlots, TransactionData $trData) : self{
52 $result = new self;
53 $result->requestId = $requestId;
54 $result->requestChangedSlots = $requestChangedSlots;
55 $result->trData = $trData;
56 return $result;
57 }
58
59 protected function decodePayload(ByteBufferReader $in) : void{
60 $this->requestId = CommonTypes::readLegacyItemStackRequestId($in);
61 $this->requestChangedSlots = [];
62 if($this->requestId !== 0){
63 for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){
64 $this->requestChangedSlots[] = InventoryTransactionChangedSlotsHack::read($in);
65 }
66 }
67
68 $transactionType = VarInt::readUnsignedInt($in);
69
70 $this->trData = match($transactionType){
71 NormalTransactionData::ID => new NormalTransactionData(),
72 MismatchTransactionData::ID => new MismatchTransactionData(),
73 UseItemTransactionData::ID => new UseItemTransactionData(),
74 UseItemOnEntityTransactionData::ID => new UseItemOnEntityTransactionData(),
75 ReleaseItemTransactionData::ID => new ReleaseItemTransactionData(),
76 default => throw new PacketDecodeException("Unknown transaction type $transactionType"),
77 };
78
79 $this->trData->decode($in);
80 }
81
82 protected function encodePayload(ByteBufferWriter $out) : void{
83 CommonTypes::writeLegacyItemStackRequestId($out, $this->requestId);
84 if($this->requestId !== 0){
85 VarInt::writeUnsignedInt($out, count($this->requestChangedSlots));
86 foreach($this->requestChangedSlots as $changedSlots){
87 $changedSlots->write($out);
88 }
89 }
90
91 VarInt::writeUnsignedInt($out, $this->trData->getTypeId());
92
93 $this->trData->encode($out);
94 }
95
96 public function handle(PacketHandlerInterface $handler) : bool{
97 return $handler->handleInventoryTransaction($this);
98 }
99}
static create(int $requestId, array $requestChangedSlots, TransactionData $trData)