using ImGuiNET;
using ILoveWomen.Common;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ILoveWomen.Modules
{
public class AutoClicker
{
private bool _enabled = false;
private float _cps = 10.0f;
private bool _randomization = false;
private CancellationTokenSource? _clickerTokenSource;
private Task? _clickerTask;
public bool IsEnabled => _enabled;
public void Render()
{
bool prevEnabled = _enabled;
ImGui.Checkbox("Enable Autoclicker", ref _enabled);
if (prevEnabled != _enabled)
{
if (_enabled)
StartClicker();
else
StopClicker();
}
ImGui.Spacing();
ImGui.Text("Clicks Per Second:");
ImGui.SliderFloat("##CPS", ref _cps, 1.0f, 20.0f, "%.1f");
ImGui.Spacing();
ImGui.Checkbox("Randomize Timing", ref _randomization);
if (_randomization)
{
ImGui.SameLine();
ImGui.TextDisabled("(?)");
if (ImGui.IsItemHovered())
{
ImGui.BeginTooltip();
ImGui.TextUnformatted("Adds slight randomness to click timing
to appear more natural");
ImGui.EndTooltip();
}
}
}
private void StartClicker()
{
_clickerTokenSource = new CancellationTokenSource();
_clickerTask = Task.Run(() => ClickerLoop(_clickerTokenSource.Token),
_clickerTokenSource.Token);
}
private void StopClicker()
{
if (_clickerTokenSource != null)
{
_clickerTokenSource.Cancel();
_clickerTokenSource = null;
}
_clickerTask = null;
}
private async Task ClickerLoop(CancellationToken token)
{
Random random = new Random();
while (!token.IsCancellationRequested)
{
if (_enabled && Utils.IsMinecraftFocused() &&
Utils.IsKeyPressed(Utils.VK_LBUTTON))
{
int baseDelay = (int)(1000 / _cps);
int delay = baseDelay;
if (_randomization)
{
int variation = (int)(baseDelay * 0.2);
delay = random.Next(baseDelay - variation, baseDelay +
variation);
}
Utils.mouse_event(Utils.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Utils.mouse_event(Utils.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
await Task.Delay(delay, token);
}
else
{
await Task.Delay(10, token);
}
}
}
public void Dispose()
{
StopClicker();
}
}
}