New automatic event listener plugin declaration

This commit is contained in:
Shoghi Cervantes 2014-03-31 17:01:56 +02:00
parent 00d777a68d
commit 0422540114
2 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,38 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace PocketMine\Plugin;
use PocketMine\Event\Event;
use PocketMine\Event\Listener;
class MethodEventExecutor implements EventExecutor{
private $method;
public function __construct($method){
$this->method = $method;
}
public function execute(Listener $listener, Event $event){
call_user_func(array($listener, $this->method), $event);
}
}

View File

@ -24,6 +24,7 @@ namespace PocketMine\Plugin;
use PocketMine\Command\PluginCommand;
use PocketMine\Command\SimpleCommandMap;
use PocketMine\Event\Event;
use PocketMine\Event\EventPriority;
use PocketMine\Event\HandlerList;
use PocketMine\Event\Listener;
use PocketMine\Permission\Permissible;
@ -601,6 +602,45 @@ class PluginManager{
}
}
/**
* Registers all the events in the given Listener class
*
* @param Listener $listener
* @param Plugin $plugin
*/
public function registerEvents(Listener $listener, Plugin $plugin){
if(!$plugin->isEnabled()){
trigger_error("Plugin attempted to register ".get_class($listener)." while not enabled", E_USER_WARNING);
return;
}
$reflection = new \ReflectionClass(get_class($listener));
foreach($reflection->getMethods() as $method){
if(!$method->isStatic()){
$priority = EventPriority::NORMAL;
$ignoreCancelled = false;
if(preg_match("/^[\t ]*\\* @priority ([a-zA-Z]{1,})$/m", (string) $method->getDocComment(), $matches) > 0){
$matches[1] = strtoupper($matches[1]);
if(defined("PocketMine\\Event\\EventPriority::".$matches[1])){
$priority = constant("PocketMine\\Event\\EventPriority::".$matches[1]);
}
}
if(preg_match("/^[\t ]*\\* @ignoreCancelled ([a-zA-Z]{1,})$/m", (string) $method->getDocComment(), $matches) > 0){
$matches[1] = strtolower($matches[1]);
if($matches[1] === "false"){
$ignoreCancelled = false;
}elseif($matches[1] === "true"){
$ignoreCancelled = true;
}
}
$parameters = $method->getParameters();
if(count($parameters) === 1 and $parameters[0]->getClass() instanceof \ReflectionClass and is_subclass_of($parameters[0]->getClass()->getName(), "PocketMine\\Event\\Event")){
$this->registerEvent($parameters[0]->getClass()->getName(), $listener, $priority, new MethodEventExecutor($method->getName()), $plugin, $ignoreCancelled);
}
}
}
}
/**
* @param string $event Class name that extends Event
* @param Listener $listener