summaryrefslogtreecommitdiffhomepage
path: root/patches/api/0395-Folia-scheduler-and-owned-region-API.patch
blob: b07676cfefb1d3acea214bb7ccf8447f3c270e3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Sat, 17 Jun 2023 11:52:41 +0200
Subject: [PATCH] Folia scheduler and owned region API

Pulling Folia API to Paper is primarily intended for plugins
that want to target both Paper and Folia without unnecessary
compatibility layers.

Add both a location based scheduler, an entity based scheduler,
and a global region scheduler.

Owned region API may be useful for plugins which want to perform
operations over large areas outside of the buffer zone provided
by the regionaliser, as it is not guaranteed that anything
outside of the buffer zone is owned. Then, the plugins may use
the schedulers depending on the result of the ownership check.

diff --git a/src/main/java/io/papermc/paper/threadedregions/scheduler/AsyncScheduler.java b/src/main/java/io/papermc/paper/threadedregions/scheduler/AsyncScheduler.java
new file mode 100644
index 0000000000000000000000000000000000000000..d9cdd04660c5e60e494a8fed91ae437e6cb733ed
--- /dev/null
+++ b/src/main/java/io/papermc/paper/threadedregions/scheduler/AsyncScheduler.java
@@ -0,0 +1,51 @@
+package io.papermc.paper.threadedregions.scheduler;
+
+import org.bukkit.plugin.Plugin;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.concurrent.TimeUnit;
+import java.util.function.Consumer;
+
+/**
+ * Scheduler that may be used by plugins to schedule tasks to execute asynchronously from the server tick process.
+ */
+public interface AsyncScheduler {
+
+    /**
+     * Schedules the specified task to be executed asynchronously immediately.
+     * @param plugin Plugin which owns the specified task.
+     * @param task Specified task.
+     * @return The {@link ScheduledTask} that represents the scheduled task.
+     */
+    @NotNull ScheduledTask runNow(@NotNull Plugin plugin, @NotNull Consumer<ScheduledTask> task);
+
+    /**
+     * Schedules the specified task to be executed asynchronously after the time delay has passed.
+     * @param plugin Plugin which owns the specified task.
+     * @param task Specified task.
+     * @param delay The time delay to pass before the task should be executed.
+     * @param unit The time unit for the time delay.
+     * @return The {@link ScheduledTask} that represents the scheduled task.
+     */
+    @NotNull ScheduledTask runDelayed(@NotNull Plugin plugin, @NotNull Consumer<ScheduledTask> task, long delay,
+                                             @NotNull TimeUnit unit);
+
+    /**
+     * Schedules the specified task to be executed asynchronously after the initial delay has passed,
+     * and then periodically executed with the specified period.
+     * @param plugin Plugin which owns the specified task.
+     * @param task Specified task.
+     * @param initialDelay The time delay to pass before the first execution of the task.
+     * @param period The time between task executions after the first execution of the task.
+     * @param unit The time unit for the initial delay and period.
+     * @return The {@link ScheduledTask} that represents the scheduled task.
+     */
+    @NotNull ScheduledTask runAtFixedRate(@NotNull Plugin plugin, @NotNull Consumer<ScheduledTask> task,
+                                                 long initialDelay, long period, @NotNull TimeUnit unit);
+
+    /**
+     * Attempts to cancel all tasks scheduled by the specified plugin.
+     * @param plugin Specified plugin.
+     */
+    void cancelTasks(@NotNull Plugin plugin);
+}
diff --git a/src/main/java/io/papermc/paper/threadedregions/scheduler/EntityScheduler.java b/src/main/java/io/papermc/paper/threadedregions/scheduler/EntityScheduler.java
new file mode 100644
index 0000000000000000000000000000000000000000..9f69e189be8202a0ab1450540f5d12187ba6c987
--- /dev/null
+++ b/src/main/java/io/papermc/paper/threadedregions/scheduler/EntityScheduler.java
@@ -0,0 +1,104 @@
+package io.papermc.paper.threadedregions.scheduler;
+
+import org.bukkit.plugin.Plugin;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.function.Consumer;
+
+/**
+ * An entity can move between worlds with an arbitrary tick delay, be temporarily removed
+ * for players (i.e end credits), be partially removed from world state (i.e inactive but not removed),
+ * teleport between ticking regions, teleport between worlds, and even be removed entirely from the server.
+ * The uncertainty of an entity's state can make it difficult to schedule tasks without worrying about undefined
+ * behaviors resulting from any of the states listed previously.
+ *
+ * <p>
+ * This class is designed to eliminate those states by providing an interface to run tasks only when an entity
+ * is contained in a world, on the owning thread for the region, and by providing the current Entity object.
+ * The scheduler also allows a task to provide a callback, the "retired" callback, that will be invoked
+ * if the entity is removed before a task that was scheduled could be executed. The scheduler is also
+ * completely thread-safe, allowing tasks to be scheduled from any thread context. The scheduler also indicates
+ * properly whether a task was scheduled successfully (i.e scheduler not retired), thus the code scheduling any task
+ * knows whether the given callbacks will be invoked eventually or not - which may be critical for off-thread
+ * contexts.
+ * </p>
+ */
+public interface EntityScheduler {
+
+    /**
+     * Schedules a task with the given delay. If the task failed to schedule because the scheduler is retired (entity
+     * removed), then returns {@code false}. Otherwise, either the run callback will be invoked after the specified delay,
+     * or the retired callback will be invoked if the scheduler is retired.
+     * Note that the retired callback is invoked in critical code, so it should not attempt to remove the entity, remove
+     * other entities, load chunks, load worlds, modify ticket levels, etc.
+     *
+     * <p>
+     * It is guaranteed that the run and retired callback are invoked on the region which owns the entity.
+     * </p>
+     * @param run The callback to run after the specified delay, may not be null.
+     * @param retired Retire callback to run if the entity is retired before the run callback can be invoked, may be null.
+     * @param delay The delay in ticks before the run callback is invoked. Any value less-than 1 is treated as 1.
+     * @return {@code true} if the task was scheduled, which means that either the run function or the retired function
+     *         will be invoked (but never both), or {@code false} indicating neither the run nor retired function will be invoked
+     *         since the scheduler has been retired.
+     */
+    boolean execute(@NotNull Plugin plugin, @NotNull Runnable run, @Nullable Runnable retired, long delay);
+
+    /**
+     * Schedules a task to execute on the next tick. If the task failed to schedule because the scheduler is retired (entity
+     * removed), then returns {@code null}. Otherwise, either the task callback will be invoked after the specified delay,
+     * or the retired callback will be invoked if the scheduler is retired.
+     * Note that the retired callback is invoked in critical code, so it should not attempt to remove the entity, remove
+     * other entities, load chunks, load worlds, modify ticket levels, etc.
+     *
+     * <p>
+     * It is guaranteed that the task and retired callback are invoked on the region which owns the entity.
+     * </p>
+     * @param plugin The plugin that owns the task
+     * @param task The task to execute
+     * @param retired Retire callback to run if the entity is retired before the run callback can be invoked, may be null.
+     * @return The {@link ScheduledTask} that represents the scheduled task, or {@code null} if the entity has been removed.
+     */
+    @Nullable ScheduledTask run(@NotNull Plugin plugin, @NotNull Consumer<ScheduledTask> task,
+                                       @Nullable Runnable retired);
+
+    /**
+     * Schedules a task with the given delay. If the task failed to schedule because the scheduler is retired (entity
+     * removed), then returns {@code null}. Otherwise, either the task callback will be invoked after the specified delay,
+     * or the retired callback will be invoked if the scheduler is retired.
+     * Note that the retired callback is invoked in critical code, so it should not attempt to remove the entity, remove
+     * other entities, load chunks, load worlds, modify ticket levels, etc.
+     *
+     * <p>
+     * It is guaranteed that the task and retired callback are invoked on the region which owns the entity.
+     * </p>
+     * @param plugin The plugin that owns the task
+     * @param task The task to execute
+     * @param retired Retire callback to run if the entity is retired before the run callback can be invoked, may be null.
+     * @param delayTicks The delay, in ticks.
+     * @return The {@link ScheduledTask} that represents the scheduled task, or {@code null} if the entity has been removed.
+     */
+    @Nullable ScheduledTask runDelayed(@NotNull Plugin plugin, @NotNull Consumer<ScheduledTask> task,
+                                              @Nullable Runnable retired, long delayTicks);
+
+    /**
+     * Schedules a repeating task with the given delay and period. If the task failed to schedule because the scheduler
+     * is retired (entity removed), then returns {@code null}. Otherwise, either the task callback will be invoked after
+     * the specified delay, or the retired callback will be invoked if the scheduler is retired.
+     * Note that the retired callback is invoked in critical code, so it should not attempt to remove the entity, remove
+     * other entities, load chunks, load worlds, modify ticket levels, etc.
+     *
+     * <p>
+     * It is guaranteed that the task and retired callback are invoked on the region which owns the entity.
+     * </p>
+     * @param plugin The plugin that owns the task
+     * @param task The task to execute
+     * @param retired Retire callback to run if the entity is retired before the run callback can be invoked, may be null.
+     * @param initialDelayTicks The initial delay, in ticks.
+     * @param periodTicks The period, in ticks.
+     * @return The {@link ScheduledTask} that represents the scheduled task, or {@code null} if the entity has been removed.
+     */
+    @Nullable ScheduledTask runAtFixedRate(@NotNull Plugin plugin, @NotNull Consumer<ScheduledTask> task,
+                                                  @Nullable Runnable retired, long initialDelayTicks, long periodTicks);
+}
diff --git a/src/main/java/io/papermc/paper/threadedregions/scheduler/GlobalRegionScheduler.java b/src/main/java/io/papermc/paper/threadedregions/scheduler/GlobalRegionScheduler.java
new file mode 100644
index 0000000000000000000000000000000000000000..365b53fea8dee09cdc11f4399dea5f00c6ee70e2
--- /dev/null
+++ b/src/main/java/io/papermc/paper/threadedregions/scheduler/GlobalRegionScheduler.java
@@ -0,0 +1,58 @@
+package io.papermc.paper.threadedregions.scheduler;
+
+import org.bukkit.plugin.Plugin;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.function.Consumer;
+
+/**
+ * The global region task scheduler may be used to schedule tasks that will execute on the global region.
+ * <p>
+ * The global region is responsible for maintaining world day time, world game time, weather cycle,
+ * sleep night skipping, executing commands for console, and other misc. tasks that do not belong to any specific region.
+ * </p>
+ */
+public interface GlobalRegionScheduler {
+
+    /**
+     * Schedules a task to be executed on the global region.
+     * @param plugin The plugin that owns the task
+     * @param run The task to execute
+     */
+    void execute(@NotNull Plugin plugin, @NotNull Runnable run);
+
+    /**
+     * Schedules a task to be executed on the global region on the next tick.
+     * @param plugin The plugin that owns the task
+     * @param task The task to execute
+     * @return The {@link ScheduledTask} that represents the scheduled task.
+     */
+    @NotNull ScheduledTask run(@NotNull Plugin plugin, @NotNull Consumer<ScheduledTask> task);
+
+    /**
+     * Schedules a task to be executed on the global region after the specified delay in ticks.
+     * @param plugin The plugin that owns the task
+     * @param task The task to execute
+     * @param delayTicks The delay, in ticks.
+     * @return The {@link ScheduledTask} that represents the scheduled task.
+     */
+    @NotNull ScheduledTask runDelayed(@NotNull Plugin plugin, @NotNull Consumer<ScheduledTask> task, long delayTicks);
+
+    /**
+     * Schedules a repeating task to be executed on the global region after the initial delay with the
+     * specified period.
+     * @param plugin The plugin that owns the task
+     * @param task The task to execute
+     * @param initialDelayTicks The initial delay, in ticks.
+     * @param periodTicks The period, in ticks.
+     * @return The {@link ScheduledTask} that represents the scheduled task.
+     */
+    @NotNull ScheduledTask runAtFixedRate(@NotNull Plugin plugin, @NotNull Consumer<ScheduledTask> task,
+                                                 long initialDelayTicks, long periodTicks);
+
+    /**
+     * Attempts to cancel all tasks scheduled by the specified plugin.
+     * @param plugin Specified plugin.
+     */
+     void cancelTasks(@NotNull Plugin plugin);
+}
diff --git a/src/main/java/io/papermc/paper/threadedregions/scheduler/RegionScheduler.java b/src/main/java/io/papermc/paper/threadedregions/scheduler/RegionScheduler.java
new file mode 100644
index 0000000000000000000000000000000000000000..7557e170f84cde7292869fbd92b44b0e6eb43b4f
--- /dev/null
+++ b/src/main/java/io/papermc/paper/threadedregions/scheduler/RegionScheduler.java
@@ -0,0 +1,127 @@
+package io.papermc.paper.threadedregions.scheduler;
+
+import org.bukkit.Location;
+import org.bukkit.World;
+import org.bukkit.entity.Entity;
+import org.bukkit.plugin.Plugin;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.function.Consumer;
+
+/**
+ * The region task scheduler can be used to schedule tasks by location to be executed on the region which owns the location.
+ * <p>
+ * <b>Note</b>: It is entirely inappropriate to use the region scheduler to schedule tasks for entities.
+ * If you wish to schedule tasks to perform actions on entities, you should be using {@link Entity#getScheduler()}
+ * as the entity scheduler will "follow" an entity if it is teleported, whereas the region task scheduler
+ * will not.
+ * </p>
+ */
+public interface RegionScheduler {
+
+    /**
+     * Schedules a task to be executed on the region which owns the location.
+     *
+     * @param plugin The plugin that owns the task
+     * @param world  The world of the region that owns the task
+     * @param chunkX The chunk X coordinate of the region that owns the task
+     * @param chunkZ The chunk Z coordinate of the region that owns the task
+     * @param run    The task to execute
+     */
+    void execute(@NotNull Plugin plugin, @NotNull World world, int chunkX, int chunkZ, @NotNull Runnable run);
+
+    /**
+     * Schedules a task to be executed on the region which owns the location.
+     *
+     * @param plugin   The plugin that owns the task
+     * @param location The location at which the region executing should own
+     * @param run      The task to execute
+     */
+    default void execute(@NotNull Plugin plugin, @NotNull Location location, @NotNull Runnable run) {
+        this.execute(plugin, location.getWorld(), location.getBlockX() >> 4, location.getBlockZ() >> 4, run);
+    }
+
+    /**
+     * Schedules a task to be executed on the region which owns the location on the next tick.
+     *
+     * @param plugin The plugin that owns the task
+     * @param world  The world of the region that owns the task
+     * @param chunkX The chunk X coordinate of the region that owns the task
+     * @param chunkZ The chunk Z coordinate of the region that owns the task
+     * @param task   The task to execute
+     * @return The {@link ScheduledTask} that represents the scheduled task.
+     */
+    @NotNull ScheduledTask run(@NotNull Plugin plugin, @NotNull World world, int chunkX, int chunkZ, @NotNull Consumer<ScheduledTask> task);
+
+    /**
+     * Schedules a task to be executed on the region which owns the location on the next tick.
+     *
+     * @param plugin   The plugin that owns the task
+     * @param location The location at which the region executing should own
+     * @param task     The task to execute
+     * @return The {@link ScheduledTask} that represents the scheduled task.
+     */
+    default @NotNull ScheduledTask run(@NotNull Plugin plugin, @NotNull Location location, @NotNull Consumer<ScheduledTask> task) {
+        return this.run(plugin, location.getWorld(), location.getBlockX() >> 4, location.getBlockZ() >> 4, task);
+    }
+
+    /**
+     * Schedules a task to be executed on the region which owns the location after the specified delay in ticks.
+     *
+     * @param plugin     The plugin that owns the task
+     * @param world      The world of the region that owns the task
+     * @param chunkX     The chunk X coordinate of the region that owns the task
+     * @param chunkZ     The chunk Z coordinate of the region that owns the task
+     * @param task       The task to execute
+     * @param delayTicks The delay, in ticks.
+     * @return The {@link ScheduledTask} that represents the scheduled task.
+     */
+    @NotNull ScheduledTask runDelayed(@NotNull Plugin plugin, @NotNull World world, int chunkX, int chunkZ, @NotNull Consumer<ScheduledTask> task,
+                                      long delayTicks);
+
+    /**
+     * Schedules a task to be executed on the region which owns the location after the specified delay in ticks.
+     *
+     * @param plugin     The plugin that owns the task
+     * @param location   The location at which the region executing should own
+     * @param task       The task to execute
+     * @param delayTicks The delay, in ticks.
+     * @return The {@link ScheduledTask} that represents the scheduled task.
+     */
+    default @NotNull ScheduledTask runDelayed(@NotNull Plugin plugin, @NotNull Location location, @NotNull Consumer<ScheduledTask> task,
+                                              long delayTicks) {
+        return this.runDelayed(plugin, location.getWorld(), location.getBlockX() >> 4, location.getBlockZ() >> 4, task, delayTicks);
+    }
+
+    /**
+     * Schedules a repeating task to be executed on the region which owns the location after the initial delay with the
+     * specified period.
+     *
+     * @param plugin            The plugin that owns the task
+     * @param world             The world of the region that owns the task
+     * @param chunkX            The chunk X coordinate of the region that owns the task
+     * @param chunkZ            The chunk Z coordinate of the region that owns the task
+     * @param task              The task to execute
+     * @param initialDelayTicks The initial delay, in ticks.
+     * @param periodTicks       The period, in ticks.
+     * @return The {@link ScheduledTask} that represents the scheduled task.
+     */
+    @NotNull ScheduledTask runAtFixedRate(@NotNull Plugin plugin, @NotNull World world, int chunkX, int chunkZ, @NotNull Consumer<ScheduledTask> task,
+                                          long initialDelayTicks, long periodTicks);
+
+    /**
+     * Schedules a repeating task to be executed on the region which owns the location after the initial delay with the
+     * specified period.
+     *
+     * @param plugin            The plugin that owns the task
+     * @param location          The location at which the region executing should own
+     * @param task              The task to execute
+     * @param initialDelayTicks The initial delay, in ticks.
+     * @param periodTicks       The period, in ticks.
+     * @return The {@link ScheduledTask} that represents the scheduled task.
+     */
+    default @NotNull ScheduledTask runAtFixedRate(@NotNull Plugin plugin, @NotNull Location location, @NotNull Consumer<ScheduledTask> task,
+                                                  long initialDelayTicks, long periodTicks) {
+        return this.runAtFixedRate(plugin, location.getWorld(), location.getBlockX() >> 4, location.getBlockZ() >> 4, task, initialDelayTicks, periodTicks);
+    }
+}
diff --git a/src/main/java/io/papermc/paper/threadedregions/scheduler/ScheduledTask.java b/src/main/java/io/papermc/paper/threadedregions/scheduler/ScheduledTask.java
new file mode 100644
index 0000000000000000000000000000000000000000..a6b50c9d8af589cc4747e14d343d2045216c249c
--- /dev/null
+++ b/src/main/java/io/papermc/paper/threadedregions/scheduler/ScheduledTask.java
@@ -0,0 +1,112 @@
+package io.papermc.paper.threadedregions.scheduler;
+
+import org.bukkit.plugin.Plugin;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Represents a task scheduled to a scheduler.
+ */
+public interface ScheduledTask {
+
+    /**
+     * Returns the plugin that scheduled this task.
+     * @return the plugin that scheduled this task.
+     */
+    @NotNull Plugin getOwningPlugin();
+
+    /**
+     * Returns whether this task executes on a fixed period, as opposed to executing only once.
+     * @return whether this task executes on a fixed period, as opposed to executing only once.
+     */
+    boolean isRepeatingTask();
+
+    /**
+     * Attempts to cancel this task, returning the result of the attempt. In all cases, if the task is currently
+     * being executed no attempt is made to halt the task, however any executions in the future are halted.
+     * @return the result of the cancellation attempt.
+     */
+    @NotNull CancelledState cancel();
+
+    /**
+     * Returns the current execution state of this task.
+     * @return the current execution state of this task.
+     */
+    @NotNull ExecutionState getExecutionState();
+
+    /**
+     * Returns whether the current execution state is {@link ExecutionState#CANCELLED} or {@link ExecutionState#CANCELLED_RUNNING}.
+     * @return whether the current execution state is {@link ExecutionState#CANCELLED} or {@link ExecutionState#CANCELLED_RUNNING}.
+     */
+    default boolean isCancelled() {
+        final ExecutionState state = this.getExecutionState();
+        return state == ExecutionState.CANCELLED || state == ExecutionState.CANCELLED_RUNNING;
+    }
+
+    /**
+     * Represents the result of attempting to cancel a task.
+     */
+    enum CancelledState {
+        /**
+         * The task (repeating or not) has been successfully cancelled by the caller thread. The task is not executing
+         * currently, and it will not begin execution in the future.
+         */
+        CANCELLED_BY_CALLER,
+        /**
+         * The task (repeating or not) is already cancelled. The task is not executing currently, and it will not
+         * begin execution in the future.
+         */
+        CANCELLED_ALREADY,
+
+        /**
+         * The task is not a repeating task, and could not be cancelled because the task is being executed.
+         */
+        RUNNING,
+        /**
+         * The task is not a repeating task, and could not be cancelled because the task has already finished execution.
+         */
+        ALREADY_EXECUTED,
+
+        /**
+         * The caller thread successfully stopped future executions of a repeating task, but the task is currently
+         * being executed.
+         */
+        NEXT_RUNS_CANCELLED,
+
+        /**
+         * The repeating task's future executions are cancelled already, but the task is currently
+         * being executed.
+         */
+        NEXT_RUNS_CANCELLED_ALREADY,
+    }
+
+    /**
+     * Represents the current execution state of the task.
+     */
+    enum ExecutionState {
+        /**
+         * The task is currently not executing, but may begin execution in the future.
+         */
+        IDLE,
+
+        /**
+         * The task is currently executing.
+         */
+        RUNNING,
+
+        /**
+         * The task is not repeating, and the task finished executing.
+         */
+        FINISHED,
+
+        /**
+         * The task is not executing and will not begin execution in the future. If this task is not repeating, then
+         * this task was never executed.
+         */
+        CANCELLED,
+
+        /**
+         * The task is repeating and currently executing, but future executions are cancelled and will not occur.
+         */
+        CANCELLED_RUNNING;
+    }
+}
diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java
index adc95cd1486791787950533ef8e4baaf5d3827cc..50efe16bb80c618c3dfae03b70c8c165183af8ec 100644
--- a/src/main/java/org/bukkit/Bukkit.java
+++ b/src/main/java/org/bukkit/Bukkit.java
@@ -2672,6 +2672,141 @@ public final class Bukkit {
     }
     // Paper end
 
