ItemStackable - jimdroberts/FishMMO GitHub Wiki

Description

Represents the stackable component of an item, managing stack size, addition, removal, and unstacking logic.


API Access

Fields

  • private Item item

    The item instance this stackable component belongs to.

  • public uint Amount

    The current amount in the stack.

Properties

  • public bool IsStackFull { get; }

    Returns true if the stack is full (reached max stack size).

Methods

  • public ItemStackable(Item item, uint amount)

    Constructs a stackable component for an item with the given amount. Parameters: - Item item: The item instance. - uint amount: The initial stack amount.*

  • public void Remove(uint amount)

    Removes the specified amount from the stack. Destroys the item if the stack reaches zero. Parameters: - uint amount: The amount to remove.*

  • public bool CanAddToStack(Item other)

    Returns true only if the entire other item can be added to this stack. Checks for matching template, seed, and stack capacity. Parameters: - Item other: The item to add to the stack.* Returns: - bool: True if the item can be fully added, false otherwise.*

  • public bool AddToStack(Item other)

    Adds the other item to this stack and sets the other stack's size to the remainder, if any. Returns false on failure. Parameters: - Item other: The item to add to the stack.* Returns: - bool: True if the item was added, false otherwise.*

  • public bool TryUnstack(uint amount, out Item instance)

    Attempts to unstack a certain amount from the item. Returns true if successful and the new instance is set. Note: The logic for creating a new item instance is unfinished. Parameters: - uint amount: The amount to unstack. - out Item instance: The new item instance, or null.* Returns: - bool: True if unstacking was successful, false otherwise.*


Basic Usage

Setup

  1. Create an ItemStackable instance and assign it to an Item.
  2. Use AddToStack and Remove to manage stack sizes as items are combined or consumed.
  3. Use TryUnstack to split stacks as needed.

Example

// Example 1: Creating a stackable item
ItemStackable stackable = new ItemStackable(item, 10); // 10 items in stack

// Example 2: Adding another item to the stack
bool canAdd = stackable.CanAddToStack(otherItem);
if (canAdd) {
    stackable.AddToStack(otherItem);
}

// Example 3: Removing items from the stack
stackable.Remove(2); // Remove 2 items from the stack

// Example 4: Unstacking items
Item instance;
bool unstacked = stackable.TryUnstack(5, out instance);

Best Practices

  • Always check CanAddToStack before combining stacks to ensure compatibility.
  • Use Remove to handle item consumption and cleanup when the stack reaches zero.
  • Document custom stack logic and edge cases for maintainability.
  • Handle unfinished unstacking logic if you need to support splitting stacks into new item instances.
⚠️ **GitHub.com Fallback** ⚠️