@ChrisCheng/

Minecraft-TUI (By @MarcusWeinberger)

Python

Chat on minecraft

fork
loading
Files
  • main.py
  • output.py
  • README.md
  • README.txt
  • requirements.txt
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Minecraft reqs
from minecraft import authentication
from minecraft.networking.connection import Connection
from minecraft.networking.packets import Packet, clientbound, serverbound
from minecraft.compat import input # not sure why
from output import msg, Notifier, color
from getpass import getpass
import json, _thread, time, termcolor, code

default_port = 25565
cmdprefix = '!!'
exitnotdisconnect = False

mc_colors = {
    "§0":lambda t: termcolor.colored(t,"grey"), # meant to be black
    "§1":lambda t: termcolor.colored(t, 'blue'), # closest to dark blue
    "§2":lambda t: color.GREEN + t + color.END,
    '§3':lambda t: color.DARKCYAN + t + color.END,
    "§4":lambda t: color.RED + t + color.END,
    '§5':lambda t: color.PURPLE + t + color.END,
    '§6':lambda t: color.YELLOW + t + color.END,
    '§7':lambda t: termcolor.colored(t, 'grey'),
    '§8':lambda t: termcolor.colored(t, 'grey'),
    '§9':lambda t: color.BLUE + t + color.END,
    '§a':lambda t: color.GREEN + t + color.END,
    '§b':lambda t: color.CYAN + t + color.END,
    '§c':lambda t: color.RED + t + color.END,
    '§d':lambda t: color.PURPLE + t + color.END,
    '§e':lambda t: color.YELLOW + t + color.END,
    '§f':lambda t: termcolor.colored(t,'white')
}

def mc_format(text):
    secs = text.split('§')
    res = ''
    for i in secs:
        if i == '':
            continue
        if '§'+i[0] in mc_colors:
            cc = '§'+i[0]
            res += mc_colors[cc](i[1:])
        else:
            res += i
    return res

class Minecraft(object):
    def __init__(self, addr, username, password):
        self.auth_token = authentication.AuthenticationToken()
        self.auth_token.authenticate(username, password)
        self.conn = Connection(addr[0], addr[1], auth_token=self.auth_token)
        self.chat = []
        self.chat_data = []
    #handling
    def handle_join_game(self,pkt):
        print(msg.plus("{minecraft} Connected."))
    def save_chat_old(self,pkt):
        self.chat.append([pkt.field_string('position'),json.loads(pkt.json_data)])
    def save_chat(self,pkt):
        try:
            _type = pkt.field_string("position")
            data = json.loads(pkt.json_data)
            self.chat_data.append(data)

            txt = ""
            for i in data['extra']:
                if 'text' in i:
                    txt += i['text']
                if 'extra' in i:
                    for x in i['extra']:
                        if 'text' in x:
                            txt += x['text']

            self.chat.append({'type':_type,'text':txt})
        except Exception as e:
            if 'translate' in json.loads(pkt.json_data):
                print(msg.alert("{minecraft} Error: "+str(json.loads(pkt.json_data)['translate'])))
                print(msg.info2('{hint} try using the local respawn command'))
            else:
                print(msg.alert("{minecraft} Error: "+str(e)))


    #main
    def start(self):
        self.conn.register_packet_listener(
            self.handle_join_game,
            clientbound.play.JoinGamePacket
        )
        self.conn.register_packet_listener(
            self.save_chat,
            clientbound.play.ChatMessagePacket
        )
        self.conn.connect()
    # functions
    def respawn(self):
        print(msg.info("{minecraft} Respawning..."))
        pkt = serverbound.play.ClientStatusPacket()
        pkt.action_id = serverbound.play.ClientStatusPacket.RESPAWN
        self.conn.write_packet(pkt)
    def send(self, msg):
        pkt = serverbound.play.ChatPacket()
        pkt.message = msg
        self.conn.write_packet(pkt) 

def update_messages(c,n):
    while True:
        if len(c.chat) > 0:
            m = c.chat[0]
            c.chat.remove(m)
            n.messages.append(mc_format(m['text']))

commands = {
    'help':'print the help menu',
    'exit':'exit the program completely',
    'disconnect':'disconnect from the server',
    'prefix':'change command prefix from the default "!!"',
    'debug':'drop into an interactive debug shell',
    'respawn':'send the respawn packet (if chat doesn\'t work, try this)',
}

def handle_command(client, cmd):
    cmd = cmd.split(" ")[0]
    if cmd == 'help':
        print('Commands:\n'+'\n'.join([("\t"+c+' - '+commands[c]) for c in commands]))
    elif cmd == 'exit':
        globals()['exitnotdisconnect'] = True
        handle_command(client,'disconnect')
        print(msg.minus("Exiting program"))
        exit(0)
    elif cmd == 'disconnect':
        print(msg.minus("Disconnecting from server"))
        client.conn.disconnect()
        if not exitnotdisconnect:
            main()
    elif cmd == 'prefix':
        np = input(msg.info2('New prefix: '))
        globals()['cmdprefix'] = np
    elif cmd == 'debug':
        code.interact(local=dict(**globals(), **locals()))
    elif cmd == 'respawn':
        print(msg.info('{minecraft} Respawning...'))
        client.respawn()
    else:
        print(msg.alert("Invalid command, sending as message"))
        client.send(cmdprefix+cmd)

def interactive(client):
    n = Notifier("[SEND]: ")
    n.start()
    _thread.start_new_thread(update_messages, (client,n))
    while True:
        try:
            inp = input("[SEND]: ")
            if inp.startswith(cmdprefix):
                handle_command(client,inp[len(cmdprefix):])
                continue
            client.send(inp) if not inp == '' else None
        except Exception as e:
            print(msg.alert("Error: "+str(e)))

def _main():
    srv = input("Minecraft server address (default: eu.hivemc.com): ") or "eu.hivemc.com"
    port = int(input("Port (default: 25565): ") or "25565")
    usr = input("Minecraft username: ")
    client = Minecraft((srv,port),usr,getpass()) # password not stored
    client.start()
    time.sleep(3)
    interactive(client)
def main():
    print("When connected, use local commands with !!. Type !!help for more.")
    while True:
        try:
            _main()
        except Exception as e:
            print(msg.alert("Error: "+str(e)))
main() if __name__ == "__main__" else None