How to get plugin object? [solved]

How do you get the plugin object and why?

I don’t know if this is a bug that needs to be reported but I been reading this Provided object is not a plugin instance?. There is a solution to that but it’s only applicable if its in the main class.

I tried:

        Sponge.getScheduler().createTaskBuilder()
            .execute(() -> player.sendMessage(Text.of("Teleporting...")))
            .delay(5, TimeUnit.SECONDS)
            .name("Delay Task")
            .submit(Sponge.getPluginManager().getPlugin("my.plugin"));

I received the error : Provided object is not a plugin instance

EDIT: Plugin is the reference to the main class.

Replace the getPlugin code with a reference to the main class provided through dependency injection. Sponge needs that information to keep the thread pool organized, I assume

1 Like

This is because PluginManager#getPlugin(String) does not return a plugin object. Rather, it returns an Optional<PluginContainer>. To properly use this, you would call .get().getInstance().get() which would return an actual plugin object.

2 Likes