mirror of
https://github.com/ammaraskar/pyCraft.git
synced 2024-11-21 09:45:28 +01:00
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:
parent
ff9a0813b6
commit
2947aa6619
@ -39,7 +39,26 @@ multiple protocol versions.
|
||||
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
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
.. autoclass:: minecraft.networking.packets.clientbound.play.ChatMessagePacket
|
||||
:undoc-members:
|
||||
:inherited-members:
|
||||
|
@ -105,7 +105,7 @@ class Connection(object):
|
||||
""" # NOQA
|
||||
|
||||
# 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.networking_thread = None
|
||||
@ -195,6 +195,10 @@ class Connection(object):
|
||||
def listener(self, *packet_types, **kwds):
|
||||
"""
|
||||
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):
|
||||
self.register_packet_listener(handler_func, *packet_types, **kwds)
|
||||
|
Loading…
Reference in New Issue
Block a user