Skip to content

Commit

Permalink
new TaskTime & TimerTaskTime with 0-delay task handling
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Sep 4, 2023
1 parent 75ab140 commit e68f21c
Show file tree
Hide file tree
Showing 8 changed files with 587 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,35 @@ public interface Runner {
* Run a delayed task
*
* @param runnable the task
* @param delay the delay in ticks before the task is run
* @param delay the delay before the task is run
*
* @return the task
*/
Task runTaskLater(Runnable runnable, long delay);
Task runTaskLater(Runnable runnable, TaskTime delay);

/**
* Run a task repeatedly
*
* @param runnable the task. Return true to continue, false to stop.
* @param delay the delay in ticks before the task is run
* @param period the period in ticks between each run
* @param runnable the task. Return true to continue, false to stop.
* @param timerTaskTime the timer task time
*
* @return the task
*/
Task runTaskTimer(BooleanSupplier runnable, long delay, long period);
Task runTaskTimer(BooleanSupplier runnable, TimerTaskTime timerTaskTime);

/**
* Run a task repeatedly
*
* @param runnable the task
* @param delay the delay in ticks before the task is run
* @param period the period in ticks between each run
* @param runnable the task
* @param timerTaskTime the timer task time
*
* @return the task
*/
default Task runTaskTimer(Runnable runnable, long delay, long period) {
default Task runTaskTimer(Runnable runnable, TimerTaskTime timerTaskTime) {
return runTaskTimer(() -> {
runnable.run();
return true;
}, delay, period);
}, timerTaskTime);
}

/**
Expand All @@ -66,6 +64,199 @@ default Task runTaskTimer(Runnable runnable, long delay, long period) {
*/
Task runEntityTask(Entity entity, Runnable runnable, Runnable retired);

/**
* Run a delayed task related to an entity
*
* @param entity the entity that the task is related to
* @param runnable the task
* @param retired the task when the entity is retired (e.g. removed, invalid)
* @param delay the delay before the task is run
*
* @return the task
*/
Task runEntityTaskLater(Entity entity, Runnable runnable, Runnable retired, TaskTime delay);

/**
* Run a task repeatedly related to an entity
*
* @param entity the entity that the task is related to
* @param runnable the task. Return true to continue, false to stop.
* @param retired the task when the entity is retired (e.g. removed, invalid)
* @param timerTaskTime the timer task time
*
* @return the task
*/
Task runEntityTaskTimer(Entity entity, BooleanSupplier runnable, Runnable retired, TimerTaskTime timerTaskTime);

/**
* Run a task repeatedly related to an entity
*
* @param entity the entity that the task is related to
* @param runnable the task
* @param retired the task when the entity is retired (e.g. removed, invalid)
* @param timerTaskTime the timer task time
*
* @return the task
*/
default Task runEntityTaskTimer(Entity entity, Runnable runnable, Runnable retired, TimerTaskTime timerTaskTime) {
return runEntityTaskTimer(entity, () -> {
runnable.run();
return true;
}, retired, timerTaskTime);
}

/**
* Run a delayed task related to an entity
*
* @param entity the entity that the task is related to
* @param runnable the task
* @param delay the delay before the task is run
*
* @return the task
*/
default Task runEntityTaskLater(Entity entity, Runnable runnable, TaskTime delay) {
return runEntityTaskLater(entity, runnable, () -> {
}, delay);
}

/**
* Run a task repeatedly related to an entity
*
* @param entity the entity that the task is related to
* @param runnable the task. Return true to continue, false to stop.
* @param timerTaskTime the timer task time
*
* @return the task
*/
default Task runEntityTaskTimer(Entity entity, BooleanSupplier runnable, TimerTaskTime timerTaskTime) {
return runEntityTaskTimer(entity, runnable, () -> {
}, timerTaskTime);
}

/**
* Run a task repeatedly related to an entity
*
* @param entity the entity that the task is related to
* @param runnable the task
* @param timerTaskTime the timer task time
*
* @return the task
*/
default Task runEntityTaskTimer(Entity entity, Runnable runnable, TimerTaskTime timerTaskTime) {
return runEntityTaskTimer(entity, runnable, () -> {
}, timerTaskTime);
}

/**
* Run a delayed task related to an entity with a finalizer.
* The finalizer will be run both after the task is run and when the entity is retired.
*
* @param entity the entity that the task is related to
* @param runnable the task
* @param finalizer the finalizer
* @param delay the delay before the task is run
*
* @return the task
*/
default Task runEntityTaskLaterWithFinalizer(Entity entity, Runnable runnable, Runnable finalizer, TaskTime delay) {
return runEntityTaskLater(entity, () -> {
try {
runnable.run();
} finally {
finalizer.run();
}
}, finalizer, delay);
}

/**
* Run a task related to a location
*
* @param location the location that the task is related to
* @param runnable the task
*
* @return the task
*/
Task runLocationTask(Location location, Runnable runnable);

/**
* Run a delayed task related to a location
*
* @param location the location that the task is related to
* @param runnable the task
* @param delay the delay before the task is run
*
* @return the task
*/
Task runLocationTaskLater(Location location, Runnable runnable, TaskTime delay);

/**
* Run a task repeatedly related to a location
*
* @param location the location that the task is related to
* @param runnable the task. Return true to continue, false to stop.
* @param timerTaskTime the timer task time
*
* @return the task
*/
Task runLocationTaskTimer(Location location, BooleanSupplier runnable, TimerTaskTime timerTaskTime);

