Skip to content

Commit 9dae401

Browse files
committed
Implement pull request #982
LookAtLocation and AutoLook
1 parent c2dc483 commit 9dae401

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

MinecraftClient/ChatBot.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,17 @@ public virtual void OnEntitySpawn(Mapping.Entity entity) { }
168168
/// <param name="entity">Entity wich has just disappeared</param>
169169
public virtual void OnEntityDespawn(Mapping.Entity entity) { }
170170

171+
/// <summary>
172+
/// Called when the player held item has changed
173+
/// </summary>
174+
/// <param name="slot">New slot ID</param>
171175
public virtual void OnHeldItemChange(byte slot) { }
172176

177+
/// <summary>
178+
/// Called when the player health has been updated
179+
/// </summary>
180+
/// <param name="health">New player health</param>
181+
/// <param name="food">New food level</param>
173182
public virtual void OnHealthUpdate(float health, int food) { }
174183

175184
/* =================================================================== */
@@ -685,6 +694,15 @@ protected bool MoveToLocation(Mapping.Location location, bool allowUnsafe = fals
685694
return Handler.MoveTo(location, allowUnsafe);
686695
}
687696

697+
/// <summary>
698+
/// Look at the specified location
699+
/// </summary>
700+
/// <param name="location">Location to look at</param>
701+
protected void LookAtLocation(Mapping.Location location)
702+
{
703+
Handler.UpdateLocation(Handler.GetCurrentLocation(), location);
704+
}
705+
688706
/// <summary>
689707
/// Get a Y-M-D h:m:s timestamp representing the current system date and time
690708
/// </summary>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//MCCScript 1.0
2+
3+
MCC.LoadBot(new AutoLook());
4+
5+
//MCCScript Extensions
6+
7+
public class AutoLook : ChatBot
8+
{
9+
private Entity _entityToLookAt;
10+
public override void Initialize()
11+
{
12+
if (GetEntityHandlingEnabled() && GetTerrainEnabled()) return;
13+
LogToConsole("Entity Handling or Terrain Handling is not enabled in the config file!");
14+
LogToConsole("This bot will be unloaded.");
15+
UnloadBot();
16+
}
17+
18+
public override void OnEntityDespawn(Entity entity)
19+
{
20+
if (entity == _entityToLookAt)
21+
{
22+
_entityToLookAt = null;
23+
}
24+
}
25+
public override void OnEntitySpawn(Entity entity)
26+
{
27+
HandleEntity(entity);
28+
}
29+
public override void OnEntityMove(Entity entity)
30+
{
31+
var tempBool = HandleEntity(entity);
32+
//LogDebugToConsole(tempBool);
33+
if (!tempBool) return;
34+
LookAtLocation(entity.Location);
35+
}
36+
37+
/// <summary>
38+
/// Handles an entity, and tracks it if it is closer then the one we are currently tracking
39+
/// </summary>
40+
/// <returns>True if found</returns>
41+
private bool HandleEntity(Entity entity)
42+
{
43+
if (entity.Type != EntityType.Player)
44+
{
45+
return false;
46+
}
47+
if (_entityToLookAt == null)
48+
{
49+
_entityToLookAt = entity;
50+
return true;
51+
}
52+
if (GetCurrentLocation().Distance(entity.Location) < GetCurrentLocation().Distance(_entityToLookAt.Location))
53+
{
54+
_entityToLookAt = entity;
55+
return true;
56+
}
57+
58+
if (entity.ID != _entityToLookAt.ID) return false;
59+
_entityToLookAt = entity; //Handle looking at the same entity
60+
return true;
61+
62+
}
63+
64+
}
File renamed without changes.

0 commit comments

Comments
 (0)