+    // Paper start - Folia region threading API
+    /**
+     * Returns the region task scheduler. The region task scheduler can be used to schedule
+     * tasks by location to be executed on the region which owns the location.
+     * <p>
+     * <b>Note</b>: It is entirely inappropriate to use the region scheduler to schedule tasks for entities.
+     * If you wish to schedule tasks to perform actions on entities, you should be using {@link Entity#getScheduler()}
+     * as the entity scheduler will "follow" an entity if it is teleported, whereas the region task scheduler
+     * will not.
+     * </p>
+     * <p><b>If you do not need/want to make your plugin run on Folia, use {@link #getScheduler()} instead.</b></p>
+     * @return the region task scheduler
+     */
+    public static @NotNull io.papermc.paper.threadedregions.scheduler.RegionScheduler getRegionScheduler() {
+        return server.getRegionScheduler();
+    }
+
+    /**
+     * Returns the async task scheduler. The async task scheduler can be used to schedule tasks
+     * that execute asynchronously from the server tick process.
+     * @return the async task scheduler
+     */
+    public static @NotNull io.papermc.paper.threadedregions.scheduler.AsyncScheduler getAsyncScheduler() {
+        return server.getAsyncScheduler();
+    }
+
+    /**
+     * Returns the global region task scheduler. The global task scheduler can be used to schedule
+     * tasks to execute on the global region.
+     * <p>
+     * The global region is responsible for maintaining world day time, world game time, weather cycle,
+     * sleep night skipping, executing commands for console, and other misc. tasks that do not belong to any specific region.
+     * </p>
+     * <p><b>If you do not need/want to make your plugin run on Folia, use {@link #getScheduler()} instead.</b></p>
+     * @return the global region scheduler
+     */
+    public static @NotNull io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler getGlobalRegionScheduler() {
+        return server.getGlobalRegionScheduler();
+    }
+
+    /**
+     * Returns whether the current thread is ticking a region and that the region being ticked
+     * owns the chunk at the specified world and block position.
+     * @param world Specified world.
+     * @param position Specified block position.
+     */
+    public static boolean isOwnedByCurrentRegion(@NotNull World world, @NotNull io.papermc.paper.math.Position position) {
+        return server.isOwnedByCurrentRegion(world, position);
+    }
+
+    /**
+     * Returns whether the current thread is ticking a region and that the region being ticked
+     * owns the chunks centered at the specified block position within the specified square radius.
+     * Specifically, this function checks that every chunk with position x in [centerX - radius, centerX + radius] and
+     * position z in [centerZ - radius, centerZ + radius] is owned by the current ticking region.
+     * @param world Specified world.
+     * @param position Specified block position.
+     * @param squareRadiusChunks Specified square radius. Must be >= 0. Note that this parameter is <i>not</i> a <i>squared</i>
+     *                           radius, but rather a <i>Chebyshev Distance</i>.
+     */
+    public static boolean isOwnedByCurrentRegion(@NotNull World world, @NotNull io.papermc.paper.math.Position position, int squareRadiusChunks) {
+        return server.isOwnedByCurrentRegion(world, position, squareRadiusChunks);
+    }
+
+    /**
+     * Returns whether the current thread is ticking a region and that the region being ticked
+     * owns the chunk at the specified world and block position as included in the specified location.
+     * @param location Specified location, must have a non-null world.
+     */
+    public static boolean isOwnedByCurrentRegion(@NotNull Location location) {
+        return server.isOwnedByCurrentRegion(location);
+    }
+
+    /**
+     * Returns whether the current thread is ticking a region and that the region being ticked
+     * owns the chunks centered at the specified world and block position as included in the specified location
+     * within the specified square radius.
+     * Specifically, this function checks that every chunk with position x in [centerX - radius, centerX + radius] and
+     * position z in [centerZ - radius, centerZ + radius] is owned by the current ticking region.
+     * @param location Specified location, must have a non-null world.
+     * @param squareRadiusChunks Specified square radius. Must be >= 0. Note that this parameter is <i>not</i> a <i>squared</i>
+     *                           radius, but rather a <i>Chebyshev Distance</i>.
+     */
+    public static boolean isOwnedByCurrentRegion(@NotNull Location location, int squareRadiusChunks) {
+        return server.isOwnedByCurrentRegion(location, squareRadiusChunks);
+    }
+
+    /**
+     * Returns whether the current thread is ticking a region and that the region being ticked
+     * owns the chunk at the specified block position.
+     * @param block Specified block position.
+     */
+    public static boolean isOwnedByCurrentRegion(@NotNull org.bukkit.block.Block block) {
+        return server.isOwnedByCurrentRegion(block.getLocation());
+    }
+
+    /**
+     * Returns whether the current thread is ticking a region and that the region being ticked
+     * owns the chunk at the specified world and chunk position.
+     * @param world Specified world.
+     * @param chunkX Specified x-coordinate of the chunk position.
+     * @param chunkZ Specified z-coordinate of the chunk position.
+     */
+    public static boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ) {
+        return server.isOwnedByCurrentRegion(world, chunkX, chunkZ);
+    }
+
+    /**
+     * Returns whether the current thread is ticking a region and that the region being ticked
+     * owns the chunks centered at the specified world and chunk position within the specified
+     * square radius.
+     * Specifically, this function checks that every chunk with position x in [centerX - radius, centerX + radius] and
+     * position z in [centerZ - radius, centerZ + radius] is owned by the current ticking region.
+     * @param world Specified world.
+     * @param chunkX Specified x-coordinate of the chunk position.
+     * @param chunkZ Specified z-coordinate of the chunk position.
+     * @param squareRadiusChunks Specified square radius. Must be >= 0. Note that this parameter is <i>not</i> a <i>squared</i>
+     *                           radius, but rather a <i>Chebyshev Distance</i>.
+     */
+    public static boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ, int squareRadiusChunks) {
+        return server.isOwnedByCurrentRegion(world, chunkX, chunkZ, squareRadiusChunks);
+    }
+
+    /**
+     * Returns whether the current thread is ticking a region and that the region being ticked
+     * owns the specified entity. Note that this function is the only appropriate method of checking
+     * for ownership of an entity, as retrieving the entity's location is undefined unless the entity is owned
+     * by the current region.
+     * @param entity Specified entity.
+     */
+    public static boolean isOwnedByCurrentRegion(@NotNull Entity entity) {
+        return server.isOwnedByCurrentRegion(entity);
+    }
+    // Paper end - Folia region threading API
+
     @NotNull
     public static Server.Spigot spigot() {
         return server.spigot();
diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java
index 0648130a6ce2e08d96b05fde1cfd58c2bb24ae07..1b8d6a3333a4fa9155b79644e683e2343c134e12 100644
--- a/src/main/java/org/bukkit/Server.java
+++ b/src/main/java/org/bukkit/Server.java
@@ -2333,4 +2333,119 @@ public interface Server extends PluginMessageRecipient, net.kyori.adventure.audi
      */
     @NotNull org.bukkit.potion.PotionBrewer getPotionBrewer();
     // Paper end
+
+    // Paper start - Folia region threading API
+    /**
+     * Returns the Folia region task scheduler. The region task scheduler can be used to schedule
+     * tasks by location to be executed on the region which owns the location.
+     * <p>
+     * <b>Note</b>: It is entirely inappropriate to use the region scheduler to schedule tasks for entities.
+     * If you wish to schedule tasks to perform actions on entities, you should be using {@link Entity#getScheduler()}
+     * as the entity scheduler will "follow" an entity if it is teleported, whereas the region task scheduler
+     * will not.
+     * </p>
+     * <p><b>If you do not need/want to make your plugin run on Folia, use {@link #getScheduler()} instead.</b></p>
+     * @return the region task scheduler
+     */
+    @NotNull io.papermc.paper.threadedregions.scheduler.RegionScheduler getRegionScheduler();
+
+    /**
+     * Returns the Folia async task scheduler. The async task scheduler can be used to schedule tasks
+     * that execute asynchronously from the server tick process.
+     * @return the async task scheduler
+     */
+    @NotNull io.papermc.paper.threadedregions.scheduler.AsyncScheduler getAsyncScheduler();
+
+    /**
+     * Returns the Folia global region task scheduler. The global task scheduler can be used to schedule
+     * tasks to execute on the global region.
+     * <p>
+     * The global region is responsible for maintaining world day time, world game time, weather cycle,
+     * sleep night skipping, executing commands for console, and other misc. tasks that do not belong to any specific region.
+     * </p>
+     * <p><b>If you do not need/want to make your plugin run on Folia, use {@link #getScheduler()} instead.</b></p>
+     * @return the global region scheduler
+     */
+    @NotNull io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler getGlobalRegionScheduler();
+
+    /**
+     * Returns whether the current thread is ticking a region and that the region being ticked
+     * owns the chunk at the specified world and block position.
+     * @param world Specified world.
+     * @param position Specified block position.
+     */
+    boolean isOwnedByCurrentRegion(@NotNull World world, @NotNull io.papermc.paper.math.Position position);
+
+    /**
+     * Returns whether the current thread is ticking a region and that the region being ticked
+     * owns the chunks centered at the specified block position within the specified square radius.
+     * Specifically, this function checks that every chunk with position x in [centerX - radius, centerX + radius] and
+     * position z in [centerZ - radius, centerZ + radius] is owned by the current ticking region.
+     * @param world Specified world.
+     * @param position Specified block position.
+     * @param squareRadiusChunks Specified square radius. Must be >= 0. Note that this parameter is <i>not</i> a <i>squared</i>
+     *                           radius, but rather a <i>Chebyshev Distance</i>.
+     */
+    boolean isOwnedByCurrentRegion(@NotNull World world, @NotNull io.papermc.paper.math.Position position, int squareRadiusChunks);
+
+    /**
+     * Returns whether the current thread is ticking a region and that the region being ticked
+     * owns the chunk at the specified world and block position as included in the specified location.
+     * @param location Specified location, must have a non-null world.
+     */
+    boolean isOwnedByCurrentRegion(@NotNull Location location);
+
+    /**
+     * Returns whether the current thread is ticking a region and that the region being ticked
+     * owns the chunks centered at the specified world and block position as included in the specified location
+     * within the specified square radius.
+     * Specifically, this function checks that every chunk with position x in [centerX - radius, centerX + radius] and
+     * position z in [centerZ - radius, centerZ + radius] is owned by the current ticking region.
+     * @param location Specified location, must have a non-null world.
+     * @param squareRadiusChunks Specified square radius. Must be >= 0. Note that this parameter is <i>not</i> a <i>squared</i>
+     *                           radius, but rather a <i>Chebyshev Distance</i>.
+     */
+    boolean isOwnedByCurrentRegion(@NotNull Location location, int squareRadiusChunks);
+
+    /**
+     * Returns whether the current thread is ticking a region and that the region being ticked
+     * owns the chunk at the specified block position.
+     * @param block Specified block position.
+     */
+    default boolean isOwnedByCurrentRegion(@NotNull org.bukkit.block.Block block) {
+        return isOwnedByCurrentRegion(block.getLocation());
+    }
+
+    /**
+     * Returns whether the current thread is ticking a region and that the region being ticked
+     * owns the chunk at the specified world and chunk position.
+     * @param world Specified world.
+     * @param chunkX Specified x-coordinate of the chunk position.
+     * @param chunkZ Specified z-coordinate of the chunk position.
+     */
+    boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ);
+
+    /**
+     * Returns whether the current thread is ticking a region and that the region being ticked
+     * owns the chunks centered at the specified world and chunk position within the specified
+     * square radius.
+     * Specifically, this function checks that every chunk with position x in [centerX - radius, centerX + radius] and
+     * position z in [centerZ - radius, centerZ + radius] is owned by the current ticking region.
+     * @param world Specified world.
+     * @param chunkX Specified x-coordinate of the chunk position.
+     * @param chunkZ Specified z-coordinate of the chunk position.
+     * @param squareRadiusChunks Specified square radius. Must be >= 0. Note that this parameter is <i>not</i> a <i>squared</i>
+     *                           radius, but rather a <i>Chebyshev Distance</i>.
+     */
+    boolean isOwnedByCurrentRegion(@NotNull World world, int chunkX, int chunkZ, int squareRadiusChunks);
+
+    /**
+     * Returns whether the current thread is ticking a region and that the region being ticked
+     * owns the specified entity. Note that this function is the only appropriate method of checking
+     * for ownership of an entity, as retrieving the entity's location is undefined unless the entity is owned
+     * by the current region.
+     * @param entity Specified entity.
+     */
+    boolean isOwnedByCurrentRegion(@NotNull Entity entity);
+    // Paper end - Folia region threading API
 }
diff --git a/src/main/java/org/bukkit/entity/Entity.java b/src/main/java/org/bukkit/entity/Entity.java
index a76e537c9b3b9519cd46894c90b750f012182be9..4580c7613fac4f1eeccc2be2d15497cec5868736 100644
--- a/src/main/java/org/bukkit/entity/Entity.java
+++ b/src/main/java/org/bukkit/entity/Entity.java
@@ -1101,4 +1101,15 @@ public interface Entity extends Metadatable, CommandSender, Nameable, Persistent
      */
     boolean wouldCollideUsing(@NotNull BoundingBox boundingBox);
     // Paper end - Collision API
+
+    // Paper start - Folia schedulers
+    /**
+     * Returns the task scheduler for this entity. The entity scheduler can be used to schedule tasks
+     * that are guaranteed to always execute on the tick thread that owns the entity.
+     * <p><b>If you do not need/want to make your plugin run on Folia, use {@link org.bukkit.Server#getScheduler()} instead.</b></p>
+     * @return the task scheduler for this entity.
+     * @see io.papermc.paper.threadedregions.scheduler.EntityScheduler
+     */
+    @NotNull io.papermc.paper.threadedregions.scheduler.EntityScheduler getScheduler();
+    // Paper end - Folia schedulers
 }