Advertisement
Guest User

Untitled

a guest
Mar 4th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.61 KB | None | 0 0
  1. import time
  2. import json
  3. import sys
  4.  
  5. from minecraft.networking.connection import Connection
  6. from minecraft.networking.packets import Packet, clientbound, serverbound
  7. from minecraft.networking.types import VarInt, Position, Float, UnsignedLong
  8.  
  9.  
  10. USERNAME = 'vxbot'
  11. PASSWORD = '123123'
  12. SERVER = 'hub.dev-urandom.eu'
  13. PORT = 25565
  14.  
  15.  
  16. class NotRetardedPosition(Position):
  17.     @staticmethod
  18.     def send(cursor_position, socket):
  19.         """Cursor_position can be either a tuple or Position object"""
  20.         x, y, z = cursor_position
  21.         value = ((x & 0x3FFFFFF) << 38) | ((y & 0xFFF) << 26) | (z & 0x3FFFFFF)
  22.         UnsignedLong.send(value, socket)
  23.  
  24.  
  25. class PlayerBlockPlacementPacket(Packet):
  26.     """Realizaton of http://wiki.vg/Protocol#Player_Block_Placement packet
  27.    Usage:
  28.        packet = PlayerBlockPlacementPacket()
  29.        packet.location = Position(x=1200, y=65, z=-420)
  30.        packet.face = 2  # 0: Bottom, 1: Top, 2: North, 3: South, 4: West, 5: East
  31.        packet.hand = 0  # 0: Main, 2: Off
  32.    Next values are called in-block coordinates.
  33.    They are calculated using raytracing. From 0 to 1:
  34.        packet.x = 0.725
  35.        packet.y = 0.125
  36.        packet.z = 0.555
  37.    https://en.wikipedia.org/wiki/Ray_tracing_(graphics)"""
  38.     id = 0x1F
  39.     packet_name = 'player block placement'
  40.     definition = [
  41.         {'location': NotRetardedPosition},
  42.         {'face': VarInt},
  43.         {'hand': VarInt},
  44.         {'x': Float},
  45.         {'y': Float},
  46.         {'z': Float},
  47.     ]
  48.  
  49.  
  50. class BotSession:
  51.     def __init__(self):
  52.         def write_tps_handler(packet):
  53.             data = json.loads(packet.json_data)
  54.             raw_messages = data.get('extra')
  55.             if raw_messages:
  56.                 messages = [m.get('text', '').strip().replace(',', '').replace('*', '') for m in raw_messages]
  57.                 print(messages)
  58.                 if messages[0] == 'TPS from last 1m 5m 15m:':
  59.                     print(float(messages[1]))   # It should be written inside redis but sqllite is good too
  60.                                                 # (I'm lazy to implement this)
  61.  
  62.         def print_incoming_handler(packet):
  63.             if type(packet) is Packet:
  64.                 # This is a direct instance of the base Packet type, meaning
  65.                 # that it is a packet of unknown type, so we do not print it.
  66.                 return
  67.             print('--> %s' % packet, file=sys.stderr)
  68.  
  69.         def print_outgoing_handler(packet):
  70.             print('<-- %s' % packet, file=sys.stderr)
  71.  
  72.         self.connection = Connection(SERVER, PORT, username=USERNAME)
  73.         self.connection.connect()
  74.         self.on_server = False  # That flag should be used in future. I'm lazy so...
  75.  
  76.         # Handlers are running in separate threads (library sends KeepAlive by itself and sends packets inside handlers)
  77.         self.connection.register_packet_listener(write_tps_handler, clientbound.play.ChatMessagePacket)
  78.         self.connection.register_packet_listener(print_incoming_handler, Packet, early=True)
  79.         self.connection.register_packet_listener(print_outgoing_handler, Packet, outgoing=True)
  80.  
  81.     def join_game(self):
  82.         def join_game_handler(join_game_packet):
  83.             print('Joined on server')
  84.  
  85.             text = '/login {}'.format(PASSWORD)
  86.             packet = serverbound.play.ChatPacket()
  87.             packet.message = text
  88.             self.connection.write_packet(packet)
  89.  
  90.             self.on_server = True
  91.  
  92.         self.connection.register_packet_listener(join_game_handler, clientbound.play.JoinGamePacket)
  93.  
  94.     def click_on_table(self):
  95.         # Clicks onto join Alpha sign (player had to join Alpha once from graphical client
  96.         # Server remembers that position after rejoin
  97.         packet = PlayerBlockPlacementPacket()
  98.         packet.location = (1399, 5, 1293)
  99.         packet.face = 4  # East
  100.         packet.hand = 0  # Main hand
  101.         packet.x = 0.875
  102.         packet.y = 0.468
  103.         packet.z = 0.686
  104.         self.connection.write_packet(packet)
  105.  
  106.     def send_tps(self):
  107.         text = '/tps'
  108.         packet = serverbound.play.ChatPacket()
  109.         packet.message = text
  110.         self.connection.write_packet(packet)
  111.  
  112.     def main(self):
  113.         while True:
  114.             # Main cycle
  115.             try:
  116.                 self.send_tps()
  117.                 time.sleep(10)
  118.             except:
  119.                 raise
  120.  
  121.  
  122. while True:
  123.     # It's used because it's hard to except various errors
  124.     try:
  125.         s = BotSession()
  126.         s.join_game()
  127.         time.sleep(2)
  128.         s.click_on_table()
  129.         time.sleep(3)
  130.         s.main()
  131.     except:
  132.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement