Posted September 8, 20178 yr 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 September 22, 20178 yr by Pixtar
September 8, 20178 yr 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 September 8, 20178 yr by Pixtar
September 8, 20178 yr 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?!
September 8, 20178 yr 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
September 8, 20178 yr 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.
September 9, 20178 yr 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 September 9, 20178 yr by Pixtar Missed the redirection
September 9, 20178 yr 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 September 9, 20178 yr by Pixtar variable text wasn't named as usageText
September 9, 20178 yr 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.