Plugin-scoped resources

This commit is contained in:
Riley Park 2024-07-18 18:14:02 -07:00
parent a8db527449
commit 49fc427fb8
2 changed files with 231 additions and 0 deletions

View file

@ -0,0 +1,115 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Riley Park <rileysebastianpark@gmail.com>
Date: Thu, 18 Jul 2024 18:12:26 -0700
Subject: [PATCH] Plugin-scoped resources
diff --git a/src/main/java/io/papermc/paper/plugin/event/PluginEventManager.java b/src/main/java/io/papermc/paper/plugin/event/PluginEventManager.java
new file mode 100644
index 0000000000000000000000000000000000000000..d3bdc9f98964c4437e4f0008353fe366c9d7ca86
--- /dev/null
+++ b/src/main/java/io/papermc/paper/plugin/event/PluginEventManager.java
@@ -0,0 +1,15 @@
+package io.papermc.paper.plugin.event;
+
+import org.bukkit.event.Listener;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * A plugin-scoped event manager.
+ */
+@ApiStatus.Experimental
+public interface PluginEventManager {
+ void register(final @NotNull Listener listener);
+
+ void unregister(final @NotNull Listener listener);
+}
diff --git a/src/main/java/io/papermc/paper/plugin/scheduler/PluginScheduler.java b/src/main/java/io/papermc/paper/plugin/scheduler/PluginScheduler.java
new file mode 100644
index 0000000000000000000000000000000000000000..201779c564e9e67ad5c216aef665f10068cff6d1
--- /dev/null
+++ b/src/main/java/io/papermc/paper/plugin/scheduler/PluginScheduler.java
@@ -0,0 +1,12 @@
+package io.papermc.paper.plugin.scheduler;
+
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * A plugin-scoped scheduler.
+ */
+@ApiStatus.Experimental
+public interface PluginScheduler {
+ void executeOnMainThread(final @NotNull Runnable runnable);
+}
diff --git a/src/main/java/org/bukkit/UnsafeValues.java b/src/main/java/org/bukkit/UnsafeValues.java
index c133564b8e8364f1eb7f0e82eed3b0add7f47351..3bb351e36f52e7030ba1e547b985865acca06191 100644
--- a/src/main/java/org/bukkit/UnsafeValues.java
+++ b/src/main/java/org/bukkit/UnsafeValues.java
@@ -283,4 +283,7 @@ public interface UnsafeValues {
<A extends Keyed, M> io.papermc.paper.registry.tag.@Nullable Tag<A> getTag(io.papermc.paper.registry.tag.@NotNull TagKey<A> tagKey); // Paper - hack to get tags for non-server backed registries
ItemStack createEmptyStack(); // Paper - proxy ItemStack
+
+ io.papermc.paper.plugin.event.PluginEventManager pluginScopedEventManager(final org.bukkit.plugin.Plugin plugin);
+ io.papermc.paper.plugin.scheduler.PluginScheduler pluginScopedScheduler(final org.bukkit.plugin.Plugin plugin);
}
diff --git a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java
index e0203f199700c397961a0667a79792497da7f796..f4d4dcc629d199aecfab9b1c6cf327cbee1186ae 100644
--- a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java
+++ b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java
@@ -2,6 +2,8 @@ package org.bukkit.plugin.java;
import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
+import io.papermc.paper.plugin.event.PluginEventManager;
+import io.papermc.paper.plugin.scheduler.PluginScheduler;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -48,6 +50,8 @@ public abstract class JavaPlugin extends PluginBase {
private FileConfiguration newConfig = null;
private File configFile = null;
private Logger logger = null; // Paper - PluginLogger -> Logger
+ private PluginEventManager eventManager; // Paper - plugin-scoped resources
+ private PluginScheduler scheduler; // Paper - plugin-scoped resources
// Paper start - lifecycle events
@SuppressWarnings("deprecation")
private final io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager<org.bukkit.plugin.Plugin> lifecycleEventManager = org.bukkit.Bukkit.getUnsafe().createPluginLifecycleEventManager(this, () -> this.allowsLifecycleRegistration);
@@ -299,10 +303,10 @@ public abstract class JavaPlugin extends PluginBase {
.orElseThrow();
}
public final void init(@NotNull PluginLoader loader, @NotNull Server server, @NotNull PluginDescriptionFile description, @NotNull File dataFolder, @NotNull File file, @NotNull ClassLoader classLoader) {
- init(server, description, dataFolder, file, classLoader, description, com.destroystokyo.paper.utils.PaperPluginLogger.getLogger(description));
+ init(server, description, dataFolder, file, classLoader, description, com.destroystokyo.paper.utils.PaperPluginLogger.getLogger(description), org.bukkit.Bukkit.getUnsafe().pluginScopedEventManager(this), org.bukkit.Bukkit.getUnsafe().pluginScopedScheduler(this));
this.pluginMeta = description;
}
- public final void init(@NotNull Server server, @NotNull PluginDescriptionFile description, @NotNull File dataFolder, @NotNull File file, @NotNull ClassLoader classLoader, @Nullable io.papermc.paper.plugin.configuration.PluginMeta configuration, @NotNull Logger logger) {
+ public final void init(@NotNull Server server, @NotNull PluginDescriptionFile description, @NotNull File dataFolder, @NotNull File file, @NotNull ClassLoader classLoader, @Nullable io.papermc.paper.plugin.configuration.PluginMeta configuration, @NotNull Logger logger, @NotNull PluginEventManager eventManager, @NotNull PluginScheduler scheduler) {
// Paper end
this.loader = DummyPluginLoaderImplHolder.INSTANCE; // Paper
this.server = server;
@@ -313,8 +317,22 @@ public abstract class JavaPlugin extends PluginBase {
this.configFile = new File(dataFolder, "config.yml");
this.pluginMeta = configuration; // Paper
this.logger = logger; // Paper
+ this.eventManager = eventManager; // Paper - plugin-scoped resources
+ this.scheduler = scheduler; // Paper - plugin-scoped resources
}
+ // Paper start - plugin-scoped resources
+ @org.jetbrains.annotations.ApiStatus.Experimental
+ public PluginEventManager getEventManager() {
+ return this.eventManager;
+ }
+
+ @org.jetbrains.annotations.ApiStatus.Experimental
+ public PluginScheduler getScheduler() {
+ return this.scheduler;
+ }
+ // Paper end - plugin-scoped resources
+
/**
* {@inheritDoc}
*/

View file

@ -0,0 +1,116 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Riley Park <rileysebastianpark@gmail.com>
Date: Thu, 18 Jul 2024 18:13:17 -0700
Subject: [PATCH] Plugin-scoped resources
diff --git a/src/main/java/io/papermc/paper/plugin/entrypoint/classloader/PaperPluginClassLoader.java b/src/main/java/io/papermc/paper/plugin/entrypoint/classloader/PaperPluginClassLoader.java
index 18401ede9cd1fc7094c6b74859929938e01795ca..b8eeedf0b2ba0c81d0e40383ff3a02cde973574d 100644
--- a/src/main/java/io/papermc/paper/plugin/entrypoint/classloader/PaperPluginClassLoader.java
+++ b/src/main/java/io/papermc/paper/plugin/entrypoint/classloader/PaperPluginClassLoader.java
@@ -1,12 +1,14 @@
package io.papermc.paper.plugin.entrypoint.classloader;
import io.papermc.paper.plugin.configuration.PluginMeta;
+import io.papermc.paper.plugin.event.PluginEventManagerImpl;
import io.papermc.paper.plugin.provider.entrypoint.DependencyContext;
import io.papermc.paper.plugin.provider.classloader.ConfiguredPluginClassLoader;
import io.papermc.paper.plugin.entrypoint.classloader.group.PaperPluginClassLoaderStorage;
import io.papermc.paper.plugin.provider.classloader.PaperClassLoaderStorage;
import io.papermc.paper.plugin.provider.classloader.PluginClassLoaderGroup;
import io.papermc.paper.plugin.provider.configuration.PaperPluginMeta;
+import io.papermc.paper.plugin.scheduler.PluginSchedulerImpl;
import org.bukkit.Bukkit;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
@@ -175,7 +177,7 @@ public class PaperPluginClassLoader extends PaperSimplePluginClassLoader impleme
File dataFolder = new File(Bukkit.getPluginsFolder(), pluginDescriptionFile.getName());
- plugin.init(Bukkit.getServer(), pluginDescriptionFile, dataFolder, this.source.toFile(), this, config, this.logger);
+ plugin.init(Bukkit.getServer(), pluginDescriptionFile, dataFolder, this.source.toFile(), this, config, this.logger, new PluginEventManagerImpl(plugin), new PluginSchedulerImpl(plugin));
this.loadedJavaPlugin = plugin;
}
diff --git a/src/main/java/io/papermc/paper/plugin/event/PluginEventManagerImpl.java b/src/main/java/io/papermc/paper/plugin/event/PluginEventManagerImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..2b034cd5f9b2f4a9a30b6ff7d9e4d52888e9846b
--- /dev/null
+++ b/src/main/java/io/papermc/paper/plugin/event/PluginEventManagerImpl.java
@@ -0,0 +1,27 @@
+package io.papermc.paper.plugin.event;
+
+import org.bukkit.Bukkit;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.Listener;
+import org.bukkit.plugin.Plugin;
+import org.bukkit.plugin.PluginManager;
+import org.jetbrains.annotations.NotNull;
+
+public final class PluginEventManagerImpl implements PluginEventManager {
+ private final PluginManager em = Bukkit.getServer().getPluginManager();
+ private final Plugin plugin;
+
+ public PluginEventManagerImpl(final Plugin plugin) {
+ this.plugin = plugin;
+ }
+
+ @Override
+ public void register(final @NotNull Listener listener) {
+ this.em.registerEvents(listener, this.plugin);
+ }
+
+ @Override
+ public void unregister(final @NotNull Listener listener) {
+ HandlerList.unregisterAll(listener);
+ }
+}
diff --git a/src/main/java/io/papermc/paper/plugin/scheduler/PluginSchedulerImpl.java b/src/main/java/io/papermc/paper/plugin/scheduler/PluginSchedulerImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..ca132f47bb617d441a7a69e745ad4909848157c0
--- /dev/null
+++ b/src/main/java/io/papermc/paper/plugin/scheduler/PluginSchedulerImpl.java
@@ -0,0 +1,20 @@
+package io.papermc.paper.plugin.scheduler;
+
+import org.bukkit.Bukkit;
+import org.bukkit.plugin.Plugin;
+import org.bukkit.scheduler.BukkitScheduler;
+import org.jetbrains.annotations.NotNull;
+
+public final class PluginSchedulerImpl implements PluginScheduler {
+ private final BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
+ private final Plugin plugin;
+
+ public PluginSchedulerImpl(final Plugin plugin) {
+ this.plugin = plugin;
+ }
+
+ @Override
+ public void executeOnMainThread(final @NotNull Runnable runnable) {
+ this.scheduler.runTask(this.plugin, runnable);
+ }
+}
diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
index 28b3ad04e1f4b38ce21084864a59a2cd81b3992c..be01657f6a2c87b8f8bdf3c1086afa03fb4d6587 100644
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
@@ -684,6 +684,18 @@ public final class CraftMagicNumbers implements UnsafeValues {
}
// Paper end - proxy ItemStack
+ // Paper start - plugin-scoped resources
+ @Override
+ public io.papermc.paper.plugin.event.PluginEventManager pluginScopedEventManager(final org.bukkit.plugin.Plugin plugin) {
+ return new io.papermc.paper.plugin.event.PluginEventManagerImpl(plugin);
+ }
+
+ @Override
+ public io.papermc.paper.plugin.scheduler.PluginScheduler pluginScopedScheduler(final org.bukkit.plugin.Plugin plugin) {
+ return new io.papermc.paper.plugin.scheduler.PluginSchedulerImpl(plugin);
+ }
+ // Paper end - plugin-scoped resources
+
/**
* This helper class represents the different NBT Tags.
* <p>