/// <summary> /// Drops the specific item from the player's inventory and adds it to the loot bag. /// </summary> /// <param name="item">item to drop</param> protected void DropInventoryItem(CharacterItem item) { CharacterItem itemData = item.Clone(); itemData.level = item.level; itemData.amount = item.amount; if (NonEquipItems.DecreaseItemsByIndex(this.IndexOfNonEquipItem(item.id), item.amount, GameInstance.Singleton.IsLimitInventorySlot)) { lootBagItems.Add(itemData); } }
/// <summary> /// This will be called at server to order character to drop items /// </summary> /// <param name="index"></param> /// <param name="amount"></param> protected virtual void NetFuncDropItem(short index, short amount) { if (!CanDoActions() || index >= nonEquipItems.Count) { return; } CharacterItem nonEquipItem = nonEquipItems[index]; if (!nonEquipItem.NotEmptySlot() || amount > nonEquipItem.amount) { return; } if (this.DecreaseItemsByIndex(index, amount)) { // Drop item to the ground CharacterItem dropData = nonEquipItem.Clone(); dropData.amount = amount; ItemDropEntity.DropItem(this, dropData, new uint[] { ObjectId }); } }
public static bool IncreaseItems(IList <CharacterItem> itemList, CharacterItem addingItem) { // If item not valid if (!addingItem.NotEmptySlot()) { return(false); } Item itemData = addingItem.GetItem(); short amount = addingItem.amount; short maxStack = itemData.maxStack; Dictionary <int, CharacterItem> emptySlots = new Dictionary <int, CharacterItem>(); Dictionary <int, CharacterItem> changes = new Dictionary <int, CharacterItem>(); // Loop to all slots to add amount to any slots that item amount not max in stack CharacterItem tempNonEquipItem; for (int i = 0; i < itemList.Count; ++i) { tempNonEquipItem = itemList[i]; if (!tempNonEquipItem.NotEmptySlot()) { // If current entry is not valid, add it to empty list, going to replacing it later emptySlots[i] = tempNonEquipItem; } else if (tempNonEquipItem.dataId == addingItem.dataId) { // If same item id, increase its amount if (tempNonEquipItem.amount + amount <= maxStack) { tempNonEquipItem.amount += amount; changes[i] = tempNonEquipItem; amount = 0; break; } else if (maxStack - tempNonEquipItem.amount >= 0) { amount -= (short)(maxStack - tempNonEquipItem.amount); tempNonEquipItem.amount = maxStack; changes[i] = tempNonEquipItem; } } } // Adding item to new slots or empty slots if needed CharacterItem tempNewItem; if (changes.Count == 0 && emptySlots.Count > 0) { // If there are no changes and there are an empty entries, fill them foreach (int emptySlotIndex in emptySlots.Keys) { tempNewItem = addingItem.Clone(); short addAmount = 0; if (amount - maxStack >= 0) { addAmount = maxStack; amount -= maxStack; } else { addAmount = amount; amount = 0; } tempNewItem.amount = addAmount; changes[emptySlotIndex] = tempNewItem; if (amount == 0) { break; } } } // Apply all changes foreach (KeyValuePair <int, CharacterItem> change in changes) { itemList[change.Key] = change.Value; } // Add new items to new slots while (amount > 0) { tempNewItem = addingItem.Clone(); short addAmount = 0; if (amount - maxStack >= 0) { addAmount = maxStack; amount -= maxStack; } else { addAmount = amount; amount = 0; } tempNewItem.amount = addAmount; itemList.Add(tempNewItem); if (amount == 0) { break; } } return(true); }