Add Entity.Item, Entity.CustomName, OnEntityMetadata event (#1222)

* Add New Event
* new Event
* Add OnEntityMetadaTa
* Update ChatBot.cs
* Update Protocol18.cs
* Update Entity.cs
* EntityCMD Update
* Update IMinecraftComHandler.cs
* Update Protocol18.cs
* Update IMinecraftComHandler.cs
* Update McClient.cs
* Update IMinecraftComHandler.cs
* Update McClient.cs
* Update McClient.cs
* Update McClient.cs
* Update McClient.cs
* Update ChatBot.cs
* Update McClient.cs
* Update Entity.cs
* Create EntityPose.cs
* Update MinecraftClient.csproj
* Update McClient.cs
* Update EntityPose.cs
* Update Entity.cs
* Update McClient.cs
* Remove debug line
* Update Entitycmd.cs
* Update Entity.cs
* Update McClient.cs
* Update Entity.cs
* Update McClient.cs
* Update McClient.cs
* Update Entity.cs
* Update McClient.cs
* Update Entitycmd.cs
* Update Entitycmd.cs
* Update McClient.cs
* Update Entitycmd.cs
* Update Entitycmd.cs
* Update Entity.cs
* Update McClient.cs
* Update Entitycmd.cs
* Update Entitycmd.cs
* Update Entitycmd.cs
* Update Entitycmd.cs
* Update Entitycmd.cs
* Update Entitycmd.cs
* Crash Fix on Item
* Crashes Fix
* Update McClient.cs
* Crashes fix
* Update McClient.cs
* Update Entity.cs
* Update Entity.cs
* Update McClient.cs
* Update McClient.cs
* Update McClient.cs
* Update McClient.cs
* Update McClient.cs
* Update McClient.cs
* Update McClient.cs
* Update ChatBot.cs
* Update IMinecraftComHandler.cs
* Update McClient.cs
* Update Protocol18.cs
* Update ChatBot.cs
* Update IMinecraftComHandler.cs
* Update Protocol18.cs
* Update McClient.cs
* Fix unaddressed issues
Co-authored-by: ORelio <oreliogitantispam.l0gin@spamgourmet.com>
This commit is contained in:
Рома Данилов 2020-08-20 21:36:50 +05:00 committed by GitHub
parent a6a5f0c333
commit c2e2e85063
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 207 additions and 31 deletions

View file

@ -333,9 +333,17 @@ namespace MinecraftClient
/// <summary>
/// Called when the health of an entity changed
/// </summary>
/// <param name="entityID">Entity ID</param>
/// <param name="entity">Entity</param>
/// <param name="health">The health of the entity</param>
public virtual void OnEntityHealth(int entityID, float health) { }
public virtual void OnEntityHealth(Entity entity, float health) { }
/// <summary>
/// Called when the metadata of an entity changed
/// </summary>
/// <param name="entity">Entity</param>
/// <param name="metadata">The metadata of the entity</param>
/// <param name="protocolversion">Ptotocol version</param>
public virtual void OnEntityMetadata(Entity entity, Dictionary<int, object> metadata) { }
/* =================================================================== */
/* ToolBox - Methods below might be useful while creating your bot. */

View file

@ -3,13 +3,14 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using MinecraftClient.Inventory;
using MinecraftClient.Mapping;
namespace MinecraftClient.Commands
{
class Entitycmd : Command
{
public override string CMDName { get { return "entity"; } }
public override string CMDDesc { get { return "entity <id> <list|attack|use>"; } }
public override string CMDDesc { get { return "entity <id|entitytype> <list|attack|use>"; } }
public override string Run(McClient handler, string command, Dictionary<string, object> localVars)
{
@ -20,8 +21,10 @@ namespace MinecraftClient.Commands
{
try
{
int entityID;
entityID = int.Parse(args[0]);
int entityID = 0;
int.TryParse(args[0], out entityID);
if (entityID != 0)
{
string action = args.Length > 1
? args[1].ToLower()
: "list";
@ -37,6 +40,38 @@ namespace MinecraftClient.Commands
return CMDDesc;
}
}
else
{
EntityType interacttype = EntityType.Player;
Enum.TryParse(args[0], out interacttype);
Dictionary<int, Mapping.Entity> entities = handler.GetEntities();
string actionst = "Entity attacked";
int actioncount = 0;
foreach (var entity2 in entities)
{
if (entity2.Value.Type == interacttype)
{
string action = args.Length > 1
? args[1].ToLower()
: "list";
if (action == "attack")
{
handler.InteractEntity(entity2.Key, 1);
actionst = "Entity attacked";
actioncount++;
}
else if (action == "use")
{
handler.InteractEntity(entity2.Key, 0);
actionst = "Entity used";
actioncount++;
}
else return CMDDesc;
}
}
return actioncount + " " + actionst;
}
}
catch (FormatException) { return CMDDesc; }
}
else
@ -46,10 +81,12 @@ namespace MinecraftClient.Commands
response.Add("Entities:");
foreach (var entity2 in entities)
{
if (entity2.Value.Type == Mapping.EntityType.Player)
response.Add(String.Format(" #{0}: {1} | {2}", entity2.Key, entity2.Value.Type, entity2.Value.Name));
if (entity2.Value.Type == EntityType.Item || entity2.Value.Type == EntityType.ItemFrame || entity2.Value.Type == Mapping.EntityType.EyeOfEnder || entity2.Value.Type == Mapping.EntityType.Egg || entity2.Value.Type == Mapping.EntityType.EnderPearl || entity2.Value.Type == Mapping.EntityType.Potion || entity2.Value.Type == Mapping.EntityType.Fireball || entity2.Value.Type == Mapping.EntityType.FireworkRocket)
response.Add(String.Format(" #{0}: Type: {1}, Item: {2}, Location: {3}", entity2.Key, entity2.Value.Type, entity2.Value.Item.Type, entity2.Value.Location));
else if (entity2.Value.Type == Mapping.EntityType.Player && entity2.Value.Name != string.Empty)
response.Add(String.Format(" #{0}: Type: {1}, Nickname: {2}, Latency: {3}, Health: {4}, Pose: {5}, Location: {6}", entity2.Key, entity2.Value.Type, entity2.Value.Name, entity2.Value.Latency, entity2.Value.Health, entity2.Value.Pose, entity2.Value.Location));
else
response.Add(String.Format(" #{0}: {1}", entity2.Key, entity2.Value.Type));
response.Add(String.Format(" #{0}: Type: {1}, Health: {2}, Location: {3}", entity2.Key, entity2.Value.Type, entity2.Value.Health, entity2.Value.Location));
}
response.Add(CMDDesc);
return String.Join("\n", response);

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using MinecraftClient.Inventory;
namespace MinecraftClient.Mapping
{
@ -23,6 +24,26 @@ namespace MinecraftClient.Mapping
/// </summary>
public string Name;
/// <summary>
/// CustomName of the entity.
/// </summary>
public string CustomNameJson;
/// <summary>
/// IsCustomNameVisible of the entity.
/// </summary>
public bool IsCustomNameVisible;
/// <summary>
/// CustomName of the entity.
/// </summary>
public string CustomName;
/// <summary>
/// Latency of the entity if it is a player.
/// </summary>
public int Latency;
/// <summary>
/// Entity type
/// </summary>
@ -38,6 +59,21 @@ namespace MinecraftClient.Mapping
/// </summary>
public float Health;
/// <summary>
/// Item of the entity if ItemFrame or Item
/// </summary>
public Item Item;
/// <summary>
/// Entity pose in the Minecraft world
/// </summary>
public EntityPose Pose;
/// <summary>
/// Entity metadata
/// </summary>
public Dictionary<int, object> Metadata;
/// <summary>
/// Create a new entity based on Entity ID, Entity Type and location
/// </summary>

View file

@ -0,0 +1,13 @@
namespace MinecraftClient.Mapping
{
public enum EntityPose
{
Standing = 0,
FallFlying = 1,
Sleeping = 2,
Swimming = 3,
SpinAttack = 4,
Sneaking = 5,
Dying = 6,
}
}

View file

@ -47,5 +47,27 @@ namespace MinecraftClient.Mapping
return false;
}
}
/// <summary>
/// Indicates whether the entity type contains an inner item
/// </summary>
/// <returns>TRUE if item holder (Item Entity, ItemFrame...)</returns>
public static bool ContainsItem(this EntityType e)
{
switch (e)
{
case EntityType.Item:
case EntityType.ItemFrame:
case EntityType.EyeOfEnder:
case EntityType.Egg:
case EntityType.EnderPearl:
case EntityType.Potion:
case EntityType.Fireball:
case EntityType.FireworkRocket:
return true;
default:
return false;
};
}
}
}

