1.7.10 Command in another command

Discussion in 'Spigot Plugin Development' started by ChemicalCopy, May 20, 2020.

  1. Hello,
    I want that if I type command /rank add (text1) (text2), server will apply command /lp user (text1) parent add (text2),
    can somebody help me please?
    Can somebody write code please?
    Thanks for all answers.
     
  2. Research how do use the "Bukkit.dispatchCommand()" method.
    I really hope no one gives you any help past what me and @B0SS-0N3 have said, I don't want you to be spoonfed.
     
    • Agree Agree x 2
  3. @ChemicalCopy If you're willing to change your command to rank-add (text1) (text2) instead, you can register it as an alias in Bukkit's commands.yml, and won't need to write any code.

    If you are, add the following to the file to register the alias:
    Code (YAML):
    aliases:
      rank-add
    :
     - lp user $$1 parent add $$2
     
  4. No, thanks
     
  5. Give this a try! Change whatever you'd like (y)
    Code (Text):
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("rank")) { //This is the base of the command
                if (!sender.hasPermission("YourPermHere")) {return true;} //If the command sender does not have permission, it will not continue
                else if (args.length > 0 && args[0].equalsIgnoreCase("add")) { //Checks if the sub command is "add"
                    try {Bukkit.dispatchCommand(sender, "lp user " + args[1] + " parent add " + args[2]);} //This will attempt to run the command
                    catch (Exception e) {sender.sendMessage("Your command usage message here");} //This is what will be returned if the command doesn't mean all the requirements
                }
            }
            return true;
        }
     
    #8 ElementalGamingX, May 21, 2020
    Last edited: May 21, 2020
    • Funny Funny x 3
  6. It'd be better if you added some explanations, as they probably won't learn much if anything at all from being spoonfed.
     
  7. > Not willing to code the solution
    > Not willing to use a solution where coding is not needed

    Better head to the hiring dev section as I'm not wasting my time on this.
     
    • Agree Agree x 2
    • Funny Funny x 1
  8. @ElementalGamingX "Unknown command. Type "/help" for help."
     
  9. Don't forget to register the command in plugin.yml
     
  10. Why would you do that lmao. Don’t encourage this type of behaviour it comes back and bites this forum section over and over.
     
    • Agree Agree x 2
  11. Still don't work
     
  12. Although I see your point where he did not want to put in any effort and immediately just asked for the code, I still wanted to help because I've been in situations similar to his where people would not help me and would just send me a useless link or say "Google is your friend". I was always willing to put in the work but never got any assistance when needed so I had to waste many hours and sometimes days, trying to figure out a solution for a simple issue
     
  13. It's better to just help someone and get it over with then make them waste so much time and end up giving up
     
  14. Don't forget to register the command in both your main class and your plugin.yml
     
  15. Strahan

    Benefactor

    If you're going to spoon feed, at least explain stuff as you go to make it useful. Also, try to avoid giving out examples with odd design. Like:
    Code (Text):
    if (!sender.hasPermission("YourPermHere")) {return true;}
    else if (args.length > 0 && args[0].equalsIgnoreCase("add")) {
        try {Bukkit.dispatchCommand(sender, "lp user " + args[1] + " parent add " + args[2]);}
        catch (Exception e) {sender.sendMessage("Your command usage message here");}
    }
    He's obviously a noob, so you should use proper spacing of commands to make it easier to read:
    Code (Text):
    if (!sender.hasPermission("YourPermHere")) {
        return true;
    } else if (args.length > 0 && args[0].equalsIgnoreCase("add")) {
        try {
          Bukkit.dispatchCommand(sender, "lp user " + args[1] + " parent add " + args[2]);
        } catch (Exception e) {
          sender.sendMessage("Your command usage message here");
        }
    }
    Except even that is still weird, because if you are returning after evaluating some logic why use an else at all? You also aren't throwing any error so it just silently fails, that's never a good idea. Also you are only checking if args is greater than zero, not checking that the right amount are there. I assume that's why you wrapped dispatch in a try/catch. If so, that is a terrible idea. Do a proper length check and return instead. This would be a little better, though I'd switch on args[0].tolower personally if I had intention to add more subcommands and instead of dispatching, I'd leverage the LP developer API.

    Code (Text):
    if (!sender.hasPermission("YourPermHere")) {
        sender.sendMessage(ChatColor.RED + "Access denied!");
        return true;
    }

    if (args.length == 0) {
      // send help to explain how the command works
      return true;
    }

    if (args[0].equalsIgnoreCase("add")) {
        Bukkit.dispatchCommand(sender, "lp user " + args[1] + " parent add " + args[2]);
    }
     
    • Like Like x 4
  16. Yeah I was on my phone so I just threw something together. I usually set it up a lot better
     
  17. The person is not a developer and clearly doesn't want to be one. The waste of time would be replying anything but a link to a Java tutorial imo.

    Here, this is where to start @ChemicalCopy once you understand the basics you'll be able to write more complex things such as Spigot plugins and custom commands.
     
    • Agree Agree x 1