from minecraft.networking.packets import ( Packet, AbstractKeepAlivePacket, AbstractPluginMessagePacket ) from minecraft.networking.types import ( Integer, FixedPointInteger, UnsignedByte, Byte, Boolean, UUID, Short, VarInt, Double, Float, String, Enum, Difficulty, Dimension, Gamemode ) from .combat_event_packet import CombatEventPacket from .map_packet import MapPacket from .player_list_item_packet import PlayerListItemPacket from .player_position_and_look_packet import PlayerPositionAndLookPacket from .spawn_object_packet import SpawnObjectPacket from .block_change_packet import BlockChangePacket, MultiBlockChangePacket from .explosion_packet import ExplosionPacket # Formerly known as state_playing_clientbound. def get_packets(context): packets = { KeepAlivePacket, JoinGamePacket, ServerDifficultyPacket, ChatMessagePacket, PlayerPositionAndLookPacket, MapPacket, PlayerListItemPacket, DisconnectPacket, SpawnPlayerPacket, EntityVelocityPacket, UpdateHealthPacket, CombatEventPacket, ExplosionPacket, SpawnObjectPacket, BlockChangePacket, MultiBlockChangePacket, RespawnPacket, PluginMessagePacket, } if context.protocol_version <= 47: packets |= { SetCompressionPacket, } return packets class KeepAlivePacket(AbstractKeepAlivePacket): @staticmethod def get_id(context): return 0x21 if context.protocol_version >= 389 else \ 0x20 if context.protocol_version >= 345 else \ 0x1F if context.protocol_version >= 332 else \ 0x20 if context.protocol_version >= 318 else \ 0x1F if context.protocol_version >= 107 else \ 0x00 class JoinGamePacket(Packet): @staticmethod def get_id(context): return 0x25 if context.protocol_version >= 389 else \ 0x24 if context.protocol_version >= 345 else \ 0x23 if context.protocol_version >= 332 else \ 0x24 if context.protocol_version >= 318 else \ 0x23 if context.protocol_version >= 107 else \ 0x01 packet_name = "join game" get_definition = staticmethod(lambda context: [ {'entity_id': Integer}, {'game_mode': UnsignedByte}, {'dimension': Integer if context.protocol_version >= 108 else Byte}, {'difficulty': UnsignedByte}, {'max_players': UnsignedByte}, {'level_type': String}, {'reduced_debug_info': Boolean} ]) # JoinGamePacket.Difficulty is an alias for Difficulty Difficulty = Difficulty # JoinGamePacket.Gamemode is an alias for Gamemode Gamemode = Gamemode # JoinGamePacket.Dimension is an alias for Dimension Dimension = Dimension class ServerDifficultyPacket(Packet): @staticmethod def get_id(context): return 0x0D if context.protocol_version >= 332 else \ 0x0E if context.protocol_version >= 318 else \ 0x0D if context.protocol_version >= 70 else \ 0x41 packet_name = 'server difficulty' get_definition = staticmethod(lambda context: [ {'difficulty_enum': UnsignedByte} ]) # ServerDifficultyPacket.Difficulty is an alias for Difficulty Difficulty = Difficulty class ChatMessagePacket(Packet): @staticmethod def get_id(context): return 0x0E if context.protocol_version >= 343 else \ 0x0F if context.protocol_version >= 332 else \ 0x10 if context.protocol_version >= 317 else \ 0x0F if context.protocol_version >= 107 else \ 0x02 packet_name = "chat message" definition = [ {'json_data': String}, {'position': Byte}] class Position(Enum): CHAT = 0 # A player-initiated chat message. SYSTEM = 1 # The result of running a command. GAME_INFO = 2 # Displayed above the hotbar in vanilla clients. class DisconnectPacket(Packet): @staticmethod def get_id(context): return 0x1B if context.protocol_version >= 345 else \ 0x1A if context.protocol_version >= 332 else \ 0x1B if context.protocol_version >= 318 else \ 0x1A if context.protocol_version >= 107 else \ 0x40 packet_name = "disconnect" definition = [ {'json_data': String}] class SetCompressionPacket(Packet): # Note: removed between protocol versions 47 and 107. id = 0x46 packet_name = "set compression" definition = [ {'threshold': VarInt}] class SpawnPlayerPacket(Packet): @staticmethod def get_id(context): return 0x05 if context.protocol_version >= 67 else \ 0x0C packet_name = 'spawn player' get_definition = staticmethod(lambda context: [ {'entity_id': VarInt}, {'player_UUID': UUID}, {'x': Double} if context.protocol_version >= 100 else {'x': FixedPointInteger}, {'y': Double} if context.protocol_version >= 100 else {'y': FixedPointInteger}, {'z': Double} if context.protocol_version >= 100 else {'z': FixedPointInteger}, {'yaw': Float}, {'pitch': Float}, # TODO: read entity metadata {'current_item': Short} if context.protocol_version <= 49 else {} ]) class EntityVelocityPacket(Packet): @staticmethod def get_id(context): return 0x41 if context.protocol_version >= 389 else \ 0x40 if context.protocol_version >= 352 else \ 0x3F if context.protocol_version >= 345 else \ 0x3E if context.protocol_version >= 336 else \ 0x3D if context.protocol_version >= 332 else \ 0x3B if context.protocol_version >= 86 else \ 0x3C if context.protocol_version >= 77 else \ 0x3B if context.protocol_version >= 67 else \ 0x12 packet_name = 'entity velocity' get_definition = staticmethod(lambda context: [ {'entity_id': VarInt}, {'velocity_x': Short}, {'velocity_y': Short}, {'velocity_z': Short} ]) class UpdateHealthPacket(Packet): @staticmethod def get_id(context): return 0x44 if context.protocol_version >= 389 else \ 0x43 if context.protocol_version >= 352 else \ 0x42 if context.protocol_version >= 345 else \ 0x41 if context.protocol_version >= 336 else \ 0x40 if context.protocol_version >= 318 else \ 0x3E if context.protocol_version >= 86 else \ 0x3F if context.protocol_version >= 77 else \ 0x3E if context.protocol_version >= 67 else \ 0x06 packet_name = 'update health' get_definition = staticmethod(lambda context: [ {'health': Float}, {'food': VarInt}, {'food_saturation': Float} ]) class RespawnPacket(Packet): @staticmethod def get_id(context): return 0x38 if context.protocol_version >= 389 else \ 0x37 if context.protocol_version >= 352 else \ 0x36 if context.protocol_version >= 345 else \ 0x35 if context.protocol_version >= 336 else \ 0x34 if context.protocol_version >= 332 else \ 0x35 if context.protocol_version >= 318 else \ 0x33 if context.protocol_version >= 70 else \ 0x07 packet_name = 'respawn' get_definition = staticmethod(lambda context: [ {'dimension': Integer}, {'difficulty': UnsignedByte}, {'game_mode': UnsignedByte}, {'level_type': String} ]) # RespawnPacket.Difficulty is an alias for Difficulty. Difficulty = Difficulty # RespawnPacket.Dimension is an alias for Dimension. Dimension = Dimension # RespawnPacket.Gamemode is an alias for Gamemode. Gamemode = Gamemode class PluginMessagePacket(AbstractPluginMessagePacket): @staticmethod def get_id(context): return 0x19 if context.protocol_version >= 345 else \ 0x18 if context.protocol_version >= 332 else \ 0x19 if context.protocol_version >= 318 else \ 0x18 if context.protocol_version >= 70 else \ 0x3F