// // Licensed under the MIT License. See LICENSE file in the project root for full license information. // namespace MUnique.OpenMU.GameLogic; using System.Collections; /// /// A wrapper for another . /// Required to split one item storage into more than one storage spaces, e.g. Inventory and Personal Store which use the same ItemStorage. /// /// public class ItemStorageAdapter : ItemStorage { private readonly CollectionAdapter _adapter; /// /// Initializes a new instance of the class. /// /// The actual storage. /// The first item slot. /// The item slot count. public ItemStorageAdapter(ItemStorage actualStorage, byte firstItemSlot, byte itemSlotCount) { this._adapter = new CollectionAdapter(actualStorage.Items, firstItemSlot, itemSlotCount); this.ActualStorage = actualStorage; } /// public override ICollection Items => this._adapter; /// /// Gets the actual storage which is wrapped by this instance. /// public ItemStorage ActualStorage { get; } /// /// A collection adapter which just returns items between certain item slots. /// private class CollectionAdapter : ICollection { private readonly ICollection _actualCollection; private readonly byte _firstItemSlot; private readonly byte _itemSlotCount; public CollectionAdapter(ICollection actualCollection, byte firstItemSlot, byte itemSlotCount) { this._actualCollection = actualCollection; this._firstItemSlot = firstItemSlot; this._itemSlotCount = itemSlotCount; } /// public int Count => this._actualCollection.Count(i => this.IsSlotOfThisStorage(i.ItemSlot)); /// public bool IsReadOnly => false; /// public IEnumerator GetEnumerator() { return this._actualCollection.Where(item => this.IsSlotOfThisStorage(item.ItemSlot)).GetEnumerator(); } /// IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); } /// public void Add(Item item) { this._actualCollection.Add(item); } /// public void Clear() { var itemsToRemove = this.ToList(); itemsToRemove.ForEach(item => this._actualCollection.Remove(item)); } /// public bool Contains(Item item) => item is { } && this.IsSlotOfThisStorage(item.ItemSlot) && this._actualCollection.Contains(item); /// public void CopyTo(Item[] array, int arrayIndex) { var i = arrayIndex; foreach (var item in this) { array[i] = item; i++; } } /// public bool Remove(Item item) { if (this.Contains(item)) { return this._actualCollection.Remove(item); } return false; } private bool IsSlotOfThisStorage(byte itemSlot) { return itemSlot >= this._firstItemSlot && itemSlot < this._firstItemSlot + this._itemSlotCount; } } }