2021-05-15 17:36:16 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Holds a task with delay
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
class TaskWithDelay
|
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
private readonly Action _task;
|
2021-05-15 17:36:16 +02:00
|
|
|
|
private int tickCounter;
|
2022-10-02 18:31:08 +08:00
|
|
|
|
private readonly DateTime dateToLaunch;
|
2021-05-15 17:36:16 +02:00
|
|
|
|
|
|
|
|
|
|
public Action Task { get { return _task; } }
|
|
|
|
|
|
|
|
|
|
|
|
public TaskWithDelay(Action task, int delayTicks)
|
|
|
|
|
|
{
|
|
|
|
|
|
_task = task;
|
|
|
|
|
|
tickCounter = delayTicks;
|
|
|
|
|
|
dateToLaunch = DateTime.MaxValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public TaskWithDelay(Action task, TimeSpan delay)
|
|
|
|
|
|
{
|
|
|
|
|
|
_task = task;
|
|
|
|
|
|
tickCounter = int.MaxValue;
|
|
|
|
|
|
dateToLaunch = DateTime.Now + delay;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Tick the counter
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>Return true if the task should run now</returns>
|
|
|
|
|
|
public bool Tick()
|
|
|
|
|
|
{
|
|
|
|
|
|
tickCounter--;
|
|
|
|
|
|
if (tickCounter <= 0 || dateToLaunch < DateTime.Now)
|
|
|
|
|
|
return true;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|