Skip to content

xerenahmed/ListenerExtended

Repository files navigation

ListenerExtended

Let's make PocketMine event listeners powerful!

Note

An example plugin is in the example folder of root project.

Table Of Contents

Quick Look

$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());

Features

AwaitGenerator

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
}

Contexts

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)

Cancel On False Return

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
}

Do you have any idea?

Open issue please!

Digital Ocean

Get 200$ credit on the best cloud service!

DigitalOcean Referral Badge

Another Projects You Might Like

  • DatabaseExecutor is a virion library for executing SQL queries asynchronously with Laravel models and builders.