Add support for Minecraft 1.18 and 1.18.1.

This commit is contained in:
joodicator committed 2021-12-16 19:39:13 +01:00
1 parent 1ae9a2b48a
commit bf49006553
17 files changed
+322 -75

No files matched your search

+7
View File
@@ -53,6 +53,13 @@ class ConnectionContext(object):
later than, or is equal to, 'other_pv', or else False."""
return utility.protocol_earlier_eq(other_pv, self.protocol_version)
def protocol_in_range(self, start_pv, end_pv):
"""Returns True if the protocol version of this context was published
later than, or is equal to, 'start_pv' and was published earlier
than 'end_pv' (analogously to Python's 'range' function)."""
return (utility.protocol_earlier(self.protocol_version, end_pv) and
utility.protocol_earlier_eq(start_pv, self.protocol_version))
class _ConnectionOptions(object):
def __init__(self, address=None, port=None, compression_threshold=-1,
@@ -1,3 +1,4 @@
from minecraft import PRE
from minecraft.networking.packets import (
Packet, AbstractKeepAlivePacket, AbstractPluginMessagePacket
)
@@ -8,7 +9,10 @@ from minecraft.networking.types import (
PositionAndLook, multi_attribute_alias, attribute_transform,
)
from .combat_event_packet import CombatEventPacket
from .combat_event_packet import (
CombatEventPacket, EnterCombatEventPacket, EndCombatEventPacket,
DeathCombatEventPacket,
)
from .map_packet import MapPacket
from .player_list_item_packet import PlayerListItemPacket
from .player_position_and_look_packet import PlayerPositionAndLookPacket
@@ -36,7 +40,6 @@ def get_packets(context):
EntityPositionDeltaPacket,
TimeUpdatePacket,
UpdateHealthPacket,
CombatEventPacket,
ExplosionPacket,
SpawnObjectPacket,
BlockChangePacket,
@@ -47,18 +50,33 @@ def get_packets(context):
EntityLookPacket,
ResourcePackSendPacket
}
if context.protocol_earlier_eq(47):
packets |= {
SetCompressionPacket,
}
if context.protocol_earlier(PRE | 15):
packets |= {
CombatEventPacket,
}
else:
packets |= {
EnterCombatEventPacket,
EndCombatEventPacket,
DeathCombatEventPacket,
}
if context.protocol_later_eq(94):
packets |= {
SoundEffectPacket,
}
if context.protocol_later_eq(352):
packets |= {
FacePlayerPacket
}
return packets
@@ -150,7 +168,7 @@ class SetCompressionPacket(Packet):
def get_id(context):
return 0x03 if context.protocol_later_eq(755) else \
0x46
packet_name = "set compression"
definition = [
{'threshold': VarInt}]
@@ -264,7 +282,8 @@ class EntityPositionDeltaPacket(Packet):
class TimeUpdatePacket(Packet):
@staticmethod
def get_id(context):
return 0x58 if context.protocol_later_eq(755) else \
return 0x59 if context.protocol_later_eq(PRE | 48) else \
0x58 if context.protocol_later_eq(755) else \
0x4E if context.protocol_later_eq(721) else \
0x4F if context.protocol_later_eq(550) else \
0x4E if context.protocol_later_eq(471) else \
@@ -332,7 +351,8 @@ class PluginMessagePacket(AbstractPluginMessagePacket):
class PlayerListHeaderAndFooterPacket(Packet):
@staticmethod
def get_id(context):
return 0x5E if context.protocol_later_eq(755) else \
return 0x5F if context.protocol_later_eq(PRE | 48) else \
0x5E if context.protocol_later_eq(755) else \
0x53 if context.protocol_later_eq(721) else \
0x54 if context.protocol_later_eq(550) else \
0x53 if context.protocol_later_eq(471) else \
@@ -374,16 +394,35 @@ class EntityLookPacket(Packet):
{'on_ground': Boolean}
]
class ResourcePackSendPacket(Packet):
@staticmethod
def get_id(context):
return 0x3C if context.protocol_later_eq(755) else \
0x38
return 0x3C if context.protocol_later_eq(PRE | 15) else \
0x39 if context.protocol_later_eq(PRE | 8) else \
0x38 if context.protocol_later_eq(741) else \
0x39 if context.protocol_later_eq(721) else \
0x3A if context.protocol_later_eq(550) else \
0x39 if context.protocol_later_eq(471) else \
0x37 if context.protocol_later_eq(461) else \
0x38 if context.protocol_later_eq(451) else \
0x37 if context.protocol_later_eq(389) else \
0x36 if context.protocol_later_eq(352) else \
0x35 if context.protocol_later_eq(345) else \
0x34 if context.protocol_later_eq(336) else \
0x33 if context.protocol_later_eq(332) else \
0x34 if context.protocol_later_eq(318) else \
0x32 if context.protocol_later_eq(70) else \
0x48
packet_name = "resource pack send"
definition = [
{"url": String},
{"hash": String},
{"forced": Boolean},
{"forced_message": String}
]
@staticmethod
def get_definition(context):
return [
{"url": String},
{"hash": String},
{"forced": Boolean} if context.protocol_later_eq(PRE | 5) else {},
{"forced_message": String}
if context.protocol_later_eq(PRE | 15) else {},
]
@@ -1,3 +1,6 @@
from abc import ABCMeta, abstractmethod
from minecraft import PRE
from minecraft.networking.packets import Packet
from minecraft.networking.types import (
@@ -5,10 +8,16 @@ from minecraft.networking.types import (
)
# Note: this packet was removed in Minecraft 21w07a (protocol PRE|15)
# and replaced with the separate EnterCombatEvent, EndCombatEvent, and
# DeathCombatEvent packets. These are subclasses of CombatEventPacket, so
# that code written to listen for CombatEventPacket instances should in most
# cases continue to work without modification.
class CombatEventPacket(Packet):
@staticmethod
def get_id(context):
return 0x31 if context.protocol_later_eq(741) else \
@classmethod
def get_id(cls, context):
return cls.deprecated() if context.protocol_later_eq(PRE | 15) else \
0x31 if context.protocol_later_eq(741) else \
0x32 if context.protocol_later_eq(721) else \
0x33 if context.protocol_later_eq(550) else \
0x32 if context.protocol_later_eq(471) else \
@@ -28,19 +37,19 @@ class CombatEventPacket(Packet):
fields = 'event',
# The abstract type of the 'event' field of this packet.
class EventType(MutableRecord):
class EventType(MutableRecord, metaclass=ABCMeta):
__slots__ = ()
type_from_id_dict = {}
# Read the fields of the event (not including the ID) from the file.
@abstractmethod
def read(self, file_object):
raise NotImplementedError(
'This abstract method must be overridden in a subclass.')
pass
# Write the fields of the event (not including the ID) to the buffer.
@abstractmethod
def write(self, packet_buffer):
raise NotImplementedError(
'This abstract method must be overridden in a subclass.')
pass
@classmethod
def type_from_id(cls, event_id):
@@ -89,10 +98,72 @@ class CombatEventPacket(Packet):
EventType.type_from_id_dict[EntityDeadEvent.id] = EntityDeadEvent
def read(self, file_object):
if self.context and self.context.protocol_later_eq(PRE | 15):
self.deprecated()
event_id = VarInt.read(file_object)
self.event = CombatEventPacket.EventType.type_from_id(event_id)()
self.event.read(file_object)
def write_fields(self, packet_buffer):
if self.context and self.context.protocol_later_eq(PRE | 15):
self.deprecated()
VarInt.send(self.event.id, packet_buffer)
self.event.write(packet_buffer)
@staticmethod
def deprecated():
raise NotImplementedError(
'`CombatEventPacket` was removed in Minecraft snapshot 21w07a '
'(protocol version 2**30 + 15). In this and later versions, one '
'of the subclasses '
+ repr(SpecialisedCombatEventPacket.__subclasses__()) + ' must be '
'used directly for usage like that which generates this message.')
# Contains the behaviour common to all concrete CombatEventPacket subclasses
class SpecialisedCombatEventPacket(CombatEventPacket):
def __init__(self, *args, **kwds):
super(SpecialisedCombatEventPacket, self).__init__(*args, **kwds)
# Prior to Minecraft 21w07a, instances of CombatEventPacket had a
# single 'event' field giving a 'MutableRecord' of one of three types
# corresponding to the type of combat event represented. For backward
# compatibility, we here present a similar interface, giving the packet
# object itself as the 'event', which should work identically in most
# use cases, since it is a virtual subclass of, and has attributes of
# the same names and contents as those of, the previous event records.
self.event = self
# The 'get_id', 'fields', 'read', and 'write_fields' attributes of the
# 'Packet' base class are all overridden in 'CombatEventPacket'. We desire
# the default behaviour of these attributes, so we restore them here:
get_id = Packet.__dict__['get_id']
fields = Packet.__dict__['fields']
read = Packet.__dict__['read']
write_fields = Packet.__dict__['write_fields']
@CombatEventPacket.EnterCombatEvent.register # virtual subclass
class EnterCombatEventPacket(SpecialisedCombatEventPacket):
packet_name = 'enter combat event'
id = 0x34
definition = []
@CombatEventPacket.EndCombatEvent.register # virtual subclass
class EndCombatEventPacket(SpecialisedCombatEventPacket):
packet_name = 'end combat event'
id = 0x33
definition = [
{'duration': VarInt},
{'entity_id': Integer}]
@CombatEventPacket.EntityDeadEvent.register # virtual subclass
class DeathCombatEventPacket(SpecialisedCombatEventPacket):
packet_name = 'death combat event'
id = 0x35
definition = [
{'player_id': VarInt},
{'entity_id': Integer},
{'message': String}]
@@ -1,5 +1,6 @@
from minecraft.networking.types import (
Vector, Float, Byte, Integer, PrefixedArray, multi_attribute_alias, Type,
VarInt,
)
from minecraft.networking.packets import Packet
@@ -34,15 +35,22 @@ class ExplosionPacket(Packet):
for coord in record:
Byte.send(coord, socket)
definition = [
{'x': Float},
{'y': Float},
{'z': Float},
{'radius': Float},
{'records': PrefixedArray(Integer, Record)},
{'player_motion_x': Float},
{'player_motion_y': Float},
{'player_motion_z': Float}]
@staticmethod
def get_definition(context):
return [
{'x': Float},
{'y': Float},
{'z': Float},
{'radius': Float},
{'records': PrefixedArray(VarInt, ExplosionPacket.Record)}
if context.protocol_later_eq(755) else
{'records': PrefixedArray(Integer, ExplosionPacket.Record)},
{'player_motion_x': Float},
{'player_motion_y': Float},
{'player_motion_z': Float},
]
# Access the 'x', 'y', 'z' fields as a Vector tuple.
position = multi_attribute_alias(Vector, 'x', 'y', 'z')
@@ -92,6 +92,8 @@ class JoinGamePacket(AbstractDimensionPacket):
VarInt if context.protocol_later_eq(749) else UnsignedByte},
{'level_type': String} if context.protocol_earlier(716) else {},
{'render_distance': VarInt} if context.protocol_later_eq(468) else {},
{'simulation_distance': VarInt}
if context.protocol_later_eq(757) else {},
{'reduced_debug_info': Boolean},
{'respawn_screen': Boolean} if context.protocol_later_eq(571) else {},
{'is_debug': Boolean} if context.protocol_later_eq(716) else {},
@@ -1,3 +1,4 @@
from minecraft import PRE
from minecraft.networking.packets import Packet
from minecraft.networking.types import (
VarInt, Byte, Boolean, UnsignedByte, VarIntPrefixedByteArray, String,
@@ -72,9 +73,9 @@ class MapPacket(Packet):
self.map_id = VarInt.read(file_object)
self.scale = Byte.read(file_object)
if self.context.protocol_later_eq(107):
if self.context.protocol_in_range(107, PRE | 6):
self.is_tracking_position = Boolean.read(file_object)
else:
elif self.context.protocol_earlier(107):
self.is_tracking_position = True
if self.context.protocol_later_eq(452):
@@ -82,6 +83,9 @@ class MapPacket(Packet):
else:
self.is_locked = False
if self.context.protocol_later_eq(PRE | 6):
self.is_tracking_position = Boolean.read(file_object)
icon_count = VarInt.read(file_object)
self.icons = []
for i in range(icon_count):
@@ -101,10 +105,7 @@ class MapPacket(Packet):
icon = MapPacket.MapIcon(type, direction, (x, z), display_name)
self.icons.append(icon)
try:
self.width = UnsignedByte.read(file_object)
except:
self.width = None
self.width = UnsignedByte.read(file_object)
if self.width:
self.height = UnsignedByte.read(file_object)
x = Byte.read(file_object)
@@ -1,3 +1,4 @@
from minecraft import PRE
from minecraft.networking.packets import Packet
from minecraft.networking.types import (
VarInt, String, Float, Byte, Type, Integer, Vector, Enum,
@@ -9,7 +10,8 @@ __all__ = 'SoundEffectPacket',
class SoundEffectPacket(Packet):
@staticmethod
def get_id(context):
return 0x5C if context.protocol_later_eq(755) else \
return 0x5D if context.protocol_later_eq(PRE | 48) else \
0x5C if context.protocol_later_eq(755) else \
0x51 if context.protocol_later_eq(721) else \
0x52 if context.protocol_later_eq(550) else \
0x51 if context.protocol_later_eq(471) else \
@@ -268,11 +268,12 @@ class UseItemPacket(Packet):
Hand = RelativeHand
class ResourcePackStatusPacket(Packet):
@staticmethod
def get_id(context):
return 0x21
packet_name = "resource pask status"
packet_name = "resource pack status"
definition = [
{"result": VarInt}
]
]
@@ -1,8 +1,11 @@
import operator
from minecraft.networking.packets import Packet
from minecraft.networking.types import (
String, Byte, VarInt, Boolean, UnsignedByte, Enum, BitFieldEnum,
AbsoluteHand
)
from minecraft.utility import attribute_transform
class ClientSettingsPacket(Packet):
@@ -18,13 +21,40 @@ class ClientSettingsPacket(Packet):
packet_name = 'client settings'
get_definition = staticmethod(lambda context: [
{'locale': String},
{'view_distance': Byte},
{'chat_mode': VarInt if context.protocol_later(47) else Byte},
{'chat_colors': Boolean},
{'displayed_skin_parts': UnsignedByte},
{'main_hand': VarInt} if context.protocol_later(49) else {}])
@staticmethod
def get_definition(context):
return [
{'locale': String},
{'view_distance': Byte},
{'chat_mode': VarInt if context.protocol_later(47) else Byte},
{'chat_colors': Boolean},
{'displayed_skin_parts': UnsignedByte},
{'main_hand': VarInt} if context.protocol_later(49) else {},
{'enable_text_filtering': Boolean}
if context.protocol_later_eq(757) else
{'disable_text_filtering': Boolean}
if context.protocol_later_eq(755) else {},
{'allow_server_listings': Boolean}
if context.protocol_later_eq(755) else {},
]
# Set a default value for 'enable_text_filtering', because most clients
# will probably want this value, and to avoid breaking old code.
enable_text_filtering = False
# To support the possibility of both 'enable_text_filtering' and
# 'disable_text_filtering' fields existing, make 'disable_text_filtering'
# (which is the less likely to be used of the two) into a property that
# stores the negation of its value in the ordinary attribute
# 'enable_text_filtering'.
disable_text_filtering = attribute_transform(
'enable_text_filtering', operator.not_, operator.not_)
# Set a default value for 'allow_server_listings', because most clients
# will probably want this value, and to avoid breaking old code.
allow_server_listings = False
field_enum = classmethod(
lambda cls, field, context: {