View file

@ -1530,6 +1530,7 @@ namespace MinecraftClient
world.Clear();
}
entities.Clear();
ClearInventories();
DispatchBotEvent(bot => bot.OnRespawn());
}
@ -2071,6 +2072,14 @@ namespace MinecraftClient
{
playerName = onlinePlayers[uuid];
playersLatency[playerName] = latency;
foreach (KeyValuePair<int, Entity> ent in entities)
{
if (ent.Value.UUID == uuid && ent.Value.Name == playerName)
{
ent.Value.Latency = latency;
break;
}
}
DispatchBotEvent(bot => bot.OnLatencyUpdate(playerName, uuid, latency));
}
}
@ -2149,7 +2158,49 @@ namespace MinecraftClient
if (entities.ContainsKey(entityID))
{
entities[entityID].Health = health;
DispatchBotEvent(bot => bot.OnEntityHealth(entityID, health));
DispatchBotEvent(bot => bot.OnEntityHealth(entities[entityID], health));
}
}
/// <summary>
/// Called when the metadata of an entity changed
/// </summary>
/// <param name="entityID">Entity ID</param>
/// <param name="metadata">The metadata of the entity</param>
public void OnEntityMetadata(int entityID, Dictionary<int, object> metadata)
{
if (entities.ContainsKey(entityID))
{
Entity entity = entities[entityID];
try
{
entity.Metadata = metadata;
if (entity.Type.ContainsItem() && metadata.ContainsKey(7) && metadata[7] != null && metadata[7].GetType() == typeof(Item))
{
try
{
entity.Item = (Item)metadata[7];
}
catch
{
entity.Item = new Item(ItemType.Air, 1, null);
}
}
if (metadata.ContainsKey(6) && metadata[6].GetType() == typeof(Int32))
{
entity.Pose = (EntityPose)metadata[6];
}
if (metadata.ContainsKey(2) && metadata.ContainsValue(metadata[2]) && metadata[2].GetType() == typeof(string))
{
entity.CustomNameJson = metadata[2].ToString();
entity.CustomName = ChatParser.ParseText(metadata[2].ToString());
}
if (metadata.ContainsKey(3) && metadata.ContainsValue(metadata[3]) && metadata[3].GetType() == typeof(bool))
{
entity.IsCustomNameVisible = (bool)metadata[3];
}
DispatchBotEvent(bot => bot.OnEntityMetadata(entity, metadata));
} catch { }
}
}
#endregion

