PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
EffectManager.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\effect;
25
29use function count;
30use function spl_object_id;
31
33
34 public function __construct(
35 private Living $entity
36 ){
37 parent::__construct();
38 }
39
43 public function remove(Effect $effectType) : void{
44 $index = spl_object_id($effectType);
45 if(isset($this->effects[$index])){
46 $effect = $this->effects[$index];
47 $ev = new EntityEffectRemoveEvent($this->entity, $effect);
48 $ev->call();
49 if($ev->isCancelled()){
50 foreach($this->effectAddHooks as $hook){
51 $hook($ev->getEffect(), true);
52 }
53 return;
54 }
55
56 $effect->getType()->remove($this->entity, $effect);
57 parent::remove($effectType);
58 }
59 }
60
61 public function add(EffectInstance $effect) : bool{
62 $index = spl_object_id($effect->getType());
63 $oldEffect = $this->effects[$index] ?? null;
64
65 $ev = new EntityEffectAddEvent($this->entity, $effect, $oldEffect);
66 if(!$this->canAdd($effect)){
67 $ev->cancel();
68 }
69
70 $ev->call();
71 if($ev->isCancelled()){
72 return false;
73 }
74
75 if($oldEffect !== null){
76 $oldEffect->getType()->remove($this->entity, $oldEffect);
77 }
78
79 $effect->getType()->add($this->entity, $effect);
80
81 return parent::add($effect);
82 }
83
84 public function tick(int $tickDiff = 1) : bool{
85 foreach($this->effects as $instance){
86 $type = $instance->getType();
87 if($type->canTick($instance)){
88 $type->applyEffect($this->entity, $instance);
89 }
90 $instance->decreaseDuration($tickDiff);
91 if($instance->hasExpired()){
92 $this->remove($instance->getType());
93 }
94 }
95
96 return count($this->effects) > 0;
97 }
98}