diff --git a/src/main/java/com/willfp/eco/util/plugin/AbstractEcoPlugin.java b/src/main/java/com/willfp/eco/util/plugin/AbstractEcoPlugin.java index 84b02b82..0993aaf7 100644 --- a/src/main/java/com/willfp/eco/util/plugin/AbstractEcoPlugin.java +++ b/src/main/java/com/willfp/eco/util/plugin/AbstractEcoPlugin.java @@ -318,6 +318,9 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { this.getScheduler().cancelAll(); this.disable(); + + INSTANCES.remove(this.getClass()); + INSTANCES.remove(this.getClass(), this); } /** diff --git a/src/main/java/com/willfp/eco/util/proxy/ProxyFactory.java b/src/main/java/com/willfp/eco/util/proxy/ProxyFactory.java index 5e10b237..0d4e2719 100644 --- a/src/main/java/com/willfp/eco/util/proxy/ProxyFactory.java +++ b/src/main/java/com/willfp/eco/util/proxy/ProxyFactory.java @@ -4,20 +4,17 @@ import com.willfp.eco.util.internal.PluginDependent; import com.willfp.eco.util.plugin.AbstractEcoPlugin; import org.jetbrains.annotations.NotNull; -import java.util.IdentityHashMap; -import java.util.Map; - public class ProxyFactory extends PluginDependent { - /** - * Cached proxy implementations in order to not perform expensive reflective class-finding. - */ - private static final Map, AbstractProxy> CACHE = new IdentityHashMap<>(); - /** * The class of the proxy interface. */ private final Class proxyClass; + /** + * The instance of the proxy. + */ + private final T instance; + /** * Create a new Proxy Factory for a specific type. * @@ -28,6 +25,20 @@ public class ProxyFactory extends PluginDependent { @NotNull final Class proxyClass) { super(plugin); this.proxyClass = proxyClass; + + try { + String className = this.getPlugin().getProxyPackage() + "." + ProxyConstants.NMS_VERSION + "." + proxyClass.getSimpleName().replace("Proxy", ""); + final Class class2 = Class.forName(className); + Object instance = class2.getConstructor().newInstance(); + + if (proxyClass.isInstance(instance)) { + this.instance = proxyClass.cast(instance); + } else { + throw new UnsupportedVersionException("You're running an unsupported server version: " + ProxyConstants.NMS_VERSION); + } + } catch (Exception e) { + throw new UnsupportedVersionException("You're running an unsupported server version: " + ProxyConstants.NMS_VERSION); + } } /** @@ -36,37 +47,6 @@ public class ProxyFactory extends PluginDependent { * @return The proxy implementation. */ public @NotNull T getProxy() { - try { - T cachedProxy = attemptCache(); - if (cachedProxy != null) { - return cachedProxy; - } - - String className = this.getPlugin().getProxyPackage() + "." + ProxyConstants.NMS_VERSION + "." + proxyClass.getSimpleName().replace("Proxy", ""); - final Class class2 = Class.forName(className); - Object instance = class2.getConstructor().newInstance(); - if (proxyClass.isAssignableFrom(class2) && proxyClass.isInstance(instance)) { - T proxy = proxyClass.cast(instance); - CACHE.put(proxyClass, proxy); - return proxy; - } - } catch (Exception e) { - // If not returned, then throw error - } - - throw new UnsupportedVersionException("You're running an unsupported server version: " + ProxyConstants.NMS_VERSION); - } - - private T attemptCache() { - Object proxy = CACHE.get(proxyClass); - if (proxy == null) { - return null; - } - - if (proxyClass.isInstance(proxy)) { - return proxyClass.cast(proxy); - } - - return null; + return instance; } } diff --git a/src/main/java/com/willfp/eco/util/proxy/ProxyFactoryFactory.java b/src/main/java/com/willfp/eco/util/proxy/ProxyFactoryFactory.java index a683c265..6bc04d6e 100644 --- a/src/main/java/com/willfp/eco/util/proxy/ProxyFactoryFactory.java +++ b/src/main/java/com/willfp/eco/util/proxy/ProxyFactoryFactory.java @@ -3,8 +3,18 @@ package com.willfp.eco.util.proxy; import com.willfp.eco.util.internal.PluginDependent; import com.willfp.eco.util.plugin.AbstractEcoPlugin; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.util.HashMap; +import java.util.Map; + +@SuppressWarnings("unchecked") public class ProxyFactoryFactory extends PluginDependent { + /** + * Cached proxy implementations in order to not perform expensive reflective class-finding. + */ + private final Map, ProxyFactory> cache = new HashMap<>(); + /** * Pass an {@link AbstractEcoPlugin} in order to interface with it. * @@ -22,6 +32,35 @@ public class ProxyFactoryFactory extends PluginDependent { * @return The factory. */ public ProxyFactory getFactory(@NotNull final Class proxyClass) { - return new ProxyFactory<>(this.getPlugin(), proxyClass); + ProxyFactory cached = getCached(proxyClass); + if (cached == null) { + cache(proxyClass, new ProxyFactory<>(this.getPlugin(), proxyClass)); + } else { + return cached; + } + return getFactory(proxyClass); + } + + /** + * Cache proxy factory. + * + * @param proxyClass The class. + * @param factory The factory. + */ + public void cache(@NotNull final Class proxyClass, + @NotNull final ProxyFactory factory) { + cache.put(proxyClass, factory); + } + + /** + * Get cached proxy factory. + * + * @param proxyClass The class. + * @param The type of proxy. + * @return The factory. + */ + @Nullable + public ProxyFactory getCached(@NotNull final Class proxyClass) { + return (ProxyFactory) cache.get(proxyClass); } }