diff --git a/minecraft/networking/packets/clientbound/play/__init__.py b/minecraft/networking/packets/clientbound/play/__init__.py index 5e1137e..692507b 100644 --- a/minecraft/networking/packets/clientbound/play/__init__.py +++ b/minecraft/networking/packets/clientbound/play/__init__.py @@ -5,6 +5,7 @@ from minecraft.networking.packets import ( from minecraft.networking.types import ( Integer, FixedPointInteger, UnsignedByte, Byte, Boolean, UUID, Short, VarInt, Double, Float, String, Enum, Difficulty, Dimension, GameMode, + Angle ) from .combat_event_packet import CombatEventPacket @@ -40,6 +41,7 @@ def get_packets(context): RespawnPacket, PluginMessagePacket, PlayerListHeaderAndFooterPacket, + EntityLookPacket } if context.protocol_version <= 47: packets |= { @@ -178,8 +180,8 @@ class SpawnPlayerPacket(Packet): else {'y': FixedPointInteger}, {'z': Double} if context.protocol_version >= 100 else {'z': FixedPointInteger}, - {'yaw': Float}, - {'pitch': Float}, + {'yaw': Angle}, + {'pitch': Angle}, # TODO: read entity metadata {'current_item': Short} if context.protocol_version <= 49 else {} ]) @@ -296,3 +298,22 @@ class PlayerListHeaderAndFooterPacket(Packet): definition = [ {'header': String}, {'footer': String}] + + +class EntityLookPacket(Packet): + @staticmethod + def get_id(context): + return 0x2A if context.protocol_version >= 389 else \ + 0x29 if context.protocol_version >= 345 else \ + 0x28 if context.protocol_version >= 318 else \ + 0x27 if context.protocol_version >= 94 else \ + 0x28 if context.protocol_version >= 70 else \ + 0x16 + + packet_name = 'entity look' + definition = [ + {'entity_id': VarInt}, + {'yaw': Angle}, + {'pitch': Angle}, + {'on_ground': Boolean} + ] \ No newline at end of file diff --git a/minecraft/networking/packets/clientbound/play/spawn_object_packet.py b/minecraft/networking/packets/clientbound/play/spawn_object_packet.py index c8e48b7..3c93de9 100644 --- a/minecraft/networking/packets/clientbound/play/spawn_object_packet.py +++ b/minecraft/networking/packets/clientbound/play/spawn_object_packet.py @@ -2,7 +2,7 @@ from minecraft.networking.packets import Packet from minecraft.networking.types.utility import descriptor from minecraft.networking.types import ( - VarInt, UUID, Byte, Double, Integer, UnsignedByte, Short, Enum, Vector, + VarInt, UUID, Byte, Double, Integer, Angle, Short, Enum, Vector, PositionAndLook, attribute_alias, multi_attribute_alias, ) @@ -103,7 +103,7 @@ class SpawnObjectPacket(Packet): for attr in 'x', 'y', 'z': setattr(self, attr, xyz_type.read(file_object)) for attr in 'pitch', 'yaw': - setattr(self, attr, UnsignedByte.read(file_object)) + setattr(self, attr, Angle.read(file_object)) self.data = Integer.read(file_object) if self.context.protocol_version >= 49 or self.data > 0: @@ -125,7 +125,7 @@ class SpawnObjectPacket(Packet): for coord in self.x, self.y, self.z: xyz_type.send(coord, packet_buffer) for coord in self.pitch, self.yaw: - UnsignedByte.send(coord, packet_buffer) + Angle.send(coord, packet_buffer) Integer.send(self.data, packet_buffer) if self.context.protocol_version >= 49 or self.data > 0: diff --git a/minecraft/networking/types/basic.py b/minecraft/networking/types/basic.py index ab43198..5d48b23 100644 --- a/minecraft/networking/types/basic.py +++ b/minecraft/networking/types/basic.py @@ -11,9 +11,10 @@ from .utility import Vector __all__ = ( 'Type', 'Boolean', 'UnsignedByte', 'Byte', 'Short', 'UnsignedShort', - 'Integer', 'FixedPointInteger', 'VarInt', 'Long', 'UnsignedLong', 'Float', - 'Double', 'ShortPrefixedByteArray', 'VarIntPrefixedByteArray', - 'TrailingByteArray', 'String', 'UUID', 'Position', + 'Integer', 'FixedPointInteger', 'Angle', 'VarInt', 'Long', + 'UnsignedLong', 'Float', 'Double', 'ShortPrefixedByteArray', + 'VarIntPrefixedByteArray', 'TrailingByteArray', 'String', 'UUID', + 'Position', ) @@ -117,6 +118,17 @@ class FixedPointInteger(Type): Integer.send(int(value * 32), socket) +class Angle(Type): + @staticmethod + def read(file_object): + # Linearly transform angle in steps of 1/256 into steps of 1/360 + return 360 * UnsignedByte.read(file_object) / 255 + + def send(value, socket): + # Normalize angle between 0 and 255 and convert to int. + UnsignedByte.send(int(255 * (value / 360)), socket) + + class VarInt(Type): @staticmethod def read(file_object): @@ -311,3 +323,4 @@ class Position(Type, Vector): if context.protocol_version >= 443 else (x & 0x3FFFFFF) << 38 | (y & 0xFFF) << 26 | (z & 0x3FFFFFF)) UnsignedLong.send(value, socket) +