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
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()

View File

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