PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
entity/projectile/SplashPotion.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
47use function count;
48use function round;
49use function sqrt;
50
51class SplashPotion extends Throwable{
52
53 public const TAG_POTION_ID = "PotionId"; //TAG_Short
54
55 public static function getNetworkTypeId() : string{ return EntityIds::SPLASH_POTION; }
56
57 protected bool $linger = false;
58 protected PotionType $potionType;
59
60 public function __construct(Location $location, ?Entity $shootingEntity, PotionType $potionType, ?CompoundTag $nbt = null){
61 $this->potionType = $potionType;
62 parent::__construct($location, $shootingEntity, $nbt);
63 }
64
65 protected function getInitialGravity() : float{ return 0.05; }
66
67 public function saveNBT() : CompoundTag{
68 $nbt = parent::saveNBT();
69 $nbt->setShort(self::TAG_POTION_ID, PotionTypeIdMap::getInstance()->toId($this->getPotionType()));
70
71 return $nbt;
72 }
73
74 public function getResultDamage() : int{
75 return -1; //no damage
76 }
77
78 protected function onHit(ProjectileHitEvent $event) : void{
79 $effects = $this->getPotionEffects();
80 $hasEffects = true;
81
82 if(count($effects) === 0){
83 $particle = new PotionSplashParticle(PotionSplashParticle::DEFAULT_COLOR());
84 $hasEffects = false;
85 }else{
86 $colors = [];
87 foreach($effects as $effect){
88 $level = $effect->getEffectLevel();
89 for($j = 0; $j < $level; ++$j){
90 $colors[] = $effect->getColor();
91 }
92 }
93 $particle = new PotionSplashParticle(Color::mix(...$colors));
94 }
95
96 $this->getWorld()->addParticle($this->location, $particle);
97 $this->broadcastSound(new PotionSplashSound());
98
99 if($hasEffects){
100 if(!$this->willLinger()){
101 foreach($this->getWorld()->getCollidingEntities($this->boundingBox->expandedCopy(4.125, 2.125, 4.125), $this) as $entity){
102 if($entity instanceof Living){
103 $distanceSquared = $entity->getEyePos()->distanceSquared($this->location);
104 if($distanceSquared > 16){ //4 blocks
105 continue;
106 }
107
108 $distanceMultiplier = 1 - (sqrt($distanceSquared) / 4);
109 if($event instanceof ProjectileHitEntityEvent && $entity === $event->getEntityHit()){
110 $distanceMultiplier = 1.0;
111 }
112
113 foreach($this->getPotionEffects() as $effect){
114 //getPotionEffects() is used to get COPIES to avoid accidentally modifying the same effect instance already applied to another entity
115
116 if(!($effect->getType() instanceof InstantEffect)){
117 $newDuration = (int) round($effect->getDuration() * 0.75 * $distanceMultiplier);
118 if($newDuration < 20){
119 continue;
120 }
121 $effect->setDuration($newDuration);
122 $entity->getEffects()->add($effect);
123 }else{
124 $effect->getType()->applyEffect($entity, $effect, $distanceMultiplier, $this);
125 }
126 }
127 }
128 }
129 }else{
130 //TODO: lingering potions
131 }
132 }elseif($event instanceof ProjectileHitBlockEvent && $this->getPotionType() === PotionType::WATER){
133 $blockIn = $event->getBlockHit()->getSide($event->getRayTraceResult()->getHitFace());
134
135 if($blockIn->hasTypeTag(BlockTypeTags::FIRE)){
136 $this->getWorld()->setBlock($blockIn->getPosition(), VanillaBlocks::AIR());
137 }
138 foreach($blockIn->getHorizontalSides() as $horizontalSide){
139 if($horizontalSide->hasTypeTag(BlockTypeTags::FIRE)){
140 $this->getWorld()->setBlock($horizontalSide->getPosition(), VanillaBlocks::AIR());
141 }
142 }
143 }
144 }
145
149 public function getPotionType() : PotionType{
150 return $this->potionType;
151 }
152
153 public function setPotionType(PotionType $type) : void{
154 $this->potionType = $type;
155 $this->networkPropertiesDirty = true;
156 }
157
161 public function willLinger() : bool{
162 return $this->linger;
163 }
164
168 public function setLinger(bool $value = true) : void{
169 $this->linger = $value;
170 $this->networkPropertiesDirty = true;
171 }
172
176 public function getPotionEffects() : array{
177 return $this->potionType->getEffects();
178 }
179
180 protected function syncNetworkData(EntityMetadataCollection $properties) : void{
181 parent::syncNetworkData($properties);
182
183 $properties->setShort(EntityMetadataProperties::POTION_AUX_VALUE, PotionTypeIdMap::getInstance()->toId($this->potionType));
184 $properties->setGenericFlag(EntityMetadataFlags::LINGER, $this->linger);
185 }
186}
setShort(string $name, int $value)