-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
97 lines (74 loc) · 2.89 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
from __future__ import print_function
import getpass
import sys
import re
import signal
from minecraft import authentication
from minecraft.exceptions import YggdrasilError
from minecraft.networking.connection import Connection
from minecraft.networking.packets import Packet, clientbound, serverbound
from minecraft.compat import input
def get_options():
options = {}
options["username"] = input("Enter your username: ")
options["password"] = getpass.getpass("Enter your password: ")
return options
def main():
auth_token = authentication.AuthenticationToken()
try:
with open('minecraft.auth', 'r') as f:
auth_token.client_token, auth_token.access_token = f.read().splitlines()
# Library has issues need to do some hackey stuff to make sure it works.
# I would use validate, but that would require some rewriting as well.
auth_token.username = "refresh"
auth_token.refresh()
except (IOError, YggdrasilError):
# IF there is no authentication file authenticate using username and password
try:
options = get_options()
auth_token.authenticate(options["username"], options["password"])
except YggdrasilError as e:
print(e)
sys.exit()
with open('minecraft.auth', 'w') as fout:
fout.write(auth_token.client_token + '\n')
fout.write(auth_token.access_token)
print("Logged in as %s..." % auth_token.username)
connection = Connection(
"localhost", 25565, auth_token=auth_token)
def handle_join_game(join_game_packet):
print('Connected.')
connection.register_packet_listener(
handle_join_game, clientbound.play.JoinGamePacket)
def print_chat(chat_packet):
print("Message (%s): %s" % (
chat_packet.field_string('position'), chat_packet.json_data))
connection.register_packet_listener(
print_chat, clientbound.play.ChatMessagePacket)
global connected
connected = True
def disconnect(disconnect_packet):
print("You were disconnected: %s" % disconnect_packet.json_data)
global connected
connected = False
connection.register_packet_listener(disconnect,
clientbound.play.DisconnectPacket)
connection.connect()
while connected:
try:
text = input()
if text == "/respawn":
print("respawning...")
packet = serverbound.play.ClientStatusPacket()
packet.action_id = serverbound.play.ClientStatusPacket.RESPAWN
connection.write_packet(packet)
else:
packet = serverbound.play.ChatPacket()
packet.message = text
connection.write_packet(packet)
except KeyboardInterrupt:
print("Bye!")
sys.exit()
if __name__ == "__main__":
main()