diff --git a/minecraft/networking/packets/serverbound/play/__init__.py b/minecraft/networking/packets/serverbound/play/__init__.py index 03c7dac..b27ce49 100644 --- a/minecraft/networking/packets/serverbound/play/__init__.py +++ b/minecraft/networking/packets/serverbound/play/__init__.py @@ -3,7 +3,8 @@ from minecraft.networking.packets import ( ) from minecraft.networking.types import ( - Double, Float, Boolean, VarInt, String, Enum, RelativeHand + Double, Float, Boolean, VarInt, String, Byte, Position, Enum, + RelativeHand, BlockFace ) from .client_settings_packet import ClientSettingsPacket @@ -19,6 +20,7 @@ def get_packets(context): ClientStatusPacket, ClientSettingsPacket, PluginMessagePacket, + PlayerBlockPlacementPacket, } if context.protocol_version >= 107: packets |= { @@ -104,12 +106,9 @@ class AnimationPacket(Packet): packet_name = "animation" get_definition = staticmethod(lambda context: [ {'hand': VarInt} if context.protocol_version >= 107 else {}]) - field_enum = classmethod( - lambda cls, field: RelativeHand if field == 'hand' else None) - # These fields are retained for backward compatibility. - HAND_MAIN = RelativeHand.MAIN - HAND_OFF = RelativeHand.OFF + Hand = RelativeHand + HAND_MAIN, HAND_OFF = Hand.MAIN, Hand.OFF # For backward compatibility. class ClientStatusPacket(Packet, Enum): @@ -144,3 +143,46 @@ class PluginMessagePacket(AbstractPluginMessagePacket): 0x0A if context.protocol_version >= 317 else \ 0x09 if context.protocol_version >= 94 else \ 0x17 + + +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 = packet.Face.TOP # See networking.types.BlockFace. + packet.hand = packet.Hand.MAIN # See networking.types.RelativeHand. + Next values are called in-block coordinates. + They are calculated using raytracing. From 0 to 1 (from Minecraft 1.11) + or integers from 0 to 15 or, in a special case, -1 (1.10.2 and earlier). + packet.x = 0.725 + packet.y = 0.125 + packet.z = 0.555""" + + @staticmethod + def get_id(context): + return 0x1F if context.protocol_version >= 345 else \ + 0x1E if context.protocol_version >= 343 else \ + 0x1F if context.protocol_version >= 332 else \ + 0x1E if context.protocol_version >= 318 else \ + 0x1C if context.protocol_version >= 94 else \ + 0x08 + + packet_name = 'player block placement' + + @staticmethod + def get_definition(context): + return [ + {'location': Position}, + {'face': VarInt if context.protocol_version >= 69 else Byte}, + {'hand': VarInt}, + {'x': Float if context.protocol_version >= 309 else Byte}, + {'y': Float if context.protocol_version >= 309 else Byte}, + {'z': Float if context.protocol_version >= 309 else Byte}, + ] + + # PlayerBlockPlacementPacket.Hand is an alias for RelativeHand. + Hand = RelativeHand + + # PlayerBlockPlacementPacket.Face is an alias for BlockFace. + Face = BlockFace diff --git a/minecraft/networking/types.py b/minecraft/networking/types.py index 7bd2e9f..52b69a5 100644 --- a/minecraft/networking/types.py +++ b/minecraft/networking/types.py @@ -307,3 +307,32 @@ class AbsoluteHand(Enum): class RelativeHand(Enum): MAIN = 0 OFF = 1 + + +# Designation of one of a block's 6 faces. +class BlockFace(Enum): + BOTTOM = 0 # -Y + TOP = 1 # +Y + NORTH = 2 # -Z + SOUTH = 3 # +Z + WEST = 4 # -X + EAST = 5 # +X + + # A dict mapping Position tuples to the corresponding BlockFace values. + # When accessing this dict, plain tuples also match. For example: + # >>> BlockFace.from_vector[0, 0, -1] == BlockFace.NORTH + # True + from_vector = { + Position(0, -1, 0): BOTTOM, + Position(0, +1, 0): TOP, + Position(0, 0, -1): NORTH, + Position(0, 0, +1): SOUTH, + Position(-1, 0, 0): WEST, + Position(+1, 0, 0): EAST, + } + + # A dict mapping BlockFace values to unit Position tuples. + # This is the inverse mapping of face_by_position. For example: + # >>> BlockFace.to_vector[BlockFace.NORTH] + # Position(x=0, y=0, z=-1) + to_vector = {fce: pos for (pos, fce) in from_vector.items()}