Entities
This part refers to the class 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.
1
IEnumerable<IPlayerEntity> GetBots();
Copied!

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.
1
// Gets the closest bot to the current bot's location.
2
IPlayerEntity GetClosestBot();
3
// Gets the closest (other) bot to the given position.
4
IPlayerEntity GetClosestBot(IPosition position);
5
// Gets the closest (other) bot to the given location.
6
IPlayerEntity GetClosestBot(ILocation position);
7
// Gets the closest (other) bot to the given position.
8
IPlayerEntity GetClosestBot(double x, double y, double z);
Copied!

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.
1
IEnumerable<IPlayerEntity> GetPlayers(bool includeBots);
Copied!

GetPlayer

Returns a single IPlayerEntity according to the specified parameters.
1
// Gets player by its entityId, where entity id is a unique
2
// number assigned to an entity by the server.
3
IPlayerEntity GetPlayer(int entityId);
4
// Gets player by its (user)name, e.g.: GetPlayer("OnlyQubes");
5
IPlayerEntity GetPlayer(string name );
Copied!

GetPlayerByUuid

Returns a single IPlayerEntity according to the specified uuid.
1
IPlayerEntity GetPlayerByUuid(string uuid);
Copied!

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.
1
// Gets the closest player to the current bot's location.
2
IPlayerEntity GetClosestPlayer(bool includeBots = false, Func<IPlayerEntity, bool> optionalValidityCheck = null);
3
// Gets the closest player to the given position.
4
IPlayerEntity GetClosestPlayer(IPosition position, bool includeBots = false, Func<IPlayerEntity, bool> optionalValidityCheck = null);
5
// Gets the closest player to the given location.
6
IPlayerEntity GetClosestPlayer(ILocation position, bool includeBots = false, Func<IPlayerEntity, bool> optionalValidityCheck = null);
7
// Gets the closest player to the given position.
8
IPlayerEntity GetClosestPlayer(double x, double y, double z, bool includeBots = false, Func<IPlayerEntity, bool> optionalValidityCheck = null);
Copied!
Example call: // Gets the closest player who's name starts with "Bot". Context.Entities.GetClosestPlayer(true, playerEntity => playerEntity.GetName().StartsWith("Bot"));

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.
1
IEnumerable<ILiving> GetEntities(bool includePlayers = false);
Copied!

GetEntity

Returns a single ILiving according to the specified entity id.
1
// Gets entity by its entityId, where entity id is a unique
2
// number assigned to an entity by the server.
3
ILiving GetEntity(int entityId);
Copied!

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.
1
// Gets the closest entity to the current bot's location.
2
ILiving GetClosestEntity(bool includePlayers = false, Func<ILiving, bool> optionalValidityCheck = null);
3
// Gets the closest entity to the given position.
4
ILiving GetClosestEntity(IPosition position, bool includePlayers = false, Func<ILiving, bool> optionalValidityCheck = null);
5
// Gets the closest entity to the given location.
6
ILiving GetClosestEntity(ILocation position, bool includePlayers = false, Func<ILiving, bool> optionalValidityCheck = null);
7
// Gets the closest entity to the given position.
8
ILiving GetClosestEntity(double x, double y, double z, bool includePlayers = false, Func<ILiving, bool> optionalValidityCheck = null);
Copied!

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).
1
IEnumerable<IMobEntity> GetMobs(MobType type = MobType.All)
Copied!

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.
1
// Gets the closest mob to the current bot's location.
2
IMobEntity GetClosestMob(MobType type = MobType.All, Func<IMobEntity, bool> optionalValidityCheck = null);
3
// Gets the closest mob to the given position.
4
IMobEntity GetClosestMob(IPosition position, MobType type = MobType.All, Func<IMobEntity, bool> optionalValidityCheck = null);
5
// Gets the closest mob to the given location.
6
IMobEntity GetClosestMob(ILocation position, MobType type = MobType.All, Func<IMobEntity, bool> optionalValidityCheck = null);
7
// Gets the closest mob to the given position.
8
IMobEntity GetClosestMob(double x, double y, double z, MobType type = MobType.All, Func<IMobEntity, bool> optionalValidityCheck = null);
Copied!

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.
1
// Type allows you to specify what type of objects you are looking for.
2
// e.g.: arrows
3
IEnumerable<IObjectEntity> GetObjects(ObjectTypes type = ObjectTypes.All);
Copied!

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().
1
IEnumerable<IObjectEntity> GetItemStackObjects();
Copied!

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.
1
// Gets the closest object to the current bot's location.
2
IObjectEntity GetClosestObject(ObjectTypes type = ObjectTypes.All, Func<IObjectEntity, bool> optionalValidityCheck = null);
3
// Gets the closest object to the given position.
4
IObjectEntity GetClosestObject(IPosition position, ObjectTypes type = ObjectTypes.All, Func<IObjectEntity, bool> optionalValidityCheck = null);
5
// Gets the closest object to the given location.
6
IObjectEntity GetClosestObject(ILocation position, ObjectTypes type = ObjectTypes.All, Func<IObjectEntity, bool> optionalValidityCheck = null);
7
// Gets the closest object to the given position.
8
IObjectEntity GetClosestObject(double x, double y, double z, ObjectTypes type = ObjectTypes.All, Func<IObjectEntity, bool> optionalValidityCheck = null);
Copied!

