// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.core.plugin.pending;

import com.hypixel.hytale.server.core.plugin.PluginBase;
import java.lang.reflect.Constructor;
import java.util.logging.Level;
import com.hypixel.hytale.server.core.plugin.PluginManager;
import com.hypixel.hytale.server.core.plugin.JavaPluginInit;
import com.hypixel.hytale.server.core.plugin.JavaPlugin;
import com.hypixel.hytale.common.plugin.PluginManifest;
import javax.annotation.Nullable;
import java.nio.file.Path;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.plugin.PluginClassLoader;
import com.hypixel.hytale.logger.HytaleLogger;

public class PendingLoadJavaPlugin extends PendingLoadPlugin
{
    private static final HytaleLogger LOGGER;
    @Nonnull
    private final PluginClassLoader urlClassLoader;
    
    public PendingLoadJavaPlugin(@Nullable final Path path, @Nonnull final PluginManifest manifest, @Nonnull final PluginClassLoader urlClassLoader) {
        super(path, manifest);
        this.urlClassLoader = urlClassLoader;
    }
    
    @Nonnull
    @Override
    public PendingLoadPlugin createSubPendingLoadPlugin(@Nonnull final PluginManifest manifest) {
        return new PendingLoadJavaPlugin(this.getPath(), manifest, this.urlClassLoader);
    }
    
    @Override
    public boolean isInServerClassPath() {
        return this.urlClassLoader.isInServerClassPath();
    }
    
    @Nullable
    @Override
    public JavaPlugin load() {
        try {
            final PluginManifest manifest = this.getManifest();
            final Class<?> mainClass = this.urlClassLoader.loadLocalClass(manifest.getMain());
            if (JavaPlugin.class.isAssignableFrom(mainClass)) {
                final Constructor<?> constructor = mainClass.getConstructor(JavaPluginInit.class);
                final Path dataDirectory = PluginManager.MODS_PATH.resolve(manifest.getGroup() + "_" + manifest.getName());
                final JavaPluginInit init = new JavaPluginInit(manifest, dataDirectory, this.getPath(), this.urlClassLoader);
                return (JavaPlugin)constructor.newInstance(init);
            }
            throw new ClassCastException(manifest.getMain() + " does not extend JavaPlugin");
        }
        catch (final ClassNotFoundException e) {
            PendingLoadJavaPlugin.LOGGER.at(Level.SEVERE).withCause(e).log("Failed to load plugin %s. Failed to find main class!", this.getPath());
        }
        catch (final NoSuchMethodException e2) {
            PendingLoadJavaPlugin.LOGGER.at(Level.SEVERE).withCause(e2).log("Failed to load plugin %s. Requires default constructor!", this.getPath());
        }
        catch (final Throwable e3) {
            PendingLoadJavaPlugin.LOGGER.at(Level.SEVERE).withCause(e3).log("Failed to load plugin %s", this.getPath());
        }
        return null;
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "PendingLoadJavaPlugin{" + super.toString();
    }
    
    static {
        LOGGER = HytaleLogger.forEnclosingClass();
    }
}
