3 from optparse
import OptionParser
5 from minecraft
import authentication
6 from minecraft
.exceptions
import YggdrasilError
7 from minecraft
.networking
.connection
import Connection
8 from minecraft
.networking
.packets
import ChatMessagePacket
, ChatPacket
9 from minecraft
.compat
import input
13 parser
= OptionParser()
15 parser
.add_option("-u", "--username", dest
="username", default
=None,
16 help="username to log in with")
18 parser
.add_option("-p", "--password", dest
="password", default
=None,
19 help="password to log in with")
21 parser
.add_option("-s", "--server", dest
="server", default
=None,
22 help="server to connect to")
24 (options
, args
) = parser
.parse_args()
26 if not options
.username
:
27 options
.username
= input("Enter your username: ")
29 if not options
.password
:
30 options
.password
= getpass
.getpass("Enter your password: ")
32 if not options
.server
:
33 options
.server
= input("Please enter server address"
34 " (including port): ")
35 # Try to split out port and address
36 if ':' in options
.server
:
37 server
= options
.server
.split(":")
38 options
.address
= server
[0]
39 options
.port
= int(server
[1])
41 options
.address
= options
.server
48 options
= get_options()
50 auth_token
= authentication
.AuthenticationToken()
52 auth_token
.authenticate(options
.username
, options
.password
)
53 except YggdrasilError
as e
:
57 print("Logged in as " + auth_token
.username
)
59 connection
= Connection(options
.address
, options
.port
, auth_token
)
62 def print_chat(chat_packet
):
63 print("Position: " + str(chat_packet
.position
))
64 print("Data: " + chat_packet
.json_data
)
66 connection
.register_packet_listener(print_chat
, ChatMessagePacket
)
72 connection
.write_packet(packet
)
73 except KeyboardInterrupt:
78 if __name__
== "__main__":