Search in sources :

Example 1 with PluginModule

use of org.spongepowered.common.inject.plugin.PluginModule in project SpongeCommon by SpongePowered.

the class JavaPluginLoader method loadPlugin.

@Override
public VanillaJavaPluginContainer loadPlugin(final Environment environment, final PluginCandidate<JVMPluginResource> candidate, final ClassLoader targetClassLoader) throws InvalidPluginException {
    final VanillaJavaPluginContainer container = new VanillaJavaPluginContainer(candidate);
    try {
        final String mainClass = container.metadata().entrypoint();
        final Class<?> pluginClass = Class.forName(mainClass, true, targetClassLoader);
        final Injector parentInjector = Launch.instance().lifecycle().platformInjector();
        final Object plugin;
        if (parentInjector != null) {
            final Injector childInjector = parentInjector.createChildInjector(new PluginModule(container, pluginClass));
            plugin = childInjector.getInstance(pluginClass);
        } else {
            plugin = pluginClass.newInstance();
        }
        container.initializeInstance(plugin);
        return container;
    } catch (final Exception ex) {
        throw new InvalidPluginException("An error occurred creating an instance of plugin '" + container.metadata().id() + "'!", ex);
    }
}
Also used : InvalidPluginException(org.spongepowered.plugin.InvalidPluginException) Injector(com.google.inject.Injector) PluginModule(org.spongepowered.common.inject.plugin.PluginModule) InvalidPluginException(org.spongepowered.plugin.InvalidPluginException)

Example 2 with PluginModule

use of org.spongepowered.common.inject.plugin.PluginModule in project SpongeCommon by SpongePowered.

the class PluginModContainer method constructPlugin.

private void constructPlugin() {
    final FMLModContainerAccessor accessor = (FMLModContainerAccessor) (Object) this;
    try {
        PluginModContainer.LOGGER.trace(Logging.LOADING, "Loading plugin instance {} of type {}", getModId(), accessor.accessor$modClass().getName());
        final Injector childInjector = Launch.instance().lifecycle().platformInjector().createChildInjector(new PluginModule((PluginContainer) (Object) this, accessor.accessor$modClass()));
        final Object instance = childInjector.getInstance(accessor.accessor$modClass());
        accessor.accessor$setModInstance(instance);
        ((ForgeEventManager) MinecraftForge.EVENT_BUS).registerListeners((PluginContainer) (Object) this, instance);
        PluginModContainer.LOGGER.trace(Logging.LOADING, "Loaded plugin instance {} of type {}", getModId(), accessor.accessor$modClass().getName());
    } catch (final Throwable e) {
        PluginModContainer.LOGGER.error(Logging.LOADING, "Failed to create plugin instance. PluginID: {}, class {}", getModId(), accessor.accessor$modClass().getName(), e);
        throw new ModLoadingException(this.modInfo, ModLoadingStage.CONSTRUCT, "fml.modloading.failedtoloadmod", e, accessor.accessor$modClass());
    }
    try {
        PluginModContainer.LOGGER.trace(Logging.LOADING, "Injecting Automatic event subscribers for {}", getModId());
        AutomaticEventSubscriber.inject(this, accessor.accessor$scanResults(), accessor.accessor$modClass().getClassLoader());
        PluginModContainer.LOGGER.trace(Logging.LOADING, "Completed Automatic event subscribers for {}", getModId());
    } catch (final Throwable e) {
        LOGGER.error(LOADING, "Failed to register automatic subscribers. PluginID: {}, class {}", getModId(), accessor.accessor$modClass().getName(), e);
        throw new ModLoadingException(this.modInfo, ModLoadingStage.CONSTRUCT, "fml.modloading.failedtoloadmod", e, accessor.accessor$modClass());
    }
}
Also used : FMLModContainerAccessor(org.spongepowered.forge.accessor.fml.javafmlmod.FMLModContainerAccessor) ModLoadingException(net.minecraftforge.fml.ModLoadingException) PluginContainer(org.spongepowered.plugin.PluginContainer) Injector(com.google.inject.Injector) ForgeEventManager(org.spongepowered.forge.launch.event.ForgeEventManager) PluginModule(org.spongepowered.common.inject.plugin.PluginModule)

Example 3 with PluginModule

use of org.spongepowered.common.inject.plugin.PluginModule in project SpongeForge by SpongePowered.

the class SpongeModPluginContainer method constructMod.

@Subscribe
public void constructMod(FMLConstructionEvent event) {
    try {
        if (this.invalid) {
            throw new InvalidPluginException();
        }
        // Add source file to classloader so we can load it.
        ModClassLoader modClassLoader = event.getModClassLoader();
        modClassLoader.addFile(getSource());
        modClassLoader.clearNegativeCacheFor(this.candidate.getClassList());
        Class<?> pluginClazz = Class.forName(this.className, true, modClassLoader);
        Injector injector = spongeInjector.getParent().createChildInjector(new PluginModule((PluginContainer) this, pluginClazz));
        this.injector = injector;
        this.instance = injector.getInstance(pluginClazz);
        // TODO: Detect Scala or use meta to know if we're scala and use proper adapter here...
        ProxyInjector.inject(this, event.getASMHarvestedData(), FMLCommonHandler.instance().getSide(), new ILanguageAdapter.JavaAdapter());
        Sponge.getEventManager().registerListeners(this, this.instance);
    } catch (Throwable t) {
        this.controller.errorOccurred(this, t);
    }
}
Also used : PluginContainer(org.spongepowered.api.plugin.PluginContainer) ModClassLoader(net.minecraftforge.fml.common.ModClassLoader) ProxyInjector(net.minecraftforge.fml.common.ProxyInjector) Injector(com.google.inject.Injector) ILanguageAdapter(net.minecraftforge.fml.common.ILanguageAdapter) PluginModule(org.spongepowered.common.inject.plugin.PluginModule) Subscribe(com.google.common.eventbus.Subscribe)

Aggregations

Injector (com.google.inject.Injector)3 PluginModule (org.spongepowered.common.inject.plugin.PluginModule)3 Subscribe (com.google.common.eventbus.Subscribe)1 ModLoadingException (net.minecraftforge.fml.ModLoadingException)1 ILanguageAdapter (net.minecraftforge.fml.common.ILanguageAdapter)1 ModClassLoader (net.minecraftforge.fml.common.ModClassLoader)1 ProxyInjector (net.minecraftforge.fml.common.ProxyInjector)1 PluginContainer (org.spongepowered.api.plugin.PluginContainer)1 FMLModContainerAccessor (org.spongepowered.forge.accessor.fml.javafmlmod.FMLModContainerAccessor)1 ForgeEventManager (org.spongepowered.forge.launch.event.ForgeEventManager)1 InvalidPluginException (org.spongepowered.plugin.InvalidPluginException)1 PluginContainer (org.spongepowered.plugin.PluginContainer)1