Minecraft-Console-Client/MinecraftClient/Inventory/ItemPalettes/ItemPalette.cs
ReinforceZwei b648e4f86d
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>
2020-08-24 22:38:08 +02:00

37 lines
1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.Inventory.ItemPalettes
{
public abstract class ItemPalette
{
protected abstract Dictionary<int, ItemType> GetDict();
private readonly Dictionary<ItemType, int> DictReverse = new Dictionary<ItemType, int>();
public ItemPalette()
{
// 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)
{
return DictReverse[itemType];
}
}
}