public static void Restock()
{
Player player = Main.player[Main.myPlayer];
Item[] inv = player.inventory;
Item[] objArray = player.bank.item;
if (player.chest > -1)
objArray = Main.chest[player.chest].item;
else if (player.chest == -2)
objArray = player.bank.item;
else if (player.chest == -3)
objArray = player.bank2.item;
HashSet<int> hashSet = new HashSet<int>();
List<int> list1 = new List<int>();
List<int> list2 = new List<int>();
for (int index = 57; index >= 0; --index)
{
if ((index < 50 || index >= 54) && (inv[index].itemId < 71 || inv[index].itemId > 74))
{
if (inv[index].stack > 0 && inv[index].maxStack > 1 && (int)inv[index].prefix == 0)
{
hashSet.Add(inv[index].netID);
if (inv[index].stack < inv[index].maxStack)
list1.Add(index);
}
else if (inv[index].stack == 0 || inv[index].netID == 0 || inv[index].itemId == 0)
list2.Add(index);
}
}
bool flag1 = false;
for (int index1 = 0; index1 < objArray.Length; ++index1)
{
if (objArray[index1].stack >= 1 && (int)objArray[index1].prefix == 0 && hashSet.Contains(objArray[index1].netID))
{
bool flag2 = false;
for (int index2 = 0; index2 < list1.Count; ++index2)
{
int slot = list1[index2];
int context = 0;
if (slot >= 50)
context = 2;
if (inv[slot].netID == objArray[index1].netID && ItemSlot.PickItemMovementAction(inv, context, slot, objArray[index1]) != -1)
{
int num = objArray[index1].stack;
if (inv[slot].maxStack - inv[slot].stack < num)
num = inv[slot].maxStack - inv[slot].stack;
inv[slot].stack += num;
objArray[index1].stack -= num;
flag1 = true;
if (inv[slot].stack == inv[slot].maxStack)
{
if (Main.netMode == 1 && Main.player[Main.myPlayer].chest > -1)
NetMessage.SendData(32, -1, -1, "", Main.player[Main.myPlayer].chest, (float)index1, 0.0f, 0.0f, 0, 0, 0);
list1.RemoveAt(index2);
--index2;
}
if (objArray[index1].stack == 0)
{
objArray[index1] = new Item();
flag2 = true;
if (Main.netMode == 1 && Main.player[Main.myPlayer].chest > -1)
{
NetMessage.SendData(32, -1, -1, "", Main.player[Main.myPlayer].chest, (float)index1, 0.0f, 0.0f, 0, 0, 0);
break;
}
break;
}
}
}
if (!flag2 && list2.Count > 0 && objArray[index1].ammo != 0)
{
for (int index2 = 0; index2 < list2.Count; ++index2)
{
int context = 0;
if (list2[index2] >= 50)
context = 2;
if (ItemSlot.PickItemMovementAction(inv, context, list2[index2], objArray[index1]) != -1)
{
Utils.Swap<Item>(ref inv[list2[index2]], ref objArray[index1]);
list1.Add(list2[index2]);
list2.RemoveAt(index2);
flag1 = true;
break;
}
}
}
}
}
if (!flag1)
return;
Main.PlaySound(7, -1, -1, 1);
}