pyCraft/start.py

79 lines
2.8 KiB
Python
Raw Normal View History

2012-04-05 04:56:21 +02:00
import getpass
import sys
import Utils
from networking import PacketSenderManager, NetworkManager
from optparse import OptionParser
2012-04-30 19:05:06 +02:00
2012-04-05 04:56:21 +02:00
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-u", "--username", dest="username", default="",
2012-11-27 16:00:39 +01:00
help="username to log in with")
parser.add_option("-p", "--password", dest="password", default="",
2012-11-27 16:00:39 +01:00
help="password to log in with")
parser.add_option("-s", "--server", dest="server", default="",
2012-11-27 16:00:39 +01:00
help="server to connect to")
parser.add_option("-d", "--dump-packets",
action="store_true", dest="dumpPackets", default=False,
help="run with this argument to dump packets")
parser.add_option("-o", "--out-file", dest="filename", default="dump.txt",
help="file to dump packets to")
2012-12-04 10:48:49 +01:00
parser.add_option("-x", "--offline-mode", dest="offlineMode",
action="store_true", default=False,
help="run in offline mode i.e don't attempt to auth via minecraft.net")
(options, args) = parser.parse_args()
if(options.username != ""):
user = options.username
else:
user = raw_input("Enter your username: ")
if(options.password != ""):
passwd = options.password
2012-12-04 10:48:49 +01:00
elif(not options.offlineMode):
passwd = getpass.getpass("Enter your password: ")
2012-12-04 10:48:49 +01:00
if (not options.offlineMode):
loginThread = Utils.MinecraftLoginThread(user, passwd)
loginThread.start()
loginThread.join()
loginResponse = loginThread.getResponse()
if(loginResponse['Response'] != "Good to go!"):
print loginResponse['Response']
sys.exit(1)
sessionid = loginResponse['SessionID']
user = loginResponse['Username']
print "Logged in as " + loginResponse['Username'] + "! Your session id is: " + sessionid
else:
sessionid = None
if(options.server != ""):
2012-12-04 10:48:49 +01:00
serverAddress = options.server
else:
2012-12-04 10:48:49 +01:00
serverAddress = raw_input("Enter host and port if any: ")
if ':' in serverAddress:
StuffEnteredIntoBox = serverAddress.split(":")
host = StuffEnteredIntoBox[0]
port = int(StuffEnteredIntoBox[1])
else:
2012-12-04 10:48:49 +01:00
host = serverAddress
port = 25565
2012-12-04 10:48:49 +01:00
connection = NetworkManager.ServerConnection(None, user, sessionid, host, port, options)
connection.start()
while True:
try:
chat_input = raw_input()
if (connection.isConnected):
PacketSenderManager.send03(connection.grabSocket(), chat_input)
else:
pass
except KeyboardInterrupt, e:
connection.disconnect()
sys.exit(1)