2022-09-09 15:39:41 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using MinecraftClient.Mapping;
|
2022-10-05 15:02:30 +08:00
|
|
|
|
using Tomlet.Attributes;
|
2022-09-09 15:39:41 +02:00
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.ChatBots
|
|
|
|
|
|
{
|
|
|
|
|
|
public class FollowPlayer : ChatBot
|
|
|
|
|
|
{
|
2022-10-05 15:02:30 +08:00
|
|
|
|
public static Configs Config = new();
|
2022-09-09 15:39:41 +02:00
|
|
|
|
|
2022-10-05 15:02:30 +08:00
|
|
|
|
[TomlDoNotInlineObject]
|
|
|
|
|
|
public class Configs
|
2022-09-09 15:39:41 +02:00
|
|
|
|
{
|
2022-10-05 15:02:30 +08:00
|
|
|
|
[NonSerialized]
|
|
|
|
|
|
private const string BotName = "FollowPlayer";
|
|
|
|
|
|
|
|
|
|
|
|
public bool Enabled = false;
|
|
|
|
|
|
|
|
|
|
|
|
[TomlInlineComment("$config.ChatBot.FollowPlayer.Update_Limit$")]
|
2022-10-12 13:03:57 +08:00
|
|
|
|
public double Update_Limit = 1.5;
|
2022-10-05 15:02:30 +08:00
|
|
|
|
|
|
|
|
|
|
[TomlInlineComment("$config.ChatBot.FollowPlayer.Stop_At_Distance$")]
|
|
|
|
|
|
public double Stop_At_Distance = 3.0;
|
|
|
|
|
|
|
|
|
|
|
|
public void OnSettingUpdate()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Update_Limit < 0)
|
|
|
|
|
|
Update_Limit = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (Stop_At_Distance < 0)
|
|
|
|
|
|
Stop_At_Distance = 0;
|
|
|
|
|
|
}
|
2022-09-09 15:39:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-05 15:02:30 +08:00
|
|
|
|
private string? _playerToFollow = null;
|
|
|
|
|
|
private int _updateCounter = 0;
|
|
|
|
|
|
private bool _unsafeEnabled = false;
|
|
|
|
|
|
|
2022-09-09 15:39:41 +02:00
|
|
|
|
public override void Initialize()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!GetEntityHandlingEnabled())
|
|
|
|
|
|
{
|
2022-10-28 11:13:20 +08:00
|
|
|
|
LogToConsole(Translations.extra_entity_required);
|
|
|
|
|
|
LogToConsole(Translations.general_bot_unload);
|
2022-09-09 15:39:41 +02:00
|
|
|
|
UnloadBot();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!GetTerrainEnabled())
|
|
|
|
|
|
{
|
2022-10-28 11:13:20 +08:00
|
|
|
|
LogToConsole(Translations.extra_terrainandmovement_required);
|
|
|
|
|
|
LogToConsole(Translations.general_bot_unload);
|
2022-09-09 15:39:41 +02:00
|
|
|
|
UnloadBot();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RegisterChatBotCommand("follow", "cmd.follow.desc", "follow <player name|stop>", OnFollowCommand);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private string OnFollowCommand(string cmd, string[] args)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (args.Length > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (args[0].Equals("stop", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_playerToFollow == null)
|
2022-10-28 11:13:20 +08:00
|
|
|
|
return Translations.cmd_follow_already_stopped;
|
2022-09-09 15:39:41 +02:00
|
|
|
|
|
|
|
|
|
|
_playerToFollow = null;
|
2022-10-28 11:13:20 +08:00
|
|
|
|
return Translations.cmd_follow_stopping;
|
2022-09-09 15:39:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!IsValidName(args[0]))
|
2022-10-28 11:13:20 +08:00
|
|
|
|
return Translations.cmd_follow_invalid_name;
|
2022-09-09 15:39:41 +02:00
|
|
|
|
|
|
|
|
|
|
Entity? player = GetEntities().Values.ToList().Find(entity =>
|
|
|
|
|
|
entity.Type == EntityType.Player && !string.IsNullOrEmpty(entity.Name) && entity.Name.Equals(args[0], StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
|
|
if (player == null)
|
2022-10-28 11:13:20 +08:00
|
|
|
|
return Translations.cmd_follow_invalid_player;
|
2022-09-09 15:39:41 +02:00
|
|
|
|
|
|
|
|
|
|
if (!CanMoveThere(player.Location))
|
2022-10-28 11:13:20 +08:00
|
|
|
|
return Translations.cmd_follow_cant_reach_player;
|
2022-09-09 15:39:41 +02:00
|
|
|
|
|
|
|
|
|
|
if (_playerToFollow != null && _playerToFollow.Equals(args[0], StringComparison.OrdinalIgnoreCase))
|
2022-10-28 11:13:20 +08:00
|
|
|
|
return string.Format(Translations.cmd_follow_already_following, _playerToFollow);
|
2022-09-09 15:39:41 +02:00
|
|
|
|
|
2022-10-28 11:13:20 +08:00
|
|
|
|
string result;
|
|
|
|
|
|
if (_playerToFollow != null)
|
|
|
|
|
|
result = string.Format(Translations.cmd_follow_switched, player.Name!);
|
|
|
|
|
|
else
|
|
|
|
|
|
result = string.Format(Translations.cmd_follow_started, player.Name!);
|
2022-09-09 15:39:41 +02:00
|
|
|
|
_playerToFollow = args[0].Trim().ToLower();
|
|
|
|
|
|
|
2022-10-28 11:13:20 +08:00
|
|
|
|
LogToConsole(Translations.cmd_follow_note);
|
2022-09-09 15:39:41 +02:00
|
|
|
|
|
|
|
|
|
|
if (args.Length == 2 && args[1].Equals("-f", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
{
|
|
|
|
|
|
_unsafeEnabled = true;
|
2022-10-28 11:13:20 +08:00
|
|
|
|
LogToConsole(Translations.cmd_follow_unsafe_enabled);
|
2022-09-09 15:39:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-10-28 11:13:20 +08:00
|
|
|
|
return Translations.cmd_follow_desc + ": " + Translations.cmd_follow_usage;
|
2022-09-09 15:39:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Update()
|
|
|
|
|
|
{
|
|
|
|
|
|
_updateCounter++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void OnEntityMove(Entity entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
2022-10-08 17:56:32 +08:00
|
|
|
|
if (_updateCounter < Settings.DoubleToTick(Config.Update_Limit))
|
2022-09-09 15:39:41 +02:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
_updateCounter = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (entity.Type != EntityType.Player)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if (_playerToFollow == null || string.IsNullOrEmpty(entity.Name))
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if (_playerToFollow != entity.Name.ToLower())
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if (!CanMoveThere(entity.Location))
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// Stop at specified distance from plater (prevents pushing player around)
|
2022-10-02 18:31:08 +08:00
|
|
|
|
double distance = entity.Location.Distance(GetCurrentLocation());
|
2022-09-09 15:39:41 +02:00
|
|
|
|
|
2022-10-05 15:02:30 +08:00
|
|
|
|
if (distance < Config.Stop_At_Distance)
|
2022-09-09 15:39:41 +02:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
MoveToLocation(entity.Location, _unsafeEnabled);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void OnEntitySpawn(Entity entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (entity.Type != EntityType.Player)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if (_playerToFollow != null && !string.IsNullOrEmpty(entity.Name) && _playerToFollow.Equals(entity.Name, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
{
|
2022-10-28 11:13:20 +08:00
|
|
|
|
LogToConsole(string.Format(Translations.cmd_follow_player_came_to_the_range, _playerToFollow));
|
|
|
|
|
|
LogToConsole(Translations.cmd_follow_resuming);
|
2022-09-09 15:39:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void OnEntityDespawn(Entity entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (entity.Type != EntityType.Player)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if (_playerToFollow != null && !string.IsNullOrEmpty(entity.Name) && _playerToFollow.Equals(entity.Name, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
{
|
2022-10-28 11:13:20 +08:00
|
|
|
|
LogToConsole(string.Format(Translations.cmd_follow_player_left_the_range, _playerToFollow));
|
|
|
|
|
|
LogToConsole(Translations.cmd_follow_pausing);
|
2022-09-09 15:39:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void OnPlayerLeave(Guid uuid, string? name)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_playerToFollow != null && !string.IsNullOrEmpty(name) && _playerToFollow.Equals(name, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
{
|
2022-10-28 11:13:20 +08:00
|
|
|
|
LogToConsole(string.Format(Translations.cmd_follow_player_left, _playerToFollow));
|
|
|
|
|
|
LogToConsole(Translations.cmd_follow_stopping);
|
2022-09-09 15:39:41 +02:00
|
|
|
|
_playerToFollow = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private bool CanMoveThere(Location location)
|
|
|
|
|
|
{
|
|
|
|
|
|
ChunkColumn? chunkColumn = GetWorld().GetChunkColumn(location);
|
|
|
|
|
|
|
|
|
|
|
|
if (chunkColumn == null || chunkColumn.FullyLoaded == false)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|