Can't use Sponge.getCommandManager

I’m trying to create a simple command for my plugin, but I am stuck at the first hurdle. I’m unable to use the command manager to register a command!

I’ve tried assigning it as a variable but that hasn’t helped. I’m assuming it’s to do with my project setup, but I can’t think of what could be causing it, as I’m new to plugin development.

I’ve put it on Github, in the hope that this will be the best way to share my project setup and allow people to have a good look around themselves.

https://github.com/Moralous/DeathRecorder/tree/master

I can use Sponge.getCommandManager when assigning it to the cmdManager variable, but other than there it doesn’t even appear on the autofiller.

Any help would be appreciated, especially if you could explain what’s causing the issue as well as providing a solution, so that I can avoid or at least fix this kind of problem in the future.

Thank you in advance!

You’re attempting to access the CommandManager while your plugin class is being initialized and it isn’t available yet. You need to be using events to listen to particular game states and handle your initialization logic at the right time. The docs have a page about the plugin lifecycle that explains how this process works, and many of the other pages will be useful to you too.

Generally, you should only be getting the CommandManager when you need it, so there’s no reason to save it to a variable. In some case, this can actually come back to hurt you if you’re not careful (see the page on services).

For a simple plugin it isn’t too much of an issue, but when you start having multiple commands it’s a good idea to place each executor in its own class. You can have the CommandSpec for the class be a static final field there too, and that can help to keep things organized when you start getting into some really crazy command hierarchies.

1 Like

An obvious oversight on by behalf to be honest, thanks for helping me out!