Minecraft-Console-Client/MinecraftClient/ChatBots/AntiAFK.cs

166 lines
5.1 KiB
C#
Raw Normal View History

using System;
using MinecraftClient.Mapping;
2022-10-05 15:02:30 +08:00
using Tomlet.Attributes;
namespace MinecraftClient.ChatBots
{
/// <summary>
/// This bot sends a command every 60 seconds in order to stay non-afk.
/// </summary>
public class AntiAFK : ChatBot
{
2022-10-05 15:02:30 +08:00
public static Configs Config = new();
2022-10-05 15:02:30 +08:00
[TomlDoNotInlineObject]
public class Configs
{
2022-10-05 15:02:30 +08:00
[NonSerialized]
private const string BotName = "AntiAFK";
2022-10-05 15:02:30 +08:00
public bool Enabled = false;
[TomlInlineComment("$config.ChatBot.AntiAfk.Delay$")]
public Range Delay = new(600);
[TomlInlineComment("$config.ChatBot.AntiAfk.Command$")]
public string Command = "/ping";
[TomlInlineComment("$config.ChatBot.AntiAfk.Use_Terrain_Handling$")]
public bool Use_Terrain_Handling = false;
[TomlInlineComment("$config.ChatBot.AntiAfk.Walk_Range$")]
public int Walk_Range = 5;
[TomlInlineComment("$config.ChatBot.AntiAfk.Walk_Retries$")]
public int Walk_Retries = 20;
public void OnSettingUpdate()
{
2022-10-05 15:02:30 +08:00
if (Walk_Range <= 0)
{
2022-10-05 15:02:30 +08:00
Walk_Range = 5;
LogToConsole(BotName, Translations.TryGet("bot.antiafk.invalid_walk_range"));
}
2022-10-05 15:02:30 +08:00
if (Delay.min > Delay.max)
2022-09-28 18:46:17 +02:00
{
2022-10-05 15:02:30 +08:00
(Delay.min, Delay.max) = (Delay.max, Delay.min);
LogToConsole(BotName, Translations.TryGet("bot.antiafk.swapping"));
2022-09-28 18:46:17 +02:00
}
}
2022-10-05 15:02:30 +08:00
public struct Range
{
2022-10-05 15:02:30 +08:00
public int min, max;
2022-10-05 15:02:30 +08:00
public Range(int value)
{
value = Math.Max(value, 10);
min = max = value;
}
2022-10-05 15:02:30 +08:00
public Range(int min, int max)
{
2022-10-05 15:02:30 +08:00
min = Math.Max(min, 10);
max = Math.Max(max, 10);
this.min = min;
this.max = max;
}
}
2022-10-05 15:02:30 +08:00
}
private int count;
private bool previousSneakState = false;
private readonly Random random = new();
2022-10-05 15:02:30 +08:00
/// <summary>
/// This bot sends a /ping command every X seconds in order to stay non-afk.
/// </summary>
public AntiAFK()
{
count = 0;
}
public override void Initialize()
{
if (Config.Use_Terrain_Handling)
{
2022-10-05 15:02:30 +08:00
if (!GetTerrainEnabled())
{
LogToConsole(Translations.TryGet("bot.antiafk.not_using_terrain_handling"));
}
}
}
public override void Update()
{
count++;
2022-10-05 15:02:30 +08:00
if (count == random.Next(Config.Delay.min, Config.Delay.max))
{
DoAntiAfkStuff();
count = 0;
}
}
private void DoAntiAfkStuff()
{
2022-10-05 15:02:30 +08:00
if (Config.Use_Terrain_Handling && GetTerrainEnabled())
{
Location currentLocation = GetCurrentLocation();
Location goal;
bool moved = false;
bool useAlternativeMethod = false;
int triesCounter = 0;
while (!moved)
{
2022-10-05 15:02:30 +08:00
if (triesCounter++ >= Config.Walk_Retries)
{
useAlternativeMethod = true;
break;
}
2022-10-05 15:02:30 +08:00
goal = GetRandomLocationWithinRangeXZ(currentLocation, Config.Walk_Range);
// Prevent getting the same location
while ((currentLocation.X == goal.X) && (currentLocation.Y == goal.Y) && (currentLocation.Z == goal.Z))
{
LogToConsole("Same location!, generating new one");
2022-10-05 15:02:30 +08:00
goal = GetRandomLocationWithinRangeXZ(currentLocation, Config.Walk_Range);
}
if (!Movement.CheckChunkLoading(GetWorld(), currentLocation, goal))
{
useAlternativeMethod = true;
break;
}
else moved = MoveToLocation(goal, allowUnsafe: false, allowDirectTeleport: false);
}
if (!useAlternativeMethod)
{
// Solve the case when the bot was closed in 1x2, was sneaking, but then he was freed, this will make him not sneak anymore
previousSneakState = false;
Sneak(false);
return;
}
}
2022-10-05 15:02:30 +08:00
SendText(Config.Command);
Sneak(previousSneakState);
previousSneakState = !previousSneakState;
count = 0;
}
private Location GetRandomLocationWithinRangeXZ(Location currentLocation, int range)
{
return new Location(currentLocation.X + random.Next(range * -1, range), currentLocation.Y, currentLocation.Z + random.Next(range * -1, range));
}
}
}