Integrate pyCraft loggin in with Flask

This commit is contained in:
Tanner Collin 2020-09-06 16:10:00 -06:00
parent fbdf8ba718
commit 7d93888798

48
main.py
View File

@ -1,8 +1,56 @@
from flask import Flask from flask import Flask
app = Flask(__name__) app = Flask(__name__)
USERNAME = ''
PASSWORD = ''
SERVER = ''
from custom.managers import DataManager, ChunksManager
from minecraft import authentication
from minecraft.exceptions import YggdrasilError
from minecraft.networking.connection import Connection
from minecraft.networking.packets import Packet, clientbound, serverbound
@app.route('/') @app.route('/')
def hello_world(): def hello_world():
return 'Hello, World!' return 'Hello, World!'
def main():
mcdata = DataManager('./mcdata')
auth_token = authentication.AuthenticationToken()
try:
auth_token.authenticate(USERNAME, PASSWORD)
except YggdrasilError as e:
print(e)
sys.exit()
print("Logged in as %s..." % auth_token.username)
connection = Connection(
SERVER, 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)
chunks = ChunksManager(mcdata)
chunks.register(connection)
connection.connect()
print('connected')
app.run() app.run()
if __name__ == '__main__':
main()