From 38d5d85fe4f93405cea99954b71bee2a15af045c Mon Sep 17 00:00:00 2001
From: Shane Freeder <theboyetronic@gmail.com>
Date: Fri, 15 Oct 2021 22:54:22 +0100
Subject: [PATCH] Clean plugin folder logic in extra-jars patch (Fixes #6347)
 (#6619)

---
 ...-option-to-load-extra-plugin-jars-no.patch | 59 +++++++++++++++----
 ...-option-to-load-extra-plugin-jars-no.patch | 32 ++++++++--
 2 files changed, 75 insertions(+), 16 deletions(-)

diff --git a/patches/api/Add-command-line-option-to-load-extra-plugin-jars-no.patch b/patches/api/Add-command-line-option-to-load-extra-plugin-jars-no.patch
index 88bfaae85c..7ecd99f7ba 100644
--- a/patches/api/Add-command-line-option-to-load-extra-plugin-jars-no.patch
+++ b/patches/api/Add-command-line-option-to-load-extra-plugin-jars-no.patch
@@ -6,18 +6,58 @@ Subject: [PATCH] Add command line option to load extra plugin jars not in the
 
 ex: java -jar paperclip.jar nogui -add-plugin=/path/to/plugin.jar -add-plugin=/path/to/another/plugin_jar.jar
 
+diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/org/bukkit/Bukkit.java
++++ b/src/main/java/org/bukkit/Bukkit.java
+@@ -0,0 +0,0 @@ public final class Bukkit {
+         return server.getCurrentTick();
+     }
+ 
++    /**
++     * Returns the de facto plugins directory, generally used for storing plugin jars to be loaded,
++     * as well as their {@link org.bukkit.plugin.Plugin#getDataFolder() data folders}.
++     *
++     * <p>Plugins should use {@link org.bukkit.plugin.Plugin#getDataFolder()} rather than traversing this
++     * directory manually when determining the location in which to store their data and configuration files.</p>
++     *
++     * @return plugins directory
++     */
++    @NotNull
++    public static File getPluginsFolder() {
++        return server.getPluginsFolder();
++    }
++
+     /**
+      * Checks if the server is in the process of being shutdown.
+      *
+diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/org/bukkit/Server.java
++++ b/src/main/java/org/bukkit/Server.java
+@@ -0,0 +0,0 @@ public interface Server extends PluginMessageRecipient, net.kyori.adventure.audi
+      */
+     int getCurrentTick();
+ 
++    /**
++     * Returns the de facto plugins directory, generally used for storing plugin jars to be loaded,
++     * as well as their {@link org.bukkit.plugin.Plugin#getDataFolder() data folders}.
++     *
++     * <p>Plugins should use {@link org.bukkit.plugin.Plugin#getDataFolder()} rather than traversing this
++     * directory manually when determining the location in which to store their data and configuration files.</p>
++     *
++     * @return plugins directory
++     */
++    @NotNull
++    File getPluginsFolder();
++
+     /**
+      * Checks if the server is in the process of being shutdown.
+      *
 diff --git a/src/main/java/org/bukkit/plugin/SimplePluginManager.java b/src/main/java/org/bukkit/plugin/SimplePluginManager.java
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
 --- a/src/main/java/org/bukkit/plugin/SimplePluginManager.java
 +++ b/src/main/java/org/bukkit/plugin/SimplePluginManager.java
-@@ -0,0 +0,0 @@ public final class SimplePluginManager implements PluginManager {
-     private final Map<String, Map<Permissible, Boolean>> permSubs = new HashMap<String, Map<Permissible, Boolean>>();
-     private final Map<Boolean, Map<Permissible, Boolean>> defSubs = new HashMap<Boolean, Map<Permissible, Boolean>>();
-     private boolean useTimings = false;
-+    private File pluginsDirectory; public @Nullable File pluginsDirectory() { return this.pluginsDirectory; } // Paper
- 
-     public SimplePluginManager(@NotNull Server instance, @NotNull SimpleCommandMap commandMap) {
-         server = instance;
 @@ -0,0 +0,0 @@ public final class SimplePluginManager implements PluginManager {
      @Override
      @NotNull
@@ -27,7 +67,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +    }
 +    @NotNull
 +    public Plugin[] loadPlugins(final @NotNull File directory, final @NotNull List<File> extraPluginJars) {
-+        this.pluginsDirectory = directory;
 +        // Paper end
          Validate.notNull(directory, "Directory cannot be null");
          Validate.isTrue(directory.isDirectory(), "Directory must be a directory");
@@ -136,7 +175,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
          }
  
 -        final File parentFile = file.getParentFile();
-+        final File parentFile = ((SimplePluginManager) this.server.getPluginManager()).pluginsDirectory(); // Paper
++        final File parentFile = this.server.getPluginsFolder(); // Paper
          final File dataFolder = new File(parentFile, description.getName());
          @SuppressWarnings("deprecation")
          final File oldDataFolder = new File(parentFile, description.getRawName());
diff --git a/patches/server/Add-command-line-option-to-load-extra-plugin-jars-no.patch b/patches/server/Add-command-line-option-to-load-extra-plugin-jars-no.patch
index c5210c96d3..ef220be4a0 100644
--- a/patches/server/Add-command-line-option-to-load-extra-plugin-jars-no.patch
+++ b/patches/server/Add-command-line-option-to-load-extra-plugin-jars-no.patch
@@ -11,8 +11,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
 +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
 @@ -0,0 +0,0 @@ public final class CraftServer implements Server {
+     public void loadPlugins() {
+         this.pluginManager.registerInterface(JavaPluginLoader.class);
  
-         File pluginFolder = (File) console.options.valueOf("plugins");
+-        File pluginFolder = (File) console.options.valueOf("plugins");
++        File pluginFolder = this.getPluginsFolder(); // Paper
  
 -        if (pluginFolder.exists()) {
 -            Plugin[] plugins = this.pluginManager.loadPlugins(pluginFolder);
@@ -31,14 +34,31 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
      }
  
 +    // Paper start
++    @Override
++    public File getPluginsFolder() {
++        return (File) this.console.options.valueOf("plugins");
++    }
++
 +    private List<File> extraPluginJars() {
 +        @SuppressWarnings("unchecked")
 +        final List<File> jars = (List<File>) this.console.options.valuesOf("add-plugin");
-+        return jars.stream()
-+            .filter(File::exists)
-+            .filter(File::isFile)
-+            .filter(file -> file.getName().endsWith(".jar"))
-+            .collect(java.util.stream.Collectors.toList());
++        final List<File> list = new ArrayList<>();
++        for (final File file : jars) {
++            if (!file.exists()) {
++                MinecraftServer.LOGGER.warn("File '{}' specified through 'add-plugin' argument does not exist, cannot load a plugin from it!", file.getAbsolutePath());
++                continue;
++            }
++            if (!file.isFile()) {
++                MinecraftServer.LOGGER.warn("File '{}' specified through 'add-plugin' argument is not a file, cannot load a plugin from it!", file.getAbsolutePath());
++                continue;
++            }
++            if (!file.getName().endsWith(".jar")) {
++                MinecraftServer.LOGGER.warn("File '{}' specified through 'add-plugin' argument is not a jar file, cannot load a plugin from it!", file.getAbsolutePath());
++                continue;
++            }
++            list.add(file);
++        }
++        return list;
 +    }
 +    // Paper end
 +