Created
December 28, 2022 20:46
-
-
Save drunderscore/e1fd33a22abdde9d7513c5b62b220458 to your computer and use it in GitHub Desktop.
TShock scheduler, made with Bukkit's scheduler in mind
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using TerrariaApi.Server; | |
public class Scheduler : IDisposable | |
{ | |
public const long TicksPerSecond = 60; | |
public long TotalTicks { get; private set; } | |
private readonly TerrariaPlugin _plugin; | |
private readonly List<Task> _tasks = new(); | |
public Scheduler(TerrariaPlugin plugin) | |
{ | |
_plugin = plugin; | |
ServerApi.Hooks.GameUpdate.Register(plugin, OnGameUpdate); | |
} | |
private void OnGameUpdate(EventArgs args) | |
{ | |
for (var i = 0; i < _tasks.Count; i++) | |
{ | |
var t = _tasks[i]; | |
if (t.Expired) | |
{ | |
t.Action(); | |
_tasks.RemoveAt(i); | |
i--; | |
} | |
} | |
TotalTicks++; | |
} | |
public void Dispose() | |
{ | |
ServerApi.Hooks.GameUpdate.Deregister(_plugin, OnGameUpdate); | |
} | |
public Task ScheduleTaskLater(Action what, long howManyTicksLater) | |
{ | |
var task = new Task(what, TotalTicks + howManyTicksLater, this); | |
_tasks.Add(task); | |
return task; | |
} | |
public class Task | |
{ | |
public Action Action { get; } | |
public long When { get; } | |
public bool Expired => _owner.TotalTicks >= When; | |
private readonly Scheduler _owner; | |
public Task(Action action, long when, Scheduler owner) | |
{ | |
When = when; | |
Action = action; | |
_owner = owner; | |
} | |
// FIXME: If a consumer cancels inside their own callback, nothing good can come out of it when iterating tasks! | |
// It is their fault, but we should throw an exception before it ruins everyone else's day. | |
public void Cancel() | |
{ | |
_owner._tasks.Remove(this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment