minecraft-bot/main.py

67 lines
1.5 KiB
Python
Raw Normal View History

import importlib
import threading
import time
2020-09-09 01:52:50 +00:00
import traceback
2020-09-06 21:39:21 +00:00
from flask import Flask
app = Flask(__name__)
from bunch import Bunch
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
2020-09-06 22:10:00 +00:00
import bot
2020-09-08 21:33:42 +00:00
global_state = Bunch()
g = global_state
2020-09-08 01:28:56 +00:00
g.connection = False
2020-09-09 01:52:50 +00:00
g.mcdata = False
2020-09-08 21:33:42 +00:00
g.pos = False
g.inv = {}
2020-09-06 22:10:00 +00:00
2020-09-06 21:39:21 +00:00
@app.route('/')
def hello_world():
#print(chunks.chunks)
return str(g.chunks.get_block_at(84,62,54))
#return 'ok'
2020-09-06 22:10:00 +00:00
reload_timeout = time.time()
2020-09-06 22:10:00 +00:00
def main():
def reload_bot(event):
global reload_timeout
2020-09-08 01:28:56 +00:00
if time.time() - reload_timeout > 2.0:
reload_timeout = time.time()
print('Reloading...')
2020-09-08 01:28:56 +00:00
g.running = False
event_handler = PatternMatchingEventHandler(patterns=['*.py'], ignore_patterns=['./main.py'])
event_handler.on_any_event = reload_bot
2020-09-06 22:10:00 +00:00
observer = Observer()
observer.schedule(event_handler, '.', recursive=True)
observer.start()
2020-09-06 22:10:00 +00:00
try:
while True:
2020-09-09 01:52:50 +00:00
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()
2020-09-06 22:10:00 +00:00
if __name__ == '__main__':
threading.Thread(target=app.run).start()
time.sleep(1)
2020-09-06 22:10:00 +00:00
main()