Final code clean up and adding summary

This commit is contained in:
ReinforceZwei 2020-03-21 21:44:33 +08:00 committed by ORelio
parent 221d2525be
commit 6ee16513e0
7 changed files with 113388 additions and 55 deletions

113223
1.15.2.json Normal file

File diff suppressed because it is too large Load diff

View file

@ -820,21 +820,23 @@ namespace MinecraftClient
// auto attack entity within range // auto attack entity within range
// by reinforce // by reinforce
if (attackCooldown == 0) if (Settings.AutoAttackMobs)
{ {
attackCooldown = 6; if (attackCooldownCounter == 0)
if (entitiesToAttack.Count > 0)
{ {
foreach(KeyValuePair<int,Location> a in entitiesToAttack) attackCooldownCounter = attackCooldown;
if (entitiesToAttack.Count > 0)
{ {
handler.SendInteractEntityPacket(a.Key, 1); foreach (KeyValuePair<int, Entity> a in entitiesToAttack)
ConsoleIO.WriteLine("Attacked Entity with ID " + a.Key); {
handler.SendInteractEntityPacket(a.Key, 1);
}
} }
} }
} else
else {
{ attackCooldownCounter--;
attackCooldown--; }
} }
} }
@ -1040,50 +1042,66 @@ namespace MinecraftClient
} }
// by reinforce // by reinforce
//public List<Entity> entitiesToAttack = new List<Entity>(); public class Entity
//public List<Entity> entitiesInAttackList = new List<Entity>(); {
public Dictionary<int,Location> entitiesToAttack = new Dictionary<int, Location>(); public int ID;
public Dictionary<int, Location> entitiesToTrack = new Dictionary<int, Location>(); public int Type;
public string Name;
public Location Location;
public Entity(int ID,Location location)
{
this.ID = ID;
this.Location = location;
}
public Entity(int ID,int Type,string Name,Location location)
{
this.ID = ID;
this.Type = Type;
this.Name = Name;
this.Location = location;
}
}
public Dictionary<int,Entity> entitiesToAttack = new Dictionary<int, Entity>(); // mobs within attack range
public Dictionary<int, Entity> entitiesToTrack = new Dictionary<int, Entity>(); // all mobs in view distance
public int attackCooldown = 6; public int attackCooldown = 6;
public int attackCooldownCounter = 6;
public Double attackSpeed; public Double attackSpeed;
public Double attackCooldownSecond; public Double attackCooldownSecond;
public int attackRange = 4;
/// <summary>
/// Called when an Entity was created/spawned.
/// </summary>
/// <param name="EntityID"></param>
/// <param name="EntityType"></param>
/// <param name="UUID"></param>
/// <param name="location"></param>
public void OnSpawnLivingEntity(int EntityID, int EntityType, Guid UUID, Location location) public void OnSpawnLivingEntity(int EntityID, int EntityType, Guid UUID, Location location)
{ {
string msg; if (!Settings.AutoAttackMobs) return;
string name = getEntityName(EntityType); string name = getEntityName(EntityType);
if (name == "") if (name != "")
{ {
//msg = "Spawn Entity with typeID " + EntityType.ToString(); Entity entity = new Entity(EntityID, EntityType, name, location);
//msg = ""; entitiesToTrack.Add(EntityID, entity);
} if (calculateDistance(location, GetCurrentLocation()) < attackRange)
else
{
msg = "Spawn Entity " + getEntityName(EntityType);
//handler.SendInteractEntityPacket(EntityID, 1);
//ConsoleIO.WriteLine("Attacked Entity with ID " + EntityID.ToString());
if (calculateDistance(location, GetCurrentLocation()) < 5)
{ {
entitiesToAttack.Add(EntityID, location); entitiesToAttack.Add(EntityID, entity);
ConsoleIO.WriteLine("Added Entity with ID " + EntityID.ToString()+" to Attack list");
} }
entitiesToTrack.Add(EntityID, location);
//ConsoleIO.WriteLine("Added Entity with ID " + EntityID.ToString() + " to Track list");
//ConsoleIO.WriteLine(msg);
} }
} }
/// <summary>
/// Called when entities dead/despawn.
/// </summary>
/// <param name="Entities"></param>
public void OnDestroyEntities(int[] Entities) public void OnDestroyEntities(int[] Entities)
{ {
foreach(int a in Entities) if (!Settings.AutoAttackMobs) return;
foreach (int a in Entities)
{ {
if (entitiesToTrack.ContainsKey(a)) if (entitiesToTrack.ContainsKey(a))
{ {
if(entitiesToAttack.ContainsKey(a)) ConsoleIO.WriteLine("Removed Entity with ID " + a.ToString());
entitiesToAttack.Remove(a); entitiesToAttack.Remove(a);
entitiesToTrack.Remove(a); entitiesToTrack.Remove(a);
} }
@ -1095,26 +1113,33 @@ namespace MinecraftClient
ConsoleIO.WriteLine("Set Cooldown on item " + itemID + " by " + tick + " ticks"); ConsoleIO.WriteLine("Set Cooldown on item " + itemID + " by " + tick + " ticks");
} }
/// <summary>
/// Called when an entity's position changed within 8 block of its previous position.
/// </summary>
/// <param name="EntityID"></param>
/// <param name="Dx"></param>
/// <param name="Dy"></param>
/// <param name="Dz"></param>
/// <param name="onGround"></param>
public void OnEntityPosition(int EntityID, Double Dx, Double Dy, Double Dz,bool onGround) public void OnEntityPosition(int EntityID, Double Dx, Double Dy, Double Dz,bool onGround)
{ {
if (entitiesToTrack.ContainsKey(EntityID)) if (entitiesToTrack.ContainsKey(EntityID))
{ {
Entity entity = entitiesToTrack[EntityID];
Location L = entitiesToTrack[EntityID]; Location L = entity.Location;
L.X += Dx; L.X += Dx;
L.Y += Dy; L.Y += Dy;
L.Z += Dz; L.Z += Dz;
entitiesToTrack[EntityID] = L; entitiesToTrack[EntityID].Location = L;
entitiesToAttack[EntityID] = L; if(entitiesToAttack.ContainsKey(EntityID))
entitiesToAttack[EntityID].Location = L;
Double distance = calculateDistance(L, GetCurrentLocation()); Double distance = calculateDistance(L, GetCurrentLocation());
if (distance < 5) if (distance < attackRange)
{ {
ConsoleIO.WriteLine("Entity Pos changed, ID " + EntityID + ", Distance: " + distance);
if (!entitiesToAttack.ContainsKey(EntityID)) if (!entitiesToAttack.ContainsKey(EntityID))
{ {
entitiesToAttack.Add(EntityID, L); entitiesToAttack.Add(EntityID, entity);
ConsoleIO.WriteLine("Added Entity with ID " + EntityID.ToString()+" to Attack list");
} }
} }
else else
@ -1124,20 +1149,23 @@ namespace MinecraftClient
} }
} }
/// <summary>
/// Called when received entity properties from server.
/// </summary>
/// <param name="EntityID"></param>
/// <param name="prop"></param>
public void OnEntityProperties(int EntityID, Dictionary<string, Double> prop) public void OnEntityProperties(int EntityID, Dictionary<string, Double> prop)
{ {
if(EntityID == playerEntityID) if(EntityID == playerEntityID)
{ {
//ConsoleIO.WriteLine("Prop On Player reviced"); // adjust auto attack cooldown for maximum attack damage
if (prop.ContainsKey("generic.attackSpeed")) if (prop.ContainsKey("generic.attackSpeed"))
{ {
if (attackSpeed != prop["generic.attackSpeed"]) if (attackSpeed != prop["generic.attackSpeed"])
{ {
ConsoleIO.WriteLine("generic.attackSpeed: " + prop["generic.attackSpeed"].ToString());
attackSpeed = prop["generic.attackSpeed"]; attackSpeed = prop["generic.attackSpeed"];
attackCooldownSecond = 1 / attackSpeed * (serverTPS / 20.0); attackCooldownSecond = 1 / attackSpeed * (serverTPS / 20.0); // server tps will affect the cooldown
attackCooldown = Convert.ToInt16(Math.Truncate(attackCooldownSecond / 0.1) + 1); attackCooldown = Convert.ToInt16(Math.Truncate(attackCooldownSecond / 0.1) + 1);
ConsoleIO.WriteLine("attack cooldown: " + attackCooldown);
} }
} }
} }
@ -1146,17 +1174,21 @@ namespace MinecraftClient
long lastAge = 0; long lastAge = 0;
DateTime lastTime; DateTime lastTime;
Double serverTPS = 0; Double serverTPS = 0;
/// <summary>
/// Called when server sent a Time Update packet.
/// </summary>
/// <param name="WorldAge"></param>
/// <param name="TimeOfDay"></param>
public void OnTimeUpdate(long WorldAge, long TimeOfDay) public void OnTimeUpdate(long WorldAge, long TimeOfDay)
{ {
//ConsoleIO.WriteLine("Time update: World age: " + WorldAge.ToString()); if (!Settings.AutoAttackMobs) return;
//ConsoleIO.WriteLine("Time update: Time of day: " + TimeOfDay.ToString()); // calculate server tps for adjusting attack cooldown
if (lastAge != 0) if (lastAge != 0)
{ {
DateTime currentTime = DateTime.Now; DateTime currentTime = DateTime.Now;
Double tps = (WorldAge - lastAge) / (currentTime - lastTime).TotalSeconds; Double tps = (WorldAge - lastAge) / (currentTime - lastTime).TotalSeconds;
lastAge = WorldAge; lastAge = WorldAge;
lastTime = currentTime; lastTime = currentTime;
ConsoleIO.WriteLine("TPS: " + tps.ToString());
if (tps <= 20 || tps >= 0) if (tps <= 20 || tps >= 0)
{ {
serverTPS = tps; serverTPS = tps;
@ -1170,20 +1202,74 @@ namespace MinecraftClient
} }
/// <summary>
/// Called when an entity moved over 8 block.
/// </summary>
/// <param name="EntityID"></param>
/// <param name="X"></param>
/// <param name="Y"></param>
/// <param name="Z"></param>
/// <param name="onGround"></param>
public void OnEntityTeleport(int EntityID, Double X, Double Y, Double Z, bool onGround)
{
if (!Settings.AutoAttackMobs) return;
if (entitiesToTrack.ContainsKey(EntityID))
{
entitiesToTrack[EntityID].Location = new Location(X, Y, Z);
}
}
/// <summary>
/// Calculate the distance between two coordinate
/// </summary>
/// <param name="l1"></param>
/// <param name="l2"></param>
/// <returns></returns>
public double calculateDistance(Location l1,Location l2) public double calculateDistance(Location l1,Location l2)
{ {
return Math.Sqrt(Math.Pow(l2.X - l1.X, 2) + Math.Pow(l2.Y - l1.Y, 2) + Math.Pow(l2.Z - l1.Z, 2)); return Math.Sqrt(Math.Pow(l2.X - l1.X, 2) + Math.Pow(l2.Y - l1.Y, 2) + Math.Pow(l2.Z - l1.Z, 2));
} }
/// <summary>
/// Get the entity name by entity type ID.
/// </summary>
/// <param name="EntityType"></param>
/// <returns></returns>
public string getEntityName(int EntityType) public string getEntityName(int EntityType)
{ {
// only mobs in this list will be auto attacked
switch (EntityType) switch (EntityType)
{ {
case 5: return "Blaze";
case 12: return "Creeper";
case 16: return "Drowned";
case 23: return "Evoker";
case 29: return "Ghast";
case 31: return "Guardian"; case 31: return "Guardian";
case 95: return "zombie"; case 33: return "Husk";
case 41: return "Magma Cube";
case 57: return "Zombie Pigman";
case 63: return "Shulker";
case 65: return "Silverfish";
case 66: return "Skeleton";
case 68: return "Slime";
case 75: return "Stray";
case 84: return "Vex";
case 87: return "Vindicator";
case 88: return "Pillager";
case 90: return "Witch";
case 92: return "Wither Skeleton";
case 95: return "Zombie";
case 97: return "Zombie Villager";
case 98: return "Phantom";
case 99: return "Ravager";
default: return ""; default: return "";
} }
} }
public int playerEntityID; public int playerEntityID;
/// <summary>
/// Set client player's ID for later receiving player's own properties
/// </summary>
/// <param name="EntityID"></param>
public void SetPlayerEntityID(int EntityID) public void SetPlayerEntityID(int EntityID)
{ {
playerEntityID = EntityID; playerEntityID = EntityID;

View file

@ -39,6 +39,7 @@ namespace MinecraftClient.Protocol.Handlers
EntityPosition, EntityPosition,
EntityPositionAndRotation, EntityPositionAndRotation,
EntityProperties, EntityProperties,
TimeUpdate TimeUpdate,
EntityTeleport
} }
} }

View file

@ -597,7 +597,7 @@ namespace MinecraftClient.Protocol.Handlers
DeltaX = DeltaX / (128 * 32); DeltaX = DeltaX / (128 * 32);
DeltaY = DeltaY / (128 * 32); DeltaY = DeltaY / (128 * 32);
DeltaZ = DeltaZ / (128 * 32); DeltaZ = DeltaZ / (128 * 32);
handler.OnEntityPosition(EntityID, DeltaX, DeltaY, DeltaZ, OnGround); //handler.OnEntityPosition(EntityID, DeltaX, DeltaY, DeltaZ, OnGround);
break; break;
case PacketIncomingType.EntityProperties: case PacketIncomingType.EntityProperties:
EntityID = dataTypes.ReadNextVarInt(packetData); EntityID = dataTypes.ReadNextVarInt(packetData);
@ -632,6 +632,16 @@ namespace MinecraftClient.Protocol.Handlers
long TimeOfday = dataTypes.ReadNextLong(packetData); long TimeOfday = dataTypes.ReadNextLong(packetData);
handler.OnTimeUpdate(WorldAge, TimeOfday); handler.OnTimeUpdate(WorldAge, TimeOfday);
break; break;
case PacketIncomingType.EntityTeleport:
EntityID = dataTypes.ReadNextVarInt(packetData);
X = dataTypes.ReadNextDouble(packetData);
Y = dataTypes.ReadNextDouble(packetData);
Z = dataTypes.ReadNextDouble(packetData);
EntityYaw = dataTypes.ReadNextByte(packetData);
EntityPitch = dataTypes.ReadNextByte(packetData);
OnGround = dataTypes.ReadNextBool(packetData);
handler.OnEntityTeleport(EntityID, X, Y, Z, OnGround);
break;
default: default:
return false; //Ignored packet return false; //Ignored packet
} }
@ -1122,6 +1132,12 @@ namespace MinecraftClient.Protocol.Handlers
} }
// reinforce // reinforce
/// <summary>
/// Send an Interact Entity Packet to server
/// </summary>
/// <param name="EntityID"></param>
/// <param name="type"></param>
/// <returns></returns>
public bool SendInteractEntityPacket(int EntityID, int type) public bool SendInteractEntityPacket(int EntityID, int type)
{ {
try try
@ -1136,6 +1152,7 @@ namespace MinecraftClient.Protocol.Handlers
catch (System.IO.IOException) { return false; } catch (System.IO.IOException) { return false; }
catch (ObjectDisposedException) { return false; } catch (ObjectDisposedException) { return false; }
} }
// TODO: Interact at block location
public bool SendInteractEntityPacket(int EntityID, int type, float X, float Y, float Z, int hand) public bool SendInteractEntityPacket(int EntityID, int type, float X, float Y, float Z, int hand)
{ {
return true; return true;

View file

@ -203,6 +203,7 @@ namespace MinecraftClient.Protocol.Handlers
case 0x2A: return PacketIncomingType.EntityPositionAndRotation; case 0x2A: return PacketIncomingType.EntityPositionAndRotation;
case 0x59: return PacketIncomingType.EntityProperties; case 0x59: return PacketIncomingType.EntityProperties;
case 0x4F: return PacketIncomingType.TimeUpdate; case 0x4F: return PacketIncomingType.TimeUpdate;
case 0x57: return PacketIncomingType.EntityTeleport;
default: return PacketIncomingType.UnknownPacket; default: return PacketIncomingType.UnknownPacket;
} }
} }

