Move bot files into mosfet/
This commit is contained in:
		
							
								
								
									
										0
									
								
								mosfet/protocol/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								mosfet/protocol/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										194
									
								
								mosfet/protocol/managers.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										194
									
								
								mosfet/protocol/managers.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,194 @@
 | 
			
		||||
import os
 | 
			
		||||
from math import floor
 | 
			
		||||
import json
 | 
			
		||||
import time
 | 
			
		||||
 | 
			
		||||
from minecraft.networking.packets import clientbound, serverbound
 | 
			
		||||
from protocol import packets
 | 
			
		||||
 | 
			
		||||
import utils
 | 
			
		||||
 | 
			
		||||
class DataManager:
 | 
			
		||||
    def __init__(self, directory):
 | 
			
		||||
        self.blocks = {}
 | 
			
		||||
        self.blocks_states = {}
 | 
			
		||||
        self.blocks_properties = {}
 | 
			
		||||
        self.registries = {}
 | 
			
		||||
        self.biomes = {}
 | 
			
		||||
        self.entity_type = {}
 | 
			
		||||
        
 | 
			
		||||
        if not os.path.isdir(directory):
 | 
			
		||||
            raise FileNotFoundError("%s is not a valid directory")
 | 
			
		||||
 | 
			
		||||
        if not os.path.isfile("%s/registries.json"%(directory)):
 | 
			
		||||
            raise FileNotFoundError("%s is not a valid minecraft data directory")
 | 
			
		||||
        
 | 
			
		||||
        with open("%s/blocks.json"%(directory)) as f:
 | 
			
		||||
            blocks = json.loads(f.read())
 | 
			
		||||
        for x in blocks:
 | 
			
		||||
            for s in blocks[x]['states']:
 | 
			
		||||
                self.blocks_states[s['id']] = x
 | 
			
		||||
                self.blocks_properties[s['id']] = s.get('properties', {})
 | 
			
		||||
 | 
			
		||||
        with open("%s/registries.json"%(directory)) as f:
 | 
			
		||||
            registries = json.loads(f.read())
 | 
			
		||||
        #for x in registries["minecraft:biome"]["entries"]:
 | 
			
		||||
        #    self.biomes[registries["minecraft:biome"]["entries"][x]["protocol_id"]] = x
 | 
			
		||||
        for x in registries["minecraft:entity_type"]["entries"]:
 | 
			
		||||
            self.entity_type[registries["minecraft:entity_type"]["entries"][x]["protocol_id"]] = x
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ChunksManager:
 | 
			
		||||
    def __init__(self, data_manager):
 | 
			
		||||
        self.data = data_manager
 | 
			
		||||
        self.chunks = {}
 | 
			
		||||
        self.biomes = {}
 | 
			
		||||
        self.index = {}
 | 
			
		||||
        self.loading = False
 | 
			
		||||
 | 
			
		||||
    def handle_block(self, block_packet):
 | 
			
		||||
        self.set_block_at(block_packet.location.x, block_packet.location.y, block_packet.location.z, block_packet.block_state_id)
 | 
			
		||||
 | 
			
		||||
    def handle_multiblock(self, multiblock_packet):
 | 
			
		||||
        dx = 16 * multiblock_packet.chunk_section_pos.x
 | 
			
		||||
        dy = 16 * multiblock_packet.chunk_section_pos.y
 | 
			
		||||
        dz = 16 * multiblock_packet.chunk_section_pos.z
 | 
			
		||||
        for b in multiblock_packet.records:
 | 
			
		||||
            self.set_block_at(dx+b.x, dy+b.y, dz+b.z, b.block_state_id)
 | 
			
		||||
 | 
			
		||||
    def handle_chunk(self, chunk_packet):
 | 
			
		||||
        for i in chunk_packet.chunks:
 | 
			
		||||
            chunk = chunk_packet.chunks[i]
 | 
			
		||||
            self.chunks[(chunk_packet.x, i, chunk_packet.z)] = chunk
 | 
			
		||||
 | 
			
		||||
            if chunk.sub_index:
 | 
			
		||||
                dx = chunk.x * 16
 | 
			
		||||
                dy = chunk.y * 16
 | 
			
		||||
                dz = chunk.z * 16
 | 
			
		||||
 | 
			
		||||
                for item_id, locations in chunk.sub_index.items():
 | 
			
		||||
                    if item_id not in self.index:
 | 
			
		||||
                        self.index[item_id] = []
 | 
			
		||||
 | 
			
		||||
                    for l in locations:
 | 
			
		||||
                        coords = (dx + l%16, dy + l//256, dz + l%256//16)
 | 
			
		||||
                        self.index[item_id].append(coords)
 | 
			
		||||
 | 
			
		||||
        self.biomes[(chunk_packet.x, None, chunk_packet.z)] = chunk_packet.biomes # FIXME
 | 
			
		||||
        if self.loading:
 | 
			
		||||
            print('.', end='', flush=True)
 | 
			
		||||
 | 
			
		||||
    def register(self, connection):
 | 
			
		||||
        connection.register_packet_listener(self.handle_block, clientbound.play.BlockChangePacket)
 | 
			
		||||
        connection.register_packet_listener(self.handle_multiblock, clientbound.play.MultiBlockChangePacket)
 | 
			
		||||
        connection.register_packet_listener(self.handle_chunk, packets.ChunkDataPacket)
 | 
			
		||||
 | 
			
		||||
    def get_chunk(self, x, y, z):
 | 
			
		||||
        index = (x, y, z)
 | 
			
		||||
        if not index in self.chunks:
 | 
			
		||||
            return None
 | 
			
		||||
            #raise ChunkNotLoadedException(index)
 | 
			
		||||
        return self.chunks[index]
 | 
			
		||||
 | 
			
		||||
    def get_loaded_area(self, ignore_empty=False):
 | 
			
		||||
        first = next(iter(self.chunks.keys()))
 | 
			
		||||
        x0 = x1 = first[0]
 | 
			
		||||
        y0 = y1 = first[1]
 | 
			
		||||
        z0 = z1 = first[2]
 | 
			
		||||
        for k in self.chunks.keys():
 | 
			
		||||
            if ignore_empty and self.chunks[k].empty:
 | 
			
		||||
                continue
 | 
			
		||||
            x0 = min(x0, k[0])
 | 
			
		||||
            x1 = max(x1, k[0])
 | 
			
		||||
            y0 = min(y0, k[1])
 | 
			
		||||
            y1 = max(y1, k[1])
 | 
			
		||||
            z0 = min(z0, k[2])
 | 
			
		||||
            z1 = max(z1, k[2])
 | 
			
		||||
        return ((x0,y0,z0),(x1,y1,z1))
 | 
			
		||||
 | 
			
		||||
    def get_block_at(self, x, y, z):
 | 
			
		||||
        c = self.get_chunk(floor(x/16), floor(y/16), floor(z/16))
 | 
			
		||||
        if not c: return None
 | 
			
		||||
        return c.get_block_at(x%16, y%16, z%16)
 | 
			
		||||
 | 
			
		||||
    def set_block_at(self, x, y, z, block):
 | 
			
		||||
        c = self.get_chunk(floor(x/16), floor(y/16), floor(z/16))
 | 
			
		||||
        if not c: return None
 | 
			
		||||
        c.set_block_at(x%16, y%16, z%16, block)
 | 
			
		||||
 | 
			
		||||
    def check_loaded(self, position, steps):
 | 
			
		||||
        x, y, z = utils.pint(position)
 | 
			
		||||
        player_chunk = (x//16, 1, z//16)
 | 
			
		||||
        for i in range(steps): # TODO: base off render_distance?
 | 
			
		||||
            offset = utils.spiral(i)
 | 
			
		||||
            check = utils.padd(player_chunk, offset)
 | 
			
		||||
 | 
			
		||||
            if check not in self.chunks:
 | 
			
		||||
                return False
 | 
			
		||||
 | 
			
		||||
        return True
 | 
			
		||||
 | 
			
		||||
    def unload_chunks(self, position):
 | 
			
		||||
        x, y, z = utils.pint(position)
 | 
			
		||||
        player_chunk = (x//16, 0, z//16)
 | 
			
		||||
        loaded_chunks = list(self.chunks.keys())
 | 
			
		||||
 | 
			
		||||
        for chunk in loaded_chunks:
 | 
			
		||||
            check = (chunk[0], 0, chunk[2])
 | 
			
		||||
            if utils.phyp_king(player_chunk, check) > 20:
 | 
			
		||||
                del self.chunks[chunk]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ChunkNotLoadedException(Exception):
 | 
			
		||||
    def __str__(self):
 | 
			
		||||
        pos = self.args[0]
 | 
			
		||||
        return "Chunk at %d %d %d not loaded (yet?)"%(pos[0], pos[1], pos[2])
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ChatManager:
 | 
			
		||||
    def __init__(self, global_state):
 | 
			
		||||
        self.g = global_state
 | 
			
		||||
        self.handler = None
 | 
			
		||||
 | 
			
		||||
        self.g.connection.register_packet_listener(self.print_chat, clientbound.play.ChatMessagePacket)
 | 
			
		||||
 | 
			
		||||
    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']
 | 
			
		||||
        elif 'with' in data:
 | 
			
		||||
            if len(data['with']) >= 2:
 | 
			
		||||
                return '<{}> {}'.format(*[self.translate_chat(x) for x in data['with']])
 | 
			
		||||
            else:
 | 
			
		||||
                return self.translate_chat(data['with'][0])
 | 
			
		||||
        elif 'translate' in data:
 | 
			
		||||
            return data['translate']
 | 
			
		||||
        else:
 | 
			
		||||
            print(data)
 | 
			
		||||
            return '?'
 | 
			
		||||
 | 
			
		||||
    def print_chat(self, chat_packet):
 | 
			
		||||
        try:
 | 
			
		||||
            source = chat_packet.field_string('position')
 | 
			
		||||
            text = self.translate_chat(json.loads(chat_packet.json_data))
 | 
			
		||||
            print('[%s] %s'%(source, text))
 | 
			
		||||
        except Exception as ex:
 | 
			
		||||
            print('Exception %r on message (%s): %s' % (ex, chat_packet.field_string('position'), chat_packet.json_data))
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        if self.handler:
 | 
			
		||||
            self.handler((source, text))
 | 
			
		||||
 | 
			
		||||
    def set_handler(self, func):
 | 
			
		||||
        self.handler = func
 | 
			
		||||
 | 
			
		||||
    def send(self, text):
 | 
			
		||||
        if not text:
 | 
			
		||||
            # Prevents connection bug when sending empty chat message
 | 
			
		||||
            return
 | 
			
		||||
        packet = serverbound.play.ChatPacket()
 | 
			
		||||
        packet.message = text
 | 
			
		||||
        self.g.connection.write_packet(packet)
 | 
			
		||||
							
								
								
									
										460
									
								
								mosfet/protocol/packets.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										460
									
								
								mosfet/protocol/packets.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,460 @@
 | 
			
		||||
from math import floor
 | 
			
		||||
 | 
			
		||||
from minecraft.networking.packets import Packet, PacketBuffer
 | 
			
		||||
from minecraft.networking.types import (
 | 
			
		||||
    VarInt, Integer, UnsignedByte, Position, Vector, MutableRecord,
 | 
			
		||||
    attribute_alias, multi_attribute_alias, Long, Boolean, VarLong,
 | 
			
		||||
    Short, UnsignedLong, Byte, BlockFace, String, UUID, Angle, Double,
 | 
			
		||||
    Float, Direction, PositionAndLook
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
from protocol.types import Nbt, Slot, Entry, Trade
 | 
			
		||||
 | 
			
		||||
import blocks
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ChunkDataPacket(Packet):
 | 
			
		||||
    id = 0x20
 | 
			
		||||
    packet_name = 'chunk data'
 | 
			
		||||
    fields = 'x', 'bit_mask_y', 'z', 'full_chunk'
 | 
			
		||||
 | 
			
		||||
    def read(self, file_object):
 | 
			
		||||
        self.x = Integer.read(file_object)
 | 
			
		||||
        self.z = Integer.read(file_object)
 | 
			
		||||
        self.full_chunk = Boolean.read(file_object)
 | 
			
		||||
        self.bit_mask_y = VarInt.read(file_object)
 | 
			
		||||
        self.heightmaps = Nbt.read(file_object)
 | 
			
		||||
        self.biomes = []
 | 
			
		||||
        if self.full_chunk:
 | 
			
		||||
            biomes_length = VarInt.read(file_object)
 | 
			
		||||
            for i in range(biomes_length):
 | 
			
		||||
                self.biomes.append(VarInt.read(file_object))
 | 
			
		||||
        size = VarInt.read(file_object)
 | 
			
		||||
        self.data = file_object.read(size)
 | 
			
		||||
        size_entities = VarInt.read(file_object)
 | 
			
		||||
        self.entities = []
 | 
			
		||||
        for i in range(size_entities):
 | 
			
		||||
            self.entities.append(Nbt.read(file_object))
 | 
			
		||||
 | 
			
		||||
        self.decode_chunk_data()
 | 
			
		||||
 | 
			
		||||
    def write_fields(self, packet_buffer):
 | 
			
		||||
        Integer.send(self.x, packet_buffer)
 | 
			
		||||
        Integer.send(self.z, packet_buffer)
 | 
			
		||||
        Boolean.send(self.full_chunk, packet_buffer)
 | 
			
		||||
        VarInt.send(self.bit_mask_y, packet_buffer)
 | 
			
		||||
        Nbt.send(self.heightmaps, packet_buffer)
 | 
			
		||||
        if self.full_chunk:
 | 
			
		||||
            for i in range(1024):
 | 
			
		||||
                Integer.send(self.biomes[i], packet_buffer)
 | 
			
		||||
        VarInt.send(len(self.data), packet_buffer)
 | 
			
		||||
        packet_buffer.send(self.data)
 | 
			
		||||
        VarInt.send(len(self.entities), packet_buffer)
 | 
			
		||||
        for e in self.entities:
 | 
			
		||||
            Nbt.send(e, packet_buffer)
 | 
			
		||||
 | 
			
		||||
    def decode_chunk_data(self):
 | 
			
		||||
        packet_data = PacketBuffer()
 | 
			
		||||
        packet_data.send(self.data)
 | 
			
		||||
        packet_data.reset_cursor()
 | 
			
		||||
 | 
			
		||||
        self.chunks = {}
 | 
			
		||||
        for i in range(16): #0-15
 | 
			
		||||
            self.chunks[i] = Chunk(self.x, i, self.z)
 | 
			
		||||
            if self.bit_mask_y & (1 << i):
 | 
			
		||||
                self.chunks[i].read(packet_data)
 | 
			
		||||
        
 | 
			
		||||
        for e in self.entities:
 | 
			
		||||
            y = e['y']
 | 
			
		||||
            self.chunks[floor(y/16)].entities.append(e)
 | 
			
		||||
 | 
			
		||||
class Chunk:
 | 
			
		||||
    position = multi_attribute_alias(Vector, 'x', 'y', 'z')
 | 
			
		||||
 | 
			
		||||
    def __init__(self, x, y, z, empty=True):
 | 
			
		||||
        self.x = x
 | 
			
		||||
        self.y = y
 | 
			
		||||
        self.z = z
 | 
			
		||||
        self.empty = empty
 | 
			
		||||
        self.entities = []
 | 
			
		||||
        self.sub_index = {}
 | 
			
		||||
 | 
			
		||||
    def __repr__(self):
 | 
			
		||||
        return 'Chunk(%r, %r, %r)' % (self.x, self.y, self.z)
 | 
			
		||||
 | 
			
		||||
    def read(self, file_object):
 | 
			
		||||
        self.empty = False
 | 
			
		||||
        self.block_count = Short.read(file_object)
 | 
			
		||||
        self.bpb = UnsignedByte.read(file_object)
 | 
			
		||||
        if self.bpb <= 4:
 | 
			
		||||
            self.bpb = 4
 | 
			
		||||
 | 
			
		||||
        if self.bpb <= 8: # Indirect palette
 | 
			
		||||
            self.palette = []
 | 
			
		||||
            size = VarInt.read(file_object)
 | 
			
		||||
            for i in range(size):
 | 
			
		||||
                self.palette.append(VarInt.read(file_object))
 | 
			
		||||
        else: # Direct palette
 | 
			
		||||
            self.palette = None
 | 
			
		||||
 | 
			
		||||
        size = VarInt.read(file_object)
 | 
			
		||||
        longs = []
 | 
			
		||||
        for i in range(size):
 | 
			
		||||
            longs.append(UnsignedLong.read(file_object))
 | 
			
		||||
 | 
			
		||||
        self.blocks = []
 | 
			
		||||
        mask = (1 << self.bpb)-1
 | 
			
		||||
        bits_used = int(64 / self.bpb) * self.bpb
 | 
			
		||||
        for i in range(4096):
 | 
			
		||||
            l1 = int((i*self.bpb)/bits_used)
 | 
			
		||||
            offset = (i*self.bpb)%bits_used
 | 
			
		||||
            n = longs[l1] >> offset
 | 
			
		||||
            n &= mask
 | 
			
		||||
            if self.palette:
 | 
			
		||||
                n = self.palette[n]
 | 
			
		||||
            self.blocks.append(n)
 | 
			
		||||
 | 
			
		||||
            if n in blocks.INDEXED_IDS:
 | 
			
		||||
                if n not in self.sub_index:
 | 
			
		||||
                    self.sub_index[n] = []
 | 
			
		||||
                self.sub_index[n].append(i)
 | 
			
		||||
 | 
			
		||||
    def write_fields(self, packet_buffer):
 | 
			
		||||
        pass # TODO
 | 
			
		||||
 | 
			
		||||
    def get_block_at(self, x, y, z):
 | 
			
		||||
        if self.empty:
 | 
			
		||||
            return 0
 | 
			
		||||
        return self.blocks[x+y*256+z*16]
 | 
			
		||||
 | 
			
		||||
    def set_block_at(self, x, y, z, block):
 | 
			
		||||
        if self.empty:
 | 
			
		||||
            self.init_empty()
 | 
			
		||||
        self.blocks[x+y*256+z*16] = block
 | 
			
		||||
 | 
			
		||||
    def init_empty(self):
 | 
			
		||||
        self.blocks = []
 | 
			
		||||
        for i in range(4096):
 | 
			
		||||
            self.blocks.append(0)
 | 
			
		||||
        self.empty = False
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def origin(self):
 | 
			
		||||
        return self.position*16
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class AcknowledgePlayerDiggingPacket(Packet):
 | 
			
		||||
    id = 0x07
 | 
			
		||||
    packet_name = 'acknowledge player digging'
 | 
			
		||||
    definition = [
 | 
			
		||||
        {'location': Position},
 | 
			
		||||
        {'block': VarInt},
 | 
			
		||||
        {'status': VarInt},
 | 
			
		||||
        {'successful': Boolean},
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class BlockBreakAnimationPacket(Packet):
 | 
			
		||||
    id = 0x08
 | 
			
		||||
    packet_name = 'block break animation'
 | 
			
		||||
    definition = [
 | 
			
		||||
        {'entity_id': VarInt},
 | 
			
		||||
        {'location': Position},
 | 
			
		||||
        {'destroy_stage': Byte},
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SetSlotPacket(Packet):
 | 
			
		||||
    id = 0x15
 | 
			
		||||
    packet_name = 'set slot'
 | 
			
		||||
    definition = [
 | 
			
		||||
        {'window_id': Byte},
 | 
			
		||||
        {'slot': Short},
 | 
			
		||||
        {'slot_data': Slot},
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class PlayerDiggingPacket(Packet):
 | 
			
		||||
    # used when player mines / breaks blocks
 | 
			
		||||
    # https://wiki.vg/Protocol#Player_Digging
 | 
			
		||||
 | 
			
		||||
    id = 0x1B
 | 
			
		||||
    packet_name = 'player digging'
 | 
			
		||||
 | 
			
		||||
    definition = [
 | 
			
		||||
        {'status': VarInt},
 | 
			
		||||
        {'location': Position},
 | 
			
		||||
        {'face': VarInt},
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    STARTED = 0
 | 
			
		||||
    CANCELLED = 1
 | 
			
		||||
    FINISHED = 2
 | 
			
		||||
 | 
			
		||||
    # PlayerBlockPlacementPacket.Face is an alias for BlockFace.
 | 
			
		||||
    Face = BlockFace
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class PickItemPacket(Packet):
 | 
			
		||||
    # used when player picks item (middle click)
 | 
			
		||||
    # https://wiki.vg/Protocol#Pick_Item
 | 
			
		||||
 | 
			
		||||
    id = 0x18
 | 
			
		||||
    packet_name = 'pick item'
 | 
			
		||||
 | 
			
		||||
    definition = [
 | 
			
		||||
        {'slot_to_use': VarInt},
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class HeldItemChangePacket(Packet):
 | 
			
		||||
    # Sent when the player changes the slot selection
 | 
			
		||||
    # https://wiki.vg/Protocol#Held_Item_Change_.28serverbound.29
 | 
			
		||||
 | 
			
		||||
    id = 0x25
 | 
			
		||||
    packet_name = 'held item change'
 | 
			
		||||
 | 
			
		||||
    definition = [
 | 
			
		||||
        {'slot': Short},
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class OpenWindowPacket(Packet):
 | 
			
		||||
    # Sent to the client when it should open an inventory, such as a chest, workbench, or furnace
 | 
			
		||||
    # https://wiki.vg/Protocol#Open_Window
 | 
			
		||||
 | 
			
		||||
    id = 0x2D
 | 
			
		||||
    packet_name = 'open window'
 | 
			
		||||
 | 
			
		||||
    definition = [
 | 
			
		||||
        {'window_id': VarInt},
 | 
			
		||||
        {'window_type': VarInt},
 | 
			
		||||
        {'window_title': String},
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
class CloseWindowPacket(Packet):
 | 
			
		||||
    # Sent by the client when closing a window
 | 
			
		||||
    # https://wiki.vg/Protocol#Close_Window_.28serverbound.29
 | 
			
		||||
 | 
			
		||||
    id = 0x0A
 | 
			
		||||
    packet_name = 'close window'
 | 
			
		||||
 | 
			
		||||
    definition = [
 | 
			
		||||
        {'window_id': UnsignedByte},
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ClickWindowPacket(Packet):
 | 
			
		||||
    # Sent by the player when it clicks on a slot in a window
 | 
			
		||||
    # https://wiki.vg/Protocol#Click_Window
 | 
			
		||||
 | 
			
		||||
    id = 0x09
 | 
			
		||||
    packet_name = 'click window'
 | 
			
		||||
 | 
			
		||||
    definition = [
 | 
			
		||||
        {'window_id': UnsignedByte},
 | 
			
		||||
        {'slot': Short},
 | 
			
		||||
        {'button': Byte},
 | 
			
		||||
        {'action_number': Short},
 | 
			
		||||
        {'mode': VarInt},
 | 
			
		||||
        {'clicked_item': Slot},
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ClientWindowConfirmationPacket(Packet):
 | 
			
		||||
    # Sent by the server indicating whether a request from the client was accepted
 | 
			
		||||
    # https://wiki.vg/Protocol#Window_Confirmation_.28clientbound.29
 | 
			
		||||
 | 
			
		||||
    id = 0x11
 | 
			
		||||
    packet_name = 'client window confirmation'
 | 
			
		||||
 | 
			
		||||
    definition = [
 | 
			
		||||
        {'window_id': Byte},
 | 
			
		||||
        {'action_number': Short},
 | 
			
		||||
        {'accepted': Boolean},
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
class ServerWindowConfirmationPacket(Packet):
 | 
			
		||||
    # Sent by the client to confirm an unaccepted click
 | 
			
		||||
    # https://wiki.vg/Protocol#Window_Confirmation_.28serverbound.29
 | 
			
		||||
 | 
			
		||||
    id = 0x07
 | 
			
		||||
    packet_name = 'client window confirmation'
 | 
			
		||||
 | 
			
		||||
    definition = [
 | 
			
		||||
        {'window_id': Byte},
 | 
			
		||||
        {'action_number': Short},
 | 
			
		||||
        {'accepted': Boolean},
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class EntityMetadataPacket(Packet):
 | 
			
		||||
    # Updates one or more metadata properties for an existing entity
 | 
			
		||||
    # https://wiki.vg/Protocol#Entity_Metadata
 | 
			
		||||
 | 
			
		||||
    id = 0x44
 | 
			
		||||
    packet_name = 'entity metadata'
 | 
			
		||||
    fields = 'entity_id', 'metadata'
 | 
			
		||||
 | 
			
		||||
    class Entry:
 | 
			
		||||
        __slots__ = 'index', 'type', 'value'
 | 
			
		||||
 | 
			
		||||
    def read(self, file_object):
 | 
			
		||||
        self.entity_id = VarInt.read(file_object)
 | 
			
		||||
        self.metadata = []
 | 
			
		||||
        for _ in range(99):
 | 
			
		||||
            entry = Entry.read(file_object, self.context)
 | 
			
		||||
            if not entry: break
 | 
			
		||||
            self.metadata.append(entry)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class SpawnLivingEntityPacket(Packet):
 | 
			
		||||
    # Sent by the server when a living entity is spawned
 | 
			
		||||
    # https://wiki.vg/Protocol#Spawn_Entity
 | 
			
		||||
 | 
			
		||||
    id = 0x02
 | 
			
		||||
    packet_name = 'spawn living entity'
 | 
			
		||||
 | 
			
		||||
    definition = [
 | 
			
		||||
        {'entity_id': VarInt},
 | 
			
		||||
        {'entity_uuid': UUID},
 | 
			
		||||
        {'type': VarInt},
 | 
			
		||||
        {'x': Double},
 | 
			
		||||
        {'y': Double},
 | 
			
		||||
        {'z': Double},
 | 
			
		||||
        {'yaw': Angle},
 | 
			
		||||
        {'pitch': Angle},
 | 
			
		||||
        {'head_pitch': Angle},
 | 
			
		||||
        {'velocity_x': Short},
 | 
			
		||||
        {'velocity_y': Short},
 | 
			
		||||
        {'velocity_z': Short},
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
class EntityTeleport(Packet):
 | 
			
		||||
    # Sent by the server when an entity moves more than 8 blocks
 | 
			
		||||
    # https://wiki.vg/Protocol#Entity_Teleport
 | 
			
		||||
 | 
			
		||||
    id = 0x56
 | 
			
		||||
    packet_name = 'entity teleport'
 | 
			
		||||
 | 
			
		||||
    definition = [
 | 
			
		||||
        {'entity_id': VarInt},
 | 
			
		||||
        {'x': Double},
 | 
			
		||||
        {'y': Double},
 | 
			
		||||
        {'z': Double},
 | 
			
		||||
        {'yaw': Angle},
 | 
			
		||||
        {'pitch': Angle},
 | 
			
		||||
        {'on_ground': Boolean},
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
class EntityPositionRotationPacket(Packet):
 | 
			
		||||
    # Sent by the server when an entity rotates and moves
 | 
			
		||||
    # https://wiki.vg/Protocol#Entity_Position_and_Rotation
 | 
			
		||||
 | 
			
		||||
    id = 0x28
 | 
			
		||||
    packet_name = 'entity position and rotation'
 | 
			
		||||
 | 
			
		||||
    definition = [
 | 
			
		||||
        {'entity_id': VarInt},
 | 
			
		||||
        {'delta_x': Short},
 | 
			
		||||
        {'delta_y': Short},
 | 
			
		||||
        {'delta_z': Short},
 | 
			
		||||
        {'yaw': Angle},
 | 
			
		||||
        {'pitch': Angle},
 | 
			
		||||
        {'on_ground': Boolean},
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
class DestroyEntitiesPacket(Packet):
 | 
			
		||||
    # Sent by the server when a list of entities is to be destroyed on the client
 | 
			
		||||
    # https://wiki.vg/Protocol#Destroy_Entities
 | 
			
		||||
 | 
			
		||||
    id = 0x36
 | 
			
		||||
    packet_name = 'destroy entities'
 | 
			
		||||
    fields = 'count', 'entity_ids'
 | 
			
		||||
 | 
			
		||||
    def read(self, file_object):
 | 
			
		||||
        self.count = VarInt.read(file_object)
 | 
			
		||||
        self.entity_ids = []
 | 
			
		||||
        for _ in range(self.count):
 | 
			
		||||
            eid = VarInt.read(file_object)
 | 
			
		||||
            self.entity_ids.append(eid)
 | 
			
		||||
 | 
			
		||||
class EntityActionPacket(Packet):
 | 
			
		||||
    # Sent by the client when it performs an action
 | 
			
		||||
    # https://wiki.vg/Protocol#Entity_Action
 | 
			
		||||
 | 
			
		||||
    id = 0x1C
 | 
			
		||||
    packet_name = 'entity action'
 | 
			
		||||
 | 
			
		||||
    definition = [
 | 
			
		||||
        {'entity_id': VarInt},
 | 
			
		||||
        {'action_id': VarInt},
 | 
			
		||||
        {'jump_boost': VarInt},
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
class InteractEntityPacket(Packet):
 | 
			
		||||
    # Sent when the client attacks or right-clicks another entity
 | 
			
		||||
    # https://wiki.vg/Protocol#Interact_Entity
 | 
			
		||||
 | 
			
		||||
    id = 0x0E
 | 
			
		||||
    packet_name = 'interact entity'
 | 
			
		||||
 | 
			
		||||
    definition = [
 | 
			
		||||
        {'entity_id': VarInt},
 | 
			
		||||
        {'type': VarInt},
 | 
			
		||||
        #{'target_x': Float},
 | 
			
		||||
        #{'target_y': Float},
 | 
			
		||||
        #{'target_z': Float},
 | 
			
		||||
        {'hand': VarInt},
 | 
			
		||||
        {'sneaking': Boolean},
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
class TradeListPacket(Packet):
 | 
			
		||||
    # The list of trades a villager NPC is offering.
 | 
			
		||||
    # https://wiki.vg/Protocol#Trade_List
 | 
			
		||||
 | 
			
		||||
    id = 0x26
 | 
			
		||||
    packet_name = 'trade list'
 | 
			
		||||
    fields = (
 | 
			
		||||
        'window_id',
 | 
			
		||||
        'size',
 | 
			
		||||
        'trades',
 | 
			
		||||
        'villager_level',
 | 
			
		||||
        'experience',
 | 
			
		||||
        'is_regular_villager',
 | 
			
		||||
        'can_restock',
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    def read(self, file_object):
 | 
			
		||||
        self.window_id = VarInt.read(file_object)
 | 
			
		||||
        self.size = Byte.read(file_object)
 | 
			
		||||
        self.trades = []
 | 
			
		||||
        for _ in range(self.size):
 | 
			
		||||
            trade = Trade.read(file_object)
 | 
			
		||||
            self.trades.append(trade)
 | 
			
		||||
        self.villager_level = VarInt.read(file_object)
 | 
			
		||||
        self.experience = VarInt.read(file_object)
 | 
			
		||||
        self.is_regular_villager = Boolean.read(file_object)
 | 
			
		||||
        self.can_restock = Boolean.read(file_object)
 | 
			
		||||
 | 
			
		||||
class SelectTradePacket(Packet):
 | 
			
		||||
    # Sent when a player selects a specific trade offered by a villager
 | 
			
		||||
    # https://wiki.vg/Protocol#Select_Trade
 | 
			
		||||
 | 
			
		||||
    id = 0x23
 | 
			
		||||
    packet_name = 'select trade entity'
 | 
			
		||||
 | 
			
		||||
    definition = [
 | 
			
		||||
        {'selected_slot': VarInt},
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
class DisconnectPacket(Packet):
 | 
			
		||||
    # Sent by the server before it disconnects a client
 | 
			
		||||
    # https://wiki.vg/Protocol#Disconnect_.28play.29
 | 
			
		||||
 | 
			
		||||
    id = 0x19
 | 
			
		||||
    packet_name = 'disconnect'
 | 
			
		||||
 | 
			
		||||
    definition = [
 | 
			
		||||
        {'reason': String},
 | 
			
		||||
    ]
 | 
			
		||||
							
								
								
									
										217
									
								
								mosfet/protocol/types.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										217
									
								
								mosfet/protocol/types.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,217 @@
 | 
			
		||||
from __future__ import division
 | 
			
		||||
 | 
			
		||||
import struct
 | 
			
		||||
 | 
			
		||||
from minecraft.networking.types.basic import (
 | 
			
		||||
    Type, Byte, Short, Integer, Long, Float, Double,
 | 
			
		||||
    ShortPrefixedByteArray, Boolean, VarInt, TrailingByteArray,
 | 
			
		||||
    Position, String, UnsignedByte
 | 
			
		||||
)
 | 
			
		||||
from minecraft.networking.types.utility import Vector
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class IntegerPrefixedByteArray(Type):
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def read(file_object):
 | 
			
		||||
        length = Integer.read(file_object)
 | 
			
		||||
        return struct.unpack(str(length) + "s", file_object.read(length))[0]
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def send(value, socket):
 | 
			
		||||
        Integer.send(len(value), socket)
 | 
			
		||||
        socket.send(value)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
TAG_End = 0
 | 
			
		||||
TAG_Byte = 1
 | 
			
		||||
TAG_Short = 2
 | 
			
		||||
TAG_Int = 3
 | 
			
		||||
TAG_Long = 4
 | 
			
		||||
TAG_Float = 5
 | 
			
		||||
TAG_Double = 6
 | 
			
		||||
TAG_Byte_Array = 7
 | 
			
		||||
TAG_String = 8
 | 
			
		||||
TAG_List = 9
 | 
			
		||||
TAG_Compound = 10
 | 
			
		||||
TAG_Int_Array = 11
 | 
			
		||||
TAG_Long_Array = 12
 | 
			
		||||
 | 
			
		||||
class Nbt(Type):
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def read(file_object):
 | 
			
		||||
        type_id = Byte.read(file_object)
 | 
			
		||||
        if type_id != TAG_Compound:
 | 
			
		||||
            #raise Exception("Invalid NBT header")
 | 
			
		||||
            return None
 | 
			
		||||
        name = ShortPrefixedByteArray.read(file_object).decode('utf-8')
 | 
			
		||||
        a = Nbt.decode_tag(file_object, TAG_Compound)
 | 
			
		||||
        a['_name'] = name
 | 
			
		||||
        return a
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def decode_tag(file_object, type_id):
 | 
			
		||||
        if type_id == TAG_Byte:
 | 
			
		||||
            return Byte.read(file_object)
 | 
			
		||||
        elif type_id == TAG_Short:
 | 
			
		||||
            return Short.read(file_object)
 | 
			
		||||
        elif type_id == TAG_Int:
 | 
			
		||||
            return Integer.read(file_object)
 | 
			
		||||
        elif type_id == TAG_Long:
 | 
			
		||||
            return Long.read(file_object)
 | 
			
		||||
        elif type_id == TAG_Float:
 | 
			
		||||
            return Float.read(file_object)
 | 
			
		||||
        elif type_id == TAG_Double:
 | 
			
		||||
            return Double.read(file_object)
 | 
			
		||||
        elif type_id == TAG_Byte_Array:
 | 
			
		||||
            return IntegerPrefixedByteArray.read(file_object).decode('utf-8')
 | 
			
		||||
        elif type_id == TAG_String:
 | 
			
		||||
            return ShortPrefixedByteArray.read(file_object)
 | 
			
		||||
        elif type_id == TAG_List:
 | 
			
		||||
            list_type_id = Byte.read(file_object)
 | 
			
		||||
            size = Integer.read(file_object)
 | 
			
		||||
            a = []
 | 
			
		||||
            for i in range(size):
 | 
			
		||||
                a.append(Nbt.decode_tag(file_object, list_type_id))
 | 
			
		||||
            return a
 | 
			
		||||
        elif type_id == TAG_Compound:
 | 
			
		||||
            c = { }
 | 
			
		||||
            child_type_id = Byte.read(file_object)
 | 
			
		||||
            while child_type_id != TAG_End:
 | 
			
		||||
                child_name = ShortPrefixedByteArray.read(file_object).decode('utf-8')
 | 
			
		||||
                c[child_name] = Nbt.decode_tag(file_object, child_type_id)
 | 
			
		||||
                child_type_id = Byte.read(file_object)
 | 
			
		||||
            return c
 | 
			
		||||
        elif type_id == TAG_Int_Array:
 | 
			
		||||
            size = Integer.read(file_object)
 | 
			
		||||
            a = []
 | 
			
		||||
            for i in range(size):
 | 
			
		||||
                a.append(Integer.read(file_object))
 | 
			
		||||
            return a
 | 
			
		||||
        elif type_id == TAG_Long_Array:
 | 
			
		||||
            size = Integer.read(file_object)
 | 
			
		||||
            a = []
 | 
			
		||||
            for i in range(size):
 | 
			
		||||
                a.append(Long.read(file_object))
 | 
			
		||||
            return a
 | 
			
		||||
        else:
 | 
			
		||||
            raise Exception("Invalid NBT tag type")
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def send(value, socket):
 | 
			
		||||
        # TODO
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Slot(Type):
 | 
			
		||||
    def __init__(self, present, item_id=None, item_count=None, nbt=None):
 | 
			
		||||
        self.present = present
 | 
			
		||||
        self.item_id = item_id
 | 
			
		||||
        self.item_count = item_count
 | 
			
		||||
        self.nbt = nbt
 | 
			
		||||
 | 
			
		||||
    def __str__(self):
 | 
			
		||||
        return str(self.__dict__)
 | 
			
		||||
    def __repr__(self):
 | 
			
		||||
        if self.present:
 | 
			
		||||
            return 'Slot(present={}, item_id={}, item_count={}, nbt={}'.format(
 | 
			
		||||
                self.present, self.item_id, self.item_count, self.nbt)
 | 
			
		||||
        else:
 | 
			
		||||
            return 'Slot(present={})'.format(self.present)
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def read(file_object):
 | 
			
		||||
        present = Boolean.read(file_object)
 | 
			
		||||
        if present:
 | 
			
		||||
            item_id = VarInt.read(file_object)
 | 
			
		||||
            item_count = Byte.read(file_object)
 | 
			
		||||
            nbt = Nbt.read(file_object)
 | 
			
		||||
        else:
 | 
			
		||||
            item_id = None
 | 
			
		||||
            item_count = None
 | 
			
		||||
            nbt = None
 | 
			
		||||
        return Slot(present, item_id, item_count, nbt)
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def send(value, socket):
 | 
			
		||||
        Boolean.send(value.present, socket)
 | 
			
		||||
        if value.present:
 | 
			
		||||
            VarInt.send(value.item_id, socket)
 | 
			
		||||
            Byte.send(value.item_count, socket)
 | 
			
		||||
            Byte.send(0x00, socket)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Entry(Type):
 | 
			
		||||
    types = {
 | 
			
		||||
        0: Byte,
 | 
			
		||||
        1: VarInt,
 | 
			
		||||
        2: Float,
 | 
			
		||||
        3: String,
 | 
			
		||||
        5: Boolean,
 | 
			
		||||
        6: Slot,
 | 
			
		||||
        7: Boolean,
 | 
			
		||||
        9: Position,
 | 
			
		||||
        18: VarInt,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    def __init__(self, index, type, value):
 | 
			
		||||
        self.index = index
 | 
			
		||||
        self.type = type
 | 
			
		||||
        self.value = value
 | 
			
		||||
 | 
			
		||||
    def __str__(self):
 | 
			
		||||
        return str(self.__dict__)
 | 
			
		||||
    def __repr__(self):
 | 
			
		||||
        return 'Entry(index={}, type={}, value={})'.format(
 | 
			
		||||
            self.index, self.type, self.value)
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def read(file_object, context):
 | 
			
		||||
        index = UnsignedByte.read(file_object)
 | 
			
		||||
        if index == 0xff: return None
 | 
			
		||||
        type = VarInt.read(file_object)
 | 
			
		||||
        try:
 | 
			
		||||
            value = Entry.types[type].read(file_object)
 | 
			
		||||
        except TypeError:
 | 
			
		||||
            value = Entry.types[type].read_with_context(file_object, context)
 | 
			
		||||
        except KeyError:
 | 
			
		||||
            return None
 | 
			
		||||
        return Entry(index, type, value)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Trade(Type):
 | 
			
		||||
    fields = (
 | 
			
		||||
        'input_item_1',
 | 
			
		||||
        'output_item',
 | 
			
		||||
        'has_second_item',
 | 
			
		||||
        'input_item_2',
 | 
			
		||||
        'trade_disabled',
 | 
			
		||||
        'num_uses',
 | 
			
		||||
        'max_num_uses',
 | 
			
		||||
        'xp',
 | 
			
		||||
        'special_price',
 | 
			
		||||
        'price_multiplier',
 | 
			
		||||
        'demand',
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    def __str__(self):
 | 
			
		||||
        return str(self.__dict__)
 | 
			
		||||
    def __repr__(self):
 | 
			
		||||
        inner_str = ', '.join('%s=%s' % (a, getattr(self, a, None)) for a in self.fields if hasattr(self, a))
 | 
			
		||||
        return 'Trade(%s)' % inner_str
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def read(file_object):
 | 
			
		||||
        trade = Trade()
 | 
			
		||||
        trade.input_item_1 = Slot.read(file_object)
 | 
			
		||||
        trade.output_item = Slot.read(file_object)
 | 
			
		||||
        trade.has_second_item = Boolean.read(file_object)
 | 
			
		||||
        if trade.has_second_item:
 | 
			
		||||
            trade.input_item_2 = Slot.read(file_object)
 | 
			
		||||
        trade.trade_disabled = Boolean.read(file_object)
 | 
			
		||||
        trade.num_uses = Integer.read(file_object)
 | 
			
		||||
        trade.max_num_uses = Integer.read(file_object)
 | 
			
		||||
        trade.xp = Integer.read(file_object)
 | 
			
		||||
        trade.special_price = Integer.read(file_object)
 | 
			
		||||
        trade.price_multiplier = Float.read(file_object)
 | 
			
		||||
        trade.demand = Integer.read(file_object)
 | 
			
		||||
        return trade
 | 
			
		||||
		Reference in New Issue
	
	Block a user