PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
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 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(!$this->willLinger()){
100 if($hasEffects){
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 }
130 }else{
131 $entity = new AreaEffectCloud(Location::fromObject($this->location->floor()->add(0.5, 0.5, 0.5), $this->getWorld()));
132 foreach($this->potionType->getEffects() as $effect){
133 $entity->getEffects()->add($effect);
134 }
135 if(($owner = $this->getOwningEntity()) !== null && !$owner->isClosed()){
136 $entity->setOwningEntity($owner);
137 }
138 $entity->spawnToAll();
139 }
140 if(!$hasEffects && $event instanceof ProjectileHitBlockEvent && $this->getPotionType() === PotionType::WATER){
141 $blockIn = $event->getBlockHit()->getSide($event->getRayTraceResult()->getHitFace());
142
143 if($blockIn->hasTypeTag(BlockTypeTags::FIRE)){
144 $this->getWorld()->setBlock($blockIn->getPosition(), VanillaBlocks::AIR());
145 }
146 foreach($blockIn->getHorizontalSides() as $horizontalSide){
147 if($horizontalSide->hasTypeTag(BlockTypeTags::FIRE)){
148 $this->getWorld()->setBlock($horizontalSide->getPosition(), VanillaBlocks::AIR());
149 }
150 }
151 }
152 }
153
157 public function getPotionType() : PotionType{
158 return $this->potionType;
159 }
160
161 public function setPotionType(PotionType $type) : void{
162 $this->potionType = $type;
163 $this->networkPropertiesDirty = true;
164 }
165
169 public function willLinger() : bool{
170 return $this->linger;
171 }
172
176 public function setLinger(bool $value = true) : void{
177 $this->linger = $value;
178 $this->networkPropertiesDirty = true;
179 }
180
184 public function getPotionEffects() : array{
185 return $this->potionType->getEffects();
186 }
187
188 protected function syncNetworkData(EntityMetadataCollection $properties) : void{
189 parent::syncNetworkData($properties);
190
191 $properties->setShort(EntityMetadataProperties::POTION_AUX_VALUE, PotionTypeIdMap::getInstance()->toId($this->potionType));
192 $properties->setGenericFlag(EntityMetadataFlags::LINGER, $this->linger);
193 }
194}
setShort(string $name, int $value)