Jump to content

[Solved] [1.12.1] Register one command for two mods?


Recommended Posts

Posted (edited)

Hi all,

 

I've written an extension for another mod and I wanted to use the same command as the mod itself. It seems that it isn't possible with my current knowledge. So my question is: Is it possible to share/use the same command as another mod without overwriting it?

 

If it's not possible, is there a known work-a-round?

 

Kind regards,

Pixtar

Edited by Pixtar
Posted (edited)

The commands are getting registered, so it could be that there is a queue where the commands get registered. I don't know about the mechanism behind it, therefore I'm asking. ^^

Edited by Pixtar
Posted

Right, that was my expectation. That if I type /<command> that both registered commands will be executed.

 

Currenlty it only executes the command of my mod, maybe because of a non exisitng queue behind it and LIFO?!

Posted

Hi diesieben07,

 

so it's like I assumed, otherwise it had shown me both commands. ^^

Nevertheless I was waiting for such the work-a-round answer of yours. I will give your method of delegation a try. Thanks you very much for the hint.

 

Pixtar

Posted

If the other mod's command extends CommandTreeBase, you could use CommandTreeBase#addSubcommand to add a sub-command to it.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted (edited)

Hi Choonster,

 

after I tried it out, I've read your answer again and then I understood it. So in general a mod developer should use CommandTreeBase to give other developers the chance to extend the mod commands if they like to?

 

Hi diesieben07,

 

I tried your method and it's working flawlessly; thanks. Just for completeness and other people reading this thread - I did the following:

//Register your command class e.g. "MyCommand":
@Mod.EventHandler
public void onServerStarting( FMLServerStartingEvent event )
{
    event.registerServerCommand( new MyCommand() );
}

//Within the "MyCommand" class extended from CommandBase overwrite "execute"
@Override
public void execute( MinecraftServer server, ICommandSender sender, String[] args ) throws CommandException
{
    if( sender instanceof EntityPlayerMP )
    {
        //Get the command you want to overwrite/extend from e.g. command "examplemod":
        ICommand base = FMLCommonHandler.instance().getMinecraftServerInstance().getCommandManager().getCommands().get( "examplemod" );

        //Handle arguments
        if( args.length > 0 )
        {
            //Handle the "newcommand" you have added
            if( args[0].equalsIgnoreCase("newcommand") )
            {
                doSomething( );
            }
            //Redirect all other command from the mod to the mod ifself
            else
            {
                base.execute( server, sender, args );
            }
        }
        //If no arguments where given print the usage
        else
        {
            EntityPlayerMP player = (EntityPlayerMP) sender;

            //Extend the usage text with the "newcommand"
            String usageText = base.getUsage( sender ) + "\n/examplemod <newcommand>";

            //Print the text to the player
            player.sendMessage( new TextComponentString(text).setStyle( new Style().setColor(TextFormatting.YELLOW) ) );
        }
    }
}

 

Edited by Pixtar
Missed the redirection
Posted (edited)

Hi diesieben07,

 

below the modified version, which now gets the base command before the registration:

//Register your command class e.g. "MyCommand":
@Mod.EventHandler
public void onServerStarting( FMLServerStartingEvent event )
{
    MyCommand cmd = new MyCommand();
    //Get the command you want to overwrite/extend from e.g. command "examplemod":
    ICommand base = FMLCommonHandler.instance().getMinecraftServerInstance().getCommandManager().getCommands().get( "examplemod" );
    cmd.registerBase( base );
    event.registerServerCommand( cmd );
}

//Within the "MyCommand" have a variable to store the base command
private static ICommand base = NULL;

//Within the "MyCommand" have a function to register the base command
public registerBase( ICommand base )
{
    base = _base;
}

//Within the "MyCommand" class extended from CommandBase overwrite "execute"
@Override
public void execute( MinecraftServer server, ICommandSender sender, String[] args ) throws CommandException
{
    if( sender instanceof EntityPlayerMP )
    {
        //Handle arguments
        if( args.length > 0 )
        {
            //Handle the "newcommand" you have added
            if( args[0].equalsIgnoreCase("newcommand") )
            {
                doSomething( );
            }
            //Redirect all other command from the mod to the mod ifself
            else
            {
                base.execute( server, sender, args );
            }
        }
        //If no arguments where given print the usage
        else
        {
            EntityPlayerMP player = (EntityPlayerMP) sender;

            //Extend the usage text with the "newcommand"
            String usageText = base.getUsage( sender ) + "\n/examplemod <newcommand>";

            //Print the text to the player
            player.sendMessage( new TextComponentString(usageText).setStyle( new Style().setColor(TextFormatting.YELLOW) ) );
        }
    }
}

 

Edited by Pixtar
variable text wasn't named as usageText
Posted
1 hour ago, Pixtar said:

after I tried it out, I've read your answer again and then I understood it. So in general a mod developer should use CommandTreeBase to give other developers the chance to extend the mod commands if they like to?

 

Yes, it also allows the mod that adds the command to cleanly handle the logic for each sub-command without jamming them all into one class.

 

CommandTreeBase is relatively new (added in commit 4e3b6b0 on 2016-09-13), so some mods may be using their own similar implementation for commands with sub-commands.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.