PocketMine-MP 5.42.2 git-40e2639b20bdc4ddba49d9f7f5fa0d5e92aa266f
Loading...
Searching...
No Matches
UseItemTransactionData.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\VarInt;
25use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait;
26
28 use GetTypeIdFromConstTrait;
29
30 public const ID = InventoryTransactionPacket::TYPE_USE_ITEM;
31
32 public const ACTION_CLICK_BLOCK = 0;
33 public const ACTION_CLICK_AIR = 1;
34 public const ACTION_BREAK_BLOCK = 2;
35 public const ACTION_USE_AS_ATTACK = 3;
36
37 private int $actionType;
38 private TriggerType $triggerType;
39 private BlockPosition $blockPosition;
40 private int $face;
41 private int $hotbarSlot;
42 private ItemStackWrapper $itemInHand;
43 private Vector3 $playerPosition;
44 private Vector3 $clickPosition;
45 private int $blockRuntimeId;
46 private PredictedResult $clientInteractPrediction;
47 private int $clientCooldownState;
48
49 public function getActionType() : int{
50 return $this->actionType;
51 }
52
53 public function getTriggerType() : TriggerType{ return $this->triggerType; }
54
55 public function getBlockPosition() : BlockPosition{
56 return $this->blockPosition;
57 }
58
59 public function getFace() : int{
60 return $this->face;
61 }
62
63 public function getHotbarSlot() : int{
64 return $this->hotbarSlot;
65 }
66
67 public function getItemInHand() : ItemStackWrapper{
68 return $this->itemInHand;
69 }
70
71 public function getPlayerPosition() : Vector3{
72 return $this->playerPosition;
73 }
74
75 public function getClickPosition() : Vector3{
76 return $this->clickPosition;
77 }
78
79 public function getBlockRuntimeId() : int{
80 return $this->blockRuntimeId;
81 }
82
83 public function getClientInteractPrediction() : PredictedResult{ return $this->clientInteractPrediction; }
84
85 public function getClientCooldownState() : int{ return $this->clientCooldownState; }
86
87 protected function decodeData(ByteBufferReader $in) : void{
88 $this->actionType = VarInt::readUnsignedInt($in);
89 $this->triggerType = TriggerType::fromPacket(VarInt::readUnsignedInt($in));
90 $this->blockPosition = CommonTypes::getBlockPosition($in);
91 $this->face = VarInt::readSignedInt($in);
92 $this->hotbarSlot = VarInt::readSignedInt($in);
93 $this->itemInHand = CommonTypes::getItemStackWrapper($in);
94 $this->playerPosition = CommonTypes::getVector3($in);
95 $this->clickPosition = CommonTypes::getVector3($in);
96 $this->blockRuntimeId = VarInt::readUnsignedInt($in);
97 $this->clientInteractPrediction = PredictedResult::fromPacket(VarInt::readUnsignedInt($in));
98 $this->clientCooldownState = Byte::readUnsigned($in);
99 }
100
101 protected function encodeData(ByteBufferWriter $out) : void{
102 VarInt::writeUnsignedInt($out, $this->actionType);
103 VarInt::writeUnsignedInt($out, $this->triggerType->value);
104 CommonTypes::putBlockPosition($out, $this->blockPosition);
105 VarInt::writeSignedInt($out, $this->face);
106 VarInt::writeSignedInt($out, $this->hotbarSlot);
107 CommonTypes::putItemStackWrapper($out, $this->itemInHand);
108 CommonTypes::putVector3($out, $this->playerPosition);
109 CommonTypes::putVector3($out, $this->clickPosition);
110 VarInt::writeUnsignedInt($out, $this->blockRuntimeId);
111 VarInt::writeUnsignedInt($out, $this->clientInteractPrediction->value);
112 Byte::writeUnsigned($out, $this->clientCooldownState);
113 }
114
118 private static function initSelf(
119 int $actionType,
120 TriggerType $triggerType,
121 BlockPosition $blockPosition,
122 int $face,
123 int $hotbarSlot,
124 ItemStackWrapper $itemInHand,
125 Vector3 $playerPosition,
126 Vector3 $clickPosition,
127 int $blockRuntimeId,
128 PredictedResult $clientInteractPrediction,
129 int $clientCooldownState,
130 ) : self{
131 $result = new self;
132 $result->actionType = $actionType;
133 $result->triggerType = $triggerType;
134 $result->blockPosition = $blockPosition;
135 $result->face = $face;
136 $result->hotbarSlot = $hotbarSlot;
137 $result->itemInHand = $itemInHand;
138 $result->playerPosition = $playerPosition;
139 $result->clickPosition = $clickPosition;
140 $result->blockRuntimeId = $blockRuntimeId;
141 $result->clientInteractPrediction = $clientInteractPrediction;
142 $result->clientCooldownState = $clientCooldownState;
143 return $result;
144 }
145
149 public static function new(array $actions, int $actionType, TriggerType $triggerType, BlockPosition $blockPosition, int $face, int $hotbarSlot, ItemStackWrapper $itemInHand, Vector3 $playerPosition, Vector3 $clickPosition, int $blockRuntimeId, PredictedResult $clientInteractPrediction, int $clientCooldownState) : self{
150 $result = self::initSelf($actionType, $triggerType, $blockPosition, $face, $hotbarSlot, $itemInHand, $playerPosition, $clickPosition, $blockRuntimeId, $clientInteractPrediction, $clientCooldownState);
151 $result->actions = $actions;
152 return $result;
153 }
154}