ItemContainer - jimdroberts/FishMMO GitHub Wiki

Description

Abstract base class for item containers, providing slot and item management for inventories, equipment, banks, etc. Implements IItemContainer and extends CharacterBehaviour for character association.


API Access

Fields

  • private readonly List items

    Internal list of items stored in this container.

Properties

  • public List Items { get; }

    Gets the list of items contained in this container.

Methods

  • public override void OnDestroying()

    Called when the container is being destroyed. Clears event handlers.

  • public virtual bool CanManipulate()

    Determines if the container can be manipulated (e.g., items moved or swapped). Checks if the character is alive and the items list is not empty. Returns: - bool: True if manipulation is allowed, false otherwise.

  • public bool IsValidSlot(int slot)

    Checks if the item slot exists (is within valid range). Parameters: - int slot: The slot index to check. Returns: - bool: True if the slot is valid, false otherwise.

  • public bool IsSlotEmpty(int slot)

    Checks if the specified slot is empty (contains no item). Parameters: - int slot: The slot index to check. Returns: - bool: True if the slot is empty, false otherwise.

  • public bool TryGetItem(int slot, out Item item)

    Attempts to get the item in the specified slot. Returns false if the item doesn't exist. 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.

  • public 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.

  • public int GetItemCount(BaseItemTemplate itemTemplate)

    Gets the count of items matching the specified template, including stack sizes. Parameters: - BaseItemTemplate itemTemplate: The item template to count. Returns: - int: The number of items matching the template.

  • public 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.

  • public void Clear()

    Clears all items from the container, destroying each item and setting slots to null.

  • public bool HasFreeSlot()

    Checks if the container has at least one free slot. Returns: - bool: True if a free slot exists, false otherwise.

  • public int FreeSlots()

    Gets the number of free slots in the container. Returns: - int: The number of free slots.

  • public int FilledSlots()

    Gets the number of filled slots in the container. Returns: - int: The number of filled slots.

  • public bool CanAddItem(Item item)

    Determines if the specified item can be added to the container, considering stack sizes and slot availability. Parameters: - Item item: The item to check. Returns: - bool: True if the item can be added, false otherwise.

  • public bool TryAddItem(Item item, out List modifiedItems)

    Attempts to add an item to the container. Returns true if the entire stack size of the item has been successfully added. All modified items are returned. Handles stacking logic and slot assignment. 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.

  • public bool SetItemSlot(Item item, int slot)

    Sets the item in the specified slot. Previous item will be lost if not referenced elsewhere. 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.

  • public 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.

  • public 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.

  • public Item RemoveItem(int slot)

    Removes an item from the specified slot and returns it. Returns null if the slot was empty. 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.


Basic Usage

Setup

  1. Inherit from the ItemContainer abstract class in your custom container class (e.g., inventory, equipment, bank).
  2. Implement or override methods as needed for custom logic.
  3. Integrate with your character and item systems as needed.

Example

// Example 1: Creating a custom item container
public class MyInventory : ItemContainer {
    // Custom logic can be added here
}

// Example 2: Adding and removing items
MyInventory inventory = new MyInventory();
Item item = new Item();
inventory.AddSlots(null, 10); // Add 10 empty slots
bool added = inventory.TryAddItem(item, out var modifiedItems);
if (added) {
    // Item added successfully
}
Item removed = inventory.RemoveItem(0); // Remove item from slot 0

Best Practices

  • Always validate slot indices and item types before performing operations.
  • Use events to notify listeners of slot updates for UI or network synchronization.
  • Override base methods for custom logic while maintaining core functionality.
  • Ensure null checks for all parameters to prevent runtime errors.
⚠️ **GitHub.com Fallback** ⚠️