Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import json
- import sys
- from minecraft.networking.connection import Connection
- from minecraft.networking.packets import Packet, clientbound, serverbound
- from minecraft.networking.types import VarInt, Position, Float, UnsignedLong
- USERNAME = 'vxbot'
- PASSWORD = '123123'
- SERVER = 'hub.dev-urandom.eu'
- PORT = 25565
- class NotRetardedPosition(Position):
- @staticmethod
- def send(cursor_position, socket):
- """Cursor_position can be either a tuple or Position object"""
- x, y, z = cursor_position
- value = ((x & 0x3FFFFFF) << 38) | ((y & 0xFFF) << 26) | (z & 0x3FFFFFF)
- UnsignedLong.send(value, socket)
- class PlayerBlockPlacementPacket(Packet):
- """Realizaton of http://wiki.vg/Protocol#Player_Block_Placement packet
- Usage:
- packet = PlayerBlockPlacementPacket()
- packet.location = Position(x=1200, y=65, z=-420)
- packet.face = 2 # 0: Bottom, 1: Top, 2: North, 3: South, 4: West, 5: East
- packet.hand = 0 # 0: Main, 2: Off
- Next values are called in-block coordinates.
- They are calculated using raytracing. From 0 to 1:
- packet.x = 0.725
- packet.y = 0.125
- packet.z = 0.555
- https://en.wikipedia.org/wiki/Ray_tracing_(graphics)"""
- id = 0x1F
- packet_name = 'player block placement'
- definition = [
- {'location': NotRetardedPosition},
- {'face': VarInt},
- {'hand': VarInt},
- {'x': Float},
- {'y': Float},
- {'z': Float},
- ]
- class BotSession:
- def __init__(self):
- def write_tps_handler(packet):
- data = json.loads(packet.json_data)
- raw_messages = data.get('extra')
- if raw_messages:
- messages = [m.get('text', '').strip().replace(',', '').replace('*', '') for m in raw_messages]
- print(messages)
- if messages[0] == 'TPS from last 1m 5m 15m:':
- print(float(messages[1])) # It should be written inside redis but sqllite is good too
- # (I'm lazy to implement this)
- def print_incoming_handler(packet):
- if type(packet) is Packet:
- # This is a direct instance of the base Packet type, meaning
- # that it is a packet of unknown type, so we do not print it.
- return
- print('--> %s' % packet, file=sys.stderr)
- def print_outgoing_handler(packet):
- print('<-- %s' % packet, file=sys.stderr)
- self.connection = Connection(SERVER, PORT, username=USERNAME)
- self.connection.connect()
- self.on_server = False # That flag should be used in future. I'm lazy so...
- # Handlers are running in separate threads (library sends KeepAlive by itself and sends packets inside handlers)
- self.connection.register_packet_listener(write_tps_handler, clientbound.play.ChatMessagePacket)
- self.connection.register_packet_listener(print_incoming_handler, Packet, early=True)
- self.connection.register_packet_listener(print_outgoing_handler, Packet, outgoing=True)
- def join_game(self):
- def join_game_handler(join_game_packet):
- print('Joined on server')
- text = '/login {}'.format(PASSWORD)
- packet = serverbound.play.ChatPacket()
- packet.message = text
- self.connection.write_packet(packet)
- self.on_server = True
- self.connection.register_packet_listener(join_game_handler, clientbound.play.JoinGamePacket)
- def click_on_table(self):
- # Clicks onto join Alpha sign (player had to join Alpha once from graphical client
- # Server remembers that position after rejoin
- packet = PlayerBlockPlacementPacket()
- packet.location = (1399, 5, 1293)
- packet.face = 4 # East
- packet.hand = 0 # Main hand
- packet.x = 0.875
- packet.y = 0.468
- packet.z = 0.686
- self.connection.write_packet(packet)
- def send_tps(self):
- text = '/tps'
- packet = serverbound.play.ChatPacket()
- packet.message = text
- self.connection.write_packet(packet)
- def main(self):
- while True:
- # Main cycle
- try:
- self.send_tps()
- time.sleep(10)
- except:
- raise
- while True:
- # It's used because it's hard to except various errors
- try:
- s = BotSession()
- s.join_game()
- time.sleep(2)
- s.click_on_table()
- time.sleep(3)
- s.main()
- except:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement