PocketMine-MP 5.35.1 git-d241807752ab518f2ffc7e3b71f6a72f9cdf0c30
Loading...
Searching...
No Matches
item/Trident.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\item;
25
29use pocketmine\entity\projectile\Trident as TridentEntity;
33use function min;
34
35class Trident extends Tool implements Releasable{
36 private const DAMAGE_ON_THROW = 1;
37
38 public function getMaxDurability() : int{
39 return 251;
40 }
41
42 public function onReleaseUsing(Player $player, array &$returnedItems) : ItemUseResult{
43 $location = $player->getLocation();
44
45 $diff = $player->getItemUseDuration();
46 if($diff < 14){
47 return ItemUseResult::FAIL;
48 }
49
50 $item = $this->pop();
51 if($player->hasFiniteResources()){
52 $item->applyDamage(self::DAMAGE_ON_THROW);
53 }
54 if($item->isNull()){
55 //canStartUsingItem() will normally prevent this, but it's possible the item might've been modified between
56 //the start action and the release, so it's best to account for this anyway
57 return ItemUseResult::FAIL;
58 }
59 $entity = new TridentEntity(Location::fromObject(
60 $player->getEyePos(),
61 $player->getWorld(),
62 ($location->yaw > 180 ? 360 : 0) - $location->yaw,
63 -$location->pitch
64 ), $item, $player);
65 $p = $diff / 20;
66 $baseForce = min((($p ** 2) + $p * 2) / 3, 1) * 2.4;
67 $entity->setMotion($player->getDirectionVector()->multiply($baseForce));
68
69 $ev = new ProjectileLaunchEvent($entity);
70 $ev->call();
71 if($ev->isCancelled()){
72 $ev->getEntity()->flagForDespawn();
73 return ItemUseResult::FAIL;
74 }
75 $ev->getEntity()->spawnToAll();
76 $location->getWorld()->addSound($location, new TridentThrowSound());
77
78 return ItemUseResult::SUCCESS;
79 }
80
81 public function getAttackPoints() : int{
82 return 9;
83 }
84
85 public function canStartUsingItem(Player $player) : bool{
86 return $this->damage < $this->getMaxDurability() - self::DAMAGE_ON_THROW;
87 }
88
89 public function onAttackEntity(Entity $victim, array &$returnedItems) : bool{
90 return $this->applyDamage(1);
91 }
92
93 public function onDestroyBlock(Block $block, array &$returnedItems) : bool{
94 if(!$block->getBreakInfo()->breaksInstantly()){
95 return $this->applyDamage(2);
96 }
97 return false;
98 }
99}
onAttackEntity(Entity $victim, array &$returnedItems)
onDestroyBlock(Block $block, array &$returnedItems)
onReleaseUsing(Player $player, array &$returnedItems)