Also set pitch when teleporting (Thanks goodlyay), add ability to cancel tasks.

This commit is contained in:
UnknownShadow200 2016-09-05 11:07:40 +10:00
parent 60e3a71611
commit aae0f6ca37
2 changed files with 30 additions and 22 deletions

View File

@ -59,7 +59,7 @@ namespace MCGalaxy.Commands {
ushort[] pos = bot != null ? bot.pos : target.pos; ushort[] pos = bot != null ? bot.pos : target.pos;
byte[] rot = bot != null ? bot.rot : target.rot; byte[] rot = bot != null ? bot.rot : target.rot;
p.BlockUntilLoad(10); //Wait for player to spawn in new map p.BlockUntilLoad(10); //Wait for player to spawn in new map
p.SendOwnHeadPos(pos[0], pos[1], pos[2], rot[0], 0); p.SendOwnHeadPos(pos[0], pos[1], pos[2], rot[0], rot[1]);
} }
static bool CheckPlayer(Player p, Player target) { static bool CheckPlayer(Player p, Player target) {

View File

@ -35,25 +35,35 @@ namespace MCGalaxy {
} }
/// <summary> Queues an action that is asynchronously executed one time, as soon as possible. </summary> /// <summary> Queues an action that is asynchronously executed one time, as soon as possible. </summary>
public void QueueOnce(Action callback) { public SchedulerTask QueueOnce(Action callback) {
EnqueueTask(new SchedulerTask(obj => callback(), null, TimeSpan.Zero, false)); return EnqueueTask(new SchedulerTask(obj => callback(), null, TimeSpan.Zero, false));
} }
/// <summary> Queues an action that is asynchronously executed one time, after a certain delay. </summary> /// <summary> Queues an action that is asynchronously executed one time, after a certain delay. </summary>
public void QueueOnce(Action<SchedulerTask> callback, object state, TimeSpan delay) { public SchedulerTask QueueOnce(Action<SchedulerTask> callback, object state, TimeSpan delay) {
EnqueueTask(new SchedulerTask(callback, state, delay, false)); return EnqueueTask(new SchedulerTask(callback, state, delay, false));
} }
/// <summary> Queues an action that is asynchronously executed repeatedly, after a certain delay. </summary> /// <summary> Queues an action that is asynchronously executed repeatedly, after a certain delay. </summary>
public void QueueRepeat(Action<SchedulerTask> callback, object state, TimeSpan delay) { public SchedulerTask QueueRepeat(Action<SchedulerTask> callback, object state, TimeSpan delay) {
EnqueueTask(new SchedulerTask(callback, state, delay, true)); return EnqueueTask(new SchedulerTask(callback, state, delay, true));
} }
void EnqueueTask(SchedulerTask task) { /// <summary> Cancels a task if it is in the tasks list.
/// <remarks> Does not cancel the task if it is currently executing. </remarks>
public bool Cancel(SchedulerTask task) {
lock (taskLock) {
return tasks.Remove(task);
}
}
SchedulerTask EnqueueTask(SchedulerTask task) {
lock (taskLock) { lock (taskLock) {
tasks.Add(task); tasks.Add(task);
handle.Set(); handle.Set();
} }
return task;
} }
void Loop() { void Loop() {
@ -68,11 +78,8 @@ namespace MCGalaxy {
SchedulerTask GetNextTask() { SchedulerTask GetNextTask() {
DateTime now = DateTime.UtcNow; DateTime now = DateTime.UtcNow;
lock (taskLock) { lock (taskLock) {
for (int i = 0; i < tasks.Count; i++) { foreach (SchedulerTask task in tasks) {
SchedulerTask task = tasks[i]; if (task.NextRun < now) return task;
if (task.NextRun < now) {
tasks.RemoveAt(i); return task;
}
} }
} }
return null; return null;
@ -84,11 +91,13 @@ namespace MCGalaxy {
} catch (Exception ex) { } catch (Exception ex) {
MCGalaxy.Server.ErrorLog(ex); MCGalaxy.Server.ErrorLog(ex);
} }
if (!task.Repeating) return;
task.NextRun = DateTime.UtcNow.Add(task.Delay); if (task.Repeating) {
lock (taskLock) task.NextRun = DateTime.UtcNow.Add(task.Delay);
tasks.Add(task); } else {
lock (taskLock)
tasks.Remove(task);
}
} }
int GetWaitTime() { int GetWaitTime() {
@ -96,9 +105,8 @@ namespace MCGalaxy {
DateTime now = DateTime.UtcNow; DateTime now = DateTime.UtcNow;
lock (taskLock) { lock (taskLock) {
for (int i = 0; i < tasks.Count; i++) { foreach (SchedulerTask task in tasks) {
SchedulerTask task = tasks[i]; int remaining = (int)(task.NextRun - now).TotalMilliseconds;
int remaining = (int)(task.NextRun - now).TotalMilliseconds;
// minimum wait time is 10 milliseconds // minimum wait time is 10 milliseconds
remaining = Math.Max(10, remaining); remaining = Math.Max(10, remaining);
wait = Math.Min(wait, remaining); wait = Math.Min(wait, remaining);
@ -121,7 +129,7 @@ namespace MCGalaxy {
/// <summary> Whether this task should continue repeating. </summary> /// <summary> Whether this task should continue repeating. </summary>
public bool Repeating; public bool Repeating;
public SchedulerTask(Action<SchedulerTask> callback, object state, public SchedulerTask(Action<SchedulerTask> callback, object state,
TimeSpan delay, bool repeating) { TimeSpan delay, bool repeating) {
Callback = callback; Callback = callback;
State = state; State = state;