Help registering Command

Help registering my command:
I am trying to make a /rules command in a class called ‘cmd’. I just need a little help registering the command in my main class.
I currently have:

event.getGame().getCommandDispatcher().register(new cmd(), help, this);

I keep getting an error when I run the code, and have been reading the JavaDocs and have not gotten anywhere. I have the command built already, I just need this line fixed so I can implement it.

Thanks for your help!
Merry Christmas, and happy new Year!

There’s no documentation on this, I have no idea

Usage is:

// plugin: object of your plugin class (which is annotated with @Plugin)
// callable: The CommandCallable for the command.
// alias: array or list of commands. The first is the MAIN COMMAND so e.g. for you probably just "rules". Any after that are aliases.

register(Object plugin, CommandCallable callable, String... alias)

So if your CommandCallback is new cmd(), the command is just /rules (no aliases), and you are calling it from your plugin class (I’m guessing you are since you have event.xxx), this should work:

event.getGame().getCommandDispatcher().register(this, new cmd(), "rules")

LWC uses it, so you may be interested in that although it might look too daunting from how it uses it (it’s not so bad!)

Sponge is adding annotated commands eventually I think (so you just add annotation(s) to a method that’s called for the command), which will be a lot easier to use for you.

Thanks so much!

@Hidendra
Sorry for the inconvenience, I followed that example and am not seeming to get anywhere.

    package mcutils;
import java.util.logging.Logger;
import org.spongepowered.api.Game;
import org.spongepowered.api.Server;
import org.spongepowered.api.util.event.Subscribe;
import org.spongepowered.api.event.state.PreInitializationEvent;
import org.spongepowered.api.event.state.ServerStartingEvent;
import org.spongepowered.api.entity.player.Player;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.util.event.Event;

@Plugin(id = "MCUtils", name = "MCUtils")    
public class MCUtils {
    Game game;
    Player player;
    Server server;
    Event events;
    Logger logger;
    @Subscribe
    public void onPreInit(PreInitializationEvent event){
        game = event.getGame();
    }
    
    @Subscribe
    public void onServerStart(ServerStartingEvent event){
        logger.info("(c) Alex Webber 2014");
        event.getGame().getEventManager().register(new PlayerEvents(), this);
        event.getGame().getEventManager().register(new ServerEvents(), this);
        event.getGame().getCommandDispatcher().register(this, new Commands(), "rules");
    }}

Any help would be greatly appreciated.

That looks fine. If you’re testing the plugin w/ Sponge, is the command just not working or are you getting an error?

What does your Commands class look like?

@Hidendra: Fixed the problem! Had a silly little extra line from another attempt!
Thanks!

1 Like

hey quick question when do you need the @Subscribe annotation?

Whenever you want to subscribe (listen) to events
See the Event Bus Documentation for more info.

2 Likes