minecraft-bot/mosfet/main.py

86 lines
1.8 KiB
Python
Raw Normal View History

import importlib
import threading
import time
2020-09-09 01:52:50 +00:00
import traceback
import json
2020-09-06 21:39:21 +00:00
from flask import Flask
app = Flask(__name__)
from munch import Munch
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 = Munch()
g = global_state
2020-09-08 01:28:56 +00:00
g.connection = False
g.name = None
2020-09-09 01:52:50 +00:00
g.mcdata = False
2020-09-08 21:33:42 +00:00
g.pos = False
g.dimension = None
g.inv = {}
g.objects = {}
2020-12-03 03:30:54 +00:00
g.mobs = {}
g.players = {}
2020-12-13 11:38:30 +00:00
g.player_names = {}
g.window = None
g.job = None
2020-10-19 21:49:14 +00:00
g.correction_count = 0
2020-12-02 11:12:51 +00:00
g.holding = 0
2020-12-03 03:30:54 +00:00
g.afk = False
2021-01-05 05:57:06 +00:00
g.health = 20
g.food = 20
2020-09-06 22:10:00 +00:00
2020-09-06 21:39:21 +00:00
@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
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()