Compare commits

...

6 Commits

6 changed files with 117 additions and 36 deletions

View File

@ -1,2 +1,3 @@
from .data import DataManager from .data import DataManager
from .chunks import ChunksManager from .chunks import ChunksManager
from .chat import ChatManager

36
custom/managers/chat.py Normal file
View File

@ -0,0 +1,36 @@
import json
from minecraft.networking.packets import clientbound, serverbound
class ChatManager:
def __init__(self):
return
def translate_chat(self, data):
if isinstance(data, str):
return data
elif 'extra' in data:
return "".join([self.translate_chat(x) for x in data['extra']])
elif 'text' in data:
return data['text']
else:
return "?"
def print_chat(self, chat_packet):
# TODO: Replace with handler
try:
print("[%s] %s"%(chat_packet.field_string('position'), self.translate_chat(json.loads(chat_packet.json_data))))
except Exception as ex:
print("Exception %r on message (%s): %s" % (ex, chat_packet.field_string('position'), chat_packet.json_data))
def register(self, connection):
connection.register_packet_listener(self.print_chat, clientbound.play.ChatMessagePacket)
def send(self, connection, text):
if not text:
# Prevents connection bug when sending empty chat message
return
packet = serverbound.play.ChatPacket()
packet.message = text
connection.write_packet(packet)

View File

