Cast plugin instance

Hey everyone,

I am trying to access my core plugin within another plugin using the following code:

PluginManager pluginManager = getGame().getPluginManager();
Optional<PluginContainer> corePlugin = pluginManager.getPlugin("CludchCore");

        if(corePlugin.isPresent()) {
            coreInstance = (Core) corePlugin.get().getInstance();
        } else {
            getLogger().error("Could not load the core");
        }

But the line coreInstance = .. says "cannot cast java.lang.util.Optional<java.lang.Object> to me.cludch.core.main.Core

Hopefully you can help me.

Thanks in advance.

you need to call .get() again at the end of said line because the getInstance() returns an Optional

1 Like