How do I get a plugin object?

Saw something like this on a thread today:

Inventory inv = Inventory.builder().build(KitPvP.instance);

Notice in particular the .build(KitPvP.instance) bit.

As shown in the javadocs, this is what has to be the parameter for .build: “plugin - The plugin building this inventory” (it is Object plugin).

So I tried doing a similar thing, but it didn’t work. For example, with the class where I have @Plugin etc., I can create an instance of it but it won’t recognise anything I do as a plugin… but the plugin does work in every other respect, so surely I will have some way of getting this plugin object?

Thanks in advance.

Example:

Sponge.pluginManager().plugin("regionguard").ifPresent(container -> {
			RegionGuard regionGuard = (RegionGuard) container.instance();
		});

But personally, I prefer to use events in my plugins to pass the necessary classes to other plugins.

1 Like

Oh right. Forgot I was using an old version of Sponge (7.4.0).

For me it was:

Object myPlugin = Sponge.getPluginManager().getPlugin("myPlugin").get().getInstance().get();

…but that is just syntax stuff, your answer is spot on - thanks!

The least resource intensive way is to get it on boot of your plugin. Take a look at the sponge docs on create your plugin.

@Inject public MyPluginMainClass(PluginContainer, Logger logger){

Thanks. How would I then refer to this when I need to get the plugin object?

I will read the documentation heh

Something like this


private final PluginContainer container;

private static MyMain plugin;

@Inject
public MyMain(PluginContainer container, Logger logger){
plugin = this;
this.container = container;
}

public PluginContainer getContainer(){
return this.container;
}

public static MyMain getPlugin(){
return plugin;
}

From there in any class you could do

PluginContainer container = MyMain.getPlugin().getContainer();

Also here is the docs locked to api 7 :slight_smile:

https://docs.spongepowered.org/7.4.0/en/plugin/index.html

1 Like