MineBot Plugin API
Search…
Movement (basic)
A basic code example that shows you how to make the bot move between two locations.

MovementTask.cs

Task class that will be registered in PluginBase.cs and will be responsible for Movement in this plugin. While the player is not then this task will move between two Locations.
Commented Code
Raw Code
1
public class MovementTask : ITask, ITickListener {
2
3
public static ILocation Location1 = new Location(100, 10, 100);
4
public static ILocation Location2 = new Location( 1, 10, 100);
5
6
public override bool Exec() {
7
// Do not execute 'OnTick' if the bot is dead.
8
return !Context.Player.IsDead();
9
}
10
11
public async Task OnTick() {
12
// The 'await' keyword waits until MoveTo completes/fails and
13
// only then continues the execution.
14
var moveResult = await Context.Player.MoveTo(Location1).Task;
15
if(moveResult.Result != MoveResultType.Completed) {
16
// Failed to move to the location, lets output an error
17
// to the console and return.
18
Console.WriteLine(quot;Failed to move to {Location1}!");
19
return;
20
}
21
22
// We have successfully reached Location1,
23
// now we can start moving to Location2.
24
var moveTask = Context.Player.MoveTo(Location2);
25
26
/*
27
As the MoveTo(Location2) code does not have the 'await' keyword
28
we can execute other actions parallel and optionally await later
29
for the task to complete.
30
*/
31
32
// Wait until MoveTo completes/fails to Location2. We do not care
33
// if it succeeded therefore we do no assign the result to any variable.
34
await moveTask.Task;
35
}
36
}
Copied!
1
public class MovementTask : ITask, ITickListener {
2
3
public static ILocation Location1 = new Location(100, 10, 100);
4
public static ILocation Location2 = new Location( 1, 10, 100);
5
6
public override bool Exec() {
7
return !Context.Player.IsDead();
8
}
9
10
public async Task OnTick() {
11
var moveResult = await Context.Player.MoveTo(Location1).Task;
12
if(moveResult.Result != MoveResultType.Completed) {
13
Console.WriteLine(quot;Failed to move to {Location1}!");
14
return;
15
}
16
17
await Context.Player.MoveTo(Location2).Task;
18
}
19
}
Copied!
It is usually desirable to use the 'await' keyword as it will pause all execution until the task completes and then the result can be used. Not awaiting for tasks to complete can cause another function (e.g.: OnTick) to start while a task is running (e.g.: while still moving).
Don't know what async programming is? Check out Microsoft's documentation here.
Last modified 1yr ago
Copy link