Added repeating task support to Scheduler

This commit is contained in:
Intelli 2023-03-31 16:43:27 -06:00
parent fe6023450c
commit f706bcf38e

View File

@ -5,7 +5,9 @@ import java.util.concurrent.TimeUnit;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.scheduler.BukkitTask;
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import net.coreprotect.CoreProtect;
import net.coreprotect.config.ConfigHandler;
@ -17,7 +19,7 @@ public class Scheduler {
public static void scheduleSyncDelayedTask(CoreProtect plugin, Runnable task, Object regionData, int delay) {
if (ConfigHandler.isFolia) {
if (regionData instanceof Location) { // REGION
if (regionData instanceof Location) {
Location location = (Location) regionData;
if (delay == 0) {
Bukkit.getServer().getRegionScheduler().run(plugin, location, value -> task.run());
@ -26,7 +28,7 @@ public class Scheduler {
Bukkit.getServer().getRegionScheduler().runDelayed(plugin, location, value -> task.run(), delay);
}
}
else if (regionData instanceof Entity) { // ENTITY
else if (regionData instanceof Entity) {
Entity entity = (Entity) regionData;
if (delay == 0) {
entity.getScheduler().run(plugin, value -> task.run(), task);
@ -35,7 +37,7 @@ public class Scheduler {
entity.getScheduler().runDelayed(plugin, value -> task.run(), task, delay);
}
}
else { // GLOBAL
else {
if (delay == 0) {
Bukkit.getServer().getGlobalRegionScheduler().run(plugin, value -> task.run());
}
@ -44,7 +46,7 @@ public class Scheduler {
}
}
}
else { // BUKKIT
else {
if (delay == 0) {
Bukkit.getServer().getScheduler().runTask(plugin, task);
}
@ -54,6 +56,25 @@ public class Scheduler {
}
}
public static Object scheduleSyncRepeatingTask(CoreProtect plugin, Runnable task, Object regionData, int delay, int period) {
if (ConfigHandler.isFolia) {
if (regionData instanceof Location) {
Location location = (Location) regionData;
return Bukkit.getServer().getRegionScheduler().runAtFixedRate(plugin, location, value -> task.run(), delay, period);
}
else if (regionData instanceof Entity) {
Entity entity = (Entity) regionData;
return entity.getScheduler().runAtFixedRate(plugin, value -> task.run(), task, delay, period);
}
else {
return Bukkit.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, value -> task.run(), delay, period);
}
}
else {
return Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, task, delay, period);
}
}
public static void scheduleAsyncDelayedTask(CoreProtect plugin, Runnable task, int delay) {
if (ConfigHandler.isFolia) {
if (delay == 0) {
@ -63,7 +84,7 @@ public class Scheduler {
Bukkit.getServer().getAsyncScheduler().runDelayed(plugin, value -> task.run(), (delay * 50L), TimeUnit.MILLISECONDS);
}
}
else { // BUKKIT
else {
if (delay == 0) {
Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, task);
}
@ -88,4 +109,21 @@ public class Scheduler {
public static void runTaskAsynchronously(CoreProtect plugin, Runnable task) {
scheduleAsyncDelayedTask(plugin, task, 0);
}
public static void runTaskLaterAsynchronously(CoreProtect plugin, Runnable task, int delay) {
scheduleAsyncDelayedTask(plugin, task, delay);
}
public static void cancelTask(Object task) {
if (ConfigHandler.isFolia) {
if (task instanceof ScheduledTask) {
ScheduledTask scheduledTask = (ScheduledTask) task;
scheduledTask.cancel();
}
}
else if (task instanceof BukkitTask) {
BukkitTask bukkitTask = (BukkitTask) task;
bukkitTask.cancel();
}
}
}