IItemContainer - jimdroberts/FishMMO GitHub Wiki
Interface for item containers, providing methods and events for managing items and slots. Used for inventories, equipment, banks, and other item storage systems.
-
event Action<IItemContainer, Item, int> OnSlotUpdated
Event triggered when an item slot is updated (item added, removed, or changed).
-
List Items { get; }
Gets the list of items contained in this container.
-
bool CanManipulate()
Determines if the container can be manipulated (e.g., items moved or swapped). Returns: - bool: True if manipulation is allowed, false otherwise.
-
bool IsValidSlot(int slot)
Checks if the specified slot index is valid for this container. Parameters: - int slot: The slot index to check. Returns: - bool: True if the slot is valid, false otherwise.
-
bool IsSlotEmpty(int slot)
Checks if the specified slot is empty. Parameters: - int slot: The slot index to check. Returns: - bool: True if the slot is empty, false otherwise.
-
bool TryGetItem(int slot, out Item item)
Attempts to get the item in the specified slot. Parameters: - int slot: The slot index to retrieve. - out Item item: The item found in the slot, or null if not found. Returns: - bool: True if an item was found, false otherwise.
-
bool ContainsItem(BaseItemTemplate itemTemplate)
Checks if the container contains an item with the specified template. Parameters: - BaseItemTemplate itemTemplate: The item template to search for. Returns: - bool: True if the item is found, false otherwise.
-
int GetItemCount(BaseItemTemplate itemTemplate)
Gets the count of items matching the specified template. Parameters: - BaseItemTemplate itemTemplate: The item template to count. Returns: - int: The number of items matching the template.
-
void AddSlots(List items, int amount)
Adds slots to the container, optionally initializing with a list of items. Parameters: - List items: The initial items to add (can be null). - int amount: The number of slots to add.
-
void Clear()
Clears all items from the container.
-
bool HasFreeSlot()
Checks if the container has at least one free slot. Returns: - bool: True if a free slot exists, false otherwise.
-
int FreeSlots()
Gets the number of free slots in the container. Returns: - int: The number of free slots.
-
int FilledSlots()
Gets the number of filled slots in the container. Returns: - int: The number of filled slots.
-
bool CanAddItem(Item item)
Determines if the specified item can be added to the container. Parameters: - Item item: The item to check. Returns: - bool: True if the item can be added, false otherwise.
-
bool TryAddItem(Item item, out List modifiedItems)
Attempts to add the specified item to the container, returning a list of modified items. Parameters: - Item item: The item to add. - out List modifiedItems: The list of items modified during the operation. Returns: - bool: True if the item was successfully added, false otherwise.
-
bool SetItemSlot(Item item, int slot)
Sets the item in the specified slot. Parameters: - Item item: The item to set. - int slot: The slot index to set the item in. Returns: - bool: True if the item was successfully set, false otherwise.
-
bool SwapItemSlots(int from, int to)
Swaps items between two slots. Parameters: - int from: The source slot index. - int to: The destination slot index. Returns: - bool: True if the swap was successful, false otherwise.
-
bool SwapItemSlots(int from, int to, out Item fromItem, out Item toItem)
Swaps items between two slots and returns the items that were swapped. Parameters: - int from: The source slot index. - int to: The destination slot index. - out Item fromItem: The item originally in the source slot. - out Item toItem: The item originally in the destination slot. Returns: - bool: True if the swap was successful, false otherwise.
-
Item RemoveItem(int slot)
Removes the item from the specified slot. Parameters: - int slot: The slot index to remove the item from. Returns: - Item: The item that was removed, or null if no item was present.
- Implement the IItemContainer interface in your item container class (e.g., inventory, equipment, bank).
- Provide logic for managing items, slots, and events as required.
- Integrate with your character and item systems as needed.
// Example 1: Implementing IItemContainer in a custom class
public class MyItemContainer : IItemContainer {
public event Action<IItemContainer, Item, int> OnSlotUpdated;
public List<Item> Items { get; private set; }
public bool CanManipulate() { return true; }
public bool IsValidSlot(int slot) { return slot >= 0 && slot < Items.Count; }
public bool IsSlotEmpty(int slot) { return Items[slot] == null; }
public bool TryGetItem(int slot, out Item item) { item = Items[slot]; return item != null; }
public bool ContainsItem(BaseItemTemplate itemTemplate) { /* ... */ return false; }
public int GetItemCount(BaseItemTemplate itemTemplate) { /* ... */ return 0; }
public void AddSlots(List<Item> items, int amount) { /* ... */ }
public void Clear() { /* ... */ }
public bool HasFreeSlot() { /* ... */ return false; }
public int FreeSlots() { /* ... */ return 0; }
public int FilledSlots() { /* ... */ return 0; }
public bool CanAddItem(Item item) { /* ... */ return false; }
public bool TryAddItem(Item item, out List<Item> modifiedItems) { modifiedItems = null; return false; }
public bool SetItemSlot(Item item, int slot) { /* ... */ return false; }
public bool SwapItemSlots(int from, int to) { /* ... */ return false; }
public bool SwapItemSlots(int from, int to, out Item fromItem, out Item toItem) { fromItem = null; toItem = null; return false; }
public Item RemoveItem(int slot) { /* ... */ return null; }
}
- Always validate slot indices and item types before performing operations.
- Ensure null checks for all parameters to prevent runtime errors.
- Document custom implementations for clarity and maintainability.
- Use events to notify listeners of slot updates for UI or network synchronization.