103 lines
4.7 KiB
Diff
103 lines
4.7 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Helvetica Volubi <suisuroru@blue-millennium.fun>
|
|
Date: Sat, 22 Nov 2025 01:43:26 +0800
|
|
Subject: [PATCH] Add cancel task by task id in FoliaGlobalRegionScheduler
|
|
|
|
|
|
diff --git a/src/main/java/io/papermc/paper/threadedregions/scheduler/FoliaGlobalRegionScheduler.java b/src/main/java/io/papermc/paper/threadedregions/scheduler/FoliaGlobalRegionScheduler.java
|
|
index 14ffbcb66006878eacf310547dad53718f8308eb..f20f2735fbb4d1e38d576eafe18a0054a45aa59e 100644
|
|
--- a/src/main/java/io/papermc/paper/threadedregions/scheduler/FoliaGlobalRegionScheduler.java
|
|
+++ b/src/main/java/io/papermc/paper/threadedregions/scheduler/FoliaGlobalRegionScheduler.java
|
|
@@ -16,6 +16,8 @@ public class FoliaGlobalRegionScheduler implements GlobalRegionScheduler {
|
|
private long tickCount = 0L;
|
|
private final Object stateLock = new Object();
|
|
private final Long2ObjectOpenHashMap<List<GlobalScheduledTask>> tasksByDeadline = new Long2ObjectOpenHashMap<>();
|
|
+ private int lastTaskId = 1; // Lophine - add task id
|
|
+ private final Object stateLockForTaskId = new Object(); // Lophine - add task id
|
|
|
|
public void tick() {
|
|
final List<GlobalScheduledTask> run;
|
|
@@ -64,7 +66,7 @@ public class FoliaGlobalRegionScheduler implements GlobalRegionScheduler {
|
|
throw new IllegalPluginAccessException("Plugin attempted to register task while disabled");
|
|
}
|
|
|
|
- final GlobalScheduledTask ret = new GlobalScheduledTask(plugin, -1, task);
|
|
+ final GlobalScheduledTask ret = new GlobalScheduledTask(plugin, -1, task, this.getNextTaskId()); // Lophine - add task id
|
|
|
|
this.scheduleInternal(ret, delayTicks);
|
|
|
|
@@ -91,7 +93,7 @@ public class FoliaGlobalRegionScheduler implements GlobalRegionScheduler {
|
|
throw new IllegalPluginAccessException("Plugin attempted to register task while disabled");
|
|
}
|
|
|
|
- final GlobalScheduledTask ret = new GlobalScheduledTask(plugin, periodTicks, task);
|
|
+ final GlobalScheduledTask ret = new GlobalScheduledTask(plugin, periodTicks, task, this.getNextTaskId()); // Lophine - add task id
|
|
|
|
this.scheduleInternal(ret, initialDelayTicks);
|
|
|
|
@@ -133,7 +135,34 @@ public class FoliaGlobalRegionScheduler implements GlobalRegionScheduler {
|
|
}
|
|
}
|
|
|
|
- private final class GlobalScheduledTask implements ScheduledTask, Runnable {
|
|
+ // Lophine start - add cancel task by task id
|
|
+ @Override
|
|
+ public void cancelTask(final int taskId) {
|
|
+ synchronized (this.stateLock) {
|
|
+ for (final List<GlobalScheduledTask> tasks : this.tasksByDeadline.values()) {
|
|
+ for (final GlobalScheduledTask task : tasks) {
|
|
+ if (task.getTaskId() == taskId) {
|
|
+ task.cancel();
|
|
+ return;
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ private int getNextTaskId() {
|
|
+ synchronized (this.stateLockForTaskId) {
|
|
+ if (this.lastTaskId == Integer.MAX_VALUE) {
|
|
+ this.lastTaskId = 1;
|
|
+ } else {
|
|
+ ++this.lastTaskId;
|
|
+ }
|
|
+ }
|
|
+ return this.lastTaskId;
|
|
+ }
|
|
+ // Lophine end - add cancel task by task id
|
|
+
|
|
+ public final class GlobalScheduledTask implements ScheduledTask, Runnable { // Lophine - private -> public
|
|
|
|
private static final int STATE_IDLE = 0;
|
|
private static final int STATE_EXECUTING = 1;
|
|
@@ -145,13 +174,15 @@ public class FoliaGlobalRegionScheduler implements GlobalRegionScheduler {
|
|
private final long repeatDelay; // in ticks
|
|
private Consumer<ScheduledTask> run;
|
|
private volatile int state;
|
|
+ private final int taskId; // Lophine - add task id
|
|
|
|
private static final VarHandle STATE_HANDLE = ConcurrentUtil.getVarHandle(GlobalScheduledTask.class, "state", int.class);
|
|
|
|
- private GlobalScheduledTask(final Plugin plugin, final long repeatDelay, final Consumer<ScheduledTask> run) {
|
|
+ private GlobalScheduledTask(final Plugin plugin, final long repeatDelay, final Consumer<ScheduledTask> run, int taskId) { // Lophine - add task id
|
|
this.plugin = plugin;
|
|
this.repeatDelay = repeatDelay;
|
|
this.run = run;
|
|
+ this.taskId = taskId; // Lophine - add task id
|
|
}
|
|
|
|
private final int getStateVolatile() {
|
|
@@ -262,5 +293,11 @@ public class FoliaGlobalRegionScheduler implements GlobalRegionScheduler {
|
|
}
|
|
}
|
|
}
|
|
+
|
|
+ // Lophine start - add task id
|
|
+ public int getTaskId() {
|
|
+ return taskId;
|
|
+ }
|
|
+ // Lophine end - add task id
|
|
}
|
|
}
|