PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
Event.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
27namespace pocketmine\event;
28
30use function count;
31use function get_class;
32
33abstract class Event{
34 private const MAX_EVENT_CALL_DEPTH = 50;
35
36 private static int $eventCallDepth = 1;
37
38 protected ?string $eventName = null;
39
40 final public function getEventName() : string{
41 return $this->eventName ?? get_class($this);
42 }
43
49 public function call() : void{
50 if(self::$eventCallDepth >= self::MAX_EVENT_CALL_DEPTH){
51 //this exception will be caught by the parent event call if all else fails
52 throw new \RuntimeException("Recursive event call detected (reached max depth of " . self::MAX_EVENT_CALL_DEPTH . " calls)");
53 }
54
55 $timings = Timings::getEventTimings($this);
56 $timings->startTiming();
57
58 $handlers = HandlerListManager::global()->getHandlersFor(static::class);
59
60 ++self::$eventCallDepth;
61 try{
62 foreach($handlers as $registration){
63 $registration->callEvent($this);
64 }
65 }finally{
66 --self::$eventCallDepth;
67 $timings->stopTiming();
68 }
69 }
70
77 public static function hasHandlers() : bool{
78 return count(HandlerListManager::global()->getHandlersFor(static::class)) > 0;
79 }
80}
static hasHandlers()
Definition: Event.php:77