Monitor files for changes and reload module

This commit is contained in:
Tanner Collin 2020-09-07 15:48:16 -06:00
parent 7196c427b7
commit f3e39802d2
3 changed files with 101 additions and 37 deletions

64
bot.py Normal file
View File

@ -0,0 +1,64 @@
if __name__ == '__main__':
print('Run main.py instead.')
exit(1)
import time
USERNAME = ''
PASSWORD = ''
SERVER = ''
from custom.managers import DataManager, ChunksManager, ChatManager
import minecraft.networking.packets
from custom.networking.packets.clientbound.play import chunk_data, block_change_packet
def get_packets(old_get_packets):
def wrapper(func, context):
print('Monkey-patched.')
packets = func(context)
packets.add(chunk_data.ChunkDataPacket)
packets.add(block_change_packet.BlockChangePacket)
packets.add(block_change_packet.MultiBlockChangePacket)
return packets
return lambda x: wrapper(old_get_packets, x)
minecraft.networking.packets.clientbound.play.get_packets = get_packets(minecraft.networking.packets.clientbound.play.get_packets)
from minecraft import authentication
from minecraft.exceptions import YggdrasilError
from minecraft.networking.connection import Connection
from minecraft.networking.packets import Packet, clientbound, serverbound
def bot(global_state):
g = global_state
if 'mcdata' not in g:
g.mcdata = DataManager('./mcdata')
if 'connection' not in g:
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)
g.connection = Connection(
SERVER, 25565, auth_token=auth_token)
def handle_join_game(join_game_packet):
print('Connected.')
g.connection.register_packet_listener(
handle_join_game, clientbound.play.JoinGamePacket)
g.chunks = ChunksManager(g.mcdata)
g.chunks.register(g.connection)
g.chat = ChatManager()
g.chat.register(g.connection)
g.connection.connect()
time.sleep(1)

71
main.py
View File

@ -1,52 +1,49 @@
import importlib
import threading
import time
from flask import Flask from flask import Flask
app = Flask(__name__) app = Flask(__name__)
USERNAME = '' from bunch import Bunch
PASSWORD = '' from watchdog.observers import Observer
SERVER = '' from watchdog.events import PatternMatchingEventHandler
from custom.managers import DataManager, ChunksManager, ChatManager import bot
global_state = Bunch()
from minecraft import authentication g = global_state
from minecraft.exceptions import YggdrasilError
from minecraft.networking.connection import Connection
from minecraft.networking.packets import Packet, clientbound, serverbound
@app.route('/') @app.route('/')
def hello_world(): 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(): 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: try:
auth_token.authenticate(USERNAME, PASSWORD) while True:
except YggdrasilError as e: bot.bot(global_state)
print(e) except KeyboardInterrupt:
sys.exit() observer.stop()
print("Logged in as %s..." % auth_token.username) observer.join()
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()
if __name__ == '__main__': if __name__ == '__main__':
threading.Thread(target=app.run).start()
main() main()

View File

@ -1,3 +1,4 @@
bunch==1.0.1
certifi==2020.6.20 certifi==2020.6.20
cffi==1.14.2 cffi==1.14.2
chardet==3.0.4 chardet==3.0.4
@ -8,10 +9,12 @@ idna==2.10
itsdangerous==1.1.0 itsdangerous==1.1.0
Jinja2==2.11.2 Jinja2==2.11.2
MarkupSafe==1.1.1 MarkupSafe==1.1.1
pathtools==0.1.2
pycparser==2.20 pycparser==2.20
pyCraft @ git+https://github.com/ammaraskar/pyCraft.git@cf93923acc2dcfbc076379b43842228d77aea188 pyCraft @ git+https://github.com/ammaraskar/pyCraft.git@cf93923acc2dcfbc076379b43842228d77aea188
PyNBT==3.0.0 PyNBT==3.0.0
requests==2.24.0 requests==2.24.0
six==1.15.0 six==1.15.0
urllib3==1.25.10 urllib3==1.25.10
watchdog==0.10.3
Werkzeug==1.0.1 Werkzeug==1.0.1