Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

  • Author

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

  • Author

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?!

  • Author

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

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.

  • Author

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

  • Author

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

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.