Let's make PocketMine event listeners powerful!
Note
An example plugin is in the example folder of root project.
$awaitStd = AwaitStd::init($this);
ListenerExtended::create()
->cancelOnFalse() // if an event handler declares return type as boolean and returns false, cancel it
->awaitGenerator() // if an event handler declares return type as Generator type, execute it with AwaitGenerator
// Contexts
->awaitContext($awaitStd) // pass only for await-generator handlers
->context($this) // pass to every handler, pass after awaitContext to await-generator handlers
->registerEvents($this, new PlayerListener());
Wrap your generator using events directly!
awaitGenerator(array|\Closure $catchers = []) // if an event handler returns Generator type, execute it in AwaitGenerator. Pass catchers for error handling.
awaitContext(mixed ...$value) // pass only for await-generator handlers
If you configure with above API, below event handler will work accurately.
public function onJoin(PlayerJoinEvent $event, AwaitStd $awaitStd): \Generator{
// start
// wait 5 seconds
yield from $awaitStd->sleep(20 * 5); // some work
// do some work
}
Pass context or some API directly to event handler parameters.
context(mixed ...$value)
Examples:
ListenerExtended::create()
// wrong
// ->context($counter = 1) // do not pass value directly, use objects instead
// correct
// ->context((new \stdClass)->counter = 1)
// you can pass more Contexts
->context(Server::getInstance(), $someApi, $databaseMaybe)
If a event handler return type is boolean
and return false, event will cancelled.
ListenerExtended::create()
->cancelOnFalse()
// ^^^^^^^^^^^^^^^^^ enable first
Example:
public function onChat(PlayerChatEvent $event, Main $main): bool{
if ($main->isChatDisabled()) {
return false; // will cancel event automaticly
}
return true; // do nothing
}
Open issue please!
Get 200$ credit on the best cloud service!
- DatabaseExecutor is a virion library for executing SQL queries asynchronously with Laravel models and builders.