Reregister packet listeners on reload

This commit is contained in:
Tanner Collin 2020-09-07 19:28:56 -06:00
parent ca40d3eba2
commit 76d5609c26
2 changed files with 48 additions and 4 deletions

45
bot.py
View File

@ -18,13 +18,21 @@ from minecraft.exceptions import YggdrasilError
from minecraft.networking.connection import Connection from minecraft.networking.connection import Connection
from minecraft.networking.packets import Packet, clientbound, serverbound from minecraft.networking.packets import Packet, clientbound, serverbound
from custom.networking.packets.clientbound.play.block_change_packet import BlockChangePacket
TICK = 0.05
last_tick = time.time()
def tick():
return
def bot(global_state): def bot(global_state):
g = global_state g = global_state
if 'mcdata' not in g: if 'mcdata' not in g:
g.mcdata = DataManager('./mcdata') g.mcdata = DataManager('./mcdata')
if 'connection' not in g: if not g.connection:
auth_token = authentication.AuthenticationToken() auth_token = authentication.AuthenticationToken()
try: try:
auth_token.authenticate(USERNAME, PASSWORD) auth_token.authenticate(USERNAME, PASSWORD)
@ -49,4 +57,37 @@ def bot(global_state):
g.connection.connect() g.connection.connect()
time.sleep(1)
def x(p):
print('sup block change:')
print(p)
g.connection.register_packet_listener(
x, BlockChangePacket)
try:
#while not player_info.pos:
# time.sleep(TICK)
#print('Player loaded.')
#x, y, z = pint(player_info.pos)
#while (floor(x/16), floor(y/16), floor(z/16)) not in player_info.chunks.chunks:
# time.sleep(TICK)
#print('Chunks loaded.')
while g.running:
tick()
global last_tick
sleep_time = TICK + last_tick - time.time()
if sleep_time < 0: sleep_time = 0
time.sleep(sleep_time)
last_tick = time.time()
finally:
print('Removing listeners...')
g.connection.packet_listeners = []
g.connection.early_packet_listeners = []
g.connection.outgoing_packet_listeners = []
g.connection.early_outgoing_packet_listeners = []
print('Bot module loaded.')

View File

@ -12,6 +12,7 @@ from watchdog.events import PatternMatchingEventHandler
import bot import bot
global_state = Bunch() global_state = Bunch()
g = global_state g = global_state
g.connection = False
@app.route('/') @app.route('/')
def hello_world(): def hello_world():
@ -24,10 +25,10 @@ reload_timeout = time.time()
def main(): def main():
def reload_bot(event): def reload_bot(event):
global reload_timeout global reload_timeout
if time.time() - reload_timeout > 5.0: if time.time() - reload_timeout > 2.0:
reload_timeout = time.time() reload_timeout = time.time()
print('Reloading...') print('Reloading...')
importlib.reload(bot) g.running = False
event_handler = PatternMatchingEventHandler(patterns=['*.py'], ignore_patterns=['./main.py']) event_handler = PatternMatchingEventHandler(patterns=['*.py'], ignore_patterns=['./main.py'])
event_handler.on_any_event = reload_bot event_handler.on_any_event = reload_bot
@ -38,7 +39,9 @@ def main():
try: try:
while True: while True:
g.running = True
bot.bot(global_state) bot.bot(global_state)
importlib.reload(bot)
except KeyboardInterrupt: except KeyboardInterrupt:
observer.stop() observer.stop()
observer.join() observer.join()