# example addon

This example demonstrates API resolution, event listening, state reading, metadata, and spawning a template bot.

## `plugin.yml`

```yaml
name: PracticeBotExampleAddon
main: com.example.practicebotaddon.PracticeBotExampleAddon
version: 1.0.0
api-version: '1.21'
depend: [PracticeBot]
```

## Main class

```java
package com.example.practicebotaddon;

import com.sheldera.practicebot.api.PracticeBotApi;
import com.sheldera.practicebot.api.PracticeBotApiProvider;
import com.sheldera.practicebot.api.bot.PracticeBotHandle;
import com.sheldera.practicebot.api.bot.PracticeBotStateSnapshot;
import com.sheldera.practicebot.api.event.PracticeBotSpawnEvent;
import com.sheldera.practicebot.api.metadata.PracticeBotMetadataType;
import com.sheldera.practicebot.api.metadata.PracticeBotMetadataValue;
import com.sheldera.practicebot.api.spawn.BotSpawnRequest;
import org.bukkit.NamespacedKey;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

public final class PracticeBotExampleAddon extends JavaPlugin implements Listener {

    private PracticeBotApi api;
    private NamespacedKey spawnCounterKey;

    @Override
    public void onEnable() {
        this.api = PracticeBotApiProvider.get();
        this.spawnCounterKey = new NamespacedKey(this, "spawn-counter");

        getServer().getPluginManager().registerEvents(this, this);
        getLogger().info("Connected to PracticeBot API " + api.getApiVersion());
    }

    @EventHandler
    public void onPracticeBotSpawn(PracticeBotSpawnEvent event) {
        PracticeBotHandle bot = event.getBot();
        PracticeBotStateSnapshot state = bot.getStateSnapshot();

        int next = bot.getMetadata(spawnCounterKey)
                .filter(value -> value.type() == PracticeBotMetadataType.INTEGER)
                .map(PracticeBotMetadataValue::value)
                .map(Integer.class::cast)
                .orElse(0) + 1;

        bot.setMetadata(spawnCounterKey, PracticeBotMetadataType.INTEGER, next);

        getLogger().info("PracticeBot spawned: " + state.name()
                + " type=" + state.botType()
                + " count=" + next);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player player)) {
            sender.sendMessage("Players only.");
            return true;
        }

        String template = args.length > 0 ? args[0] : "arena_normal";

        try {
            BotSpawnRequest request = BotSpawnRequest.builder(template, player.getLocation())
                    .targetEntityUuid(player.getUniqueId())
                    .build();

            PracticeBotHandle bot = api.spawnBot(request);
            player.sendMessage("Spawned PracticeBot: " + bot.getStateSnapshot().name());
        } catch (RuntimeException ex) {
            player.sendMessage("Could not spawn PracticeBot: " + ex.getMessage());
        }

        return true;
    }
}
```

## Notes

* Keep the API dependency as `provided`.
* Use `depend: [PracticeBot]` for mandatory integrations.
* Keep all PracticeBot API calls on the main thread.
* Do not import internal PracticeBot classes.


---

# 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://edelweiss-network.gitbook.io/practicebot/developer-guide/example-addon.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.
