parent
7196c427b7
commit
f3e39802d2
3 changed files with 100 additions and 36 deletions
@ -0,0 +1,64 @@ |
||||
if __name__ == '__main__': |
||||
print('Run main.py instead.') |
||||
exit(1) |
||||
|
||||
import time |
||||
|
||||
USERNAME = '' |
||||
PASSWORD = '' |
||||
SERVER = '' |
||||
|
||||
from custom.managers import DataManager, ChunksManager, ChatManager |
||||
|
||||
import minecraft.networking.packets |
||||
from custom.networking.packets.clientbound.play import chunk_data, block_change_packet |
||||
|
||||
def get_packets(old_get_packets): |
||||
def wrapper(func, context): |
||||
print('Monkey-patched.') |
||||
packets = func(context) |
||||
packets.add(chunk_data.ChunkDataPacket) |
||||
packets.add(block_change_packet.BlockChangePacket) |
||||
packets.add(block_change_packet.MultiBlockChangePacket) |
||||
return packets |
||||
return lambda x: wrapper(old_get_packets, x) |
||||
|
||||
minecraft.networking.packets.clientbound.play.get_packets = get_packets(minecraft.networking.packets.clientbound.play.get_packets) |
||||
|
||||
from minecraft import authentication |
||||
from minecraft.exceptions import YggdrasilError |
||||
from minecraft.networking.connection import Connection |
||||
from minecraft.networking.packets import Packet, clientbound, serverbound |
||||
|
||||
def bot(global_state): |
||||
g = global_state |
||||
|
||||
if 'mcdata' not in g: |
||||
g.mcdata = DataManager('./mcdata') |
||||
|
||||
if 'connection' not in g: |
||||
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) |
||||
g.connection = Connection( |
||||
SERVER, 25565, auth_token=auth_token) |
||||
|
||||
def handle_join_game(join_game_packet): |
||||
print('Connected.') |
||||
|
||||
g.connection.register_packet_listener( |
||||
handle_join_game, clientbound.play.JoinGamePacket) |
||||
|
||||
g.chunks = ChunksManager(g.mcdata) |
||||
g.chunks.register(g.connection) |
||||
|
||||
g.chat = ChatManager() |
||||
g.chat.register(g.connection) |
||||
|
||||
g.connection.connect() |
||||
|
||||
time.sleep(1) |
@ -1,52 +1,49 @@ |
||||
import importlib |
||||
import threading |
||||
import time |
||||
|
||||
from flask import Flask |
||||
app = Flask(__name__) |
||||
|
||||
USERNAME = '' |
||||
PASSWORD = '' |
||||
SERVER = '' |
||||
|
||||
from custom.managers import DataManager, ChunksManager, ChatManager |
||||
from bunch import Bunch |
||||
from watchdog.observers import Observer |
||||
from watchdog.events import PatternMatchingEventHandler |
||||
|
||||
from minecraft import authentication |
||||
from minecraft.exceptions import YggdrasilError |
||||
from minecraft.networking.connection import Connection |
||||
from minecraft.networking.packets import Packet, clientbound, serverbound |
||||
import bot |
||||
global_state = Bunch() |
||||
g = global_state |
||||
|
||||
@app.route('/') |
||||
def 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) |
||||
#print(chunks.chunks) |
||||
return str(g.chunks.get_block_at(84,62,54)) |
||||
#return 'ok' |
||||
|
||||
chunks = ChunksManager(mcdata) |
||||
chunks.register(connection) |
||||
reload_timeout = time.time() |
||||
|
||||
chat = ChatManager() |
||||
chat.register(connection) |
||||
def main(): |
||||
def reload_bot(event): |
||||
global reload_timeout |
||||
if time.time() - reload_timeout > 5.0: |
||||
reload_timeout = time.time() |
||||
print('Reloading...') |
||||
importlib.reload(bot) |
||||
|
||||
connection.connect() |
||||
event_handler = PatternMatchingEventHandler(patterns=['*.py'], ignore_patterns=['./main.py']) |
||||
event_handler.on_any_event = reload_bot |
||||
|
||||
print('connected') |
||||
observer = Observer() |
||||
observer.schedule(event_handler, '.', recursive=True) |
||||
observer.start() |
||||
|
||||
app.run() |
||||
try: |
||||
while True: |
||||
bot.bot(global_state) |
||||
except KeyboardInterrupt: |
||||
observer.stop() |
||||
observer.join() |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
threading.Thread(target=app.run).start() |
||||
main() |
||||
|
Loading…
Reference in new issue