Implement pull request #982

LookAtLocation and AutoLook
This commit is contained in:
ORelio 2020-05-01 14:02:23 +02:00
parent c2dc483d36
commit 9dae40153c
3 changed files with 82 additions and 0 deletions

View file

@ -168,8 +168,17 @@ namespace MinecraftClient
/// <param name="entity">Entity wich has just disappeared</param>
public virtual void OnEntityDespawn(Mapping.Entity entity) { }
/// <summary>
/// Called when the player held item has changed
/// </summary>
/// <param name="slot">New slot ID</param>
public virtual void OnHeldItemChange(byte slot) { }
/// <summary>
/// Called when the player health has been updated
/// </summary>
/// <param name="health">New player health</param>
/// <param name="food">New food level</param>
public virtual void OnHealthUpdate(float health, int food) { }
/* =================================================================== */
@ -685,6 +694,15 @@ namespace MinecraftClient
return Handler.MoveTo(location, allowUnsafe);
}
/// <summary>
/// Look at the specified location
/// </summary>
/// <param name="location">Location to look at</param>
protected void LookAtLocation(Mapping.Location location)
{
Handler.UpdateLocation(Handler.GetCurrentLocation(), location);
}
/// <summary>
/// Get a Y-M-D h:m:s timestamp representing the current system date and time
/// </summary>

View file

@ -0,0 +1,64 @@
//MCCScript 1.0
MCC.LoadBot(new AutoLook());
//MCCScript Extensions
public class AutoLook : ChatBot
{
private Entity _entityToLookAt;
public override void Initialize()
{
if (GetEntityHandlingEnabled() && GetTerrainEnabled()) return;
LogToConsole("Entity Handling or Terrain Handling is not enabled in the config file!");
LogToConsole("This bot will be unloaded.");
UnloadBot();
}
public override void OnEntityDespawn(Entity entity)
{
if (entity == _entityToLookAt)
{
_entityToLookAt = null;
}
}
public override void OnEntitySpawn(Entity entity)
{
HandleEntity(entity);
}
public override void OnEntityMove(Entity entity)
{
var tempBool = HandleEntity(entity);
//LogDebugToConsole(tempBool);
if (!tempBool) return;
LookAtLocation(entity.Location);
}
/// <summary>
/// Handles an entity, and tracks it if it is closer then the one we are currently tracking
/// </summary>
/// <returns>True if found</returns>
private bool HandleEntity(Entity entity)
{
if (entity.Type != EntityType.Player)
{
return false;
}
if (_entityToLookAt == null)
{
_entityToLookAt = entity;
return true;
}
if (GetCurrentLocation().Distance(entity.Location) < GetCurrentLocation().Distance(_entityToLookAt.Location))
{
_entityToLookAt = entity;
return true;
}
if (entity.ID != _entityToLookAt.ID) return false;
_entityToLookAt = entity; //Handle looking at the same entity
return true;
}
}