@ -1,20 +1,12 @@
from minecraft.networking.packets import Packet from minecraft.networking.packets import Packet
from minecraft.networking.types import ( from minecraft.networking.types import (
VarInt, Integer, UnsignedByte, Position, Vector, MutableRecord, VarInt, Integer, UnsignedByte, Position, Vector, MutableRecord,
attribute_alias, multi_attribute_alias, attribute_alias, multi_attribute_alias, Long, Boolean, VarLong,
) )
class BlockChangePacket(Packet): class BlockChangePacket(Packet):
@staticmethod id = 0x0B
def get_id(context):
return 0x0C if context.protocol_version >= 550 else \
0x0B if context.protocol_version >= 332 else \
0x0C if context.protocol_version >= 318 else \
0x0B if context.protocol_version >= 67 else \
0x24 if context.protocol_version >= 62 else \
0x23
packet_name = 'block change' packet_name = 'block change'
definition = [ definition = [
{'location': Position}, {'location': Position},
@ -44,15 +36,7 @@ class BlockChangePacket(Packet):
class MultiBlockChangePacket(Packet): class MultiBlockChangePacket(Packet):
@staticmethod id = 0x3B
def get_id(context):
return 0x10 if context.protocol_version >= 550 else \
0x0F if context.protocol_version >= 343 else \
0x10 if context.protocol_version >= 332 else \
0x11 if context.protocol_version >= 318 else \
0x10 if context.protocol_version >= 67 else \
0x22
packet_name = 'multi block change' packet_name = 'multi block change'
fields = 'chunk_x', 'chunk_z', 'records' fields = 'chunk_x', 'chunk_z', 'records'
@ -92,29 +76,35 @@ class MultiBlockChangePacket(Packet):
blockStateId = attribute_alias('block_state_id') blockStateId = attribute_alias('block_state_id')
def read(self, file_object, parent): def read(self, file_object, parent):
h_position = UnsignedByte.read(file_object) data = VarLong.read(file_object)
self.x, self.z = h_position >> 4, h_position & 0xF self.block_state_id = int(data >> 12)
self.y = UnsignedByte.read(file_object) self.x = int(data >> 8 & 0xf)
self.block_state_id = VarInt.read(file_object) self.z = int(data >> 4 & 0xf)
self.y = int(data & 0xf)
# Absolute position in world to be compatible with BlockChangePacket # Absolute position in world to be compatible with BlockChangePacket
self.location = Vector(self.position.x + parent.chunk_x*16, self.position.y, self.position.z + parent.chunk_z*16) self.location = Vector(self.position.x + parent.chunk_x*16, self.position.y, self.position.z + parent.chunk_z*16)
def write(self, packet_buffer): def write(self, packet_buffer):
raise
UnsignedByte.send(self.x << 4 | self.z & 0xF, packet_buffer) UnsignedByte.send(self.x << 4 | self.z & 0xF, packet_buffer)
UnsignedByte.send(self.y, packet_buffer) UnsignedByte.send(self.y, packet_buffer)
VarInt.send(self.block_state_id, packet_buffer) VarInt.send(self.block_state_id, packet_buffer)
def read(self, file_object): def read(self, file_object):
self.chunk_x = Integer.read(file_object) coords = Long.read(file_object)
self.chunk_z = Integer.read(file_object) self.chunk_x = int(coords >> 42 & 0x3fffff)
records_count = VarInt.read(file_object) self.chunk_z = int(coords >> 20 & 0x3fffff)
self.chunk_y = int(coords & 0xfffff)
self.unknown = Boolean.read(file_object)
array_size = VarInt.read(file_object)
self.records = [] self.records = []
for i in range(records_count): for i in range(array_size):
record = self.Record() record = self.Record()
record.read(file_object, self) record.read(file_object, self)
self.records.append(record) self.records.append(record)
def write_fields(self, packet_buffer): def write_fields(self, packet_buffer):
raise
Integer.send(self.chunk_x, packet_buffer) Integer.send(self.chunk_x, packet_buffer)
Integer.send(self.chunk_z, packet_buffer) Integer.send(self.chunk_z, packet_buffer)
VarInt.send(len(self.records), packet_buffer) VarInt.send(len(self.records), packet_buffer)

52
main.py Normal file
View File

@ -0,0 +1,52 @@
from flask import Flask
app = Flask(__name__)
USERNAME = ''
PASSWORD = ''
SERVER = ''
from custom.managers import DataManager, ChunksManager, ChatManager
from minecraft import authentication
from minecraft.exceptions import YggdrasilError
from minecraft.networking.connection import Connection
from minecraft.networking.packets import Packet, clientbound, serverbound
@app.route('/')
def hello_world():
return 'Hello, World!'
def main():
mcdata = DataManager('./mcdata')
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)
connection = Connection(
SERVER, 25565, auth_token=auth_token)
def handle_join_game(join_game_packet):
print('Connected.')
connection.register_packet_listener(
handle_join_game, clientbound.play.JoinGamePacket)
chunks = ChunksManager(mcdata)
chunks.register(connection)
chat = ChatManager()
chat.register(connection)
connection.connect()
print('connected')
app.run()
if __name__ == '__main__':
main()

View File

@ -1,11 +1,17 @@
certifi==2020.6.20 certifi==2020.6.20
cffi==1.14.2 cffi==1.14.2
chardet==3.0.4 chardet==3.0.4
click==7.1.2
cryptography==3.1 cryptography==3.1
Flask==1.1.2
idna==2.10 idna==2.10
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
pycparser==2.20 pycparser==2.20
pyCraft @ git+https://github.com/ammaraskar/pyCraft.git@cf93923acc2dcfbc076379b43842228d77aea188 pyCraft @ git+https://github.com/ammaraskar/pyCraft.git@cf93923acc2dcfbc076379b43842228d77aea188
PyNBT==3.0.0 PyNBT==3.0.0
requests==2.24.0 requests==2.24.0
six==1.15.0 six==1.15.0
urllib3==1.25.10 urllib3==1.25.10
Werkzeug==1.0.1

View File

@ -6,7 +6,7 @@ import re
from optparse import OptionParser from optparse import OptionParser
from custom.managers import DataManager, ChunksManager from custom.managers import DataManager, ChunksManager
from custom.networking.packets.clientbound.play import chunk_data from custom.networking.packets.clientbound.play import chunk_data, block_change_packet
import minecraft.networking.packets import minecraft.networking.packets
@ -15,6 +15,8 @@ def get_packets(old_get_packets):
print('Monkey-patched.') print('Monkey-patched.')
packets = func(context) packets = func(context)
packets.add(chunk_data.ChunkDataPacket) packets.add(chunk_data.ChunkDataPacket)
packets.add(block_change_packet.BlockChangePacket)
packets.add(block_change_packet.MultiBlockChangePacket)
return packets return packets
return lambda x: wrapper(old_get_packets, x) return lambda x: wrapper(old_get_packets, x)
@ -124,18 +126,12 @@ def main():
print("Message (%s): %s" % ( print("Message (%s): %s" % (
chat_packet.field_string('position'), chat_packet.json_data)) chat_packet.field_string('position'), chat_packet.json_data))
#chunks = ChunksManager(mcdata) chunks = ChunksManager(mcdata)
#chunks.register(connection) chunks.register(connection)
connection.register_packet_listener( connection.register_packet_listener(
print_chat, clientbound.play.ChatMessagePacket) print_chat, clientbound.play.ChatMessagePacket)
def handle_chunk(chunk_packet):
print(chunk_packet)
def register(self, connection):
connection.register_packet_listener(handle_chunk, chunk_data.ChunkDataPacket)
connection.connect() connection.connect()
while True: while True: