From b648e4f86df6208033951180fc12d8210b5b898b Mon Sep 17 00:00:00 2001 From: ReinforceZwei <39955851+ReinforceZwei@users.noreply.github.com> Date: Tue, 25 Aug 2020 04:38:08 +0800 Subject: [PATCH] Fix crash on custom item IDs from Forge servers (#1233) * Fix unknown item id cause crashes for handling non-vanilla item (i.e. forge item) * Add Unknown item type and use ItemType.Unknown for unknown items Co-authored-by: ORelio --- MinecraftClient/Inventory/ItemPalettes/ItemPalette.cs | 9 +++++++-- MinecraftClient/Inventory/ItemType.cs | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/MinecraftClient/Inventory/ItemPalettes/ItemPalette.cs b/MinecraftClient/Inventory/ItemPalettes/ItemPalette.cs index 76edb098..54afe69b 100644 --- a/MinecraftClient/Inventory/ItemPalettes/ItemPalette.cs +++ b/MinecraftClient/Inventory/ItemPalettes/ItemPalette.cs @@ -15,17 +15,22 @@ namespace MinecraftClient.Inventory.ItemPalettes // Index reverse mappings for use in ToId() foreach (KeyValuePair entry in GetDict()) DictReverse.Add(entry.Value, entry.Key); + + DictReverse[ItemType.Unknown] = -2; + DictReverse[ItemType.Null] = -1; } public ItemType FromId(int id) { + // Unknown item types may appear of Forge servers for custom items + if (!GetDict().ContainsKey(id)) + return ItemType.Unknown; + return GetDict()[id]; } public int ToId(ItemType itemType) { - if (itemType == ItemType.Null) - return -1; return DictReverse[itemType]; } } diff --git a/MinecraftClient/Inventory/ItemType.cs b/MinecraftClient/Inventory/ItemType.cs index 10b6ac8b..ed37efff 100644 --- a/MinecraftClient/Inventory/ItemType.cs +++ b/MinecraftClient/Inventory/ItemType.cs @@ -12,7 +12,8 @@ namespace MinecraftClient.Inventory /// public enum ItemType { - Null = -1, // Unspecified item type + Unknown = -2, // Unsupported item type (Forge mod custom item...) + Null = -1, // Unspecified item type (Used in the network protocol) AcaciaBoat, AcaciaButton,