Create own listener

Please post any questions about developing your plugin here. Please use the search function before posting!
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Create own listener

Postby decompile » Mon Apr 17, 2017 2:01 pm

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
existenz
Senior Member
Posts: 111
Joined: Thu Feb 09, 2017 3:33 pm
Location: France
Contact:

Re: Create own listener

Postby existenz » Mon Apr 17, 2017 5:00 pm

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 :)
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Create own listener

Postby Ayuto » Tue Apr 18, 2017 10:12 am

Yep, that's exactly how you do it.
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: Create own listener

Postby decompile » Tue Apr 18, 2017 2:05 pm

Why do we need that __all__ or what is it function?
Predz
Senior Member
Posts: 158
Joined: Wed Aug 08, 2012 9:05 pm
Location: Bristol, United Kingdom

Re: Create own listener

Postby Predz » Tue Apr 18, 2017 2:18 pm

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__
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Create own listener

Postby Ayuto » Tue Apr 18, 2017 5:22 pm

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.

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 34 guests