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);
}
}
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());
}
}
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);
}
}
Aggregations