Fix: non-monotonic protocol versions are not correctly handled

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`.
This commit is contained in:
joo committed 2020-12-02 15:11:39 +01:00
1 parent 4052136d30
commit 969419da3f
29 files changed
+1241 -983

No files matched your search

@@ -10,7 +10,7 @@ from minecraft.networking.types import (
class SpawnObjectPacket(Packet):
@staticmethod
def get_id(context):
return 0x00 if context.protocol_version >= 67 else \
return 0x00 if context.protocol_later_eq(67) else \
0x0E
packet_name = 'spawn object'
@@ -43,43 +43,44 @@ class SpawnObjectPacket(Packet):
if field != 'type_id' or context is None:
return
pv = context.protocol_version
name = 'EntityType_%d' % pv
name = 'EntityType_%d' % context.protocol_version
if hasattr(cls, name):
return getattr(cls, name)
era = 0 if context.protocol_earlier(458) else 1
class EntityType(Enum):
# XXX This has not been updated for >= v1.15
ACTIVATED_TNT = 50 if pv < 458 else 55 # PrimedTnt
AREA_EFFECT_CLOUD = 3 if pv < 458 else 0
ARMORSTAND = 78 if pv < 458 else 1
ARROW = 60 if pv < 458 else 2
BOAT = 1 if pv < 458 else 5
DRAGON_FIREBALL = 93 if pv < 458 else 13
EGG = 62 if pv < 458 else 74 # ThrownEgg
ENDERCRYSTAL = 51 if pv < 458 else 16
ENDERPEARL = 65 if pv < 458 else 75 # ThrownEnderpearl
EVOCATION_FANGS = 79 if pv < 458 else 20
EXP_BOTTLE = 75 if pv < 458 else 76 # ThrownExpBottle
EYE_OF_ENDER = 72 if pv < 458 else 23 # EyeOfEnderSignal
FALLING_OBJECT = 70 if pv < 458 else 24 # FallingSand
FIREBALL = 63 if pv < 458 else 34 # Fireball (ghast)
FIRECHARGE = 64 if pv < 458 else 65 # SmallFireball (blaze)
FIREWORK_ROCKET = 76 if pv < 458 else 25 # FireworksRocketEntity
FISHING_HOOK = 90 if pv < 458 else 93 # Fishing bobber
ITEM_FRAMES = 71 if pv < 458 else 33 # ItemFrame
ITEM_STACK = 2 if pv < 458 else 32 # Item
LEASH_KNOT = 77 if pv < 458 else 35
LLAMA_SPIT = 68 if pv < 458 else 37
MINECART = 10 if pv < 458 else 39 # MinecartRideable
POTION = 73 if pv < 458 else 77 # ThrownPotion
SHULKER_BULLET = 67 if pv < 458 else 60
SNOWBALL = 61 if pv < 458 else 67
SPECTRAL_ARROW = 91 if pv < 458 else 68
WITHER_SKULL = 66 if pv < 458 else 85
if pv >= 393:
ACTIVATED_TNT = (50, 55)[era] # PrimedTnt
AREA_EFFECT_CLOUD = ( 3, 0)[era]
ARMORSTAND = (78, 1)[era]
ARROW = (60, 2)[era]
BOAT = ( 1, 5)[era]
DRAGON_FIREBALL = (93, 13)[era]
EGG = (62, 74)[era] # ThrownEgg
ENDERCRYSTAL = (51, 16)[era]
ENDERPEARL = (65, 75)[era] # ThrownEnderpearl
EVOCATION_FANGS = (79, 20)[era]
EXP_BOTTLE = (75, 76)[era] # ThrownExpBottle
EYE_OF_ENDER = (72, 23)[era] # EyeOfEnderSignal
FALLING_OBJECT = (70, 24)[era] # FallingSand
FIREBALL = (63, 34)[era] # Fireball (ghast)
FIRECHARGE = (64, 65)[era] # SmallFireball (blaze)
FIREWORK_ROCKET = (76, 25)[era] # FireworksRocketEntity
FISHING_HOOK = (90, 93)[era] # Fishing bobber
ITEM_FRAMES = (71, 33)[era] # ItemFrame
ITEM_STACK = ( 2, 32)[era] # Item
LEASH_KNOT = (77, 35)[era]
LLAMA_SPIT = (68, 37)[era]
MINECART = (10, 39)[era] # MinecartRideable
POTION = (73, 77)[era] # ThrownPotion
SHULKER_BULLET = (67, 60)[era]
SNOWBALL = (61, 67)[era]
SPECTRAL_ARROW = (91, 68)[era]
WITHER_SKULL = (66, 85)[era]
if context.protocol_later_eq(393):
TRIDENT = 94
if pv >= 458:
if context.protocol_later_eq(458):
MINECART_CHEST = 40
MINECART_COMMAND_BLOCK = 41
MINECART_FURNACE = 42
@@ -92,44 +93,44 @@ class SpawnObjectPacket(Packet):
def read(self, file_object):
self.entity_id = VarInt.read(file_object)
if self.context.protocol_version >= 49:
if self.context.protocol_later_eq(49):
self.object_uuid = UUID.read(file_object)
if self.context.protocol_version >= 458:
if self.context.protocol_later_eq(458):
self.type_id = VarInt.read(file_object)
else:
self.type_id = Byte.read(file_object)
xyz_type = Double if self.context.protocol_version >= 100 else Integer
xyz_type = Double if self.context.protocol_later_eq(100) else Integer
for attr in 'x', 'y', 'z':
setattr(self, attr, xyz_type.read(file_object))
for attr in 'pitch', 'yaw':
setattr(self, attr, Angle.read(file_object))
self.data = Integer.read(file_object)
if self.context.protocol_version >= 49 or self.data > 0:
if self.context.protocol_later_eq(49) or self.data > 0:
for attr in 'velocity_x', 'velocity_y', 'velocity_z':
setattr(self, attr, Short.read(file_object))
def write_fields(self, packet_buffer):
VarInt.send(self.entity_id, packet_buffer)
if self.context.protocol_version >= 49:
if self.context.protocol_later_eq(49):
UUID.send(self.object_uuid, packet_buffer)
if self.context.protocol_version >= 458:
if self.context.protocol_later_eq(458):
VarInt.send(self.type_id, packet_buffer)
else:
Byte.send(self.type_id, packet_buffer)
# pylint: disable=no-member
xyz_type = Double if self.context.protocol_version >= 100 else Integer
xyz_type = Double if self.context.protocol_later_eq(100) else Integer
for coord in self.x, self.y, self.z:
xyz_type.send(coord, packet_buffer)
for coord in self.pitch, self.yaw:
Angle.send(coord, packet_buffer)
Integer.send(self.data, packet_buffer)
if self.context.protocol_version >= 49 or self.data > 0:
if self.context.protocol_later_eq(49) or self.data > 0:
for coord in self.velocity_x, self.velocity_y, self.velocity_z:
Short.send(coord, packet_buffer)