2017-08-08 15:07:33 +02:00
|
|
|
from minecraft.networking.packets import Packet
|
|
|
|
|
|
|
|
from minecraft.networking.types import (
|
2018-05-27 16:36:13 +02:00
|
|
|
VarInt, Integer, String, MutableRecord
|
2017-08-08 15:07:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class CombatEventPacket(Packet):
|
|
|
|
@staticmethod
|
|
|
|
def get_id(context):
|
2018-07-19 02:59:48 +02:00
|
|
|
return 0x2F if context.protocol_version >= 389 else \
|
|
|
|
0x2E if context.protocol_version >= 345 else \
|
2018-01-13 02:03:04 +01:00
|
|
|
0x2D if context.protocol_version >= 336 else \
|
2017-08-08 15:07:33 +02:00
|
|
|
0x2C if context.protocol_version >= 332 else \
|
|
|
|
0x2D if context.protocol_version >= 318 else \
|
|
|
|
0x2C if context.protocol_version >= 86 else \
|
|
|
|
0x2D if context.protocol_version >= 80 else \
|
|
|
|
0x2C if context.protocol_version >= 67 else \
|
|
|
|
0x42
|
|
|
|
|
|
|
|
packet_name = 'combat event'
|
|
|
|
|
2018-05-27 14:28:01 +02:00
|
|
|
# The abstract type of the 'event' field of this packet.
|
2018-05-27 16:36:13 +02:00
|
|
|
class EventType(MutableRecord):
|
2018-05-27 14:28:01 +02:00
|
|
|
__slots__ = ()
|
|
|
|
type_from_id_dict = {}
|
|
|
|
|
|
|
|
# Read the fields of the event (not including the ID) from the file.
|
2017-08-08 15:07:33 +02:00
|
|
|
def read(self, file_object):
|
2018-05-27 14:28:01 +02:00
|
|
|
raise NotImplementedError(
|
|
|
|
'This abstract method must be overridden in a subclass.')
|
2017-08-08 15:07:33 +02:00
|
|
|
|
2018-05-27 14:28:01 +02:00
|
|
|
# Write the fields of the event (not including the ID) to the buffer.
|
|
|
|
def write(self, packet_buffer):
|
2017-08-08 15:07:33 +02:00
|
|
|
raise NotImplementedError(
|
|
|
|
'This abstract method must be overridden in a subclass.')
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def type_from_id(cls, event_id):
|
2018-05-27 14:28:01 +02:00
|
|
|
subcls = cls.type_from_id_dict.get(event_id)
|
2017-08-08 15:07:33 +02:00
|
|
|
if subcls is None:
|
2018-05-27 14:28:01 +02:00
|
|
|
raise ValueError('Unknown combat event ID: %s.' % event_id)
|
2017-08-08 15:07:33 +02:00
|
|
|
return subcls
|
|
|
|
|
|
|
|
class EnterCombatEvent(EventType):
|
2018-05-27 14:28:01 +02:00
|
|
|
__slots__ = ()
|
|
|
|
id = 0
|
|
|
|
|
|
|
|
def read(self, file_object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def write(self, packet_buffer):
|
2017-08-08 15:07:33 +02:00
|
|
|
pass
|
2018-05-27 14:28:01 +02:00
|
|
|
EventType.type_from_id_dict[EnterCombatEvent.id] = EnterCombatEvent
|
2017-08-08 15:07:33 +02:00
|
|
|
|
|
|
|
class EndCombatEvent(EventType):
|
|
|
|
__slots__ = 'duration', 'entity_id'
|
2018-05-27 14:28:01 +02:00
|
|
|
id = 1
|
2017-08-08 15:07:33 +02:00
|
|
|
|
2018-05-27 14:28:01 +02:00
|
|
|
def read(self, file_object):
|
2017-08-08 15:07:33 +02:00
|
|
|
self.duration = VarInt.read(file_object)
|
|
|
|
self.entity_id = Integer.read(file_object)
|
|
|
|
|
2018-05-27 14:28:01 +02:00
|
|
|
def write(self, packet_buffer):
|
|
|
|
VarInt.send(self.duration, packet_buffer)
|
|
|
|
Integer.send(self.entity_id, packet_buffer)
|
|
|
|
EventType.type_from_id_dict[EndCombatEvent.id] = EndCombatEvent
|
|
|
|
|
2017-08-08 15:07:33 +02:00
|
|
|
class EntityDeadEvent(EventType):
|
|
|
|
__slots__ = 'player_id', 'entity_id', 'message'
|
2018-05-27 14:28:01 +02:00
|
|
|
id = 2
|
2017-08-08 15:07:33 +02:00
|
|
|
|
2018-05-27 14:28:01 +02:00
|
|
|
def read(self, file_object):
|
2017-08-08 15:07:33 +02:00
|
|
|
self.player_id = VarInt.read(file_object)
|
|
|
|
self.entity_id = Integer.read(file_object)
|
|
|
|
self.message = String.read(file_object)
|
|
|
|
|
2018-05-27 14:28:01 +02:00
|
|
|
def write(self, packet_buffer):
|
|
|
|
VarInt.send(self.player_id, packet_buffer)
|
|
|
|
Integer.send(self.entity_id, packet_buffer)
|
|
|
|
String.send(self.message, packet_buffer)
|
|
|
|
EventType.type_from_id_dict[EntityDeadEvent.id] = EntityDeadEvent
|
|
|
|
|
2017-08-08 15:07:33 +02:00
|
|
|
def read(self, file_object):
|
|
|
|
event_id = VarInt.read(file_object)
|
2018-05-27 14:28:01 +02:00
|
|
|
self.event = CombatEventPacket.EventType.type_from_id(event_id)()
|
|
|
|
self.event.read(file_object)
|
2017-08-08 15:07:33 +02:00
|
|
|
|
2018-05-27 14:28:01 +02:00
|
|
|
def write_fields(self, packet_buffer):
|
|
|
|
VarInt.send(self.event.id, packet_buffer)
|
|
|
|
self.event.write(packet_buffer)
|