minecraft-bot/bot.py

94 lines
2.4 KiB
Python

if __name__ == '__main__':
print('Run main.py instead.')
exit(1)
import os
import time
USERNAME = os.environ['USERNAME']
PASSWORD = os.environ['PASSWORD']
SERVER = os.environ['SERVER']
from custom.managers import DataManager, ChunksManager, ChatManager
import monkey_patch
from minecraft import authentication
from minecraft.exceptions import YggdrasilError
from minecraft.networking.connection import Connection
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):
g = global_state
if 'mcdata' not in g:
g.mcdata = DataManager('./mcdata')
if not g.connection:
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()
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.')