From 5ff87e597feaa69c51c18d66c24e3166b01867db Mon Sep 17 00:00:00 2001
From: Riley Park <rileysebastianpark@gmail.com>
Date: Sat, 20 Jul 2024 13:00:29 -0700
Subject: [PATCH] Properly lookup plugin classes when looked up by spark

fixes #11124
---
 patches/server/Bundle-spark.patch | 80 ++++++++++++++++++++++++++++++-
 1 file changed, 79 insertions(+), 1 deletion(-)

diff --git a/patches/server/Bundle-spark.patch b/patches/server/Bundle-spark.patch
index 24857b754e..8b441d6d65 100644
--- a/patches/server/Bundle-spark.patch
+++ b/patches/server/Bundle-spark.patch
@@ -28,6 +28,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +package io.papermc.paper;
 +
 +import io.papermc.paper.configuration.GlobalConfiguration;
++import io.papermc.paper.plugin.ClassLoaderStorage;
++import io.papermc.paper.plugin.entrypoint.classloader.PaperSimplePluginClassLoader;
 +import io.papermc.paper.util.MCUtil;
 +import java.util.List;
 +import java.util.logging.Level;
@@ -42,6 +44,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +import org.bukkit.command.Command;
 +import org.bukkit.command.CommandSender;
 +import org.bukkit.craftbukkit.CraftServer;
++import org.bukkit.plugin.java.PluginClassLoader;
 +
 +// It's like electricity.
 +public final class SparksFly {
@@ -84,7 +87,25 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +        }, new PaperClassLookup() {
 +            @Override
 +            public Class<?> lookup(final String className) throws Exception {
-+                return Class.forName(className);
++                try {
++                    return Class.forName(className);
++                } catch (final Exception e) {
++                    for (final PaperSimplePluginClassLoader loader : ClassLoaderStorage.MODERN_LOADERS) {
++                        final Class<?> loadedClass = loader.loadClass(className);
++                        if (loadedClass != null) {
++                            return loadedClass;
++                        }
++                    }
++                    for (final PluginClassLoader loader : ClassLoaderStorage.LEGACY_LOADERS) {
++                        final Class<?> loadedClass = loader.loadClass(className, true, false, true);
++                        if (loadedClass != null) {
++                            return loadedClass;
++                        }
++                    }
++                    final ClassNotFoundException exception = new ClassNotFoundException("Could not find class " + className);
++                    exception.addSuppressed(e);
++                    throw exception;
++                }
 +            }
 +        });
 +    }
@@ -204,6 +225,43 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +        }
 +    }
 +}
+diff --git a/src/main/java/io/papermc/paper/plugin/ClassLoaderStorage.java b/src/main/java/io/papermc/paper/plugin/ClassLoaderStorage.java
+new file mode 100644
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
+--- /dev/null
++++ b/src/main/java/io/papermc/paper/plugin/ClassLoaderStorage.java
+@@ -0,0 +0,0 @@
++package io.papermc.paper.plugin;
++
++import io.papermc.paper.plugin.entrypoint.classloader.PaperSimplePluginClassLoader;
++import java.util.HashSet;
++import java.util.Set;
++import org.bukkit.plugin.java.PluginClassLoader;
++
++public final class ClassLoaderStorage {
++    public static final Set<PaperSimplePluginClassLoader> MODERN_LOADERS = new HashSet<>();
++    public static final Set<PluginClassLoader> LEGACY_LOADERS = new HashSet<>();
++}
+diff --git a/src/main/java/io/papermc/paper/plugin/entrypoint/classloader/PaperSimplePluginClassLoader.java b/src/main/java/io/papermc/paper/plugin/entrypoint/classloader/PaperSimplePluginClassLoader.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/io/papermc/paper/plugin/entrypoint/classloader/PaperSimplePluginClassLoader.java
++++ b/src/main/java/io/papermc/paper/plugin/entrypoint/classloader/PaperSimplePluginClassLoader.java
+@@ -0,0 +0,0 @@
+ package io.papermc.paper.plugin.entrypoint.classloader;
+ 
++import io.papermc.paper.plugin.ClassLoaderStorage;
+ import io.papermc.paper.plugin.configuration.PluginMeta;
+ import io.papermc.paper.plugin.util.NamespaceChecker;
+ import org.jetbrains.annotations.ApiStatus;
+@@ -0,0 +0,0 @@ public class PaperSimplePluginClassLoader extends URLClassLoader {
+         this.jarUrl = source.toUri().toURL();
+         this.configuration = configuration;
+         this.jar = file;
++
++        ClassLoaderStorage.MODERN_LOADERS.add(this); // Paper - spark
+     }
+ 
+     @Override
 diff --git a/src/main/java/io/papermc/paper/plugin/provider/source/FileProviderSource.java b/src/main/java/io/papermc/paper/plugin/provider/source/FileProviderSource.java
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
 --- a/src/main/java/io/papermc/paper/plugin/provider/source/FileProviderSource.java
@@ -246,6 +304,26 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
          type.register(entrypointHandler, file, context);
      }
  
+diff --git a/src/main/java/io/papermc/paper/plugin/provider/type/spigot/SpigotPluginProvider.java b/src/main/java/io/papermc/paper/plugin/provider/type/spigot/SpigotPluginProvider.java
+index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
+--- a/src/main/java/io/papermc/paper/plugin/provider/type/spigot/SpigotPluginProvider.java
++++ b/src/main/java/io/papermc/paper/plugin/provider/type/spigot/SpigotPluginProvider.java
+@@ -0,0 +0,0 @@ package io.papermc.paper.plugin.provider.type.spigot;
+ 
+ import com.destroystokyo.paper.util.SneakyThrow;
+ import com.destroystokyo.paper.utils.PaperPluginLogger;
++import io.papermc.paper.plugin.ClassLoaderStorage;
+ import io.papermc.paper.plugin.manager.PaperPluginManagerImpl;
+ import io.papermc.paper.plugin.provider.configuration.LoadOrderConfiguration;
+ import io.papermc.paper.plugin.provider.entrypoint.DependencyContext;
+@@ -0,0 +0,0 @@ public class SpigotPluginProvider implements PluginProvider<JavaPlugin>, Provide
+             // We must provide a temporary context in order to properly handle dependencies on the plugin classloader constructor.
+             loader.dependencyContext = PaperPluginManagerImpl.getInstance();
+ 
++            ClassLoaderStorage.LEGACY_LOADERS.add(loader); // Paper - spark
+ 
+             this.status = ProviderStatus.INITIALIZED;
+             return loader.getPlugin();
 diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
 --- a/src/main/java/net/minecraft/server/MinecraftServer.java