pyCraft/start.py

130 lines
4.4 KiB
Python
Raw Normal View History

#!/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
import re
from optparse import OptionParser
2014-10-08 19:12:37 +02:00
from minecraft import authentication
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
def get_options():
parser = OptionParser()
2012-12-25 21:38:02 +01: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
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
parser.add_option("-s", "--server", dest="server", default=None,
help="server host or host:port "
"(enclose IPv6 addresses in square brackets)")
2012-12-25 21:38:02 +01:00
parser.add_option("-o", "--offline", dest="offline", action="store_true",
help="connect to a server in offline mode "
"(no password required)")
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")
(options, args) = parser.parse_args()
2012-12-25 21:38:02 +01:00
if not options.username:
2015-04-06 02:11:26 +02:00
options.username = input("Enter your username: ")
if not options.password and not options.offline:
options.password = getpass.getpass("Enter your password (leave "
"blank for offline mode): ")
options.offline = options.offline or (options.password == "")
2012-12-25 21:38:02 +01:00
if not options.server:
options.server = input("Enter server host or host:port "
"(enclose IPv6 addresses in square brackets): ")
# Try to split out port and address
match = re.match(r"((?P<host>[^\[\]:]+)|\[(?P<addr>[^\[\]]+)\])"
r"(:(?P<port>\d+))?$", options.server)
if match is None:
raise ValueError("Invalid server address: '%s'." % options.server)
options.address = match.group("host") or match.group("addr")
options.port = int(match.group("port") or 25565)
return options
def main():
options = get_options()
if options.offline:
2017-09-24 06:51:28 +02:00
print("Connecting in offline mode...")
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)
connection = Connection(
options.address, options.port, auth_token=auth_token)
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)
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))
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()
while True:
try:
2015-04-06 02:11:26 +02:00
text = input()
if text == "/respawn":
print("respawning...")
2017-08-21 22:06:39 +02:00
packet = serverbound.play.ClientStatusPacket()
packet.action_id = serverbound.play.ClientStatusPacket.RESPAWN
connection.write_packet(packet)
else:
2017-08-21 22:06:39 +02:00
packet = serverbound.play.ChatPacket()
packet.message = text
connection.write_packet(packet)
except KeyboardInterrupt:
print("Bye!")
sys.exit()
if __name__ == "__main__":
2015-04-06 02:11:26 +02:00
main()