public bool MoveToBag(ItemBag Bag, bool PlaySoundEffect, IList <Item> Source, bool NotifyIfContentsChanged) { if (!IsValidBag(Bag) || NestedBags.Any(x => x.GetTypeId() == Bag.GetTypeId()) || !Source.Contains(Bag)) { if (PlaySoundEffect) { Game1.playSound(MoveContentsFailedSound); } return(false); } else { NestedBags.Add(Bag); Source[Source.IndexOf(Bag)] = null; if (NotifyIfContentsChanged) { OnContentsChanged?.Invoke(this, EventArgs.Empty); } Resync(); if (PlaySoundEffect) { Game1.playSound(MoveContentsSuccessSound); } return(true); } }
/// <summary> /// Gets or sets the count of the specified `Item`. /// <para/> Set removes the `Item` if its value is set to 0 or below. /// <para/> Get returns 0 if the `Item` is not found. /// </summary> public int this[Item key] { get { // https://stackoverflow.com/questions/14150508/how-to-get-null-instead-of-the-keynotfoundexception-accessing-dictionary-value-b return(backingDictionary.TryGetValue(key, out int result) ? result : 0); // return backingDictionary[key]; } set { var prevValue = this[key]; if (key.unique && value > 1) { value = 1; } backingDictionary[key] = value; if (backingDictionary[key] <= 0) { backingDictionary.Remove(key); } if (this[key] != prevValue) { OnContentsChanged?.Invoke(key, this[key]); } } }
public bool MoveFromBag(ItemBag Bag, bool PlaySoundEffect, IList <Item> Target, int ActualTargetCapacity, bool NotifyIfContentsChanged) { if (Bag == null || !NestedBags.Contains(Bag)) { if (PlaySoundEffect) { Game1.playSound(MoveContentsFailedSound); } return(false); } else { // Find index to place this bag at in the target list int TargetCapacity = Math.Max(ActualTargetCapacity, Target.Count); int ItemIndex = -1; for (int i = 0; i < TargetCapacity; i++) { if (i >= Target.Count || Target[i] == null) { ItemIndex = i; break; } } if (ItemIndex >= 0) { // Put bag in target, remove from source if (ItemIndex >= Target.Count) { Target.Add(Bag); } else { Target[ItemIndex] = Bag; } NestedBags.Remove(Bag); if (NotifyIfContentsChanged) { OnContentsChanged?.Invoke(this, EventArgs.Empty); } Resync(); if (PlaySoundEffect) { Game1.playSound(MoveContentsSuccessSound); } return(true); } else { if (PlaySoundEffect) { Game1.playSound(MoveContentsFailedSound); } return(false); } } }
public void Add(Item item, int count = 1) { if (items.ContainsKey(item)) { items[item] += count; } else { items.Add(item, count); } OnContentsChanged?.Invoke(new InventoryEventArgs(InventoryOperation.Add, item, items[item])); }
public void Remove(Item item, int count = 1) { if (items.ContainsKey(item)) { if (items[item] > count) { items[item] -= count; } else { items.Remove(item); } OnContentsChanged?.Invoke(new InventoryEventArgs(InventoryOperation.Remove, item, items.ContainsKey(item) ? items[item] : 0)); } else { Debug.LogWarning($"You cannot remove {item.Name} because you don't have it"); } }
public override bool MoveToBag(Object Item, int Qty, out int MovedQty, bool PlaySoundEffect, IList <Item> Source, bool NotifyIfContentsChanged = true, bool ResyncMultiplayerData = true) { ObjectQuality OriginalQuality = (ObjectQuality)Item.Quality; bool CanDowngradeQuality = OriginalQuality > ObjectQuality.Regular && this.AllowDowngradeItemQuality && Source != null && Item != null && Source.Contains(Item); if (!CanDowngradeQuality) { return(base.MoveToBag(Item, Qty, out MovedQty, PlaySoundEffect, Source, NotifyIfContentsChanged, ResyncMultiplayerData, false)); } else { //Dictionary<ObjectQuality, int> RequiredQuantities = GetRequiredQuantities(Item); // Keep trying to transfer the next worse quality until all have been transferred, or we reach the minimum quality int RemainingQty = Qty; int TotalMovedQty = 0; List <ObjectQuality> ValidQualities = Enum.GetValues(typeof(ObjectQuality)).Cast <ObjectQuality>().Where(x => x <= OriginalQuality).OrderByDescending(x => x).ToList(); foreach (ObjectQuality CurrentQuality in ValidQualities) { Item.Quality = (int)CurrentQuality; if (base.MoveToBag(Item, RemainingQty, out int CurrentMovedQty, false, Source, false, false, false)) { TotalMovedQty += CurrentMovedQty; RemainingQty -= CurrentMovedQty; } if (TotalMovedQty >= Qty) { break; } } // Put any leftover stack back to the original quality Item.Quality = (int)OriginalQuality; MovedQty = TotalMovedQty; if (MovedQty > 0) { if (NotifyIfContentsChanged) { OnContentsChanged?.Invoke(this, EventArgs.Empty); } if (ResyncMultiplayerData) { Resync(); } if (PlaySoundEffect) { Game1.playSound(MoveContentsSuccessSound); } return(true); } else { if (PlaySoundEffect) { Game1.playSound(MoveContentsFailedSound); } return(false); } } }
public void Sort() { items = items.OrderBy(x => x.Key.ID).ToDictionary(pair => pair.Key, pair => pair.Value); OnContentsChanged?.Invoke(new InventoryEventArgs(InventoryOperation.Sort)); }