Exemplo n.º 1
0
 /**
  * Registers all the events in the given Listener class
  *
  * @param Listener $listener
  * @param Plugin   $plugin
  *
  * @throws PluginException
  */
 public function registerEvents(Listener $listener, Plugin $plugin)
 {
     if (!$plugin->isEnabled()) {
         throw new PluginException("Plugin attempted to register " . get_class($listener) . " while not enabled");
     }
     $reflection = new \ReflectionClass(get_class($listener));
     foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
         if (!$method->isStatic()) {
             $priority = EventPriority::NORMAL;
             $ignoreCancelled = false;
             if (preg_match("/^[\t ]*\\* @priority[\t ]{1,}([a-zA-Z]{1,})/m", (string) $method->getDocComment(), $matches) > 0) {
                 $matches[1] = strtoupper($matches[1]);
                 if (defined(EventPriority::class . "::" . $matches[1])) {
                     $priority = constant(EventPriority::class . "::" . $matches[1]);
                 }
             }
             if (preg_match("/^[\t ]*\\* @ignoreCancelled[\t ]{1,}([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(), Event::class)) {
                 $class = $parameters[0]->getClass()->getName();
                 $reflection = new \ReflectionClass($class);
                 if (strpos((string) $reflection->getDocComment(), "@deprecated") !== false and $this->server->getProperty("settings.deprecated-verbose", true)) {
                     $this->server->getLogger()->warning($this->server->getLanguage()->translateString("BukkitPE.plugin.deprecatedEvent", [$plugin->getName(), $class, get_class($listener) . "->" . $method->getName() . "()"]));
                 }
                 $this->registerEvent($class, $listener, $priority, new MethodEventExecutor($method->getName()), $plugin, $ignoreCancelled);
             }
         }
     }
 }