mirror of
https://github.com/ammaraskar/pyCraft.git
synced 2025-01-24 00:21:31 +01:00
969419da3f
After 1.16.3, Mojang started publishing snapshot, pre-release and release candidate versions of Minecraft with protocol version numbers of the form `(1 << 30) | n' where 'n' is a small non-negative integer increasing with each such version; the release versions continued to use the old format. For example, these are the last 8 published Minecraft versions as of this commit: release 1.16.3 uses protocol version 753 pre-release 1.16.4-pre1 uses protocol version 1073741825 == (1 << 30) | 1 pre-release 1.16.4-pre2 uses protocol version 1073741826 == (1 << 30) | 2 release candidate 1.16.4-rc1 uses protocol version 1073741827 == (1 << 30) | 3 release 1.16.4 uses protocol version 754 snapshot 20w45a uses protocol version 1073741829 == (1 << 30) | 5 snapshot 20w46a uses protocol version 1073741830 == (1 << 30) | 6 snapshot 20w48a uses protocol version 1073741831 == (1 << 30) | 7 This means that protocol versions no longer increase monotonically with respect to publication history, a property that was assumed to hold in much of pyCraft's code relating to support of multiple protocol versions. This commit rectifies the issue by replacing any comparison of protocol versions by their numerical value with a comparison based on their publication time. Newly defined is the dictionary `minecraft.PROTOCOL_VERSION_INDICES', which maps each known protocol version to its index in the protocol chronology. As such, the bound method `minecraft.PROTOCOL_VERSION_INDICES.get` can be used as a key function for the built-in `sorted`, `min` and `max` functions to collate protocol versions chronologically. Two utility functions are provided for direct comparison of protocol versions: `minecraft.utility.protocol_earlier` and `minecraft.utility.protocol_earlier_eq`. Additionally, four methods are added to the `ConnectionContext` type to ease the most common cases where the protocol of a given context must be compared to a given version number: `minecraft.connection.ConnectionContext.protocol_earlier`, `minecraft.connection.ConnectionContext.protocol_earlier_eq`, `minecraft.connection.ConnectionContext.protocol_later` and `minecraft.connection.ConnectionContext.protocol_later_eq`.
79 lines
3.0 KiB
Python
79 lines
3.0 KiB
Python
from minecraft.networking.types import (
|
|
VarInt, Double, Boolean, OriginPoint, Vector, multi_attribute_alias
|
|
)
|
|
|
|
from minecraft.networking.packets import Packet
|
|
|
|
|
|
class FacePlayerPacket(Packet):
|
|
@staticmethod
|
|
def get_id(context):
|
|
return 0x33 if context.protocol_later_eq(741) else \
|
|
0x34 if context.protocol_later_eq(721) else \
|
|
0x35 if context.protocol_later_eq(550) else \
|
|
0x34 if context.protocol_later_eq(471) else \
|
|
0x32 if context.protocol_later_eq(451) else \
|
|
0x31 if context.protocol_later_eq(389) else \
|
|
0x30
|
|
|
|
packet_name = 'face player'
|
|
|
|
@property
|
|
def fields(self):
|
|
return ('origin', 'x', 'y', 'z', 'entity_id', 'entity_origin') \
|
|
if self.context.protocol_later_eq(353) else \
|
|
('entity_id', 'x', 'y', 'z')
|
|
|
|
# Access the 'x', 'y', 'z' fields as a Vector tuple.
|
|
target = multi_attribute_alias(Vector, 'x', 'y', 'z')
|
|
|
|
def read(self, file_object):
|
|
if self.context.protocol_later_eq(353):
|
|
self.origin = VarInt.read(file_object)
|
|
self.x = Double.read(file_object)
|
|
self.y = Double.read(file_object)
|
|
self.z = Double.read(file_object)
|
|
is_entity = Boolean.read(file_object)
|
|
if is_entity:
|
|
# If the entity given by entity ID cannot be found,
|
|
# this packet should be treated as if is_entity was false.
|
|
self.entity_id = VarInt.read(file_object)
|
|
self.entity_origin = VarInt.read(file_object)
|
|
else:
|
|
self.entity_id = None
|
|
|
|
else: # Protocol version 352
|
|
is_entity = Boolean.read(file_object)
|
|
self.entity_id = VarInt.read(file_object) if is_entity else None
|
|
if not is_entity:
|
|
self.x = Double.read(file_object)
|
|
self.y = Double.read(file_object)
|
|
self.z = Double.read(file_object)
|
|
|
|
def write_fields(self, packet_buffer):
|
|
if self.context.protocol_later_eq(353):
|
|
VarInt.send(self.origin, packet_buffer)
|
|
Double.send(self.x, packet_buffer)
|
|
Double.send(self.y, packet_buffer)
|
|
Double.send(self.z, packet_buffer)
|
|
if self.entity_id is not None:
|
|
Boolean.send(True, packet_buffer)
|
|
VarInt.send(self.entity_id, packet_buffer)
|
|
VarInt.send(self.entity_origin, packet_buffer)
|
|
else:
|
|
Boolean.send(False, packet_buffer)
|
|
|
|
else: # Protocol version 352
|
|
if self.entity_id is not None:
|
|
Boolean.send(True, packet_buffer)
|
|
VarInt.send(self.entity_id, packet_buffer)
|
|
else:
|
|
Boolean.send(False, packet_buffer)
|
|
Double.send(self.x, packet_buffer)
|
|
Double.send(self.y, packet_buffer)
|
|
Double.send(self.z, packet_buffer)
|
|
|
|
# These aliases declare the Enum type corresponding to each field:
|
|
Origin = OriginPoint
|
|
EntityOrigin = OriginPoint
|