Added Player Killed event (Combat and CombatDeath packets).

This commit is contained in:
Milutinke 2022-10-26 20:53:09 +02:00
parent 01eee22921
commit 80e227c3a7
5 changed files with 65 additions and 1 deletions

View file

@ -0,0 +1,9 @@
namespace MinecraftClient.Mapping
{
public enum CombatEventType
{
EnterCombat = 0,
EndCombat,
EntityDead
}
}

View file

@ -2878,6 +2878,19 @@ namespace MinecraftClient
DispatchBotEvent(bot => bot.OnPlayerLeave(uuid, username));
}
// <summary>
/// This method is called when a player has been killed by another entity
/// </summary>
/// <param name="playerEntity">Victim's entity</param>
/// <param name="killerEntity">Killer's entity</param>
public void OnPlayerKilled(int killerEntityId, string chatMessage)
{
if (!entities.ContainsKey(killerEntityId))
return;
DispatchBotEvent(bot => bot.OnKilled(entities[killerEntityId], chatMessage));
}
/// <summary>
/// Called when a plugin channel message was sent from the server.
/// </summary>

View file

@ -596,6 +596,33 @@ namespace MinecraftClient.Protocol.Handlers
Acknowledge(chat);
handler.OnTextReceived(chat);
}
break;
case PacketTypesIn.CombatEvent:
// 1.8 - 1.16.5
if (protocolVersion >= MC_1_8_Version && protocolVersion <= MC_1_16_5_Version)
{
CombatEventType eventType = (CombatEventType)dataTypes.ReadNextVarInt(packetData);
if (eventType == CombatEventType.EntityDead)
{
dataTypes.SkipNextVarInt(packetData);
handler.OnPlayerKilled(
dataTypes.ReadNextInt(packetData),
ChatParser.ParseText(dataTypes.ReadNextString(packetData))
);
}
}
break;
case PacketTypesIn.DeathCombatEvent:
dataTypes.SkipNextVarInt(packetData);
handler.OnPlayerKilled(
dataTypes.ReadNextInt(packetData),
ChatParser.ParseText(dataTypes.ReadNextString(packetData))
);
break;
case PacketTypesIn.MessageHeader:
if (protocolVersion >= MC_1_19_2_Version)

View file

@ -159,6 +159,13 @@ namespace MinecraftClient.Protocol
/// <param name="uuid">UUID of the player</param>
void OnPlayerLeave(Guid uuid);
/// <summary>
/// This method is called when a player has been killed by another entity
/// </summary>
/// <param name="killerEntityId">Killer's entity if</param>
/// <param name="chatMessage">message sent in chat when player is killed</param>
void OnPlayerKilled(int killerEntityId, string chatMessage);
/// <summary>
/// Called when the server sets the new location for the player
/// </summary>

View file

@ -417,7 +417,15 @@ namespace MinecraftClient
public virtual void OnPlayerLeave(Guid uuid, string? name) { }
/// <summary>
/// Called when the player deaths
/// This method is called when a player has been killed by another entity
/// </summary>
/// <param name="killerEntity">Killer's entity</param>
/// <param name="chatMessage">message sent in chat when player is killed</param>
public virtual void OnKilled(Entity killerEntity, string chatMessage) { }
/// <summary>
/// Called when the player dies
/// For getting the info about the player/entity who killed the player use OnPlayerKilled
/// </summary>
public virtual void OnDeath() { }