Small paper plugin fixes (#8866)

Co-authored-by: Bjarne Koll <git@lynxplay.dev>
Co-authored-by: Jake Potrebic <jake.m.potrebic@gmail.com>
This commit is contained in:
Owen1212055
2023-02-20 13:02:53 -05:00
parent 0ce11aca74
commit 1c5cb2f43a
3 changed files with 170 additions and 137 deletions

View File

@@ -464,6 +464,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+package io.papermc.paper.plugin;
+
+import com.mojang.logging.LogUtils;
+import io.papermc.paper.configuration.PaperConfigurations;
+import joptsimple.OptionSet;
+import org.bukkit.configuration.file.YamlConfiguration;
+import org.jetbrains.annotations.NotNull;
@@ -482,45 +483,54 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ private final Path pluginDirectory;
+ private final Path updateDirectory;
+
+ PluginInitializerManager(@NotNull OptionSet minecraftOptionSet) {
+ // We have to load the bukkit configuration inorder to get the update folder location.
+ File configFileLocationBukkit = (File) minecraftOptionSet.valueOf("bukkit-settings");
+ this.pluginDirectory = ((File) minecraftOptionSet.valueOf("plugins")).toPath();
+
+ String updateDirectory = YamlConfiguration.loadConfiguration(configFileLocationBukkit).getString("settings.update-folder", "update");
+ if (updateDirectory.isBlank()) {
+ this.updateDirectory = null;
+ } else {
+ Path resolvedUpdateDirectory = this.pluginDirectory.resolve(updateDirectory);
+ if (!Files.isDirectory(resolvedUpdateDirectory)) {
+ this.updateDirectory = null;
+ return;
+ }
+
+ boolean isSameFile = true;
+ try {
+ isSameFile = Files.isSameFile(resolvedUpdateDirectory, this.pluginDirectory);
+ } catch (IOException e) {
+ LOGGER.error("Misconfigured update directory!");
+ LOGGER.error("Failed to compare update/plugin directory", e);
+ }
+
+ if (isSameFile) {
+ LOGGER.error("Misconfigured update directory!");
+ LOGGER.error(("Your configured update directory (%s) in bukkit.yml is pointing to the same location as the plugin directory (%s). " +
+ "Disabling auto updating functionality.").formatted(resolvedUpdateDirectory, this.pluginDirectory));
+
+ this.updateDirectory = null;
+ } else {
+ this.updateDirectory = resolvedUpdateDirectory;
+ }
+
+ }
+
+ PluginInitializerManager(final Path pluginDirectory, final Path updateDirectory) {
+ this.pluginDirectory = pluginDirectory;
+ this.updateDirectory = updateDirectory;
+ }
+
+ public static PluginInitializerManager init(OptionSet optionSet) {
+ impl = new PluginInitializerManager(optionSet);
+ private static PluginInitializerManager parse(@NotNull final OptionSet minecraftOptionSet) throws Exception {
+ // We have to load the bukkit configuration inorder to get the update folder location.
+ final File configFileLocationBukkit = (File) minecraftOptionSet.valueOf("bukkit-settings");
+
+ final Path pluginDirectory = ((File) minecraftOptionSet.valueOf("plugins")).toPath();
+
+ final YamlConfiguration configuration = PaperConfigurations.loadLegacyConfigFile(configFileLocationBukkit);
+
+ final String updateDirectoryName = configuration.getString("settings.update-folder", "update");
+ if (updateDirectoryName.isBlank()) {
+ return new PluginInitializerManager(pluginDirectory, null);
+ }
+
+ final Path resolvedUpdateDirectory = pluginDirectory.resolve(updateDirectoryName);
+ if (!Files.isDirectory(resolvedUpdateDirectory)) {
+ LOGGER.error("Misconfigured update directory!");
+ LOGGER.error(("Your configured update directory (%s) in bukkit.yml is pointing to a non-directory path. " +
+ "Disabling auto updating functionality.").formatted(resolvedUpdateDirectory));
+ return new PluginInitializerManager(pluginDirectory, null);
+ }
+
+ boolean isSameFile;
+ try {
+ isSameFile = Files.isSameFile(resolvedUpdateDirectory, pluginDirectory);
+ } catch (final IOException e) {
+ LOGGER.error("Misconfigured update directory!");
+ LOGGER.error("Failed to compare update/plugin directory", e);
+ return new PluginInitializerManager(pluginDirectory, null);
+ }
+
+ if (isSameFile) {
+ LOGGER.error("Misconfigured update directory!");
+ LOGGER.error(("Your configured update directory (%s) in bukkit.yml is pointing to the same location as the plugin directory (%s). " +
+ "Disabling auto updating functionality.").formatted(resolvedUpdateDirectory, pluginDirectory));
+
+ return new PluginInitializerManager(pluginDirectory, null);
+ }
+
+ return new PluginInitializerManager(pluginDirectory, resolvedUpdateDirectory);
+ }
+
+ public static PluginInitializerManager init(final OptionSet optionSet) throws Exception {
+ impl = parse(optionSet);
+ return impl;
+ }
+
@@ -2069,8 +2079,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+
+ @Override
+ public List<T> loadProviders(List<PluginProvider<T>> providers) {
+ List<T> javapluginsLoaded = new ArrayList<>();
+ public List<ProviderPair<T>> loadProviders(List<PluginProvider<T>> providers) {
+ List<ProviderPair<T>> javapluginsLoaded = new ArrayList<>();
+ MutableGraph<String> dependencyGraph = GraphBuilder.directed().build();
+ GraphDependencyContext dependencyContext = new GraphDependencyContext(dependencyGraph);
+
@@ -2240,7 +2250,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ if (this.configuration.load(file, loadedPlugin)) {
+ loadedPlugins.add(file.getMeta().getName());
+ loadedPlugins.addAll(file.getMeta().getProvidedPlugins());
+ javapluginsLoaded.add(loadedPlugin);
+ javapluginsLoaded.add(new ProviderPair<>(file, loadedPlugin));
+ }
+
+ } catch (Exception ex) {
@@ -2271,7 +2281,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ if (this.configuration.load(file, loadedPlugin)) {
+ loadedPlugins.add(file.getMeta().getName());
+ loadedPlugins.addAll(file.getMeta().getProvidedPlugins());
+ javapluginsLoaded.add(loadedPlugin);
+ javapluginsLoaded.add(new ProviderPair<>(file, loadedPlugin));
+ }
+ break;
+ } catch (Exception ex) {
@@ -2332,7 +2342,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+
+ @Override
+ public List<T> loadProviders(List<PluginProvider<T>> pluginProviders) {
+ public List<ProviderPair<T>> loadProviders(List<PluginProvider<T>> pluginProviders) {
+ MutableGraph<String> dependencyGraph = GraphBuilder.directed().build();
+ Map<String, PluginProviderEntry<T>> providerMap = new HashMap<>();
+ List<PluginProvider<T>> validatedProviders = new ArrayList<>();
@@ -2408,7 +2418,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+
+ GraphDependencyContext graphDependencyContext = new GraphDependencyContext(dependencyGraph);
+ List<T> loadedPlugins = new ArrayList<>();
+ List<ProviderPair<T>> loadedPlugins = new ArrayList<>();
+ for (String providerIdentifier : reversedTopographicSort) {
+ // It's possible that this will be null because the above dependencies for soft/load before aren't validated if they exist.
+ // The graph could be MutableGraph<PluginProvider<T>>, but we would have to check if each dependency exists there... just
@@ -2426,7 +2436,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+
+ T instance = retrievedProvider.createInstance();
+ if (this.configuration.load(retrievedProvider, instance)) {
+ loadedPlugins.add(instance);
+ loadedPlugins.add(new ProviderPair<>(retrievedProvider, instance));
+ }
+ } catch (Exception ex) {
+ LOGGER.error("Could not load plugin '%s' in folder '%s'".formatted(retrievedProvider.getFileName(), retrievedProvider.getParentSource()), ex); // Paper
@@ -2523,7 +2533,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ */
+public interface ProviderLoadingStrategy<P> {
+
+ List<P> loadProviders(List<PluginProvider<P>> providers);
+ List<ProviderPair<P>> loadProviders(List<PluginProvider<P>> providers);
+
+ record ProviderPair<P>(PluginProvider<P> provider, P provided) {
+
+ }
+}
diff --git a/src/main/java/io/papermc/paper/plugin/entrypoint/strategy/TopographicGraphSorter.java b/src/main/java/io/papermc/paper/plugin/entrypoint/strategy/TopographicGraphSorter.java
new file mode 100644
@@ -2656,7 +2670,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+
+ try {
+ return new PaperPluginClassLoader(logger, source, jarFile, configuration, this.getClass().getClassLoader(), new URLClassLoader(urls));
+ return new PaperPluginClassLoader(logger, source, jarFile, configuration, this.getClass().getClassLoader(), new URLClassLoader(urls, getClass().getClassLoader()));
+ } catch (IOException exception) {
+ throw new RuntimeException(exception);
+ }
@@ -2689,6 +2703,69 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ return this.paths;
+ }
+}
diff --git a/src/main/java/io/papermc/paper/plugin/manager/DummyBukkitPluginLoader.java b/src/main/java/io/papermc/paper/plugin/manager/DummyBukkitPluginLoader.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/io/papermc/paper/plugin/manager/DummyBukkitPluginLoader.java
@@ -0,0 +0,0 @@
+package io.papermc.paper.plugin.manager;
+
+import org.bukkit.Bukkit;
+import org.bukkit.event.Event;
+import org.bukkit.event.Listener;
+import org.bukkit.plugin.InvalidDescriptionException;
+import org.bukkit.plugin.InvalidPluginException;
+import org.bukkit.plugin.Plugin;
+import org.bukkit.plugin.PluginDescriptionFile;
+import org.bukkit.plugin.PluginLoader;
+import org.bukkit.plugin.RegisteredListener;
+import org.bukkit.plugin.UnknownDependencyException;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+
+import java.io.File;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+/**
+ * A purely internal type that implements the now deprecated {@link PluginLoader} after the implementation
+ * of papers new plugin system.
+ */
+@ApiStatus.Internal
+public class DummyBukkitPluginLoader implements PluginLoader {
+
+ @Override
+ public @NotNull Plugin loadPlugin(@NotNull File file) throws InvalidPluginException, UnknownDependencyException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public @NotNull PluginDescriptionFile getPluginDescription(@NotNull File file) throws InvalidDescriptionException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public @NotNull Pattern[] getPluginFileFilters() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public @NotNull Map<Class<? extends Event>, Set<RegisteredListener>> createRegisteredListeners(@NotNull Listener listener, @NotNull Plugin plugin) {
+ return PaperPluginManagerImpl.getInstance().paperEventManager.createRegisteredListeners(listener, plugin);
+ }
+
+ @Override
+ public void enablePlugin(@NotNull Plugin plugin) {
+ Bukkit.getPluginManager().enablePlugin(plugin);
+ }
+
+ @Override
+ public void disablePlugin(@NotNull Plugin plugin) {
+ Bukkit.getPluginManager().disablePlugin(plugin);
+ }
+}
diff --git a/src/main/java/io/papermc/paper/plugin/manager/MultiRuntimePluginProviderStorage.java b/src/main/java/io/papermc/paper/plugin/manager/MultiRuntimePluginProviderStorage.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
@@ -2729,8 +2806,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+
+ @Override
+ public void processProvided(JavaPlugin provided) {
+ super.processProvided(provided);
+ public void processProvided(PluginProvider<JavaPlugin> provider, JavaPlugin provided) {
+ super.processProvided(provider, provided);
+ this.provided.add(provided);
+ }
+
@@ -3548,9 +3625,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+
+public class PaperPluginManagerImpl implements PluginManager, DependencyContext {
+
+ private final PaperPluginInstanceManager instanceManager;
+ private final PaperEventManager paperEventManager;
+ private PermissionManager permissionManager;
+ final PaperPluginInstanceManager instanceManager;
+ final PaperEventManager paperEventManager;
+ PermissionManager permissionManager;
+
+ public PaperPluginManagerImpl(Server server, CommandMap commandMap, @Nullable SimplePluginManager permissionManager) {
+ this.instanceManager = new PaperPluginInstanceManager(this, commandMap, server);
@@ -3882,8 +3959,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ }
+
+ @Override
+ public void processProvided(JavaPlugin provided) {
+ super.processProvided(provided);
+ public void processProvided(PluginProvider<JavaPlugin> provider, JavaPlugin provided) {
+ super.processProvided(provider, provided);
+ this.singleLoaded = provided;
+ }
+
@@ -5761,18 +5838,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+
+ @Override
+ public boolean load(PluginProvider<JavaPlugin> provider, JavaPlugin provided) {
+ try {
+ provided.getLogger().info(String.format("Loading server plugin %s", provided.getPluginMeta().getDisplayName()));
+ PaperPluginManagerImpl.getInstance().loadPlugin(provided); // We have to add it to the map before the plugin is loaded
+ provided.onLoad();
+ return true;
+ } catch (Throwable ex) {
+ if (provider instanceof ProviderStatusHolder statusHolder) {
+ statusHolder.setStatus(ProviderStatus.ERRORED);
+ }
+ LOGGER.error("Could not load server plugin '%s' in folder '%s' (Is it up to date?)".formatted(provider.getFileName(), provider.getParentSource()), ex);
+ return false;
+ }
+ // Add it to the map here, we have to run the actual loading logic later.
+ PaperPluginManagerImpl.getInstance().loadPlugin(provided);
+ return true;
+ }
+
+ @Override
@@ -5801,6 +5869,18 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ pluginProviders.removeIf((provider) -> (provider instanceof PaperPluginParent.PaperServerPluginProvider pluginProvider && pluginProvider.shouldSkipCreation()));
+ }
+
+ // We need to call the load methods AFTER all plugins are constructed
+ @Override
+ public void processProvided(PluginProvider<JavaPlugin> provider, JavaPlugin provided) {
+ try {
+ provided.getLogger().info(String.format("Loading server plugin %s", provided.getPluginMeta().getDisplayName()));
+ provided.onLoad();
+ } catch (Throwable ex) {
+ // Don't mark that provider as ERRORED, as this apparently still needs to run the onEnable logic.
+ provided.getSLF4JLogger().error("Error initializing plugin '%s' in folder '%s' (Is it up to date?)".formatted(provider.getFileName(), provider.getParentSource()), ex);
+ }
+ }
+
+ @Override
+ public String toString() {
+ return "PLUGIN:" + super.toString();
@@ -5841,8 +5921,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ this.filterLoadingProviders(providerList);
+
+ try {
+ for (T plugin : this.strategy.loadProviders(providerList)) {
+ this.processProvided(plugin);
+ for (ProviderLoadingStrategy.ProviderPair<T> providerPair : this.strategy.loadProviders(providerList)) {
+ this.processProvided(providerPair.provider(), providerPair.provided());
+ }
+ } catch (PluginGraphCycleException exception) {
+ this.handleCycle(exception);
@@ -5854,7 +5934,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ return this.providers;
+ }
+
+ public void processProvided(T provided) {}
+ public void processProvided(PluginProvider<T> provider, T provided) {}
+
+ // Mutable enter
+ protected void filterLoadingProviders(List<PluginProvider<T>> providers) {}
@@ -6157,6 +6237,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+++ b/src/main/resources/META-INF/services/io.papermc.paper.plugin.provider.classloader.PaperClassLoaderStorage
@@ -0,0 +1 @@
+io.papermc.paper.plugin.entrypoint.classloader.group.PaperPluginClassLoaderStorage
diff --git a/src/main/resources/META-INF/services/org.bukkit.plugin.PluginLoader b/src/main/resources/META-INF/services/org.bukkit.plugin.PluginLoader
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/resources/META-INF/services/org.bukkit.plugin.PluginLoader
@@ -0,0 +1 @@
+io.papermc.paper.plugin.manager.DummyBukkitPluginLoader
diff --git a/src/test/java/io/papermc/paper/plugin/PaperTestPlugin.java b/src/test/java/io/papermc/paper/plugin/PaperTestPlugin.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000