# Containers

| Methods          | Events          |
| ---------------- | --------------- |
| GetInventory     | onWindowAdded   |
| GetOpenWindow    | onWindowRemoved |
| CloseWindows     |                 |
| GetWindow        |                 |
| GetWindowByTitle |                 |
| GetWindowByType  |                 |

## Methods

### GetInventory

Returns the Inventory of the bot.\
Note: *IInventory extends from IWindow, which should be used as documentation when working with the inventory.*

```csharp
IInventory GetInventory();
```

### GetOpenWindow

Gets the currently open window.\
Note: *This can return null if no window is open.*

```csharp
IWindow GetOpenWindow();
```

### CloseWindows

Closes the currently open window, if no window is open then it will send an inventory close packet, however the server does not normally keep track if the inventory is open.

```csharp
Task CloseWindows();
```

### GetWindow

Gets the window by ID.\
Note: *the bot's inventory always has the id 0, therefore if the parameter id is 0 then the bot's inventory will be returned.*

```csharp
IWindow GetWindow(int id);
```

### GetWindowByTitle

Gets the window by title of the container.\
Note: the title must be unformatted/raw text, meaning it does not contain any color codes, or any text effects such as bold, or strikethrough.

```csharp
IWindow GetWindowByTitle(string title);
```

### GetWindowByType

Gets the window by type of container. The type of the window always starts with "minecraft:" and a list of container types can be found [here](https://i.imgur.com/LeWMMFd.png).

```csharp
IWindow GetWindowByType (string type);
```

## Example code using Containers

The code below searches the bot's inventory for slots that have diamonds in them and then drops the slots that were found if they have less than 64 diamonds.

{% tabs %}
{% tab title="Commented Code" %}

```csharp
public async void OnTick() {
    // Find all slots that have diamonds (id 264) in them.
    var diamondSlots = Context.Containers.GetInventory().Find(264);
    
    // Loop through each slot that has a diamond in it.
    foreach (var diamondSlot in diamondSlots) {
        // Check if the amount of diamonds in the current slot is
        // less than 64. If it is less than 64, then we drop the item stack.
        if(diamondSlot.Count < 64)
            await diamondSlot.DropStack();
    }
}
```

{% endtab %}

{% tab title="Raw Code" %}

```csharp
public async void OnTick() {
    var diamondSlots = Context.Containers.GetInventory().Find(264);
    
    foreach (var diamondSlot in diamondSlots) {
        if(diamondSlot.Count < 64)
            await diamondSlot.DropStack();
    }
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.minecraftbot.com/api/context/containers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
