Page 1 of 1

Create own listener

Posted: Mon Apr 17, 2017 2:01 pm
by decompile
Hey,

Im currently writing a quick afk manager api and I thought it would be good to add a "On Player AFK" listener and reverse.

Couldnt understand it from wikis

Re: Create own listener

Posted: Mon Apr 17, 2017 5:00 pm
by existenz
Hey !

I have one solution but i don't know if it's the best xD.

Syntax: Select all

## listeners.py

## IMPORTS

from listeners import ListenerManager
from listeners import ListenerManagerDecorator

## ALL DECLARATION

__all__ = (
'OnPlayerAfk',
)

class OnPlayerAfk(ListenerManagerDecorator):
manager = ListenerManager()


Syntax: Select all

from .listeners import OnPlayerAfk

# In your fonction which check afk
def _on_afk_check():
OnPlayerAfk.manager.notify(player=player)

@OnPlayerAfk
def _on_player_afk(player):
# Do something


If i m not wrong :)

Re: Create own listener

Posted: Tue Apr 18, 2017 10:12 am
by Ayuto
Yep, that's exactly how you do it.

Re: Create own listener

Posted: Tue Apr 18, 2017 2:05 pm
by decompile
Why do we need that __all__ or what is it function?

Re: Create own listener

Posted: Tue Apr 18, 2017 2:18 pm
by Predz
The __all__ attribute allows python to know what to define when trying to import from a module.

Doing:

Syntax: Select all

from <module> import *

Would import all things from the <module> but only define values which are named from inside the iterable.

You can still do:

Syntax: Select all

from <module> import <object>

The * is just a way of checking what is inside __all__

Re: Create own listener

Posted: Tue Apr 18, 2017 5:22 pm
by Ayuto
To clarify it: you don't need it, but it's commonly used when creating modules to prevent cluttering your namespace when you import everything with the asterisk.