Monitor files for changes and reload module
This commit is contained in:
@@ -1,52 +1,49 @@
|
||||
import importlib
|
||||
import threading
|
||||
import time
|
||||
|
||||
from flask import Flask
|
||||
app = Flask(__name__)
|
||||
|
||||
USERNAME = ''
|
||||
PASSWORD = ''
|
||||
SERVER = ''
|
||||
from bunch import Bunch
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import PatternMatchingEventHandler
|
||||
|
||||
from custom.managers import DataManager, ChunksManager, ChatManager
|
||||
|
||||
from minecraft import authentication
|
||||
from minecraft.exceptions import YggdrasilError
|
||||
from minecraft.networking.connection import Connection
|
||||
from minecraft.networking.packets import Packet, clientbound, serverbound
|
||||
import bot
|
||||
global_state = Bunch()
|
||||
g = global_state
|
||||
|
||||
@app.route('/')
|
||||
def hello_world():
|
||||
return 'Hello, World!'
|
||||
#print(chunks.chunks)
|
||||
return str(g.chunks.get_block_at(84,62,54))
|
||||
#return 'ok'
|
||||
|
||||
reload_timeout = time.time()
|
||||
|
||||
def main():
|
||||
mcdata = DataManager('./mcdata')
|
||||
def reload_bot(event):
|
||||
global reload_timeout
|
||||
if time.time() - reload_timeout > 5.0:
|
||||
reload_timeout = time.time()
|
||||
print('Reloading...')
|
||||
importlib.reload(bot)
|
||||
|
||||
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()
|
||||
|
||||
auth_token = authentication.AuthenticationToken()
|
||||
try:
|
||||
auth_token.authenticate(USERNAME, PASSWORD)
|
||||
except YggdrasilError as e:
|
||||
print(e)
|
||||
sys.exit()
|
||||
print("Logged in as %s..." % auth_token.username)
|
||||
connection = Connection(
|
||||
SERVER, 25565, auth_token=auth_token)
|
||||
|
||||
def handle_join_game(join_game_packet):
|
||||
print('Connected.')
|
||||
|
||||
connection.register_packet_listener(
|
||||
handle_join_game, clientbound.play.JoinGamePacket)
|
||||
|
||||
chunks = ChunksManager(mcdata)
|
||||
chunks.register(connection)
|
||||
|
||||
chat = ChatManager()
|
||||
chat.register(connection)
|
||||
|
||||
connection.connect()
|
||||
|
||||
print('connected')
|
||||
|
||||
app.run()
|
||||
while True:
|
||||
bot.bot(global_state)
|
||||
except KeyboardInterrupt:
|
||||
observer.stop()
|
||||
observer.join()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
threading.Thread(target=app.run).start()
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user