package com.ikar.examples.myfarm

import com.ikar.script.api.IkaScript
import com.ikar.script.api.L2Bot
import com.ikar.script.api.ScriptMeta
import com.ikar.script.api.model.nearest
import kotlinx.coroutines.delay

/**
 * Minimal IkaScript plugin: attack the nearest living mob, over and over.
 *
 * The plugin is picked up through ServiceLoader — the class name is listed in
 * `src/main/resources/META-INF/services/com.ikar.script.api.IkaScript`.
 *
 * `bot` is the single entry point into the API: `bot.npcs`, `bot.user` and the
 * other properties read the world, `bot.attack(...)` and friends are commands,
 * `bot.log(...)` writes to the script log.
 *
 * Read the world on every iteration: entities are a snapshot taken at read time
 * and do not update themselves.
 */
class MyFarm : IkaScript {

    override val meta = ScriptMeta(id = "my-farm", name = "My Farm (example)")

    override suspend fun run(bot: L2Bot) {
        bot.log("MyFarm started")
        while (true) {
            val mob = bot.npcs.nearest { it.attackable && !it.dead }
            if (mob != null) bot.attack(mob)
            delay(1000)
        }
    }

    override fun onStop() {
    }
}
