Change entity scheduler API to use Runnables

The generics pose a problem, and the parameter passed to the
Consumer is not needed in API.

Additionally, stop trying to cancel Bukkit scheduler tasks on
plugin disable as the Bukkit scheduler does not work.
This commit is contained in:
Spottedleaf 2023-03-06 12:57:29 -08:00
parent 71c4d0f41f
commit 4e7205d401
2 changed files with 23 additions and 13 deletions

View File

@ -7,17 +7,15 @@ Add both a location based scheduler and an entity based scheduler
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..edf4fe717c72aa147e5c0dee758bc7a133a19aa3
index 0000000000000000000000000000000000000000..4193b13f1f51c2fb8da76f3e03187d859eaa8e10
--- /dev/null
+++ b/src/main/java/io/papermc/paper/threadedregions/scheduler/EntityScheduler.java
@@ -0,0 +1,49 @@
@@ -0,0 +1,47 @@
+package io.papermc.paper.threadedregions.scheduler;
+
+import org.bukkit.entity.Entity;
+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
@ -56,8 +54,8 @@ index 0000000000000000000000000000000000000000..edf4fe717c72aa147e5c0dee758bc7a1
+ * 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.
+ */
+ public boolean execute(@NotNull final Plugin plugin, @NotNull final Consumer<? extends Entity> run,
+ @Nullable final Consumer<? extends Entity> retired, final long delay);
+ public boolean execute(@NotNull final Plugin plugin, @NotNull final Runnable run, @Nullable final Runnable retired,
+ final long delay);
+
+}
diff --git a/src/main/java/io/papermc/paper/threadedregions/scheduler/RegionisedScheduler.java b/src/main/java/io/papermc/paper/threadedregions/scheduler/RegionisedScheduler.java
@ -160,3 +158,16 @@ index cdbc7329cf5f67d66e31eb31e83b9e7997040f72..90451ed12b2c95bb372ac2e3cbb57b8b
+ @NotNull io.papermc.paper.threadedregions.scheduler.EntityScheduler getScheduler();
+ // Folia end - region threading API
}
diff --git a/src/main/java/org/bukkit/plugin/SimplePluginManager.java b/src/main/java/org/bukkit/plugin/SimplePluginManager.java
index b012ce40d82389c29d1b841ff685425ac10a7f9e..499dc8309a16b33d16b57b433c3c5b4330323717 100644
--- a/src/main/java/org/bukkit/plugin/SimplePluginManager.java
+++ b/src/main/java/org/bukkit/plugin/SimplePluginManager.java
@@ -585,7 +585,7 @@ public final class SimplePluginManager implements PluginManager {
}
try {
- server.getScheduler().cancelTasks(plugin);
+ //server.getScheduler().cancelTasks(plugin); // Folia - Bukkit scheduler not supported
} catch (Throwable ex) {
handlePluginException("Error occurred (in the plugin loader) while cancelling tasks for "
+ plugin.getDescription().getFullName() + " (Is it up to date?)", ex, plugin); // Paper

View File

@ -9427,15 +9427,14 @@ index 0000000000000000000000000000000000000000..5bf205d8c0a03ba932be85cc1a63d6ce
+}
diff --git a/src/main/java/io/papermc/paper/threadedregions/scheduler/FoliaEntityScheduler.java b/src/main/java/io/papermc/paper/threadedregions/scheduler/FoliaEntityScheduler.java
new file mode 100644
index 0000000000000000000000000000000000000000..b924146112006e85dfc12c2b303a2eecf23741ca
index 0000000000000000000000000000000000000000..c0b9f43fc53a5c9b3d9788f080e4fd3f4f94ebfe
--- /dev/null
+++ b/src/main/java/io/papermc/paper/threadedregions/scheduler/FoliaEntityScheduler.java
@@ -0,0 +1,39 @@
@@ -0,0 +1,38 @@
+package io.papermc.paper.threadedregions.scheduler;
+
+import ca.spottedleaf.concurrentutil.util.Validate;
+import org.bukkit.craftbukkit.entity.CraftEntity;
+import org.bukkit.entity.Entity;
+import org.bukkit.plugin.Plugin;
+import java.util.function.Consumer;
+import java.util.logging.Level;
@ -9448,13 +9447,13 @@ index 0000000000000000000000000000000000000000..b924146112006e85dfc12c2b303a2eec
+ this.entity = entity;
+ }
+
+ private static Consumer<? extends net.minecraft.world.entity.Entity> wrap(final Plugin plugin, final Consumer<? extends Entity> consumer) {
+ private static Consumer<? extends net.minecraft.world.entity.Entity> wrap(final Plugin plugin, final Runnable runnable) {
+ Validate.notNull(plugin, "Plugin may not be null");
+ Validate.notNull(consumer, "Consumer may not be null");
+ Validate.notNull(runnable, "Runnable may not be null");
+
+ return (final net.minecraft.world.entity.Entity nmsEntity) -> {
+ try {
+ ((Consumer<Entity>)consumer).accept(nmsEntity.getBukkitEntity());
+ runnable.run();
+ } catch (final Throwable throwable) {
+ plugin.getLogger().log(Level.WARNING, "Entity task for " + plugin.getDescription().getFullName() + " generated an exception", throwable);
+ }
@ -9462,7 +9461,7 @@ index 0000000000000000000000000000000000000000..b924146112006e85dfc12c2b303a2eec
+ }
+
+ @Override
+ public boolean execute(final Plugin plugin, final Consumer<? extends Entity> run, final Consumer<? extends Entity> retired,
+ public boolean execute(final Plugin plugin, final Runnable run, final Runnable retired,
+ final long delay) {
+ final Consumer<? extends net.minecraft.world.entity.Entity> runNMS = wrap(plugin, run);
+ final Consumer<? extends net.minecraft.world.entity.Entity> runRetired = retired == null ? null : wrap(plugin, retired);