From e444cf66773670f72f03e5b26f2f579a52f38e44 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Tue, 8 Sep 2020 19:52:50 -0600 Subject: [PATCH] Improve exception handling --- bot.py | 8 +++++--- main.py | 15 ++++++++++++--- packet_handlers.py | 6 ++++-- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/bot.py b/bot.py index 68a7d3c..cbe72dc 100644 --- a/bot.py +++ b/bot.py @@ -14,6 +14,7 @@ SERVER = os.environ['SERVER'] import monkey_patch # must be before any possible pyCraft imports from custom.managers import DataManager, ChunksManager, ChatManager +from custom.managers.chunks import ChunkNotLoadedException from minecraft import authentication from minecraft.exceptions import YggdrasilError @@ -53,7 +54,7 @@ def tick(global_state): try: g.chunks.get_block_at(*utils.pint(p)) - except chunks.ChunkNotLoadedException: + except ChunkNotLoadedException: return #l.jobstate.run() @@ -161,7 +162,7 @@ def bot(global_state): g = global_state g.local_state = Bunch() - if 'mcdata' not in g: + if not g.mcdata: g.mcdata = DataManager('./mcdata') if not g.connection: @@ -175,7 +176,6 @@ def bot(global_state): g.connection = Connection(SERVER, 25565, auth_token=auth_token) g.chunks = ChunksManager(g.mcdata) - g.chunks.register(g.connection) g.connection.connect() @@ -184,6 +184,8 @@ def bot(global_state): handler(packet, g) return wrapper + g.chunks.register(g.connection) + h1 = packet_wrapper(packet_handlers.handle_join_game) g.connection.register_packet_listener(h1, clientbound.play.JoinGamePacket) diff --git a/main.py b/main.py index 7e894cf..54922c5 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ import importlib import threading import time +import traceback from flask import Flask app = Flask(__name__) @@ -15,6 +16,7 @@ global_state = Bunch() g = global_state g.local_state = False g.connection = False +g.mcdata = False g.pos = False @app.route('/') @@ -42,9 +44,16 @@ def main(): try: while True: - g.running = True - bot.bot(global_state) - importlib.reload(bot) + try: + g.running = True + bot.bot(global_state) + importlib.reload(bot) + except BaseException as e: + g.running = True + traceback.print_exc() + print('Locking...') + while g.running: + time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join() diff --git a/packet_handlers.py b/packet_handlers.py index a102d6d..3c6aa39 100644 --- a/packet_handlers.py +++ b/packet_handlers.py @@ -18,8 +18,6 @@ def handle_join_game(packet, g): def handle_block_change(packet, g): l = g.local_state - print('block change:') - print(packet) if packet.block_state_id == 3887: try: @@ -92,6 +90,10 @@ def handle_chat(message, g): if command == 'afk': reply = '/afk' + if command == 'error': + reply = 'ok' + raise + if reply: print(reply) g.chat.send(reply)