start.py: add --dump-packets option.

This commit is contained in:
joo committed 2017-08-21 21:06:39 +01:00
1 parent 593c98f168
commit f1d04e6610
1 file changed
+28 -8
+28 -8
View File
@@ -1,5 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import getpass import getpass
import sys import sys
from optparse import OptionParser from optparse import OptionParser
@@ -7,10 +9,7 @@ from optparse import OptionParser
from minecraft import authentication from minecraft import authentication
from minecraft.exceptions import YggdrasilError from minecraft.exceptions import YggdrasilError
from minecraft.networking.connection import Connection from minecraft.networking.connection import Connection
from minecraft.networking.packets import ( from minecraft.networking.packets import Packet, clientbound, serverbound
ChatMessagePacket, ChatPacket
)
from minecraft.networking.packets.serverbound.play import ClientStatusPacket
from minecraft.compat import input from minecraft.compat import input
@@ -29,6 +28,10 @@ def get_options():
parser.add_option("-o", "--offline", dest="offline", action="store_true", parser.add_option("-o", "--offline", dest="offline", action="store_true",
help="connect to a server in offline mode") help="connect to a server in offline mode")
parser.add_option("-d", "--dump-packets", dest="dump_packets",
action="store_true",
help="print sent and received packets to standard error")
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
if not options.username: if not options.username:
@@ -70,23 +73,40 @@ def main():
connection = Connection( connection = Connection(
options.address, options.port, auth_token=auth_token) options.address, options.port, auth_token=auth_token)
if options.dump_packets:
def print_incoming(packet):
if type(packet) is Packet:
# This is a direct instance of the base Packet type, meaning
# that it is a packet of unknown type, so we do not print it.
return
print('--> %s' % packet, file=sys.stderr)
def print_outgoing(packet):
print('<-- %s' % packet, file=sys.stderr)
connection.register_packet_listener(
print_incoming, Packet, early=True)
connection.register_packet_listener(
print_outgoing, Packet, outgoing=True)
connection.connect() connection.connect()
def print_chat(chat_packet): def print_chat(chat_packet):
print("Position: " + str(chat_packet.position)) print("Position: " + str(chat_packet.position))
print("Data: " + chat_packet.json_data) print("Data: " + chat_packet.json_data)
connection.register_packet_listener(print_chat, ChatMessagePacket) connection.register_packet_listener(
print_chat, clientbound.play.ChatMessagePacket)
while True: while True:
try: try:
text = input() text = input()
if text == "/respawn": if text == "/respawn":
print("respawning...") print("respawning...")
packet = ClientStatusPacket() packet = serverbound.play.ClientStatusPacket()
packet.action_id = ClientStatusPacket.RESPAWN packet.action_id = serverbound.play.ClientStatusPacket.RESPAWN
connection.write_packet(packet) connection.write_packet(packet)
else: else:
packet = ChatPacket() packet = serverbound.play.ChatPacket()
packet.message = text packet.message = text
connection.write_packet(packet) connection.write_packet(packet)
except KeyboardInterrupt: except KeyboardInterrupt: