# Events

| Events                 |
| ---------------------- |
| onTick                 |
| onChat                 |
| onDisconnected         |
| onGameJoined           |
| onSpawned              |
| onWorldReload          |
| onHealthUpdate         |
| onDeath                |
| onStartedStarving      |
| onBlockChanged         |
| onChunkLoaded          |
| onPlayerMoved          |
| onInventoryChanged     |
| onSprintingChanged     |
| onExplosion            |
| onEntityEffectAdded    |
| onObjectSpawned        |
| onEntityVelocity       |
| onPlayerUpdate         |
| onExperienceChanged    |
| onResourcePackReceived |
| onEntityAttached       |

## Usage

You will generally be Registering tasks in the Start method of the task class. You will also generally want to unregister them in Stop method, otherwise the plugin may not stop execution.

```csharp
/* We assume that this is in an ITask class and the
   class is registered using 'RegisterTask' in the PluginCore class.
*/

public override async Task Start() {
   Context.Events.onChat += OnChat;
}
public override async Task Stop() {
   // The plugin has been stopped, therefore we should
   // unregister all the events.
   Context.Events.onChat -= OnChat;   
}

private void OnChat(IBotContext context, IChat message, byte position) {
   Console.WriteLine($"Bot '{context.Player.GetUsername()}' " +
                     $"received the message '{message.GetText()}'");
}
```

## Events

### onTick

Called each client tick, which is around 50ms. ITickListener uses this.

```csharp
event IPlayerDelegates.PlayerDelegate onTick;
```

### onChat

Called when the bot receives a chat message from the server. This also includes middle of the screen titles and above the hotbar messages. The type of message (chat, screen, above hotbar) can be determined by the position variable.

```csharp
event IPlayerDelegates.OnChatDelegate onChat;
```

### onDisconnected

Called once the bot is disconnected from the server.

```csharp
event IPlayerDelegates.PlayerReasonDelegate onDisconnected;
```

### onGameJoined

Called once the player joins the game. It is important to note that this will most likely run only if the plugin is enabled before the bot is started.

```csharp
event IPlayerDelegates.OnGameJoinedDelegate onGameJoined;
```

### onSpawned

Called once the player spawns into the game. This usually signifies that the bot's entity has been spawned by the server and is visible by other players.

```csharp
event IPlayerDelegates.OnSpawnedDelegate onSpawned;
```

### onHealthUpdate

Called once the bot's health or hunger is updated by the server. It is important to note that this is also for hunger changes as well, while the name suggest that it's only for health updates.

```csharp
event IPlayerDelegates.OnHealthUpdateDelegate onHealthUpdate;
```

### onDeath

Called when the bot's health drops to 0, which signifies that it has died.

```csharp
event IPlayerDelegates.OnDeathDelegate onDeath;
```

### onStartedStarving

Called when the bot starts starving, which is when it reaches 0 food.

```csharp
event IPlayerDelegates.OnStartedStartvingDelegate onStartedStarving;
```

### onBlockChange

Called once a block in the world changes.

```csharp
event IPlayerDelegates.OnBlockChangedDelegate onBlockChanged;
```

### onChunkLoaded

Called once a chunk is loaded/reloaded.

```csharp
event IPlayerDelegates.OnBlockChangedDelegate onBlockChanged;
```

### onPlayerMoved

Called once this bot is **moved by the server. This will not be triggered when the bot moves it self.**

```csharp
event IPlayerDelegates.OnPlayerMovedDelegate onPlayerMoved;
```

### onInventoryChanged

Called once an slot gets set or updated. This will also usually trigger when a new container is opened, as the server updates each slot of the container window.

```csharp
event IPlayerDelegates.OnInventoryChangedDelegate onInventoryChanged;
```

### onSprintChanged

Called once the bot's sprint state gets updated.

```csharp
event IPlayerDelegates.OnPlayerSprintUpdateDelegate onSprintingChanged;
```

### onWorldReload

Called once the world is fully reloaded, which is generally when respawning or teleporting.

```csharp
event IPlayerDelegates.PlayerDelegate onWorldReload;
```

### onEntityAttached <a href="#onentityattached" id="onentityattached"></a>

Called when the server sends an entity attached packet (e.g.: player sits in a minecart).\
Note: *This is only supported for 1.8.\**

```csharp
event IPlayerDelegates.OnEntityAttached onEntityAttached;
```

​

### OnResourcePackReceived <a href="#onresourcepackreceived" id="onresourcepackreceived"></a>

Called when the server sends a resource pack URL and hash to the client.

```csharp
event IPlayerDelegates.OnResourcePackReceived onResourcePackReceived
```

​

### onExperienceChanged <a href="#onexperiencechanged" id="onexperiencechanged"></a>

Called when the server updates the bot's experience

```csharp
event IPlayerDelegates.OnExperienceChanged onExperienceChanged
```

​

### onPlayerUpdate <a href="#onplayerupdate" id="onplayerupdate"></a>

Called before the player update, allows you to cancel all physics.

```csharp
event IPlayerDelegates.OnPlayerUpdate onPlayerUpdate;
```

​

### onEntityVelocity <a href="#onentityvelocity" id="onentityvelocity"></a>

Called once the server set's an entities velocity,

```csharp
event IPlayerDelegates.OnEntityVelocity onEntityVelocity;
```

​

### onObjectSpawned <a href="#onobjectspawned" id="onobjectspawned"></a>

Called once an object spawns.

```csharp
 event IPlayerDelegates.OnObjectSpawned onObjectSpawned;
```

​

### onEntityEffectAdded <a href="#onentityeffectadded" id="onentityeffectadded"></a>

Called once an Entity receives an effect.

```csharp
event IPlayerDelegates.OnEntityEffect onEntityEffectAdded;
```

​

### onExplosion <a href="#onexplosion" id="onexplosion"></a>

Called once an explosion occurs.

```csharp
event IPlayerDelegates.OnExplosionDelegate onExplosion;
```


---

# 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/api/events.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.