View file

@ -144,6 +144,7 @@
<Compile Include="Mapping\EntityPalettes\EntityPalette115.cs" />
<Compile Include="Mapping\EntityPalettes\EntityPalette1161.cs" />
<Compile Include="Mapping\EntityPalettes\EntityPalette1162.cs" />
<Compile Include="Mapping\EntityPose.cs" />
<Compile Include="Mapping\EntityType.cs" />
<Compile Include="Mapping\EntityPalettes\EntityPaletteGenerator.cs" />
<Compile Include="Mapping\EntityTypeExtensions.cs" />

View file

@ -909,10 +909,11 @@ namespace MinecraftClient.Protocol.Handlers
if (handler.GetEntityHandlingEnabled())
{
int EntityID = dataTypes.ReadNextVarInt(packetData);
Dictionary<int, object> metadata = dataTypes.ReadNextMetadata(packetData, itemPalette); // need itemPalette because metadata need to read slot item
Dictionary<int, object> metadata = dataTypes.ReadNextMetadata(packetData, itemPalette);
int healthField = protocolversion >= MC114Version ? 8 : 7; // Health is field no. 7 in 1.10+ and 8 in 1.14+
if (metadata.ContainsKey(healthField) && metadata[healthField].GetType() == typeof(float))
handler.OnEntityHealth(EntityID, (float)metadata[healthField]);
handler.OnEntityMetadata(EntityID, metadata);
}
break;
case PacketIncomingType.TimeUpdate:

View file

@ -221,6 +221,20 @@ namespace MinecraftClient.Protocol
/// <param name="food"></param>
void OnUpdateHealth(float health, int food);
/// <summary>
/// Called when the health of an entity changed
/// </summary>
/// <param name="entityID">Entity ID</param>
/// <param name="health">The health of the entity</param>
void OnEntityHealth(int entityID, float health);
/// <summary>
/// Called when entity metadata or metadata changed.
/// </summary>
/// <param name="EntityID">Entity ID</param>
/// <param name="metadata">Entity metadata</param>
void OnEntityMetadata(int EntityID, Dictionary<int, object> metadata);
/// <summary>
/// Called when and explosion occurs on the server
/// </summary>
@ -301,12 +315,5 @@ namespace MinecraftClient.Protocol
/// <param name="objectivename">The name of the objective the score belongs to</param>
/// <param name="value">he score to be displayed next to the entry. Only sent when Action does not equal 1.</param>
void OnUpdateScore(string entityname, byte action, string objectivename, int value);
/// <summary>
/// Called when the health of an entity changed
/// </summary>
/// <param name="entityID">Entity ID</param>
/// <param name="health">The health of the entity</param>
void OnEntityHealth(int entityID, float health);
}
}