GetAllLoadedNames

Returns all loaded uuid to name links from the server.
1
IEnumerable<UUID> GetAllLoadedNames();
Copied!

Events

You can register a function to an event with the '+=' operator. E.g.: Context.Events.OnChat += ChatMessageHandler;

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.
1
EntityDelegate onEntityAdded;
2
// onEntityAdded += (entity, token) =>
3
// Console.WriteLine(quot;Loaded entity with id {entity.entitiId}");
Copied!

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.
1
EntityDelegate onEntityRemoved;
2
// onEntityRemoved += (entity, token) =>
3
// Console.WriteLine(quot;Unloaded entity with id {entity.entityId}");
Copied!

onEntityMoved

Event that is called when any entity moves/is moved. This includes players, mobs, and other entity types.
1
EntityInformationDelegate onEntityMoved;
2
// onEntityMoved += (entity) =>
3
// Console.WriteLine(quot;Entity with id {entity.entityId} moved");
Copied!

onPlayerMoved

Event that is called when a player entity moves/is moved.
1
EntityInformationDelegate onPlayerMoved;
2
// onPlayerMoved += (entity) =>
3
// Console.WriteLine(quot;Player with name {((IPlayerEntity)entity).GetName()} moved");
Copied!

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.
Commented Code
Raw Code
1
public async void OnTick() {
2
// Attempt to find the player with the username "OnlyQubes",
3
// if the user is not found then we return/do nothing.
4
IPlayerEntity playerEntity = Context.Entities.GetPlayer("OnlyQubes");
5
if(playerEntity == null) return;
6
7
// Attempt to move to the "OnlyQubes" player, while
8
// moving attempt (as we don't check for distance) to hit the closest
9
// aggresive mob.
10
var moveTask = playerEntity.MoveTo();
11
while(!moveTask.complete) {
12
IMobEntity mobEntity = Context.Entities.GetClosestMob(MobType.Aggressive);
13
if(mobEntity != null) await mobEntity.Attack();
14
else await Context.TickManager.Wait(1); // no monster found, wait 1 tick before continuing the while loop.
15
}
16
}
Copied!
1
public async void OnTick() {
2
IPlayerEntity playerEntity = Context.Entities.GetPlayer("OnlyQubes");
3
if(playerEntity == null) return;
4
5
var moveTask = playerEntity.MoveTo();
6
while(!moveTask.complete) {
7
IMobEntity mobEntity = Context.Entities.GetClosestMob(MobType.Aggressive);
8
if(mobEntity != null) await mobEntity.Attack();
9
else await Context.TickManager.Wait(1);
10
}
11
}
Copied!
Last modified 1yr ago