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 <ORelio@users.noreply.github.com>
This commit is contained in:
ReinforceZwei 2020-08-25 04:38:08 +08:00 committed by GitHub
parent 70c991cd30
commit b648e4f86d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 3 deletions

View file

@ -15,17 +15,22 @@ namespace MinecraftClient.Inventory.ItemPalettes
// Index reverse mappings for use in ToId()
foreach (KeyValuePair<int, ItemType> 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];
}
}

View file

@ -12,7 +12,8 @@ namespace MinecraftClient.Inventory
/// </summary>
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,