View file

@ -138,6 +138,8 @@ namespace MinecraftClient.Protocol
void OnTimeUpdate(long WorldAge, long TimeOfDay); void OnTimeUpdate(long WorldAge, long TimeOfDay);
void OnEntityTeleport(int EntityID, Double X, Double Y, Double Z, bool onGround);
void SetPlayerEntityID(int EntityID); void SetPlayerEntityID(int EntityID);
} }
} }

View file

@ -95,6 +95,7 @@ namespace MinecraftClient
public static bool DebugMessages = false; public static bool DebugMessages = false;
public static bool ResolveSrvRecords = true; public static bool ResolveSrvRecords = true;
public static bool ResolveSrvRecordsShortTimeout = true; public static bool ResolveSrvRecordsShortTimeout = true;
public static bool AutoAttackMobs = false;
//AntiAFK Settings //AntiAFK Settings
public static bool AntiAFK_Enabled = false; public static bool AntiAFK_Enabled = false;
@ -230,6 +231,7 @@ namespace MinecraftClient
case "privatemsgscmdname": PrivateMsgsCmdName = argValue.ToLower().Trim(); break; case "privatemsgscmdname": PrivateMsgsCmdName = argValue.ToLower().Trim(); break;
case "botmessagedelay": botMessageDelay = TimeSpan.FromSeconds(str2int(argValue)); break; case "botmessagedelay": botMessageDelay = TimeSpan.FromSeconds(str2int(argValue)); break;
case "debugmessages": DebugMessages = str2bool(argValue); break; case "debugmessages": DebugMessages = str2bool(argValue); break;
case "autoattackmobs": AutoAttackMobs = str2bool(argValue); break;
case "botowners": case "botowners":
Bots_Owners.Clear(); Bots_Owners.Clear();
@ -558,6 +560,7 @@ namespace MinecraftClient
+ "debugmessages=false # Please enable this before submitting bug reports. Thanks!\r\n" + "debugmessages=false # Please enable this before submitting bug reports. Thanks!\r\n"
+ "scriptcache=true # Cache compiled scripts for faster load on low-end devices\r\n" + "scriptcache=true # Cache compiled scripts for faster load on low-end devices\r\n"
+ "timestamps=false # Prepend timestamps to chat messages\r\n" + "timestamps=false # Prepend timestamps to chat messages\r\n"
+ "autoattackmobs=false # Auto attack mobs around client player\r\n"
+ "\r\n" + "\r\n"
+ "[AppVars]\r\n" + "[AppVars]\r\n"
+ "# yourvar=yourvalue\r\n" + "# yourvar=yourvalue\r\n"