# Movement (basic)

#### 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.

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

```csharp
public class MovementTask : ITask, ITickListener {
    
    public static ILocation Location1 = new Location(100, 10, 100);
    public static ILocation Location2 = new Location(  1, 10, 100);
    
    public override bool Exec() {
        // Do not execute 'OnTick' if the bot is dead.
        return !Context.Player.IsDead();
    }
    
    public async Task OnTick() {
        // The 'await' keyword waits until MoveTo completes/fails and
        // only then continues the execution.
        var moveResult = await Context.Player.MoveTo(Location1).Task;
        if(moveResult.Result != MoveResultType.Completed) {
            // Failed to move to the location, lets output an error 
            // to the console and return.
            Console.WriteLine($"Failed to move to {Location1}!");
            return;
        }
        
        // We have successfully reached Location1, 
        // now we can start moving to Location2.
        var moveTask = Context.Player.MoveTo(Location2);
        
        /*
            As the MoveTo(Location2) code does not have the 'await' keyword 
            we can execute other actions parallel and optionally await later
            for the task to complete.
        */
        
        // Wait until MoveTo completes/fails to Location2. We do not care
        // if it succeeded therefore we do no assign the result to any variable.
        await moveTask.Task;
    }
}
```

{% endtab %}

{% tab title="Raw Code" %}

```csharp
public class MovementTask : ITask, ITickListener {
    
    public static ILocation Location1 = new Location(100, 10, 100);
    public static ILocation Location2 = new Location(  1, 10, 100);
    
    public override bool Exec() {
        return !Context.Player.IsDead();
    }
    
    public async Task OnTick() {
        var moveResult = await Context.Player.MoveTo(Location1).Task;
        if(moveResult.Result != MoveResultType.Completed) {
            Console.WriteLine($"Failed to move to {Location1}!");
            return;
        }
        
        await Context.Player.MoveTo(Location2).Task;
    }
}
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
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](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/).
{% endhint %}


---

# 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/movement-basic.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.
