2017-04-06 14:45:14 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2017-08-21 22:06:39 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2012-04-05 04:56:21 +02:00
|
|
|
import getpass
|
|
|
|
import sys
|
2012-10-26 02:02:42 +02:00
|
|
|
from optparse import OptionParser
|
2014-10-08 19:12:37 +02:00
|
|
|
|
2015-04-01 17:43:16 +02:00
|
|
|
from minecraft import authentication
|
2015-04-01 23:38:10 +02:00
|
|
|
from minecraft.exceptions import YggdrasilError
|
2015-04-01 17:19:12 +02:00
|
|
|
from minecraft.networking.connection import Connection
|
2017-08-21 22:06:39 +02:00
|
|
|
from minecraft.networking.packets import Packet, clientbound, serverbound
|
2015-04-06 02:11:26 +02:00
|
|
|
from minecraft.compat import input
|
2012-04-30 19:05:06 +02:00
|
|
|
|
2014-10-08 19:12:37 +02:00
|
|
|
|
2015-04-01 23:38:10 +02:00
|
|
|
def get_options():
|
2012-10-26 02:02:42 +02:00
|
|
|
parser = OptionParser()
|
2012-12-25 21:38:02 +01:00
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
parser.add_option("-u", "--username", dest="username", default=None,
|
2014-10-08 19:12:37 +02:00
|
|
|
help="username to log in with")
|
2012-12-25 21:38:02 +01:00
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
parser.add_option("-p", "--password", dest="password", default=None,
|
2014-10-08 19:12:37 +02:00
|
|
|
help="password to log in with")
|
2012-12-25 21:38:02 +01:00
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
parser.add_option("-s", "--server", dest="server", default=None,
|
2014-10-08 19:12:37 +02:00
|
|
|
help="server to connect to")
|
2012-12-25 21:38:02 +01:00
|
|
|
|
2016-06-18 21:12:01 +02:00
|
|
|
parser.add_option("-o", "--offline", dest="offline", action="store_true",
|
|
|
|
help="connect to a server in offline mode")
|
|
|
|
|
2017-08-21 22:06:39 +02:00
|
|
|
parser.add_option("-d", "--dump-packets", dest="dump_packets",
|
|
|
|
action="store_true",
|
|
|
|
help="print sent and received packets to standard error")
|
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
(options, args) = parser.parse_args()
|
2012-12-25 21:38:02 +01:00
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
if not options.username:
|
2015-04-06 02:11:26 +02:00
|
|
|
options.username = input("Enter your username: ")
|
2012-12-31 03:20:06 +01:00
|
|
|
|
2016-06-18 21:12:01 +02:00
|
|
|
if not options.password and not options.offline:
|
2014-06-07 01:47:34 +02:00
|
|
|
options.password = getpass.getpass("Enter your password: ")
|
2012-12-25 21:38:02 +01:00
|
|
|
|
2014-06-07 01:47:34 +02:00
|
|
|
if not options.server:
|
2015-04-06 02:11:26 +02:00
|
|
|
options.server = input("Please enter server address"
|
|
|
|
" (including port): ")
|
2014-06-07 01:47:34 +02:00
|
|
|
# Try to split out port and address
|
|
|
|
if ':' in options.server:
|
|
|
|
server = options.server.split(":")
|
2015-04-01 23:38:10 +02:00
|
|
|
options.address = server[0]
|
|
|
|
options.port = int(server[1])
|
2012-10-26 15:09:16 +02:00
|
|
|
else:
|
2015-04-01 23:38:10 +02:00
|
|
|
options.address = options.server
|
|
|
|
options.port = 25565
|
|
|
|
|
|
|
|
return options
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
options = get_options()
|
|
|
|
|
2016-06-18 21:12:01 +02:00
|
|
|
if options.offline:
|
2017-09-24 06:51:28 +02:00
|
|
|
print("Connecting in offline mode...")
|
2016-06-18 21:12:01 +02:00
|
|
|
connection = Connection(
|
|
|
|
options.address, options.port, username=options.username)
|
|
|
|
else:
|
|
|
|
auth_token = authentication.AuthenticationToken()
|
|
|
|
try:
|
|
|
|
auth_token.authenticate(options.username, options.password)
|
|
|
|
except YggdrasilError as e:
|
|
|
|
print(e)
|
|
|
|
sys.exit()
|
2017-09-24 06:51:28 +02:00
|
|
|
print("Logged in as %s..." % auth_token.username)
|
2016-06-18 21:12:01 +02:00
|
|
|
connection = Connection(
|
|
|
|
options.address, options.port, auth_token=auth_token)
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2017-08-21 22:06:39 +02:00
|
|
|
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)
|
|
|
|
|
2017-09-24 06:51:28 +02:00
|
|
|
def handle_join_game(join_game_packet):
|
|
|
|
print('Connected.')
|
|
|
|
|
|
|
|
connection.register_packet_listener(
|
|
|
|
handle_join_game, clientbound.play.JoinGamePacket)
|
2014-06-07 01:47:34 +02:00
|
|
|
|
2015-03-22 15:05:55 +01:00
|
|
|
def print_chat(chat_packet):
|
2017-09-24 06:51:28 +02:00
|
|
|
print("Message (%s): %s" % (
|
|
|
|
chat_packet.field_string('position'), chat_packet.json_data))
|
2015-03-22 15:05:55 +01:00
|
|
|
|
2017-08-21 22:06:39 +02:00
|
|
|
connection.register_packet_listener(
|
|
|
|
print_chat, clientbound.play.ChatMessagePacket)
|
2017-09-24 06:51:28 +02:00
|
|
|
|
|
|
|
connection.connect()
|
|
|
|
|
2012-10-26 15:09:16 +02:00
|
|
|
while True:
|
|
|
|
try:
|
2015-04-06 02:11:26 +02:00
|
|
|
text = input()
|
2017-07-31 14:34:03 +02:00
|
|
|
if text == "/respawn":
|
|
|
|
print("respawning...")
|
2017-08-21 22:06:39 +02:00
|
|
|
packet = serverbound.play.ClientStatusPacket()
|
|
|
|
packet.action_id = serverbound.play.ClientStatusPacket.RESPAWN
|
2017-07-31 14:34:03 +02:00
|
|
|
connection.write_packet(packet)
|
|
|
|
else:
|
2017-08-21 22:06:39 +02:00
|
|
|
packet = serverbound.play.ChatPacket()
|
2017-07-31 14:34:03 +02:00
|
|
|
packet.message = text
|
|
|
|
connection.write_packet(packet)
|
2014-06-07 01:47:34 +02:00
|
|
|
except KeyboardInterrupt:
|
2015-04-01 23:38:10 +02:00
|
|
|
print("Bye!")
|
2014-06-07 01:47:34 +02:00
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2015-04-06 02:11:26 +02:00
|
|
|
main()
|