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