Killaura (intermediate)
This example will show you code for a basic plugin that will follow and attack the closest target-able player. The plugin incorporates multiple tasks and therefore is marked as intermediate.
FightTask.cs
This task is responsible for targeting, moving, and attacking the closest non-friendly player.
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);
            }
        }
    }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);
            }
        }
    }RespawnTask.cs
This task is responsible for respawning the bot when it dies.
    public class RespawnTask : ITask, IDeathListener
    {
        public override bool Exec() {
            return true;
        }
        public async Task OnDeath() {
            await Context.Player.Respawn();
        }
    }Last updated