2020-09-07 21:48:16 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
print('Run main.py instead.')
|
|
|
|
exit(1)
|
|
|
|
|
2020-09-07 22:32:19 +00:00
|
|
|
import os
|
2020-09-07 21:48:16 +00:00
|
|
|
import time
|
2020-09-08 04:31:55 +00:00
|
|
|
import importlib
|
2020-09-07 21:48:16 +00:00
|
|
|
|
2020-09-07 22:32:19 +00:00
|
|
|
USERNAME = os.environ['USERNAME']
|
|
|
|
PASSWORD = os.environ['PASSWORD']
|
|
|
|
SERVER = os.environ['SERVER']
|
2020-09-07 21:48:16 +00:00
|
|
|
|
2020-09-08 04:31:55 +00:00
|
|
|
import monkey_patch # must be before any possible pyCraft imports
|
2020-09-07 21:48:16 +00:00
|
|
|
|
2020-09-08 04:31:55 +00:00
|
|
|
from custom.managers import DataManager, ChunksManager, ChatManager
|
2020-09-07 21:48:16 +00:00
|
|
|
|
|
|
|
from minecraft import authentication
|
|
|
|
from minecraft.exceptions import YggdrasilError
|
|
|
|
from minecraft.networking.connection import Connection
|
|
|
|
from minecraft.networking.packets import Packet, clientbound, serverbound
|
|
|
|
|
2020-09-08 01:28:56 +00:00
|
|
|
from custom.networking.packets.clientbound.play.block_change_packet import BlockChangePacket
|
|
|
|
|
2020-09-08 20:07:09 +00:00
|
|
|
from panda3d.core import LPoint3f
|
|
|
|
|
2020-09-08 04:31:55 +00:00
|
|
|
import packet_handlers
|
|
|
|
importlib.reload(packet_handlers)
|
|
|
|
|
2020-09-08 01:28:56 +00:00
|
|
|
TICK = 0.05
|
|
|
|
last_tick = time.time()
|
|
|
|
|
|
|
|
def tick():
|
|
|
|
return
|
|
|
|
|
2020-09-07 21:48:16 +00:00
|
|
|
def bot(global_state):
|
|
|
|
g = global_state
|
|
|
|
|
|
|
|
if 'mcdata' not in g:
|
|
|
|
g.mcdata = DataManager('./mcdata')
|
|
|
|
|
2020-09-08 01:28:56 +00:00
|
|
|
if not g.connection:
|
2020-09-07 21:48:16 +00:00
|
|
|
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)
|
2020-09-08 20:07:09 +00:00
|
|
|
g.connection = Connection(SERVER, 25565, auth_token=auth_token)
|
2020-09-07 21:48:16 +00:00
|
|
|
|
|
|
|
g.chunks = ChunksManager(g.mcdata)
|
|
|
|
g.chunks.register(g.connection)
|
|
|
|
|
|
|
|
g.chat = ChatManager()
|
|
|
|
g.chat.register(g.connection)
|
|
|
|
|
|
|
|
g.connection.connect()
|
|
|
|
|
2020-09-08 04:31:55 +00:00
|
|
|
def packet_wrapper(handler):
|
|
|
|
def wrapper(packet):
|
|
|
|
print('called')
|
|
|
|
handler(packet, g)
|
|
|
|
return wrapper
|
2020-09-08 01:28:56 +00:00
|
|
|
|
2020-09-08 20:07:09 +00:00
|
|
|
h = packet_wrapper(packet_handlers.handle_join_game)
|
|
|
|
g.connection.register_packet_listener(h, clientbound.play.JoinGamePacket)
|
|
|
|
|
2020-09-08 04:31:55 +00:00
|
|
|
h = packet_wrapper(packet_handlers.handle_block_change)
|
|
|
|
g.connection.register_packet_listener(h, BlockChangePacket)
|
2020-09-08 01:28:56 +00:00
|
|
|
|
2020-09-08 20:07:09 +00:00
|
|
|
h = packet_wrapper(packet_handlers.handle_position_and_look)
|
|
|
|
g.connection.register_packet_listener(h, clientbound.play.PlayerPositionAndLookPacket)
|
|
|
|
|
2020-09-08 01:28:56 +00:00
|
|
|
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.')
|