context: using System.Collections.Generic;
  using System.Linq;
  using MiNET.Blocks;
  using MiNET.Items;
  using MiNET.Net;
  using MiNET.Utils;
  
  namespace MiNET
  {
      public class PlayerInventory
      {
          public const int HotbarSize = 9;
          public const int InventorySize = HotbarSize + 36;
          public Player Player { get; private set; }
  
          public List<ItemStack> Slots { get; private set; }
          public int[] ItemHotbar { get; private set; }
          public int InHandSlot { get; set; }
  
  
          // Armour
          public Item Boots { get; set; }
          public Item Leggings { get; set; }
          public Item Chest { get; set; }
          public Item Helmet { get; set; }
  
          public PlayerInventory(Player player)
          {
              Player = player;
  
              Slots = Enumerable.Repeat(new ItemStack(), InventorySize).ToList();
              //Slots = Enumerable.Repeat(new ItemStack(new ItemIronSword(0), 1), InventorySize).ToList();
              //Slots[Slots.Count-10] = new ItemStack(new ItemDiamondAxe(0), 1);
              //Slots[Slots.Count-9] = new ItemStack(new ItemDiamondAxe(0), 1);
              //int c = -1;
              //Slots[++c] = new ItemStack(new ItemIronSword(0), 1);
              //Slots[++c] = new ItemStack(new ItemBow(0), 1);
              //Slots[++c] = new ItemStack(new ItemSnowball(0), 64);
              //Slots[++c] = new ItemStack(new ItemEgg(0), 64);
              //Slots[++c] = new ItemStack(262, 32);
              //Slots[++c] = new ItemStack(new ItemBucket(10), 1);
              //Slots[++c] = new ItemStack(new ItemChest(0), 1);
              ////Slots[++c] = new ItemStack(new ItemBlock(new DiamondOre(), 0), 64);
              //Slots[++c] = new ItemStack(new ItemBlock(new GoldBlock(), 0), 64);
              //Slots[++c] = new ItemStack(new ItemBlock(new GoldBlock(), 0), 64);
              //Slots[++c] = new ItemStack(new ItemBlock(new GoldBlock(), 0), 64);
              //Slots[++c] = new ItemStack(new ItemBlock(new GoldBlock(), 0), 64);
              //Slots[++c] = new ItemStack(new ItemBlock(new GoldBlock(), 0), 64);
              //Slots[++c] = new ItemStack(new ItemBlock(new GoldBlock(), 0), 64);
              //Slots[++c] = new ItemStack(new ItemBlock(new GoldBlock(), 0), 64);
              //Slots[++c] = new ItemStack(new ItemBlock(new GoldBlock(), 0), 64);
              //Slots[++c] = new ItemStack(new ItemBlock(new GoldBlock(), 0), 64);
              //Slots[++c] = new ItemStack(new ItemBlock(new GoldBlock(), 0), 64);
              //Slots[++c] = new ItemStack(new ItemBlock(new GoldBlock(), 0), 64);
              //Slots[++c] = new ItemStack(new ItemBlock(new GoldBlock(), 0), 64);
              //Slots[++c] = new ItemStack(new ItemBlock(new GoldBlock(), 0), 64);
              //Slots[++c] = new ItemStack(new ItemBlock(new GoldBlock(), 0), 64);
              //Slots[++c] = new ItemStack(new ItemBlock(new GoldBlock(), 0), 64);
              //Slots[++c] = new ItemStack(new ItemBlock(new CoalBlock(), 0), 64);
  
              //Slots = new List<ItemStack>();
              //for (int i = 0; i < 100; i++)
              //{
              //	Slots.Add(new ItemStack(ItemFactory.GetItem(i, 0), 1));
              //}
  
              ItemHotbar = new int[HotbarSize];
              InHandSlot = 0;
  
              for (byte i = 0; i < ItemHotbar.Length; i++)
              {
                  ItemHotbar[i] = i;
              }
  
              Boots = new Item(0, 0);
              Leggings = new Item(0, 0);
              Chest = new Item(0, 0);
              Helmet = new Item(0, 0);
  
              //Boots = new ItemDiamondBoots(0);
              //Leggings = new ItemDiamondLeggings(0);
              //Chest = new ItemDiamondChestplate(0);
              //Helmet = new ItemDiamondHelmet(0);
          }
  
          public virtual ItemStack GetItemInHand()
          {
              var index = ItemHotbar[InHandSlot];
              if (index == -1 || index >= Slots.Count) return new ItemStack();
  
              return Slots[index];
          }
  
          public virtual int GetItemInHandSlot()
          {
              return ItemHotbar[InHandSlot];
          }
  
          [Wired]
          public void SetInventorySlot(byte slot, short itemId, byte amount = 1, short metadata = 0)
          {
              Slots[slot] = new ItemStack(itemId, amount, metadata);
  
              var containerSetContent = McpeContainerSetContent.CreateObject();
              containerSetContent.windowId = 0;
              containerSetContent.slotData = GetSlots();
              containerSetContent.hotbarData = GetHotbar();
              Player.SendPackage(containerSetContent);
          }
  
          public MetadataInts GetHotbar()
          {
              MetadataInts metadata = new MetadataInts();
              for (byte i = 0; i < ItemHotbar.Length; i++)
              {
                  metadata[i] = new MetadataInt(ItemHotbar[i] + HotbarSize);
              }
  
              return metadata;
          }
  
          public MetadataSlots GetSlots()
          {
              var slotData = new MetadataSlots();
              for (byte i = 0; i < Slots.Count; i++)
              {
                  if (Slots[i].Count == 0) Slots[i] = new ItemStack();
                  slotData[i] = new MetadataSlot(Slots[i]);
              }
  
              return slotData;
          }
  
          public MetadataSlots GetArmor()
          {
              var slotData = new MetadataSlots();
              slotData[0] = new MetadataSlot(new ItemStack((short)Helmet.Id, 1, Helmet.Metadata));
              slotData[1] = new MetadataSlot(new ItemStack((short)Chest.Id, 1, Helmet.Metadata));
              slotData[2] = new MetadataSlot(new ItemStack((short)Leggings.Id, 1, Helmet.Metadata));
              slotData[3] = new MetadataSlot(new ItemStack((short)Boots.Id, 1, Helmet.Metadata));
              return slotData;
          }
  
          public bool SetFirstEmptySlot(short itemId, byte amount = 1, short metadata = 0)
          {
              Item item = ItemFactory.GetItem(itemId, metadata);
  
              for (byte s = 0; s < Slots.Count; s++)
              {
                  var b = Slots[s];
                  if (b.Id == itemId && b.Metadata == metadata && b.Count + amount <= item.MaxStackSize)
                  {
                      SetInventorySlot(s, itemId, (byte)(b.Count + amount), metadata);
                      return true;
                  }
                  else if (b.Id == 0 || b.Id == -1)
                  {
                      SetInventorySlot(s, itemId, amount, metadata);
                      return true;
                  }
              }
  
              return false;
          }
  
          public void SetHeldItemSlot(int slot)
          {
              InHandSlot = slot;
  
              McpePlayerEquipment order = McpePlayerEquipment.CreateObject();
              order.entityId = 0;
              order.item = GetItemInHand();
              order.selectedSlot = (byte)slot;
              Player.SendPackage(order);
  
              McpePlayerEquipment broadcast = McpePlayerEquipment.CreateObject();
              broadcast.entityId = Player.EntityId;
              broadcast.item = GetItemInHand();
              broadcast.selectedSlot = (byte)slot;
              Player.Level?.RelayBroadcast(broadcast);
          }
  
          /// <summary>
          ///     Empty the specified slot
          /// </summary>
          /// <param name="slot">The slot to empty.</param>
          public void ClearInventorySlot(byte slot)
          {
              SetInventorySlot(slot, 0, 0);
          }
  
          public bool HasItem(MetadataSlot item)
          {
              for (byte i = 0; i < Slots.Count; i++)
              {
                  if (((MetadataSlot)Slots[i]).Value.Id == item.Value.Id && ((MetadataSlot)Slots[i]).Value.Metadata == item.Value.Metadata)
                  {
                      return true;
                  }
              }
              return false;
          }
      }
  
      public static class InventoryUtils
      {
          public static void Clear(this PlayerInventory inv)
          {
              for (byte i = 0; i < inv.Slots.Count; ++i)
              {
                  if (inv.Slots[i] == null || inv.Slots[i].Id != 0) inv.Slots[i] = new ItemStack();
              }
  
              if (inv.Helmet.Id != 0) inv.Helmet = new Item(0, 0);
  
gt:             if (inv.Chest.Id != 0) inv.Chest = new Item(0, 0);