minecraft-bot/main.py
2021-04-22 00:46:54 +00:00

86 lines
1.9 KiB
Python

import importlib
import threading
import time
import traceback
import json
from flask import Flask
app = Flask(__name__)
from munch import Munch
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
from mosfet import bot
global_state = Munch()
g = global_state
g.connection = False
g.name = None
g.mcdata = False
g.pos = False
g.dimension = None
g.inv = {}
g.objects = {}
g.mobs = {}
g.players = {}
g.player_names = {}
g.window = None
g.job = None
g.correction_count = 0
g.holding = 0
g.afk = False
g.health = 20
g.food = 20
@app.route('/')
def hello_world():
data = json.dumps(g, default=lambda o: str(o), indent=4)
response = app.response_class(
response=data,
status=200,
mimetype='application/json'
)
return response
reload_timeout = time.time()
def main():
def reload_bot(event):
global reload_timeout
if time.time() - reload_timeout > 2.0:
reload_timeout = time.time()
print('Reloading...')
g.running = False
event_handler = PatternMatchingEventHandler(patterns=['*.py'], ignore_patterns=['./main.py'])
event_handler.on_any_event = reload_bot
observer = Observer()
observer.schedule(event_handler, '.', recursive=True)
observer.start()
try:
while True:
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)
importlib.reload(bot)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == '__main__':
threading.Thread(target=app.run).start()
time.sleep(1)
main()