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