Improve exception handling

This commit is contained in:
Tanner Collin 2020-09-08 19:52:50 -06:00
parent a76d02d53c
commit e444cf6677
3 changed files with 21 additions and 8 deletions

8
bot.py
View File

@ -14,6 +14,7 @@ SERVER = os.environ['SERVER']
import monkey_patch # must be before any possible pyCraft imports import monkey_patch # must be before any possible pyCraft imports
from custom.managers import DataManager, ChunksManager, ChatManager from custom.managers import DataManager, ChunksManager, ChatManager
from custom.managers.chunks import ChunkNotLoadedException
from minecraft import authentication from minecraft import authentication
from minecraft.exceptions import YggdrasilError from minecraft.exceptions import YggdrasilError
@ -53,7 +54,7 @@ def tick(global_state):
try: try:
g.chunks.get_block_at(*utils.pint(p)) g.chunks.get_block_at(*utils.pint(p))
except chunks.ChunkNotLoadedException: except ChunkNotLoadedException:
return return
#l.jobstate.run() #l.jobstate.run()
@ -161,7 +162,7 @@ def bot(global_state):
g = global_state g = global_state
g.local_state = Bunch() g.local_state = Bunch()
if 'mcdata' not in g: if not g.mcdata:
g.mcdata = DataManager('./mcdata') g.mcdata = DataManager('./mcdata')
if not g.connection: if not g.connection:
@ -175,7 +176,6 @@ def bot(global_state):
g.connection = Connection(SERVER, 25565, auth_token=auth_token) g.connection = Connection(SERVER, 25565, auth_token=auth_token)
g.chunks = ChunksManager(g.mcdata) g.chunks = ChunksManager(g.mcdata)
g.chunks.register(g.connection)
g.connection.connect() g.connection.connect()
@ -184,6 +184,8 @@ def bot(global_state):
handler(packet, g) handler(packet, g)
return wrapper return wrapper
g.chunks.register(g.connection)
h1 = packet_wrapper(packet_handlers.handle_join_game) h1 = packet_wrapper(packet_handlers.handle_join_game)
g.connection.register_packet_listener(h1, clientbound.play.JoinGamePacket) g.connection.register_packet_listener(h1, clientbound.play.JoinGamePacket)

15
main.py
View File

@ -1,6 +1,7 @@
import importlib import importlib
import threading import threading
import time import time
import traceback
from flask import Flask from flask import Flask
app = Flask(__name__) app = Flask(__name__)
@ -15,6 +16,7 @@ global_state = Bunch()
g = global_state g = global_state
g.local_state = False g.local_state = False
g.connection = False g.connection = False
g.mcdata = False
g.pos = False g.pos = False
@app.route('/') @app.route('/')
@ -42,9 +44,16 @@ def main():
try: try:
while True: while True:
g.running = True try:
bot.bot(global_state) g.running = True
importlib.reload(bot) 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: except KeyboardInterrupt:
observer.stop() observer.stop()
observer.join() observer.join()

View File

@ -18,8 +18,6 @@ def handle_join_game(packet, g):
def handle_block_change(packet, g): def handle_block_change(packet, g):
l = g.local_state l = g.local_state
print('block change:')
print(packet)
if packet.block_state_id == 3887: if packet.block_state_id == 3887:
try: try:
@ -92,6 +90,10 @@ def handle_chat(message, g):
if command == 'afk': if command == 'afk':
reply = '/afk' reply = '/afk'
if command == 'error':
reply = 'ok'
raise
if reply: if reply:
print(reply) print(reply)
g.chat.send(reply) g.chat.send(reply)