# Killaura (intermediate)

**FightTask.cs**

This task is responsible for targeting, moving, and attacking the closest non-friendly player.

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

```csharp
public class FightTask : ITask, ITickListener
    {
        private string[] Friendly;

        public FightTask(string[] friendly) {
            this.Friendly = friendly;
        }

        public override bool Exec() {
            return !Context.Player.IsDead() && !Context.Player.State.Eating;
        }

        public async Task OnTick() {

            // Get the closest player to us that is not in our friends list.
            var target = Context.Entities.GetClosestPlayer(false,
                         entity => !Friendly.Contains(entity.GetName()));
            if (target == null)
                // No target found, so do nothing this tick.
                // Optionally you could make the bot roam in this case.
                return;

            // Begin moving towards the target. We do not await for the task
            // to complete since we also want to attack the target while we
            // are running.
            var movementTask = target.Follow();

            // Find the best sword in our inventory and attempt to select it.
            // This should/could be in a different task, as this task should
            // be only responsible for moving and attacking targets.
            var sword = Inventory.FindBest(EquipmentType.Sword);
            if(sword != null) await sword.Select();

            // Run this for as long as we are chasing this player OR
            // if we are already very close to the target player.
            while (movementTask.IsMoving() ||
                   target.Position.Distance(Context.Player.GetPosition()) < 2) {
                
                // Only attack the player if it is within 4 blocks to us.
                if(target.Position.Distance(Context.Player.GetPosition()) < 5)
                    target.Attack();

                // Wait 2 ticks before each attack.
                await Context.TickManager.Sleep(2);
            }
        }
    }
```

{% endtab %}

{% tab title="Raw Code" %}

```csharp
public class FightTask : ITask, ITickListener
    {
        private string[] Friendly;

        public FightTask(string[] friendly) {
            this.Friendly = friendly;
        }

        public override bool Exec() {
            return !Context.Player.IsDead() && !Context.Player.State.Eating;
        }

        public async Task OnTick() {

            var target = Context.Entities.GetClosestPlayer(false, entity => !Friendly.Contains(entity.GetName()));
            if (target == null) return;

            var movementTask = target.Follow();

            var sword = Inventory.FindBest(EquipmentType.Sword);
            if(sword != null) await sword.Select();

            while (movementTask.IsMoving() ||
                   target.Position.Distance(Context.Player.GetPosition()) < 2) {
                
                if(target.Position.Distance(Context.Player.GetPosition()) < 5)
                    target.Attack();

                await Context.TickManager.Sleep(2);
            }
        }
    }
```

{% endtab %}
{% endtabs %}

**RespawnTask.cs**

This task is responsible for respawning the bot when it dies.&#x20;

```csharp
    public class RespawnTask : ITask, IDeathListener
    {
        public override bool Exec() {
            return true;
        }

        public async Task OnDeath() {
            await Context.Player.Respawn();
        }
    }
```

{% file src="/files/-LtGcvuNGvR0xvdmwDq7" %}
Full Plugin Source Code
{% endfile %}


---

# 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/examples/start-plugins/killaura-advanced.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.
