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.
Commented Code
Raw Code
1
public class FightTask : ITask, ITickListener
2
{
3
private string[] Friendly;
4
5
public FightTask(string[] friendly) {
6
this.Friendly = friendly;
7
}
8
9
public override bool Exec() {
10
return !Context.Player.IsDead() && !Context.Player.State.Eating;
11
}
12
13
public async Task OnTick() {
14
15
// Get the closest player to us that is not in our friends list.
16
var target = Context.Entities.GetClosestPlayer(false,
17
entity => !Friendly.Contains(entity.GetName()));
18
if (target == null)
19
// No target found, so do nothing this tick.
20
// Optionally you could make the bot roam in this case.
21
return;
22
23
// Begin moving towards the target. We do not await for the task
24
// to complete since we also want to attack the target while we
25
// are running.
26
var movementTask = target.Follow();
27
28
// Find the best sword in our inventory and attempt to select it.
29
// This should/could be in a different task, as this task should
30
// be only responsible for moving and attacking targets.
31
var sword = Inventory.FindBest(EquipmentType.Sword);
32
if(sword != null) await sword.Select();
33
34
// Run this for as long as we are chasing this player OR
35
// if we are already very close to the target player.
36
while (movementTask.IsMoving() ||
37
target.Position.Distance(Context.Player.GetPosition()) < 2) {
38
39
// Only attack the player if it is within 4 blocks to us.
40
if(target.Position.Distance(Context.Player.GetPosition()) < 5)
41
target.Attack();
42
43
// Wait 2 ticks before each attack.
44
await Context.TickManager.Sleep(2);
45
}
46
}
47
}
Copied!
1
public class FightTask : ITask, ITickListener
2
{
3
private string[] Friendly;
4
5
public FightTask(string[] friendly) {
6
this.Friendly = friendly;
7
}
8
9
public override bool Exec() {
10
return !Context.Player.IsDead() && !Context.Player.State.Eating;
11
}
12
13
public async Task OnTick() {
14
15
var target = Context.Entities.GetClosestPlayer(false, entity => !Friendly.Contains(entity.GetName()));
16
if (target == null) return;
17
18
var movementTask = target.Follow();
19
20
var sword = Inventory.FindBest(EquipmentType.Sword);
21
if(sword != null) await sword.Select();
22
23
while (movementTask.IsMoving() ||
24
target.Position.Distance(Context.Player.GetPosition()) < 2) {
25
26
if(target.Position.Distance(Context.Player.GetPosition()) < 5)
27
target.Attack();
28
29
await Context.TickManager.Sleep(2);
30
}
31
}
32
}
Copied!
RespawnTask.cs
This task is responsible for respawning the bot when it dies.
1
public class RespawnTask : ITask, IDeathListener
2
{
3
public override bool Exec() {
4
return true;
5
}
6
7
public async Task OnDeath() {
8
await Context.Player.Respawn();
9
}
10
}
Copied!
KillauraExample.zip
124KB
Binary
Full Plugin Source Code
Last modified 1yr ago
Copy link