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

183 lines
5.7 KiB
C#
Raw Normal View History

using System;
using Brigadier.NET;
using MinecraftClient.CommandHandler;
using MinecraftClient.Mapping;
using MinecraftClient.Scripting;
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$")]
2022-10-06 18:12:32 +08:00
public Range Delay = new(60);
2022-10-05 15:02:30 +08:00
[TomlInlineComment("$config.ChatBot.AntiAfk.Command$")]
public string Command = "/ping";
2022-10-07 16:13:27 +08:00
[TomlInlineComment("$config.ChatBot.AntiAfk.Use_Sneak$")]
public bool Use_Sneak = false;
2022-10-05 15:02:30 +08:00
[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.bot_antiafk_invalid_walk_range);
}
2022-10-05 15:02:30 +08:00
2022-10-06 18:12:32 +08:00
Delay.min = Math.Max(1.0, Delay.min);
Delay.max = Math.Max(1.0, Delay.max);
Delay.min = Math.Min(int.MaxValue / 10, Delay.min);
Delay.max = Math.Min(int.MaxValue / 10, Delay.max);
2022-10-05 17:33:21 +08:00
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.bot_antiafk_swapping);
2022-09-28 18:46:17 +02:00
}
2022-10-05 15:39:42 +08:00
Command ??= string.Empty;
}
2022-10-05 15:02:30 +08:00
public struct Range
{
2022-10-06 18:12:32 +08:00
public double min, max;
2022-10-05 15:02:30 +08:00
public Range(int value)
{
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
this.min = min;
this.max = max;
}
}
2022-10-05 15:02:30 +08:00
}
2022-10-07 16:13:27 +08:00
private int count, nextrun = 50;
2022-10-05 15:02:30 +08:00
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(CommandDispatcher<CmdResult> dispatcher)
2022-10-05 15:02:30 +08:00
{
if (Config.Use_Terrain_Handling)
{
2022-10-05 15:02:30 +08:00
if (!GetTerrainEnabled())
{
LogToConsole(Translations.bot_antiafk_not_using_terrain_handling);
2022-10-05 15:02:30 +08:00
}
}
}
public override void Update()
{
count++;
2022-10-07 16:13:27 +08:00
if (count >= nextrun)
{
DoAntiAfkStuff();
count = 0;
2022-10-08 17:56:32 +08:00
nextrun = random.Next(Settings.DoubleToTick(Config.Delay.min), Settings.DoubleToTick(Config.Delay.max));
}
}
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;
}
2022-10-07 16:13:27 +08:00
else
{
moved = MoveToLocation(goal, allowUnsafe: false, allowDirectTeleport: false);
}
}
2022-10-07 16:13:27 +08:00
if (!useAlternativeMethod && Config.Use_Sneak)
{
// 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);
2022-10-07 16:13:27 +08:00
if (Config.Use_Sneak)
{
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));
}
}
}