/**
* Run a task repeatedly related to a location
*
* @param location the location that the task is related to
* @param runnable the task
* @param timerTaskTime the timer task time
*
* @return the task
*/
default Task runLocationTaskTimer(Location location, Runnable runnable, TimerTaskTime timerTaskTime) {
return runLocationTaskTimer(location, () -> {
runnable.run();
return true;
}, timerTaskTime);
}

/**
* Run a delayed task
*
* @param runnable the task
* @param delay the delay in ticks before the task is run
*
* @return the task
*/
default Task runTaskLater(Runnable runnable, long delay) {
return runTaskLater(runnable, TaskTime.of(delay));
}

/**
* Run a task repeatedly
*
* @param runnable the task. Return true to continue, false to stop.
* @param delay the delay in ticks before the task is run
* @param period the period in ticks between each run
*
* @return the task
*/
default Task runTaskTimer(BooleanSupplier runnable, long delay, long period) {
return runTaskTimer(runnable, TimerTaskTime.of(delay, period));
}

/**
* Run a task repeatedly
*
* @param runnable the task
* @param delay the delay in ticks before the task is run
* @param period the period in ticks between each run
*
* @return the task
*/
default Task runTaskTimer(Runnable runnable, long delay, long period) {
return runTaskTimer(() -> {
runnable.run();
return true;
}, delay, period);
}

/**
* Run a delayed task related to an entity
*
Expand All @@ -76,7 +267,9 @@ default Task runTaskTimer(Runnable runnable, long delay, long period) {
*
* @return the task
*/
Task runEntityTaskLater(Entity entity, Runnable runnable, Runnable retired, long delay);
default Task runEntityTaskLater(Entity entity, Runnable runnable, Runnable retired, long delay) {
return runEntityTaskLater(entity, runnable, retired, TaskTime.of(delay));
}

/**
* Run a task repeatedly related to an entity
Expand All @@ -89,7 +282,9 @@ default Task runTaskTimer(Runnable runnable, long delay, long period) {
*
* @return the task
*/
Task runEntityTaskTimer(Entity entity, BooleanSupplier runnable, Runnable retired, long delay, long period);
default Task runEntityTaskTimer(Entity entity, BooleanSupplier runnable, Runnable retired, long delay, long period) {
return runEntityTaskTimer(entity, runnable, retired, TimerTaskTime.of(delay, period));
}

/**
* Run a task repeatedly related to an entity
Expand Down Expand Up @@ -127,6 +322,7 @@ default Task runEntityTask(Entity entity, Runnable runnable) {
*
* @param entity the entity that the task is related to
* @param runnable the task
* @param delay the delay in ticks before the task is run
*
* @return the task
*/
Expand Down Expand Up @@ -155,6 +351,8 @@ default Task runEntityTaskTimer(Entity entity, BooleanSupplier runnable, long de
*
* @param entity the entity that the task is related to
* @param runnable the task
* @param delay the delay in ticks before the task is run
* @param period the period in ticks between each run
*
* @return the task
*/
Expand Down Expand Up @@ -204,16 +402,6 @@ default Task runEntityTaskLaterWithFinalizer(Entity entity, Runnable runnable, R
}, finalizer, delay);
}

/**
* Run a task related to a location
*
* @param location the location that the task is related to
* @param runnable the task
*
* @return the task
*/
Task runLocationTask(Location location, Runnable runnable);

/**
* Run a delayed task related to a location
*
Expand All @@ -223,7 +411,9 @@ default Task runEntityTaskLaterWithFinalizer(Entity entity, Runnable runnable, R
*
* @return the task
*/
Task runLocationTaskLater(Location location, Runnable runnable, long delay);
default Task runLocationTaskLater(Location location, Runnable runnable, long delay) {
return runLocationTaskLater(location, runnable, TaskTime.of(delay));
}

/**
* Run a task repeatedly related to a location
Expand All @@ -235,7 +425,9 @@ default Task runEntityTaskLaterWithFinalizer(Entity entity, Runnable runnable, R
*
* @return the task
*/
Task runLocationTaskTimer(Location location, BooleanSupplier runnable, long delay, long period);
default Task runLocationTaskTimer(Location location, BooleanSupplier runnable, long delay, long period) {
return runLocationTaskTimer(location, runnable, TimerTaskTime.of(delay, period));
}

/**
* Run a task repeatedly related to a location
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package me.hsgamer.hscore.bukkit.scheduler;

import java.util.concurrent.TimeUnit;

/**
* The time for the task
*/
public class TaskTime {
private final long time;
private final TimeUnit unit;
private final long ticks;

private TaskTime(long time, TimeUnit unit) {
this.time = Math.max(time, 0);
this.unit = unit;
this.ticks = unit.toMillis(this.time) / 50;
}

/**
* Create a new task time
*
* @param time the time
* @param unit the unit
*
* @return the task time
*/
public static TaskTime of(long time, TimeUnit unit) {
return new TaskTime(time, unit);
}

/**
* Create a new task time from ticks
*
* @param ticks the ticks
*
* @return the task time
*/
public static TaskTime of(long ticks) {
return new TaskTime(ticks * 50, TimeUnit.MILLISECONDS);
}

/**
* Get the time
*
* @return the time
*/
public long getTime() {
return time;
}

/**
* Get the unit
*
* @return the unit
*/
public TimeUnit getUnit() {
return unit;
}

/**
* Get the ticks
*
* @return the ticks
*/
public long getTicks() {
return ticks;
}
}
Loading

0 comments on commit e68f21c

Please sign in to comment.