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
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)

15
main.py
View File

@ -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()

View File

@ -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)