2014-05-31 01:59:03 +02:00
|
|
|
|
using System;
|
2022-12-06 15:50:17 +08:00
|
|
|
|
using MinecraftClient.Scripting;
|
2022-10-05 15:02:30 +08:00
|
|
|
|
using Tomlet.Attributes;
|
2014-05-31 01:59:03 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.ChatBots
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// This bot automatically re-join the server if kick message contains predefined string (Server is restarting ...)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class AutoRelog : ChatBot
|
|
|
|
|
|
{
|
2022-10-05 15:02:30 +08:00
|
|
|
|
public static Configs Config = new();
|
|
|
|
|
|
|
|
|
|
|
|
[TomlDoNotInlineObject]
|
|
|
|
|
|
public class Configs
|
|
|
|
|
|
{
|
|
|
|
|
|
[NonSerialized]
|
|
|
|
|
|
private const string BotName = "AutoRelog";
|
|
|
|
|
|
|
|
|
|
|
|
public bool Enabled = false;
|
|
|
|
|
|
|
2022-11-30 16:22:48 +08:00
|
|
|
|
[TomlInlineComment("$ChatBot.AutoRelog.Delay$")]
|
2022-10-06 18:12:32 +08:00
|
|
|
|
public Range Delay = new(3);
|
2022-10-05 15:02:30 +08:00
|
|
|
|
|
2022-11-30 16:22:48 +08:00
|
|
|
|
[TomlInlineComment("$ChatBot.AutoRelog.Retries$")]
|
2022-10-05 15:02:30 +08:00
|
|
|
|
public int Retries = 3;
|
|
|
|
|
|
|
2022-11-30 16:22:48 +08:00
|
|
|
|
[TomlInlineComment("$ChatBot.AutoRelog.Ignore_Kick_Message$")]
|
2022-10-05 15:02:30 +08:00
|
|
|
|
public bool Ignore_Kick_Message = false;
|
|
|
|
|
|
|
2022-11-30 16:22:48 +08:00
|
|
|
|
[TomlPrecedingComment("$ChatBot.AutoRelog.Kick_Messages$")]
|
2022-10-05 19:39:21 +08:00
|
|
|
|
public string[] Kick_Messages = new string[] { "Connection has been lost", "Server is restarting", "Server is full", "Too Many people" };
|
2022-10-05 15:02:30 +08:00
|
|
|
|
|
2022-10-06 18:12:32 +08:00
|
|
|
|
[NonSerialized]
|
2022-10-07 21:49:05 +08:00
|
|
|
|
public static int _BotRecoAttempts = 0;
|
2022-10-06 18:12:32 +08:00
|
|
|
|
|
2022-10-05 15:02:30 +08:00
|
|
|
|
public void OnSettingUpdate()
|
|
|
|
|
|
{
|
2022-10-06 18:12:32 +08:00
|
|
|
|
Delay.min = Math.Max(0.1, Delay.min);
|
|
|
|
|
|
Delay.max = Math.Max(0.1, Delay.max);
|
|
|
|
|
|
|
|
|
|
|
|
Delay.min = Math.Min(int.MaxValue / 10, Delay.min);
|
|
|
|
|
|
Delay.max = Math.Min(int.MaxValue / 10, Delay.max);
|
|
|
|
|
|
|
2022-10-05 15:02:30 +08:00
|
|
|
|
if (Delay.min > Delay.max)
|
|
|
|
|
|
(Delay.min, Delay.max) = (Delay.max, Delay.min);
|
2022-12-06 15:50:17 +08:00
|
|
|
|
|
2022-10-05 15:02:30 +08:00
|
|
|
|
if (Retries == -1)
|
|
|
|
|
|
Retries = int.MaxValue;
|
2022-10-05 17:33:21 +08:00
|
|
|
|
|
|
|
|
|
|
if (Enabled)
|
|
|
|
|
|
for (int i = 0; i < Kick_Messages.Length; i++)
|
|
|
|
|
|
Kick_Messages[i] = Kick_Messages[i].ToLower();
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Range(int min, int max)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.min = min;
|
|
|
|
|
|
this.max = max;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-02 18:31:08 +08:00
|
|
|
|
private static readonly Random random = new();
|
2014-05-31 01:59:03 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// This bot automatically re-join the server if kick message contains predefined string
|
|
|
|
|
|
/// </summary>
|
2020-11-04 19:23:46 +01:00
|
|
|
|
/// <param name="DelayBeforeRelogMin">Minimum delay before re-joining the server (in seconds)</param>
|
|
|
|
|
|
/// <param name="DelayBeforeRelogMax">Maximum delay before re-joining the server (in seconds)</param>
|
2014-05-31 01:59:03 +02:00
|
|
|
|
/// <param name="retries">Number of retries if connection fails (-1 = infinite)</param>
|
2022-10-05 15:02:30 +08:00
|
|
|
|
public AutoRelog()
|
2014-05-31 01:59:03 +02:00
|
|
|
|
{
|
2022-10-28 11:13:20 +08:00
|
|
|
|
LogDebugToConsole(string.Format(Translations.bot_autoRelog_launch, Config.Retries));
|
2014-05-31 01:59:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Initialize()
|
2022-12-06 15:50:17 +08:00
|
|
|
|
{
|
2022-12-06 20:32:46 +08:00
|
|
|
|
_Initialize();
|
2022-12-06 15:50:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-06 20:32:46 +08:00
|
|
|
|
private void _Initialize()
|
2014-05-31 01:59:03 +02:00
|
|
|
|
{
|
2022-10-05 15:02:30 +08:00
|
|
|
|
McClient.ReconnectionAttemptsLeft = Config.Retries;
|
|
|
|
|
|
if (Config.Ignore_Kick_Message)
|
2014-05-31 01:59:03 +02:00
|
|
|
|
{
|
2022-10-28 11:13:20 +08:00
|
|
|
|
LogDebugToConsole(Translations.bot_autoRelog_no_kick_msg);
|
2020-04-02 18:19:37 +02:00
|
|
|
|
}
|
2014-05-31 01:59:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool OnDisconnect(DisconnectReason reason, string message)
|
|
|
|
|
|
{
|
2019-12-08 22:24:20 +01:00
|
|
|
|
if (reason == DisconnectReason.UserLogout)
|
|
|
|
|
|
{
|
2022-10-28 11:13:20 +08:00
|
|
|
|
LogDebugToConsole(Translations.bot_autoRelog_ignore_user_logout);
|
2019-12-08 22:24:20 +01:00
|
|
|
|
}
|
2022-10-07 21:49:05 +08:00
|
|
|
|
else if (Config.Retries < 0 || Configs._BotRecoAttempts < Config.Retries)
|
2019-12-08 22:24:20 +01:00
|
|
|
|
{
|
|
|
|
|
|
message = GetVerbatim(message);
|
|
|
|
|
|
string comp = message.ToLower();
|
2019-05-30 11:45:43 +02:00
|
|
|
|
|
2022-10-28 11:13:20 +08:00
|
|
|
|
LogDebugToConsole(string.Format(Translations.bot_autoRelog_disconnect_msg, message));
|
2019-05-30 11:45:43 +02:00
|
|
|
|
|
2022-10-05 15:02:30 +08:00
|
|
|
|
if (Config.Ignore_Kick_Message)
|
2020-04-02 18:19:37 +02:00
|
|
|
|
{
|
2022-10-07 21:49:05 +08:00
|
|
|
|
Configs._BotRecoAttempts++;
|
2020-11-04 19:23:46 +01:00
|
|
|
|
LaunchDelayedReconnection(null);
|
2020-04-02 18:19:37 +02:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-05 17:33:21 +08:00
|
|
|
|
foreach (string msg in Config.Kick_Messages)
|
2014-05-31 01:59:03 +02:00
|
|
|
|
{
|
2019-12-08 22:24:20 +01:00
|
|
|
|
if (comp.Contains(msg))
|
|
|
|
|
|
{
|
2022-10-07 21:49:05 +08:00
|
|
|
|
Configs._BotRecoAttempts++;
|
2020-11-04 19:23:46 +01:00
|
|
|
|
LaunchDelayedReconnection(msg);
|
2019-12-08 22:24:20 +01:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2014-05-31 01:59:03 +02:00
|
|
|
|
}
|
2019-05-30 11:45:43 +02:00
|
|
|
|
|
2022-10-28 11:13:20 +08:00
|
|
|
|
LogDebugToConsole(Translations.bot_autoRelog_reconnect_ignore);
|
2019-12-08 22:24:20 +01:00
|
|
|
|
}
|
2019-05-30 11:45:43 +02:00
|
|
|
|
|
2014-05-31 01:59:03 +02:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2015-04-20 17:26:16 +02:00
|
|
|
|
|
2022-10-02 18:31:08 +08:00
|
|
|
|
private void LaunchDelayedReconnection(string? msg)
|
2020-11-04 19:23:46 +01:00
|
|
|
|
{
|
2022-10-12 13:03:57 +08:00
|
|
|
|
double delay = random.NextDouble() * (Config.Delay.max - Config.Delay.min) + Config.Delay.min;
|
2022-10-28 11:13:20 +08:00
|
|
|
|
LogDebugToConsole(string.Format(string.IsNullOrEmpty(msg) ? Translations.bot_autoRelog_reconnect_always : Translations.bot_autoRelog_reconnect, msg));
|
|
|
|
|
|
LogToConsole(string.Format(Translations.bot_autoRelog_wait, delay));
|
2022-10-12 13:03:57 +08:00
|
|
|
|
System.Threading.Thread.Sleep((int)Math.Floor(delay * 1000));
|
2020-11-04 19:23:46 +01:00
|
|
|
|
ReconnectToTheServer();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-04-20 17:26:16 +02:00
|
|
|
|
public static bool OnDisconnectStatic(DisconnectReason reason, string message)
|
|
|
|
|
|
{
|
2022-10-05 15:02:30 +08:00
|
|
|
|
if (Config.Enabled)
|
2015-04-20 17:26:16 +02:00
|
|
|
|
{
|
2022-10-05 15:02:30 +08:00
|
|
|
|
AutoRelog bot = new();
|
2016-03-21 13:26:45 +01:00
|
|
|
|
bot.Initialize();
|
2015-04-20 17:26:16 +02:00
|
|
|
|
return bot.OnDisconnect(reason, message);
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2014-05-31 01:59:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|