Evade ranged weapons and rush target
This commit is contained in:
1 parent
dbd2831dda
commit
0722924263
10 files changed
+474
-14
No files matched your search
@@ -496,6 +496,7 @@ class EntityManager:
|
||||
(SetEntityDataPacket, self.handle_entity_data),
|
||||
(BlockEntityDataPacket, self.handle_block_entity_data),
|
||||
(RemoveEntitiesPacket, self.handle_remove_entities),
|
||||
(packets.SetEquipmentPacket, self.handle_equipment),
|
||||
(MoveEntityPositionPacket, self.handle_move),
|
||||
(MoveEntityPositionRotationPacket, self.handle_move),
|
||||
(MoveEntityRotationPacket, self.handle_rotation),
|
||||
@@ -507,13 +508,17 @@ class EntityManager:
|
||||
|
||||
def handle_add_entity(self, packet):
|
||||
name = mobs.MOB_NAMES.get(packet.entity_type_id, 'unknown')
|
||||
common = dict(entity_id=packet.entity_id, x=packet.x,
|
||||
y=packet.y, z=packet.z)
|
||||
common = dict(
|
||||
entity_id=packet.entity_id, x=packet.x, y=packet.y, z=packet.z,
|
||||
spawn_x=packet.x, spawn_y=packet.y, spawn_z=packet.z,
|
||||
spawned_at=time.monotonic())
|
||||
if name == 'player':
|
||||
player_uuid = str(packet.entity_uuid)
|
||||
self.g.players[packet.entity_id] = Munch(
|
||||
**common, player_uuid=player_uuid,
|
||||
yaw=packet.yaw, pitch=packet.pitch)
|
||||
yaw=packet.yaw, pitch=packet.pitch,
|
||||
equipment={}, main_hand_item_id=None,
|
||||
position_updated_at=time.monotonic())
|
||||
elif name == 'item':
|
||||
self.g.objects[packet.entity_id] = Munch(
|
||||
**common, entity_uuid=str(packet.entity_uuid),
|
||||
@@ -524,7 +529,45 @@ class EntityManager:
|
||||
self.g.mobs[packet.entity_id] = Munch(
|
||||
**common, entity_uuid=str(packet.entity_uuid),
|
||||
type=packet.entity_type_id, yaw=packet.yaw,
|
||||
pitch=packet.pitch)
|
||||
pitch=packet.pitch, equipment={}, main_hand_item_id=None,
|
||||
position_updated_at=time.monotonic(),
|
||||
velocity_x=None, velocity_y=None, velocity_z=None,
|
||||
velocity_updated_at=None)
|
||||
|
||||
def handle_equipment(self, packet):
|
||||
entity = self._entity(packet.entity_id)
|
||||
if entity is None:
|
||||
return
|
||||
equipment = entity.get('equipment') or {}
|
||||
for slot, item in packet.equipment.items():
|
||||
if item.present:
|
||||
equipment[slot] = item
|
||||
else:
|
||||
equipment.pop(slot, None)
|
||||
entity.equipment = equipment
|
||||
main_hand = equipment.get(0)
|
||||
entity.main_hand_item_id = (
|
||||
main_hand.item_id if main_hand and main_hand.present else None)
|
||||
|
||||
def _record_entity_position(self, entity, position):
|
||||
now = time.monotonic()
|
||||
previous_time = entity.get('position_updated_at')
|
||||
previous = (entity.x, entity.y, entity.z)
|
||||
entity.x, entity.y, entity.z = position
|
||||
entity.position_updated_at = now
|
||||
|
||||
if (entity.get('type') not in mobs.RANGED_PROJECTILE_IDS or
|
||||
previous_time is None):
|
||||
return
|
||||
elapsed = now - previous_time
|
||||
if not 0.005 <= elapsed <= 0.5:
|
||||
return
|
||||
velocity = tuple((position[i] - previous[i]) / elapsed
|
||||
for i in range(3))
|
||||
if sum(component * component for component in velocity) > 2500:
|
||||
return
|
||||
entity.velocity_x, entity.velocity_y, entity.velocity_z = velocity
|
||||
entity.velocity_updated_at = now
|
||||
|
||||
def handle_block_entity_data(self, packet):
|
||||
location = packet.location
|
||||
@@ -551,9 +594,10 @@ class EntityManager:
|
||||
entity = self._entity(packet.entity_id)
|
||||
if entity is None:
|
||||
return
|
||||
entity.x += packet.delta_x
|
||||
entity.y += packet.delta_y
|
||||
entity.z += packet.delta_z
|
||||
self._record_entity_position(entity, (
|
||||
entity.x + packet.delta_x,
|
||||
entity.y + packet.delta_y,
|
||||
entity.z + packet.delta_z))
|
||||
if hasattr(packet, 'yaw'):
|
||||
entity.yaw = packet.yaw
|
||||
entity.pitch = packet.pitch
|
||||
@@ -567,7 +611,8 @@ class EntityManager:
|
||||
def handle_position_sync(self, packet):
|
||||
entity = self._entity(packet.entity_id)
|
||||
if entity is not None:
|
||||
entity.x, entity.y, entity.z = packet.x, packet.y, packet.z
|
||||
self._record_entity_position(
|
||||
entity, (packet.x, packet.y, packet.z))
|
||||
entity.yaw = packet.yaw
|
||||
entity.pitch = packet.pitch
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
from math import floor
|
||||
|
||||
from minecraft.networking.packets import Packet, PacketBuffer
|
||||
from minecraft.networking.modern import (
|
||||
UnsupportedItemComponents, read_item_stack,
|
||||
)
|
||||
from minecraft.networking.types import (
|
||||
VarInt, Integer, UnsignedByte, Position, Vector, MutableRecord,
|
||||
attribute_alias, multi_attribute_alias, Long, Boolean, VarLong,
|
||||
@@ -24,6 +27,32 @@ class SetHealthPacket(Packet):
|
||||
]
|
||||
|
||||
|
||||
class SetEquipmentPacket(Packet):
|
||||
"""Decode protocol-777 entity equipment updates."""
|
||||
id = 0x68
|
||||
packet_name = 'set equipment'
|
||||
|
||||
def read(self, file_object):
|
||||
self.entity_id = VarInt.read(file_object)
|
||||
self.equipment = {}
|
||||
self.incomplete = False
|
||||
for _ in range(16):
|
||||
packed_slot = UnsignedByte.read(file_object)
|
||||
slot = packed_slot & 0x7F
|
||||
has_more = bool(packed_slot & 0x80)
|
||||
try:
|
||||
self.equipment[slot] = read_item_stack(
|
||||
file_object, self.context)
|
||||
except UnsupportedItemComponents as exc:
|
||||
self.equipment[slot] = exc.stack
|
||||
self.incomplete = True
|
||||
file_object.read()
|
||||
return
|
||||
if not has_more:
|
||||
return
|
||||
raise ValueError('set equipment contains too many slots')
|
||||
|
||||
|
||||
class ChunkDataPacket(Packet):
|
||||
id = 0x20
|
||||
packet_name = 'chunk data'
|
||||
|
||||
Reference in new issue
Block a user