From d24b6eaded6f8a1ee794996bc73b96898db01fe1 Mon Sep 17 00:00:00 2001 From: joodicator Date: Mon, 13 May 2019 21:58:59 +0200 Subject: [PATCH] Update SoundEffectPacket and UseItemPacket to 1.14; misc improvements --- .../packets/clientbound/play/__init__.py | 45 ++-------- .../clientbound/play/sound_effect_packet.py | 83 +++++++++++++++++++ .../packets/serverbound/play/__init__.py | 10 ++- minecraft/networking/types/utility.py | 2 +- tests/test_packets.py | 7 ++ 5 files changed, 104 insertions(+), 43 deletions(-) create mode 100644 minecraft/networking/packets/clientbound/play/sound_effect_packet.py diff --git a/minecraft/networking/packets/clientbound/play/__init__.py b/minecraft/networking/packets/clientbound/play/__init__.py index b7c685e..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. @@ -38,12 +39,15 @@ def get_packets(context): RespawnPacket, PluginMessagePacket, PlayerListHeaderAndFooterPacket, - SoundEffectPacket, } if context.protocol_version <= 47: packets |= { SetCompressionPacket, } + if context.protocol_version >= 94: + packets |= { + SoundEffectPacket, + } return packets @@ -287,40 +291,3 @@ class PlayerListHeaderAndFooterPacket(Packet): definition = [ {'header': String}, {'footer': String}] - - -class SoundEffectPacket(Packet): - @staticmethod - def get_id(context): - return 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 if context.protocol_version >= 94 else \ - 0x23 if context.protocol_version >= 72 else \ - 0x29 if context.protocol_version >= 0 else \ - 0x2C - - packet_name = 'sound effect' - - @staticmethod - def get_definition(context): - definition = [ - {'sound_id': VarInt}, - {'sound_category': VarInt} if context.protocol_version >= 95 else {}, - {'parrotted_entity_type': String} if 326 > context.protocol_version >= 321 else {}, - {'effect_position_x': Integer}, - {'effect_position_y': Integer}, - {'effect_position_z': Integer}, - {'volume': Float}, - {'pitch': Float if context.protocol_version >= 201 else UnsignedByte} - ] - - if 326 > context.protocol_version >= 321: - # Swaps the position of sound category and -id - definition[0], definition[1] = definition[1], definition[0] - return definition 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 7b09f82..54a7fdd 100644 --- a/minecraft/networking/packets/serverbound/play/__init__.py +++ b/minecraft/networking/packets/serverbound/play/__init__.py @@ -21,8 +21,11 @@ def get_packets(context): ClientSettingsPacket, PluginMessagePacket, PlayerBlockPlacementPacket, - UseItemPacket, } + if context.protocol_version >= 69: + packets |= { + UseItemPacket, + } if context.protocol_version >= 107: packets |= { TeleportConfirmPacket, @@ -217,7 +220,9 @@ class PlayerBlockPlacementPacket(Packet): class UseItemPacket(Packet): @staticmethod def get_id(context): - return 0x2A if context.protocol_version >= 389 else \ + 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 \ @@ -232,4 +237,3 @@ class UseItemPacket(Packet): {'hand': VarInt}]) Hand = RelativeHand - HAND_MAIN, HAND_OFF = Hand.MAIN, Hand.OFF # For backward compatibility. 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()