PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
entity/projectile/Arrow.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\entity\projectile;
25
43use function ceil;
44use function mt_rand;
45use function sqrt;
46
47class Arrow extends Projectile{
48
49 public static function getNetworkTypeId() : string{ return EntityIds::ARROW; }
50
51 public const PICKUP_NONE = 0;
52 public const PICKUP_ANY = 1;
53 public const PICKUP_CREATIVE = 2;
54
55 private const TAG_PICKUP = "pickup"; //TAG_Byte
56 public const TAG_CRIT = "crit"; //TAG_Byte
57 private const TAG_LIFE = "life"; //TAG_Short
58
59 protected float $damage = 2.0;
60 protected int $pickupMode = self::PICKUP_ANY;
61 protected float $punchKnockback = 0.0;
62 protected int $collideTicks = 0;
63 protected bool $critical = false;
64
65 public function __construct(Location $location, ?Entity $shootingEntity, bool $critical, ?CompoundTag $nbt = null){
66 parent::__construct($location, $shootingEntity, $nbt);
67 $this->setCritical($critical);
68 }
69
70 protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(0.25, 0.25); }
71
72 protected function getInitialDragMultiplier() : float{ return 0.01; }
73
74 protected function getInitialGravity() : float{ return 0.05; }
75
76 protected function initEntity(CompoundTag $nbt) : void{
77 parent::initEntity($nbt);
78
79 $this->pickupMode = $nbt->getByte(self::TAG_PICKUP, self::PICKUP_ANY);
80 $this->critical = $nbt->getByte(self::TAG_CRIT, 0) === 1;
81 $this->collideTicks = $nbt->getShort(self::TAG_LIFE, $this->collideTicks);
82 }
83
84 public function saveNBT() : CompoundTag{
85 $nbt = parent::saveNBT();
86 $nbt->setByte(self::TAG_PICKUP, $this->pickupMode);
87 $nbt->setByte(self::TAG_CRIT, $this->critical ? 1 : 0);
88 $nbt->setShort(self::TAG_LIFE, $this->collideTicks);
89 return $nbt;
90 }
91
92 public function isCritical() : bool{
93 return $this->critical;
94 }
95
96 public function setCritical(bool $value = true) : void{
97 $this->critical = $value;
98 $this->networkPropertiesDirty = true;
99 }
100
101 public function getResultDamage() : int{
102 $base = (int) ceil($this->motion->length() * parent::getResultDamage());
103 if($this->isCritical()){
104 return ($base + mt_rand(0, (int) ($base / 2) + 1));
105 }else{
106 return $base;
107 }
108 }
109
110 public function getPunchKnockback() : float{
111 return $this->punchKnockback;
112 }
113
114 public function setPunchKnockback(float $punchKnockback) : void{
115 $this->punchKnockback = $punchKnockback;
116 }
117
118 protected function entityBaseTick(int $tickDiff = 1) : bool{
119 if($this->closed){
120 return false;
121 }
122
123 $hasUpdate = parent::entityBaseTick($tickDiff);
124
125 if($this->blockHit !== null){
126 $this->collideTicks += $tickDiff;
127 if($this->collideTicks > 1200){
128 $this->flagForDespawn();
129 $hasUpdate = true;
130 }
131 }else{
132 $this->collideTicks = 0;
133 }
134
135 return $hasUpdate;
136 }
137
138 protected function onHit(ProjectileHitEvent $event) : void{
139 $this->setCritical(false);
140 $this->broadcastSound(new ArrowHitSound());
141 }
142
143 protected function onHitBlock(Block $blockHit, RayTraceResult $hitResult) : void{
144 parent::onHitBlock($blockHit, $hitResult);
145 $this->broadcastAnimation(new ArrowShakeAnimation($this, 7));
146 }
147
148 protected function onHitEntity(Entity $entityHit, RayTraceResult $hitResult) : void{
149 parent::onHitEntity($entityHit, $hitResult);
150 if($this->punchKnockback > 0){
151 $horizontalSpeed = sqrt($this->motion->x ** 2 + $this->motion->z ** 2);
152 if($horizontalSpeed > 0){
153 $multiplier = $this->punchKnockback * 0.6 / $horizontalSpeed;
154 $entityHit->setMotion($entityHit->getMotion()->add($this->motion->x * $multiplier, 0.1, $this->motion->z * $multiplier));
155 }
156 }
157 }
158
159 public function getPickupMode() : int{
160 return $this->pickupMode;
161 }
162
163 public function setPickupMode(int $pickupMode) : void{
164 $this->pickupMode = $pickupMode;
165 }
166
167 public function onCollideWithPlayer(Player $player) : void{
168 if($this->blockHit === null){
169 return;
170 }
171
172 $item = VanillaItems::ARROW();
173 $playerInventory = match(true){
174 !$player->hasFiniteResources() => null, //arrows are not picked up in creative
175 $player->getOffHandInventory()->getItem(0)->canStackWith($item) && $player->getOffHandInventory()->canAddItem($item) => $player->getOffHandInventory(),
176 $player->getInventory()->canAddItem($item) => $player->getInventory(),
177 default => null
178 };
179
180 $ev = new EntityItemPickupEvent($player, $this, $item, $playerInventory);
181 if($player->hasFiniteResources() && $playerInventory === null){
182 $ev->cancel();
183 }
184 if($this->pickupMode === self::PICKUP_NONE || ($this->pickupMode === self::PICKUP_CREATIVE && !$player->isCreative())){
185 $ev->cancel();
186 }
187
188 $ev->call();
189 if($ev->isCancelled()){
190 return;
191 }
192
193 NetworkBroadcastUtils::broadcastEntityEvent(
194 $this->getViewers(),
195 fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onPickUpItem($recipients, $player, $this)
196 );
197
198 $ev->getInventory()?->addItem($ev->getItem());
199 $this->flagForDespawn();
200 }
201
202 protected function syncNetworkData(EntityMetadataCollection $properties) : void{
203 parent::syncNetworkData($properties);
204
205 $properties->setGenericFlag(EntityMetadataFlags::CRITICAL, $this->critical);
206 }
207}
onHitBlock(Block $blockHit, RayTraceResult $hitResult)
onHitEntity(Entity $entityHit, RayTraceResult $hitResult)
setByte(string $name, int $value)
setShort(string $name, int $value)