From 02e294e3e8c6cdaef885535a79e5fdd2ff3e258e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=BE=D0=BC=D0=B0=20=D0=94=D0=B0=D0=BD=D0=B8=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2?= <35975332+Nekiplay@users.noreply.github.com> Date: Thu, 27 Aug 2020 22:33:45 +0500 Subject: [PATCH] Fix entity handling crash (#1241) Fix #1237 * Fix crash * Update Entity.cs * Update Entitycmd.cs * Update McClient.cs --- MinecraftClient/Commands/Entitycmd.cs | 3 ++- MinecraftClient/Mapping/Entity.cs | 2 ++ MinecraftClient/McClient.cs | 10 +++------- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/MinecraftClient/Commands/Entitycmd.cs b/MinecraftClient/Commands/Entitycmd.cs index 6f72d95d..a757a15e 100644 --- a/MinecraftClient/Commands/Entitycmd.cs +++ b/MinecraftClient/Commands/Entitycmd.cs @@ -144,10 +144,11 @@ namespace MinecraftClient.Commands string customname = entity2.Value.CustomName; EntityPose pose = entity2.Value.Pose; EntityType type = entity2.Value.Type; + Item item = entity2.Value.Item; string location = String.Format("X:{0}, Y:{1}, Z:{2}", Math.Round(entity2.Value.Location.X, 2), Math.Round(entity2.Value.Location.Y, 2), Math.Round(entity2.Value.Location.Y, 2)); if (type == EntityType.Item || type == EntityType.ItemFrame || type == Mapping.EntityType.EyeOfEnder || type == Mapping.EntityType.Egg || type == Mapping.EntityType.EnderPearl || type == Mapping.EntityType.Potion || type == Mapping.EntityType.Fireball || type == Mapping.EntityType.FireworkRocket) - response.Add(String.Format(" #{0}: Type: {1}, Item: {2}, Location: {3}", id, type, entity2.Value.Item.Type, location)); + response.Add(String.Format(" #{0}: Type: {1}, Item: {2}, Location: {3}", id, type, item.Type, location)); else if (type == Mapping.EntityType.Player && !String.IsNullOrEmpty(nickname)) response.Add(String.Format(" #{0}: Type: {1}, Nickname: §8{2}§8, Latency: {3}, Health: {4}, Pose: {5}, Location: {6}", id, type, nickname, latency, health, pose, location)); else if (type == Mapping.EntityType.Player && !String.IsNullOrEmpty(customname)) diff --git a/MinecraftClient/Mapping/Entity.cs b/MinecraftClient/Mapping/Entity.cs index 7dc36c97..740100da 100644 --- a/MinecraftClient/Mapping/Entity.cs +++ b/MinecraftClient/Mapping/Entity.cs @@ -92,6 +92,7 @@ namespace MinecraftClient.Mapping this.Location = location; this.Health = 1.0f; this.Equipment = new Dictionary(); + this.Item = new Item(ItemType.Air, 0, null); } /// /// Create a new entity based on Entity ID, Entity Type, location, name and UUID @@ -110,6 +111,7 @@ namespace MinecraftClient.Mapping this.Name = name; this.Health = 1.0f; this.Equipment = new Dictionary(); + this.Item = new Item(ItemType.Air, 0, null); } } } diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index 1d966897..3984a3b9 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -2180,14 +2180,10 @@ namespace MinecraftClient 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 - { + Item item = (Item)metadata[7]; + if (item == null) entity.Item = new Item(ItemType.Air, 0, null); - } + else entity.Item = item; } if (metadata.ContainsKey(6) && metadata[6] != null && metadata[6].GetType() == typeof(Int32)) {