From 01b59cf81914aeaf433397cfb3eacbec60216779 Mon Sep 17 00:00:00 2001
From: Spigot <noreply+git-spigot@papermc.io>
Date: Thu, 24 Jan 2013 09:33:34 +1100
Subject: [PATCH] Make the plugin classloader a bit more thread safe to prevent
 class not found errors when a class is loaded at the same time from another
 thread.

By: md_5 <md_5@live.com.au>
---
 ...-the-plugin-class-loader-thread-safe.patch | 65 +++++++++++++++++++
 1 file changed, 65 insertions(+)
 create mode 100644 Bukkit-Patches/0004-Make-the-plugin-class-loader-thread-safe.patch

diff --git a/Bukkit-Patches/0004-Make-the-plugin-class-loader-thread-safe.patch b/Bukkit-Patches/0004-Make-the-plugin-class-loader-thread-safe.patch
new file mode 100644
index 0000000000..bbefcfd086
--- /dev/null
+++ b/Bukkit-Patches/0004-Make-the-plugin-class-loader-thread-safe.patch
@@ -0,0 +1,65 @@
+From a62dabb02fe7405059ceb42495ed5dbc9704c1ea Mon Sep 17 00:00:00 2001
+From: snowleo <schneeleo@gmail.com>
+Date: Wed, 17 Oct 2012 22:30:45 +0200
+Subject: [PATCH] Make the plugin class loader thread safe
+
+---
+ .../org/bukkit/plugin/java/JavaPluginLoader.java    | 21 ++++++++++++---------
+ 1 file changed, 12 insertions(+), 9 deletions(-)
+
+diff --git a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
+index 10fc26a..f9a09ef 100644
+--- a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
++++ b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
+@@ -45,6 +45,7 @@ import org.bukkit.plugin.UnknownDependencyException;
+ import org.yaml.snakeyaml.error.YAMLException;
+ 
+ import com.google.common.collect.ImmutableList;
++import java.util.concurrent.ConcurrentHashMap;
+ 
+ /**
+  * Represents a Java plugin loader, allowing plugins in the form of .jar
+@@ -61,14 +62,14 @@ public class JavaPluginLoader implements PluginLoader {
+     @Deprecated
+     protected final Pattern[] fileFilters = fileFilters0;
+ 
+-    private final Map<String, Class<?>> classes0 = new HashMap<String, Class<?>>();
++    private final Map<String, Class<?>> classes0 = new ConcurrentHashMap<String, Class<?>>();
+     /**
+      * @deprecated Internal field that wasn't intended to be exposed
+      */
+     @Deprecated
+     protected final Map<String, Class<?>> classes = classes0;
+ 
+-    private final Map<String, PluginClassLoader> loaders0 = new LinkedHashMap<String, PluginClassLoader>();
++    private final Map<String, PluginClassLoader> loaders0 = new ConcurrentHashMap<String, PluginClassLoader>();
+     /**
+      * @deprecated Internal field that wasn't intended to be exposed
+      */
+@@ -293,14 +294,16 @@ public class JavaPluginLoader implements PluginLoader {
+         if (cachedClass != null) {
+             return cachedClass;
+         } else {
+-            for (String current : loaders0.keySet()) {
+-                PluginClassLoader loader = loaders0.get(current);
++            synchronized (loaders) {
++                for (String current : loaders0.keySet()) {
++                    PluginClassLoader loader = loaders0.get(current);
+ 
+-                try {
+-                    cachedClass = loader.extended ? loader.findClass(name, false) : loader.findClass0(name, false); // Don't warn on deprecation, but maintain overridability
+-                } catch (ClassNotFoundException cnfe) {}
+-                if (cachedClass != null) {
+-                    return cachedClass;
++                    try {
++                        cachedClass = loader.extended ? loader.findClass(name, false) : loader.findClass0(name, false); // Don't warn on deprecation, but maintain overridability
++                    } catch (ClassNotFoundException cnfe) {}
++                    if (cachedClass != null) {
++                        return cachedClass;
++                    }
+                 }
+             }
+         }
+-- 
+1.8.1-rc2
+