Added listener decorator documentation (#161)

* added decorator example to connection.rst

since decorators are more pythonic, it was put in front of the register method.

* Expanded listener decorator docstring

* changed autofunction to autodecorator

* removed whitespace in empty line

* remvoed trailing whitespace

i didn't even edit there WTF
This commit is contained in:
laundmo 2020-08-17 17:49:25 +02:00 committed by GitHub
parent ff9a0813b6
commit 2947aa6619
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 2 deletions

View File

@ -39,7 +39,26 @@ multiple protocol versions.
Listening for Certain Packets Listening for Certain Packets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Let's look at how to listen for certain packets, the relevant method being Let's look at how to listen for certain packets, the relevant decorator being
A decorator can be used to register a packet listener:
.. autodecorator:: Connection.listener
Example usage::
connection = Connection(options.address, options.port, auth_token=auth_token)
connection.connect()
from minecraft.networking.packets.clientbound.play import ChatMessagePacket
@connection.listener(ChatMessagePacket)
def print_chat(chat_packet):
print "Position: " + str(chat_packet.position)
print "Data: " + chat_packet.json_data
Altenatively, packet listeners can also be registered seperate from the function definition.
.. automethod:: Connection.register_packet_listener .. automethod:: Connection.register_packet_listener
@ -57,6 +76,7 @@ An example of this can be found in the ``start.py`` headless client, it is recre
The field names ``position`` and ``json_data`` are inferred by again looking at the definition attribute as before The field names ``position`` and ``json_data`` are inferred by again looking at the definition attribute as before
.. autoclass:: minecraft.networking.packets.clientbound.play.ChatMessagePacket .. autoclass:: minecraft.networking.packets.clientbound.play.ChatMessagePacket
:undoc-members: :undoc-members:
:inherited-members: :inherited-members:

View File

@ -105,7 +105,7 @@ class Connection(object):
""" # NOQA """ # NOQA
# This lock is re-entrant because it may be acquired in a re-entrant # This lock is re-entrant because it may be acquired in a re-entrant
# manner from within an outgoing packet listener # manner from within an outgoing packet
self._write_lock = RLock() self._write_lock = RLock()
self.networking_thread = None self.networking_thread = None
@ -195,6 +195,10 @@ class Connection(object):
def listener(self, *packet_types, **kwds): def listener(self, *packet_types, **kwds):
""" """
Shorthand decorator to register a function as a packet listener. Shorthand decorator to register a function as a packet listener.
Wraps :meth:`minecraft.networking.connection.register_packet_listener`
:param packet_types: Packet types to listen for.
:param kwds: Keyword arguments for `register_packet_listener`
""" """
def listener_decorator(handler_func): def listener_decorator(handler_func):
self.register_packet_listener(handler_func, *packet_types, **kwds) self.register_packet_listener(handler_func, *packet_types, **kwds)