diff --git a/examples/ListenExample/plugin.yml b/examples/ListenExample/plugin.yml index d31b5f1..c3c818e 100644 --- a/examples/ListenExample/plugin.yml +++ b/examples/ListenExample/plugin.yml @@ -1,7 +1,7 @@ name: ListenExample main: FlowyExamples\ListenExample\Main version: 0.1.0 -api: [3.12.0] +api: [4.0.0] author: WhiteGrouse virions: diff --git a/examples/ListenExample/src/FlowyExamples/ListenExample/Main.php b/examples/ListenExample/src/FlowyExamples/ListenExample/Main.php index e40616c..1245485 100644 --- a/examples/ListenExample/src/FlowyExamples/ListenExample/Main.php +++ b/examples/ListenExample/src/FlowyExamples/ListenExample/Main.php @@ -8,7 +8,7 @@ class Main extends PluginBase { - function onEnable() + protected function onEnable(): void { Flowy::bootstrap(); $stream = start($this); diff --git a/flowy/src/flowy/EventHelper.php b/flowy/src/flowy/EventHelper.php index 4696d10..412d451 100755 --- a/flowy/src/flowy/EventHelper.php +++ b/flowy/src/flowy/EventHelper.php @@ -2,14 +2,10 @@ namespace flowy; use pocketmine\event\Event; -use pocketmine\event\HandlerList; -use pocketmine\event\Listener; -use pocketmine\plugin\EventExecutor; -use pocketmine\plugin\MethodEventExecutor; +use pocketmine\event\HandlerListManager; use pocketmine\plugin\Plugin; use pocketmine\plugin\PluginException; -use pocketmine\plugin\RegisteredListener; -use pocketmine\Server; +use pocketmine\event\RegisteredListener; use pocketmine\timings\TimingsHandler; use pocketmine\utils\Utils; @@ -21,50 +17,31 @@ private function __construct() public static function register( string $event, - Listener $listener, + \Closure $handler, int $priority, - EventExecutor $executor, Plugin $plugin, - bool $ignoreCancelled = false + bool $handleCancelled = false ): RegisteredListener { - if (!is_subclass_of($event, Event::class)) { - throw new PluginException("{$event} is not an Event"); + if(!is_subclass_of($event, Event::class)){ + throw new PluginException($event . " is not an Event"); } - $server = Server::getInstance(); + $handlerName = Utils::getNiceClosureName($handler); - $tags = Utils::parseDocComment((string)(new \ReflectionClass($event))->getDocComment()); - if (isset($tags["deprecated"]) and $server->getProperty("settings.deprecated-verbose", true)) { - $server->getLogger()->warning($server->getLanguage()->translateString("pocketmine.plugin.deprecatedEvent", [ - $plugin->getName(), - $event, - get_class($listener) . "->" . ($executor instanceof MethodEventExecutor ? $executor->getMethod() : "") - ])); + if(!$plugin->isEnabled()){ + throw new PluginException("Plugin attempted to register event handler " . $handlerName . "() to event " . $event . " while not enabled"); } - if (!$plugin->isEnabled()) { - throw new PluginException("Plugin attempted to register {$event} while not enabled"); - } + $timings = new TimingsHandler("Plugin: " . $plugin->getDescription()->getFullName() . " Event: " . $handlerName . "(" . (new \ReflectionClass($event))->getShortName() . ")"); - $timings = new TimingsHandler("Plugin: " . $plugin->getDescription()->getFullName() . " Event: " . get_class($listener) . "::" . ($executor instanceof MethodEventExecutor ? $executor->getMethod() : "???") . "(" . (new \ReflectionClass($event))->getShortName() . ")"); - $registeredListener = new RegisteredListener($listener, $executor, $priority, $plugin, $ignoreCancelled, - $timings); - self::getEventListeners($event)->register($registeredListener); + $registeredListener = new RegisteredListener($handler, $priority, $plugin, $handleCancelled, $timings); + HandlerListManager::global()->getListFor($event)->register($registeredListener); return $registeredListener; } - private static function getEventListeners(string $event): HandlerList - { - $list = HandlerList::getHandlerListFor($event); - if ($list === null) { - throw new PluginException("Abstract events not declaring @allowHandle cannot be handled (tried to register listener for {$event})"); - } - return $list; - } - public static function unregister(string $event, RegisteredListener $listener): void { - self::getEventListeners($event)->unregister($listener); + HandlerListManager::global()->getListFor($event)->unregister($listener); } } \ No newline at end of file diff --git a/flowy/src/flowy/EventListener.php b/flowy/src/flowy/EventListener.php index 0454640..829be4f 100755 --- a/flowy/src/flowy/EventListener.php +++ b/flowy/src/flowy/EventListener.php @@ -1,12 +1,10 @@ handler = $handler; } - public function onEvent(Event $event): void - { - $this->handler->handle($event); - } - public function listen(string $event): void { if (isset($this->registeredListeners[$event])) { @@ -44,9 +37,10 @@ public function listen(string $event): void $this->registeredListeners[$event] = EventHelper::register( $event, - $this, + function($event) { + $this->handler->handle($event); + }, EventPriority::NORMAL, - new MethodEventExecutor("onEvent"), $this->plugin ); } diff --git a/flowy/virion.yml b/flowy/virion.yml index 2887e71..6f7ba35 100644 --- a/flowy/virion.yml +++ b/flowy/virion.yml @@ -2,4 +2,4 @@ name: Flowy author: WhiteGrouse antigen: flowy version: 0.1.0 -api: [3.12.0] \ No newline at end of file +api: [4.0.0] \ No newline at end of file diff --git a/standard/src/flowy/standard/Delay.php b/standard/src/flowy/standard/Delay.php index 48009f2..3972119 100644 --- a/standard/src/flowy/standard/Delay.php +++ b/standard/src/flowy/standard/Delay.php @@ -9,6 +9,6 @@ function delay(TaskScheduler $scheduler, int $tick) { $handler = $scheduler->scheduleDelayedTask(new DelayTask(), $tick); yield listen(DelayCallbackEvent::class)->filter(function($ev) use ($handler) { - return $ev->getTaskId() === $handler->getTaskId(); + return $ev->getTask() === $handler->getTask(); }); } diff --git a/standard/src/flowy/standard/delay/DelayCallbackEvent.php b/standard/src/flowy/standard/delay/DelayCallbackEvent.php index 9839e1f..2fd0c66 100644 --- a/standard/src/flowy/standard/delay/DelayCallbackEvent.php +++ b/standard/src/flowy/standard/delay/DelayCallbackEvent.php @@ -2,15 +2,16 @@ namespace flowy\standard\delay; use pocketmine\event\Event; +use pocketmine\scheduler\Task; class DelayCallbackEvent extends Event { - protected $taskId; + protected $task; - public function __construct(int $taskId) { - $this->taskId = $taskId; + public function __construct(Task $task) { + $this->task = $task; } - public function getTaskId(): int { - return $this->taskId; + public function getTask(): Task { + return $this->task; } } diff --git a/standard/src/flowy/standard/delay/DelayTask.php b/standard/src/flowy/standard/delay/DelayTask.php index e25021f..ec4b0bb 100644 --- a/standard/src/flowy/standard/delay/DelayTask.php +++ b/standard/src/flowy/standard/delay/DelayTask.php @@ -4,7 +4,7 @@ use pocketmine\scheduler\Task; class DelayTask extends Task { - public function onRun($currentTick) { - (new DelayCallbackEvent($this->getTaskId()))->call(); + public function onRun(): void { + (new DelayCallbackEvent($this))->call(); } } diff --git a/standard/virion.yml b/standard/virion.yml index 6d0f74c..19fe826 100644 --- a/standard/virion.yml +++ b/standard/virion.yml @@ -2,4 +2,4 @@ name: Standard author: WhiteGrouse antigen: flowy\standard version: 0.1.0 -api: [3.12.0] +api: [4.0.0]