From e1626ea7e048ac7c66713675c5c5e562ed0f6d30 Mon Sep 17 00:00:00 2001 From: joo Date: Mon, 28 Mar 2016 05:08:01 +0100 Subject: [PATCH] Add TeleportConfirmPacket and AnimationPacketServerBound packets. Add PlayerPositionAndLookPacket.PositionAndLook utility class. Fix a bug causing Disconnect packets to be ignored in some circumstances. --- minecraft/networking/connection.py | 23 +++++++++---- minecraft/networking/packets.py | 52 ++++++++++++++++++++++++++++-- 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/minecraft/networking/connection.py b/minecraft/networking/connection.py index a427eff..76add07 100644 --- a/minecraft/networking/connection.py +++ b/minecraft/networking/connection.py @@ -225,14 +225,19 @@ class NetworkingThread(threading.Thread): while True: if self.interrupt: break + # Attempt to write out as many as 300 packets as possible every # 0.05 seconds (20 ticks per second) num_packets = 0 self.connection._write_lock.acquire() - while self.connection._pop_packet(): - num_packets += 1 - if num_packets >= 300: - break + try: + while self.connection._pop_packet(): + num_packets += 1 + if num_packets >= 300: + break + exc_info = None + except: + exc_info = sys.exc_info() self.connection._write_lock.release() # Read and react to as many as 50 packets @@ -241,7 +246,6 @@ class NetworkingThread(threading.Thread): self.connection.file_object) while packet: num_packets += 1 - try: self.connection.reactor.react(packet) for listener in self.connection.packet_listeners: @@ -254,6 +258,9 @@ class NetworkingThread(threading.Thread): packet = self.connection.reactor.read_packet( self.connection.file_object) + if exc_info is not None: + raise exc_info[0], exc_info[1], exc_info[2] + time.sleep(0.05) @@ -415,6 +422,10 @@ class PlayingReactor(PacketReactor): self.connection.write_packet(keep_alive_packet) if packet.packet_name == "player position and look": + teleport_confirm = packets.TeleportConfirmPacket() + teleport_confirm.teleport_id = packet.teleport_id + self.connection.write_packet(teleport_confirm) + ''' position_response = packets.PositionAndLookPacket() position_response.x = packet.x position_response.feet_y = packet.y @@ -422,8 +433,8 @@ class PlayingReactor(PacketReactor): position_response.yaw = packet.yaw position_response.pitch = packet.pitch position_response.on_ground = True - self.connection.write_packet(position_response) + ''' self.connection.spawned = True ''' diff --git a/minecraft/networking/packets.py b/minecraft/networking/packets.py index 5c747cb..3fff58d 100644 --- a/minecraft/networking/packets.py +++ b/minecraft/networking/packets.py @@ -76,6 +76,7 @@ class Packet(object): def __init__(self, context=None, **kwargs): self.context = context + self.set_values(**kwargs) @property def context(self): @@ -321,6 +322,32 @@ class PlayerPositionAndLookPacket(Packet): {'teleport_id': VarInt} if context.protocol_version >= 107 else {}, ]) + FLAG_REL_X = 0x01 + FLAG_REL_Y = 0x02 + FLAG_REL_Z = 0x04 + FLAG_REL_YAW = 0x08 + FLAG_REL_PITCH = 0x10 + + class PositionAndLook(object): + __slots__ = 'x', 'y', 'z', 'yaw', 'pitch' + def __init__(self, **kwds): + for attr in self.__slots__: + setattr(self, attr, kwds.get(attr)) + + # Update a PositionAndLook instance using this packet. + def apply(self, target): + if self.flags & self.FLAG_REL_X: target.x += self.x + else: target.x = self.x + if self.flags & self.FLAG_REL_Y: target.y += self.y + else: target.y = self.y + if self.flags & self.FLAG_REL_Z: target.z += self.z + else: target.z = self.z + if self.flags & self.FLAG_REL_YAW: target.yaw += self.yaw + else: target.yaw = self.yaw + if self.flags & self.FLAG_REL_PITCH: target.pitch += self.pitch + else: target.pitch = self.pitch + self.yaw %= 360 + self.pitch %= 360 class DisconnectPacketPlayState(Packet): get_id = staticmethod(lambda context: @@ -606,10 +633,31 @@ class PositionAndLookPacket(Packet): {'pitch': Float}, {'on_ground': Boolean}] +class TeleportConfirmPacket(Packet): + # Note: added between protocol versions 47 and 107. + id = 0x00 + packet_name = "teleport confirm" + definition = [ + {'teleport_id': VarInt}] + +class AnimationPacketServerbound(Packet): + get_id = staticmethod(lambda context: + 0x1A if context.protocol_version >= 107 else + 0x0A) + packet_name = "animation" + get_definition = staticmethod(lambda context: [ + {'hand': VarInt} if context.protocol_version >= 107 else {}]) + HAND_MAIN = 0 + HAND_OFF = 1 def state_playing_serverbound(context): - return { + packets = { KeepAlivePacketServerbound, ChatPacket, - PositionAndLookPacket + PositionAndLookPacket, + AnimationPacketServerbound, } + if context.protocol_version >= 107: packets |= { + TeleportConfirmPacket, + } + return packets