> For the complete documentation index, see [llms.txt](https://docs.quickclient.cc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.quickclient.cc/en/examples/chest-taker.md).

# Chest to inventory

A script that takes everything from an open chest and closes it.

When the player opens a chest, the script takes everything out with shift clicks, pausing between them, and then closes it. The example shows how to catch a screen opening, how to click one at a time and why one click per tick is better than thirty.

```java
package my;

import hex.script.api.CheckBox;
import hex.script.api.Script;
import hex.script.api.Slider;
import hex.script.api.event.ScreenEvent;

public class ChestTaker extends Script {

    private final Slider delay = slider("Delay", 2f, 0f, 10f, 1f).postfix("t");
    private final CheckBox autoClose = checkBox("Close", true);
    private final CheckBox onlyChests = checkBox("Chests only", true);

    private boolean working;
    private int cooldown;

    @Override
    public String description() {
        return "Takes everything from an open chest";
    }

    @Override
    public void activate() {
        working = false;
        on(ScreenEvent.class, event -> {
            if (event.opened() && event.screen().container()) {
                working = !onlyChests.value() || container().type().contains("generic_9x");
                cooldown = delay.intValue();
            } else if (event.closed()) {
                working = false;
            }
        });
    }

    @Override
    public void onTick() {
        if (!working || !container().open()) return;
        if (cooldown-- > 0) return;
        cooldown = delay.intValue();

        int slot = nextFilled();
        if (slot >= 0) {
            container().shiftClick(slot);
            return;
        }
        if (autoClose.value()) container().close();
        working = false;
    }

    private int nextFilled() {
        for (int slot = 0; slot < container().size(); slot++) {
            if (!container().slot(slot).empty()) return slot;
        }
        return -1;
    }
}
```

## How it works

**Start on the event, work in the tick.** `ScreenEvent` reports that the container opened, but it is too early to click inside the handler: the contents arrive from the server a little later. The `working` flag and the first `cooldown` give them time to arrive.

**One click per tick.** The server will notice thirty shift clicks in one tick right away. The pause between clicks in ticks is configurable. A value of zero also works, but even then there is at most one click per tick.

**Finding the slot every tick.** After a shift click the items in the chest shift, and if the inventory is full, the stack stays in place. Searching from the start handles both cases: with a full inventory the script keeps clicking the same stack until closing kicks in.

**Container type instead of the title.** The server can rename the title to anything, but the menu type `generic_9x3` or `generic_9x6` is still a chest type.

**Closing in the same queue.** Clicks go into the Quick queue, and `close()` goes into it too. The chest closes after the last click, not instead of it.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.quickclient.cc/en/examples/chest-taker.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
