mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-12 12:55:21 +00:00
Move event calling functionality to Event->call() method
This is dependent on the changes made in b1e0f82cbf2f585ed729245a6883d713effd1793. This now makes it possible to call events without fetching a Server reference, allowing to eliminate a vast array of Server dependencies.
This commit is contained in:
parent
b1e0f82cbf
commit
6efef3bbc7
@ -27,6 +27,9 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\event;
|
namespace pocketmine\event;
|
||||||
|
|
||||||
abstract class Event{
|
abstract class Event{
|
||||||
|
private const MAX_EVENT_CALL_DEPTH = 50;
|
||||||
|
/** @var int */
|
||||||
|
private static $eventCallDepth = 1;
|
||||||
|
|
||||||
/** @var string|null */
|
/** @var string|null */
|
||||||
protected $eventName = null;
|
protected $eventName = null;
|
||||||
@ -67,4 +70,38 @@ abstract class Event{
|
|||||||
/** @var Event $this */
|
/** @var Event $this */
|
||||||
$this->isCancelled = $value;
|
$this->isCancelled = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calls event handlers registered for this event.
|
||||||
|
*
|
||||||
|
* @throws \RuntimeException if event call recursion reaches the max depth limit
|
||||||
|
*
|
||||||
|
* @throws \ReflectionException
|
||||||
|
*/
|
||||||
|
public function call() : void{
|
||||||
|
if(self::$eventCallDepth >= self::MAX_EVENT_CALL_DEPTH){
|
||||||
|
//this exception will be caught by the parent event call if all else fails
|
||||||
|
throw new \RuntimeException("Recursive event call detected (reached max depth of " . self::MAX_EVENT_CALL_DEPTH . " calls)");
|
||||||
|
}
|
||||||
|
|
||||||
|
$handlerList = HandlerList::getHandlerListFor(get_class($this));
|
||||||
|
assert($handlerList !== null, "Called event should have a valid HandlerList");
|
||||||
|
|
||||||
|
++self::$eventCallDepth;
|
||||||
|
foreach(EventPriority::ALL as $priority){
|
||||||
|
$currentList = $handlerList;
|
||||||
|
while($currentList !== null){
|
||||||
|
foreach($currentList->getListenersByPriority($priority) as $registration){
|
||||||
|
if(!$registration->getPlugin()->isEnabled()){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$registration->callEvent($this);
|
||||||
|
}
|
||||||
|
|
||||||
|
$currentList = $currentList->getParent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
--self::$eventCallDepth;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,6 @@ use pocketmine\utils\Utils;
|
|||||||
* Manages all the plugins
|
* Manages all the plugins
|
||||||
*/
|
*/
|
||||||
class PluginManager{
|
class PluginManager{
|
||||||
private const MAX_EVENT_CALL_DEPTH = 50;
|
|
||||||
|
|
||||||
/** @var Server */
|
/** @var Server */
|
||||||
private $server;
|
private $server;
|
||||||
@ -66,9 +65,6 @@ class PluginManager{
|
|||||||
*/
|
*/
|
||||||
protected $fileAssociations = [];
|
protected $fileAssociations = [];
|
||||||
|
|
||||||
/** @var int */
|
|
||||||
private $eventCallDepth = 0;
|
|
||||||
|
|
||||||
/** @var string|null */
|
/** @var string|null */
|
||||||
private $pluginDataDirectory;
|
private $pluginDataDirectory;
|
||||||
|
|
||||||
@ -678,33 +674,13 @@ class PluginManager{
|
|||||||
/**
|
/**
|
||||||
* Calls an event
|
* Calls an event
|
||||||
*
|
*
|
||||||
|
* @deprecated
|
||||||
|
* @see Event::call()
|
||||||
|
*
|
||||||
* @param Event $event
|
* @param Event $event
|
||||||
*/
|
*/
|
||||||
public function callEvent(Event $event){
|
public function callEvent(Event $event){
|
||||||
if($this->eventCallDepth >= self::MAX_EVENT_CALL_DEPTH){
|
$event->call();
|
||||||
//this exception will be caught by the parent event call if all else fails
|
|
||||||
throw new \RuntimeException("Recursive event call detected (reached max depth of " . self::MAX_EVENT_CALL_DEPTH . " calls)");
|
|
||||||
}
|
|
||||||
|
|
||||||
$handlerList = HandlerList::getHandlerListFor(get_class($event));
|
|
||||||
assert($handlerList !== null, "Called event should have a valid HandlerList");
|
|
||||||
|
|
||||||
++$this->eventCallDepth;
|
|
||||||
foreach(EventPriority::ALL as $priority){
|
|
||||||
$currentList = $handlerList;
|
|
||||||
while($currentList !== null){
|
|
||||||
foreach($currentList->getListenersByPriority($priority) as $registration){
|
|
||||||
if(!$registration->getPlugin()->isEnabled()){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$registration->callEvent($event);
|
|
||||||
}
|
|
||||||
|
|
||||||
$currentList = $currentList->getParent();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
--$this->eventCallDepth;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user