diff --git a/minecraft/networking/packets/clientbound/play/__init__.py b/minecraft/networking/packets/clientbound/play/__init__.py index b13a576..7d31b44 100644 --- a/minecraft/networking/packets/clientbound/play/__init__.py +++ b/minecraft/networking/packets/clientbound/play/__init__.py @@ -4,7 +4,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 + VarInt, Double, Float, String, Enum, Difficulty, Dimension, GameMode, ) from .combat_event_packet import CombatEventPacket @@ -14,6 +14,7 @@ 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 +from .sound_effect_packet import SoundEffectPacket # Formerly known as state_playing_clientbound. @@ -43,6 +44,10 @@ def get_packets(context): packets |= { SetCompressionPacket, } + if context.protocol_version >= 94: + packets |= { + SoundEffectPacket, + } return packets diff --git a/minecraft/networking/packets/clientbound/play/sound_effect_packet.py b/minecraft/networking/packets/clientbound/play/sound_effect_packet.py new file mode 100644 index 0000000..50ded23 --- /dev/null +++ b/minecraft/networking/packets/clientbound/play/sound_effect_packet.py @@ -0,0 +1,83 @@ +from minecraft.networking.packets import Packet +from minecraft.networking.types import ( + VarInt, String, Float, Byte, Type, Integer, Vector, Enum, +) + +__all__ = 'SoundEffectPacket', + + +class SoundEffectPacket(Packet): + @staticmethod + def get_id(context): + return 0x51 if context.protocol_version >= 471 else \ + 0x4D if context.protocol_version >= 461 else \ + 0x4E if context.protocol_version >= 451 else \ + 0x4E if context.protocol_version >= 451 else \ + 0x4D if context.protocol_version >= 389 else \ + 0x4C if context.protocol_version >= 352 else \ + 0x4B if context.protocol_version >= 345 else \ + 0x4A if context.protocol_version >= 343 else \ + 0x49 if context.protocol_version >= 336 else \ + 0x48 if context.protocol_version >= 318 else \ + 0x46 if context.protocol_version >= 110 else \ + 0x47 + + packet_name = 'sound effect' + + @staticmethod + def get_definition(context): + return [ + ({'sound_category': VarInt} + if 326 > context.protocol_version >= 321 else {}), + {'sound_id': VarInt}, + ({'sound_category': VarInt} + if 95 <= context.protocol_version < 321 + or context.protocol_version >= 326 else {}), + ({'parroted_entity_type': String} + if 326 > context.protocol_version >= 321 else {}), + {'effect_position': SoundEffectPacket.EffectPosition}, + {'volume': Float}, + {'pitch': SoundEffectPacket.Pitch}, + ] + + class SoundCategory(Enum): + MASTER = 0 + MUSIC = 1 + RECORDS = 2 + WEATHER = 3 + BLOCKS = 4 + HOSTILE = 5 + NEUTRAL = 6 + PLAYERS = 7 + AMBIENT = 8 + VOICE = 9 + + class EffectPosition(Type): + @classmethod + def read(cls, file_object): + return Vector(*(Integer.read(file_object) / 8.0 for i in range(3))) + + @classmethod + def send(cls, value, socket): + for coordinate in value: + Integer.send(int(coordinate * 8), socket) + + class Pitch(Type): + @staticmethod + def read_with_context(file_object, context): + if context.protocol_version >= 201: + value = Float.read(file_object) + else: + value = Byte.read(file_object) / 63.5 + if context.protocol_version < 204: + value /= 63.5 + return value + + @staticmethod + def send_with_context(value, socket, context): + if context.protocol_version < 204: + value *= 63.5 + if context.protocol_version >= 201: + Float.send(value, socket) + else: + Byte.send(int(value), socket) diff --git a/minecraft/networking/packets/serverbound/play/__init__.py b/minecraft/networking/packets/serverbound/play/__init__.py index e72de2d..54a7fdd 100644 --- a/minecraft/networking/packets/serverbound/play/__init__.py +++ b/minecraft/networking/packets/serverbound/play/__init__.py @@ -22,6 +22,10 @@ def get_packets(context): PluginMessagePacket, PlayerBlockPlacementPacket, } + if context.protocol_version >= 69: + packets |= { + UseItemPacket, + } if context.protocol_version >= 107: packets |= { TeleportConfirmPacket, @@ -211,3 +215,25 @@ class PlayerBlockPlacementPacket(Packet): # PlayerBlockPlacementPacket.Face is an alias for BlockFace. Face = BlockFace + + +class UseItemPacket(Packet): + @staticmethod + def get_id(context): + return 0x2D if context.protocol_version >= 468 else \ + 0x2C if context.protocol_version >= 464 else \ + 0x2A if context.protocol_version >= 389 else \ + 0x28 if context.protocol_version >= 386 else \ + 0x20 if context.protocol_version >= 345 else \ + 0x1F if context.protocol_version >= 343 else \ + 0x20 if context.protocol_version >= 332 else \ + 0x1F if context.protocol_version >= 318 else \ + 0x1D if context.protocol_version >= 94 else \ + 0x1A if context.protocol_version >= 70 else \ + 0x08 + + packet_name = "use item" + get_definition = staticmethod(lambda context: [ + {'hand': VarInt}]) + + Hand = RelativeHand diff --git a/minecraft/networking/types/utility.py b/minecraft/networking/types/utility.py index 891e0e8..1b94918 100644 --- a/minecraft/networking/types/utility.py +++ b/minecraft/networking/types/utility.py @@ -6,7 +6,7 @@ from collections import namedtuple __all__ = ( - 'Vector', 'MutableRecord', 'descriptor', + 'Vector', 'MutableRecord', 'PositionAndLook', 'descriptor', ) diff --git a/tests/test_packets.py b/tests/test_packets.py index c01543e..72d113a 100644 --- a/tests/test_packets.py +++ b/tests/test_packets.py @@ -221,6 +221,13 @@ class TestReadWritePackets(unittest.TestCase): self._test_read_write_packet(packet) self._test_read_write_packet(packet2) + def test_sound_effect_packet(self): + SoundCategory = clientbound.play.SoundEffectPacket.SoundCategory + packet = clientbound.play.SoundEffectPacket( + sound_category=SoundCategory.NEUTRAL, sound_id=545, + effect_position=Vector(0.125, 300.0, 50.5), volume=0.75, pitch=1.5) + self._test_read_write_packet(packet) + def _test_read_write_packet(self, packet_in): packet_in.context = self.context packet_buffer = PacketBuffer()