From 6d6a592f070d5779bdde345f52252d0da6a41a93 Mon Sep 17 00:00:00 2001 From: Zachy Date: Mon, 13 Aug 2018 01:57:16 +0100 Subject: [PATCH 1/3] Add decorator for register_packet_listener() --- minecraft/networking/connection.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/minecraft/networking/connection.py b/minecraft/networking/connection.py index 4a40da7..e1b75db 100644 --- a/minecraft/networking/connection.py +++ b/minecraft/networking/connection.py @@ -187,6 +187,15 @@ class Connection(object): else: self._outgoing_packet_queue.append(packet) + def listener(self, *packet_types, **kwds): + """ + Shorthand decorator to register a function as a packet listener. + """ + def _method_func(method): + self.register_packet_listener(method, *packet_types, **kwds) + + return _method_func + def register_packet_listener(self, method, *packet_types, **kwds): """ Registers a listener method which will be notified when a packet of From 409c619eb0f4a210a45370f17e23fb011c39a451 Mon Sep 17 00:00:00 2001 From: Zachy Date: Wed, 15 Aug 2018 20:53:13 +0100 Subject: [PATCH 2/3] return method --- minecraft/networking/connection.py | 1 + 1 file changed, 1 insertion(+) diff --git a/minecraft/networking/connection.py b/minecraft/networking/connection.py index e1b75db..b026e83 100644 --- a/minecraft/networking/connection.py +++ b/minecraft/networking/connection.py @@ -193,6 +193,7 @@ class Connection(object): """ def _method_func(method): self.register_packet_listener(method, *packet_types, **kwds) + return method return _method_func From 41ea36c642aaa8161d9be706a9534be9f0ed2e69 Mon Sep 17 00:00:00 2001 From: joodicator Date: Mon, 13 May 2019 19:04:35 +0200 Subject: [PATCH 3/3] Add test coverage for @listener. --- tests/test_connection.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/test_connection.py b/tests/test_connection.py index d5375c8..42ce099 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -224,26 +224,23 @@ class EarlyPacketListenerTest(ConnectTest): the early packet listener is registered afterwards. """ def _start_client(self, client): + @client.listener(clientbound.play.JoinGamePacket) def handle_join(packet): assert early_handle_join.called, \ 'Ordinary listener called before early listener.' handle_join.called = True handle_join.called = False - client.register_packet_listener( - handle_join, clientbound.play.JoinGamePacket) + @client.listener(clientbound.play.JoinGamePacket, early=True) def early_handle_join(packet): early_handle_join.called = True - client.register_packet_listener( - early_handle_join, clientbound.play.JoinGamePacket, early=True) early_handle_join.called = False + @client.listener(clientbound.play.DisconnectPacket) def handle_disconnect(packet): assert early_handle_join.called, 'Early listener not called.' assert handle_join.called, 'Ordinary listener not called.' raise fake_server.FakeServerTestSuccess - client.register_packet_listener( - handle_disconnect, clientbound.play.DisconnectPacket) client.connect()