Solved How to make a command with arguments

Discussion in 'Spigot Plugin Development' started by TechyGaming, Feb 2, 2020.

  1. Hi,
    I am kind of new to Minecraft Java coding and have only made some basic commands with a config. I was just wondering how to make a command with arguments.

    Thanks.
     
    #1 TechyGaming, Feb 2, 2020
    Last edited: Feb 19, 2021
    • Funny Funny x 1
  2. Don't know why you decide to use JS instead of Java which is what Minecraft built-on but fine.

    You haven't mention which library you use to write Minecraft in Javascript though.
     
    • Agree Agree x 1
  3. Strahan

    Benefactor

    I think it's far more likely that he is using Java and just called it JavaScript for some inexplicable reason.

    As to your question OP, the onCommand constructor's last argument is the arguments passed to the command. So like if you have public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) the "args" is what you want to look at.

    So let's say you want to make a command /heal. Heal by itself with no args heals the player or they can pass a player to heal. Since args is a String array, it has a "length" property to see how many arguments there are. So you'd check if the length is zero, and if so heal the player. If it's >0, it means they passed an argument so you get the value (args[0]) and use that. Remember, arrays are zero based so the first argument is 0, second is 1, etc.
     
  4. Choco

    Moderator

    Please do some research before creating a thread. Especially for very common beginner questions such as these because they will have been answered.

    Help thread: https://www.spigotmc.org/threads/command-arguments.241682/
    Bukkit tutorial thread: https://bukkit.org/threads/tutorial-command-arguments.186497/

    Thread moved to Spigot Plugin Development (create threads here when you want to ask a question regarding the development of a plugin), prefixed with 1.15.2. If the prefix is incorrect, please change it, but we highly advise the use of the latest version.

    Also, for future reference, JavaScript = mostly web browsers, Java = mostly applications (including Minecraft plugins)
     
  5. Really sorry for the wrong name for Java, it was late when I was writing this and I got confused.

    Also I had looked on the forums but couldn't find anything similar that could help me. The attached threads and help are perfect, thanks!
     
    • Like Like x 1
  6. Create a class, and implements CommandExecutor
    create an instance

    public "MainClassName" plugin;

    public "CommandClassName"(MainClassName instance){
    this.plugin = instance;

    add
    public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args) {
    if(!(sender instanceof Player)){
    sender.sendMessage("You must be a player to run this command!");
    return false;
    }

    Player p = (Player) sender;

    after this you can work without specifing if command label.equals to /hello but you can just start working with arguments, because the plugin will recognise that the basic command is already "hello". So if you want to do something when a player digits "hello" do if(args.lenght == 0) then..
    if you want to do something if player digits "hello world" just do
    if(args.lenght == 1){
    if(args[0].equalsignorecase("world"){
    then ....

    after that remember to go in your main class and put
    onEnable(){
    getCommand("hello").setExecutor(new "CommandClassName"(this));

    at the end just add in your plugin.yml the command
     
  7. Assuming your question has been solved, could you please mark your thread as such? That prevents other people from thinking you need any further help.
     
  8. Strahan

    Benefactor

    One thing to add, consider first if the command really should be for players only. So many people implement console restrictions on commands that don't need to be console restricted. Also, you spelled length wrong both times and it's equalsIgnoreCase not equalsignorecase. You know you can use [CODE] tags to preserve formatting and make it look better. Oh and OP, don't literally put double quotes around the class names if you copy and paste.

    Personally, I prefer handling args with a switch:
    Code (Text):
    if (args.length == 0) {
      // send help message
      return true;
    }

    switch (args[0].toLowerCase()) {
    case "whatever":
      break;
    default:
      // send help message
      break;
    }
     
  9. I know Strahan but this is a basic tutorial, because he said that does not know how to make a command so switch is a bit ""more professional"" but if he only has to check for 2-3 args[0] this one works fine.
    Sorry for my bad syntax but however when you copy in Eclipse it will autocorrect the words ignoreCase ecc...
     
  10. If you don’t know the basics of java then many would recommend to stay away from coding plugins until you do know them. It will save you a lot of confusion once you dive in to the api
    I use strahan’s switch method as the code is far cleaner than is bunch of if-elses. Easy to read for me and other people looking at the code. But as a beginner I would point you to using if-elses as its probably simpler to understand
     
  11. Strahan

    Benefactor

    ...what? Switch is just another Java keyword, lol. It has its use cases, just like if statements have their use cases. What makes you think it's more "professional" than any other keyword? I like it because I find it much easier to read cases to determine what the following code is for versus the clutter of a million if/elses. Now if I had to do compound comparisons, then a switch would not be the way to go. Use case determines which is better / more appropriate.

    Also I dunno what Eclipse you are using, but mine does not autocorrect errors like equalsignorecase -> equalsIgnoreCase.
     
  12. Just pass the mouse on it. However I was meaning that to start if was more basic, but it's okay.
     
  13. Done, Sorry for not updating sooner
     
    • Like Like x 1