Search in sources :

Example 6 with PluginContainer

use of org.spongepowered.plugin.PluginContainer 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 7 with PluginContainer

use of org.spongepowered.plugin.PluginContainer in project SpongeCommon by SpongePowered.

the class SpongeServiceProvider method attemptRegistration.

@Nullable
private <T> Registration<T> attemptRegistration(final Collection<PluginContainer> pluginContainers, final Service<T> service) {
    Registration<T> registration = null;
    final Iterator<PluginContainer> pluginContainerIterator = pluginContainers.iterator();
    while (registration == null && pluginContainerIterator.hasNext()) {
        final PluginContainer pluginContainer = pluginContainerIterator.next();
        if (!Launch.instance().launcherPlugins().contains(pluginContainer)) {
            // If this succeeds, the while loop will end.
            registration = this.getSpecificRegistration(pluginContainer, service);
        }
    }
    return registration;
}
Also used : PluginContainer(org.spongepowered.plugin.PluginContainer) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 8 with PluginContainer

use of org.spongepowered.plugin.PluginContainer in project SpongeCommon by SpongePowered.

the class SnapshotGenerationTest method init.

@Before
public void init() {
    PluginManager manager = Mockito.mock(PluginManager.class);
    this.eventManager = new SpongeEventManager(this.logger, manager);
    try {
        Field field = Timings.class.getDeclaredField("factory");
        field.setAccessible(true);
        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
        field.set(null, Mockito.mock(TimingsFactory.class));
    } catch (IllegalAccessException | NoSuchFieldException e) {
        e.printStackTrace();
    }
    this.plugin = new Object();
    PluginContainer container = Mockito.mock(PluginContainer.class);
    Mockito.when(manager.fromInstance(this.plugin)).thenReturn(Optional.of(container));
    Cause cause = Cause.of(EventContext.empty(), this);
    this.entity = Mockito.mock(Entity.class, withSettings().defaultAnswer(Mockito.RETURNS_MOCKS));
    this.event = SpongeEventFactory.createSpawnEntityEvent(cause, Lists.newArrayList(this.entity));
    Game game = mock(Game.class);
    CauseStackManager csm = mock(CauseStackManager.class);
    Mockito.when(game.getCauseStackManager()).thenReturn(csm);
}
Also used : Entity(org.spongepowered.api.entity.Entity) PluginContainer(org.spongepowered.plugin.PluginContainer) PluginManager(org.spongepowered.api.plugin.PluginManager) Field(java.lang.reflect.Field) Game(org.spongepowered.api.Game) CauseStackManager(org.spongepowered.api.event.CauseStackManager) TimingsFactory(co.aikar.timings.TimingsFactory) Cause(org.spongepowered.api.event.cause.Cause) Before(org.junit.Before)

Example 9 with PluginContainer

use of org.spongepowered.plugin.PluginContainer in project SpongeCommon by SpongePowered.

the class TestImplementationModule method configure.

@Override
protected void configure() {
    super.configure();
    this.bind(Server.class).to(TestServer.class);
    this.bind(SpongeGame.class).to(TestGame.class);
    Platform platform = mock(Platform.class);
    when(platform.getExecutionType()).thenReturn(Platform.Type.SERVER);
    PluginContainer mock = mock(PluginContainer.class);
    when(platform.getContainer(any())).thenReturn(mock);
    this.bind(Platform.class).toInstance(platform);
    PluginManager manager = mock(PluginManager.class);
    when(mock.getId()).thenReturn("sponge");
    when(manager.getPlugin(anyString())).thenReturn(Optional.of(mock));
    when(manager.fromInstance(any())).thenReturn(Optional.of(mock));
    this.bind(PluginManager.class).toInstance(manager);
    PluginContainer common = mock(PluginContainer.class);
    SpongeImpl.setSpongePlugin(common);
    this.bind(EventManager.class).toInstance(mock(EventManager.class));
    this.bind(ChannelRegistrar.class).toInstance(mock(ChannelRegistrar.class));
}
Also used : PluginManager(org.spongepowered.api.plugin.PluginManager) PluginContainer(org.spongepowered.plugin.PluginContainer) TestServer(org.spongepowered.common.test.TestServer) Server(org.spongepowered.api.Server) Platform(org.spongepowered.api.Platform) EventManager(org.spongepowered.api.event.EventManager) ChannelRegistrar(org.spongepowered.api.network.ChannelRegistrar) SpongeGame(org.spongepowered.common.SpongeGame)

Example 10 with PluginContainer

use of org.spongepowered.plugin.PluginContainer in project SpongeCommon by SpongePowered.

the class EventManagerRegistrationTest method successRegistration.

@Test
public void successRegistration() {
    final EventManager eventManager = new TestEventManager();
    final PluginContainer mock = Mockito.mock(PluginContainer.class);
    eventManager.registerListeners(mock, new Dummy());
}
Also used : TestEventManager(org.spongepowered.common.test.TestEventManager) PluginContainer(org.spongepowered.plugin.PluginContainer) EventManager(org.spongepowered.api.event.EventManager) TestEventManager(org.spongepowered.common.test.TestEventManager) Test(org.junit.jupiter.api.Test)

Aggregations

PluginContainer (org.spongepowered.plugin.PluginContainer)22 NonNull (org.checkerframework.checker.nullness.qual.NonNull)6 Collection (java.util.Collection)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 Component (net.kyori.adventure.text.Component)4 EventManager (org.spongepowered.api.event.EventManager)4 PluginMetadata (org.spongepowered.plugin.metadata.PluginMetadata)4 ArrayList (java.util.ArrayList)3 Optional (java.util.Optional)3 Identity (net.kyori.adventure.identity.Identity)3 TextComponent (net.kyori.adventure.text.TextComponent)3 Nullable (org.checkerframework.checker.nullness.qual.Nullable)3 Test (org.junit.jupiter.api.Test)3 Sponge (org.spongepowered.api.Sponge)3 Command (org.spongepowered.api.command.Command)3 CommandResult (org.spongepowered.api.command.CommandResult)3 Parameter (org.spongepowered.api.command.parameter.Parameter)3 RefreshGameEvent (org.spongepowered.api.event.lifecycle.RefreshGameEvent)3 SpongeEventManager (org.spongepowered.common.event.manager.SpongeEventManager)3