# Entities

This part refers to the class [**IEntityList.cs**](https://github.com/OnlyQubes/OQ.MineBot.PluginBase/blob/master/Classes/Entity/Lists/IEntityList.cs), which can be accessed through **Context.Entities**. This should be used when getting/finding entities/mobs/other players and will generally only return those values which have not been (yet) unloaded by the server, usually meaning that they are still in the entity render distance of the server.

| Methods             | Events          |
| ------------------- | --------------- |
| GetBots             | onEntityAdded   |
| GetClosestBot       | onEntityRemoved |
| GetPlayers          | onEntityMoved   |
| GetPlayer           | onPlayerMoved   |
| GetPlayerByUuid     |                 |
| GetClosestPlayer    |                 |
| GetEntities         |                 |
| GetEntity           |                 |
| GetClosestEntity    |                 |
| GetMobs             |                 |
| GetClosestMob       |                 |
| GetObjects          |                 |
| GetItemStackObjects |                 |
| GetClosestObject    |                 |
| GetAllLoadedNames   |                 |

## Methods

### GetBots

Returns a collection of **IPlayerEntity** where each entry corresponds to a (rendered) bot's entity.\
This may not get all bot entities in cases where the other bots are outside of this bot's entity render distance.

```csharp
IEnumerable<IPlayerEntity> GetBots();
```

###

### GetClosestBot

Returns the closest other bot's entity to the given position.\
This may return null if no bots are connected/in entity render distance.

```csharp
// Gets the closest bot to the current bot's location.
IPlayerEntity GetClosestBot();
// Gets the closest (other) bot to the given position.
IPlayerEntity GetClosestBot(IPosition position);
// Gets the closest (other) bot to the given location.
IPlayerEntity GetClosestBot(ILocation position);
// Gets the closest (other) bot to the given position.
IPlayerEntity GetClosestBot(double x, double y, double z);
```

###

### GetPlayers

Returns a collection of **IPlayerEntity** where each entry corresponds to a (rendered) player on the server. This can include bot entities depending on the parameters.

```csharp
IEnumerable<IPlayerEntity> GetPlayers(bool includeBots);
```

###

### GetPlayer

Returns a single **IPlayerEntity** according to the specified parameters.

```csharp
// Gets player by its entityId, where entity id is a unique
// number assigned to an entity by the server.
IPlayerEntity GetPlayer(int entityId);
// Gets player by its (user)name, e.g.: GetPlayer("OnlyQubes");
IPlayerEntity GetPlayer(string name );
```

###

### GetPlayerByUuid

Returns a single **IPlayerEntity** according to the specified [uuid](https://mcuuid.net/).

```csharp
IPlayerEntity GetPlayerByUuid(string uuid);
```

###

### GetClosestPlayer

Returns the closest other **IPlayerEntity** to the specified position. This can include bot entities depending on the parameters.

\* optionalValidityCheck - *optional parameter, which if not null will be called with a IPlayerEntity parameter. If this functions returns true then the entity will be processed, otherwise it will not be considered for the returned collection.*

```csharp
// Gets the closest player to the current bot's location.
IPlayerEntity GetClosestPlayer(bool includeBots = false, Func<IPlayerEntity, bool> optionalValidityCheck = null);
// Gets the closest player to the given position.
IPlayerEntity GetClosestPlayer(IPosition position, bool includeBots = false, Func<IPlayerEntity, bool> optionalValidityCheck = null);
// Gets the closest player to the given location.
IPlayerEntity GetClosestPlayer(ILocation position, bool includeBots = false, Func<IPlayerEntity, bool> optionalValidityCheck = null);
// Gets the closest player to the given position.
IPlayerEntity GetClosestPlayer(double x, double y, double z, bool includeBots = false, Func<IPlayerEntity, bool> optionalValidityCheck = null);
```

{% hint style="info" %}
Example call:\
`// Gets the closest player who's name starts with "Bot".`\
`Context.Entities.GetClosestPlayer(true, playerEntity => playerEntity.GetName().StartsWith("Bot"));`
{% endhint %}

{% content-ref url="entities/iplayerentity" %}
[iplayerentity](https://docs.minecraftbot.com/api/context/entities/iplayerentity)
{% endcontent-ref %}

###

### GetEntities

Returns a collection of **ILiving** where each entry corresponds to a (rendered) entity, such as an animal, a monster, and optionally can include players.

```csharp
IEnumerable<ILiving> GetEntities(bool includePlayers = false);
```

###

### GetEntity

Returns a single **ILiving** according to the specified entity id.

```csharp
// Gets entity by its entityId, where entity id is a unique
// number assigned to an entity by the server.
ILiving GetEntity(int entityId);
```

###

### GetClosestEntity

Returns the closest other **ILiving** to the specified position. This can include player entities depending on the parameters.

\* optionalValidityCheck - *optional parameter, which if not null will be called with a ILiving parameter. If this functions returns true then the entity will be processed, otherwise it will not be considered for the returned collection.*

```csharp
// Gets the closest entity to the current bot's location.
ILiving GetClosestEntity(bool includePlayers = false, Func<ILiving, bool> optionalValidityCheck = null);
// Gets the closest entity to the given position.
ILiving GetClosestEntity(IPosition position, bool includePlayers = false, Func<ILiving, bool> optionalValidityCheck = null);
// Gets the closest entity to the given location.
ILiving GetClosestEntity(ILocation position, bool includePlayers = false, Func<ILiving, bool> optionalValidityCheck = null);
// Gets the closest entity to the given position.
ILiving GetClosestEntity(double x, double y, double z, bool includePlayers = false, Func<ILiving, bool> optionalValidityCheck = null);
```

###

### GetMobs

Returns a collection of **IMobEntity** where each entry corresponds to a (rendered) mob, such as an animal, or a monster. The parameters of this function allow you to specify what sort of monsters can be included in the collection (e.g.: all, aggressive, passive, zombies).

```csharp
IEnumerable<IMobEntity> GetMobs(MobType type = MobType.All)
```

###

### GetClosestMob

Returns the closest **IMobEntity** to the specified position. This can look only for specific mob types, such as aggressive (MobType.Aggressive), passive (MobType.Pasive), zombies (MobType.Zomboe).

\* optionalValidityCheck - *optional parameter, which if not null will be called with a IMobEntity parameter. If this functions returns true then the entity will be processed, otherwise it will not be considered for the returned collection.*

```csharp
// Gets the closest mob to the current bot's location.
IMobEntity GetClosestMob(MobType type = MobType.All, Func<IMobEntity, bool> optionalValidityCheck = null);
// Gets the closest mob to the given position.
IMobEntity GetClosestMob(IPosition position, MobType type = MobType.All, Func<IMobEntity, bool> optionalValidityCheck = null);
// Gets the closest mob to the given location.
IMobEntity GetClosestMob(ILocation position, MobType type = MobType.All, Func<IMobEntity, bool> optionalValidityCheck = null);
// Gets the closest mob to the given position.
IMobEntity GetClosestMob(double x, double y, double z, MobType type = MobType.All, Func<IMobEntity, bool> optionalValidityCheck = null);
```

{% content-ref url="entities/imobentity" %}
[imobentity](https://docs.minecraftbot.com/api/context/entities/imobentity)
{% endcontent-ref %}

###

### GetObjects

Returns a collection of **IObjectEntity** where each entry corresponds to an object, which is a special type of entity that will not show up when searching the internal entity list with GetEntities(). An example of an object is a dropped stack of items on the ground or an arrow.

```csharp
// Type allows you to specify what type of objects you are looking for.
// e.g.: arrows
IEnumerable<IObjectEntity> GetObjects(ObjectTypes type = ObjectTypes.All);
```

###

### GetItemStackObjects

Returns a collection of **IObjectEntity** where each entry corresponds to an object, where each entry corresponds to a stack of items on the ground. This is a wrapper function for GetObjects().

```csharp
IEnumerable<IObjectEntity> GetItemStackObjects();
```

###

### GetClosestObject

Returns the closest **IObjectEntity** to the specified position. This can look for specific types of objects (e.g.: arrows, item stacks).

\* optionalValidityCheck - *optional parameter, which if not null will be called with a IObjectEntity parameter. If this functions returns true then the entity will be processed, otherwise it will not be considered for the returned collection.*

```csharp
// Gets the closest object to the current bot's location.
IObjectEntity GetClosestObject(ObjectTypes type = ObjectTypes.All, Func<IObjectEntity, bool> optionalValidityCheck = null);
// Gets the closest object to the given position.
IObjectEntity GetClosestObject(IPosition position, ObjectTypes type = ObjectTypes.All, Func<IObjectEntity, bool> optionalValidityCheck = null);
// Gets the closest object to the given location.
IObjectEntity GetClosestObject(ILocation position, ObjectTypes type = ObjectTypes.All, Func<IObjectEntity, bool> optionalValidityCheck = null);
// Gets the closest object to the given position.
IObjectEntity GetClosestObject(double x, double y, double z, ObjectTypes type = ObjectTypes.All, Func<IObjectEntity, bool> optionalValidityCheck = null);
```

{% content-ref url="entities/iobjectentity" %}
[iobjectentity](https://docs.minecraftbot.com/api/context/entities/iobjectentity)
{% endcontent-ref %}

### GetAllLoadedNames

Returns all loaded uuid to name links from the server.

```csharp
IEnumerable<UUID> GetAllLoadedNames();
```

## Events

{% hint style="info" %}
You can register a function to an event with the '+=' operator.\
E.g.: `Context.Events.OnChat += ChatMessageHandler;`
{% endhint %}

### onEntityAdded

Event that is called when any entity is added to the loaded entity list. This includes players, mobs, and other entity types.\
The add event can be cancelled by setting the EvenCancelToken to isCancelled = true, which would make it not add the entity to the bot's entity list.

```csharp
EntityDelegate onEntityAdded;
// onEntityAdded += (entity, token) => 
//                  Console.WriteLine($"Loaded entity with id {entity.entitiId}");
```

###

### onEntityRemoved

Event that is called when any entity is removed from the loaded entity list. This includes players, mobs, and other entity types.\
The add event can be cancelled by setting the EvenCancelToken to isCancelled = true, which would make it not remove the entity from the bot's entity list.

```csharp
EntityDelegate onEntityRemoved;
// onEntityRemoved += (entity, token) => 
//                    Console.WriteLine($"Unloaded entity with id {entity.entityId}");
```

###

### onEntityMoved

Event that is called when any entity moves/is moved. This includes players, mobs, and other entity types.

```csharp
EntityInformationDelegate onEntityMoved;
// onEntityMoved += (entity) =>
//                  Console.WriteLine($"Entity with id {entity.entityId} moved");
```

###

### onPlayerMoved

Event that is called when a player entity moves/is moved.

```csharp
EntityInformationDelegate onPlayerMoved;
// onPlayerMoved += (entity) =>
// Console.WriteLine($"Player with name {((IPlayerEntity)entity).GetName()} moved");
```

## Example code using Entities

The code below searches for an player entity with the name "OnlyQubes". If the player is not found then the bot does nothing, otherwise if the player is found then we move to it's location and attempt to hit the closest aggressive mob while moving.

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

```csharp
public async void OnTick() {
    // Attempt to find the player with the username "OnlyQubes",
    // if the user is not found then we return/do nothing.
    IPlayerEntity playerEntity = Context.Entities.GetPlayer("OnlyQubes");
    if(playerEntity == null) return;
    
    // Attempt to move to the "OnlyQubes" player, while
    // moving attempt (as we don't check for distance) to hit the closest
    // aggresive mob.
    var moveTask = playerEntity.MoveTo();
    while(!moveTask.complete) {
        IMobEntity mobEntity = Context.Entities.GetClosestMob(MobType.Aggressive);
        if(mobEntity != null) await mobEntity.Attack();
        else await Context.TickManager.Wait(1); // no monster found, wait 1 tick before continuing the while loop.
    }
}
```

{% endtab %}

{% tab title="Raw Code" %}

```csharp
public async void OnTick() {
    IPlayerEntity playerEntity = Context.Entities.GetPlayer("OnlyQubes");
    if(playerEntity == null) return;
    
    var moveTask = playerEntity.MoveTo();
    while(!moveTask.complete) {
        IMobEntity mobEntity = Context.Entities.GetClosestMob(MobType.Aggressive);
        if(mobEntity != null) await mobEntity.Attack();
        else await Context.TickManager.Wait(1);
    }
}
```

{% endtab %}
{% endtabs %}
