Multiple Args To A Command.

Discussion in 'Plugin Development' started by TheGamesHawk2001, Oct 26, 2014.

Thread Status:
Not open for further replies.
  1. Hey, can someone tell me how I would add another arg to a command and for a different arg to do a different thing.
     
  2. Offline

    Googlelover1234

    You just use the String[] of args you put in your onCommand. Example:

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
    2. if(cmd.getName().equalsIgnoreCase("AE") {
    3. if(args[0].equalsIgnoreCase("help") {
    4. // Do stuff
    5. }
    6. }
    7. }
     
  3. Offline

    es359

    TheGamesHawk2001 You can also use a switch() { } statement which can be more efficient in some ways.
     
    Googlelover1234 likes this.
  4. Googlelover1234 es359 Thxx and if I did if args0.equals ignore case"help" below the base command, and I typed /as help it would display the stuff below the arg 0 etc? I'm on a tablet so soz if it doesn't make much sense and I didnt put brackets cus its effort lol
     
  5. Offline

    Googlelover1234

    TheGamesHawk2001

    Yes, for example, if you had the base command "msg" and the args[0] "cheeseburger" you would use /msg cheeseburger. You can keep going with this, like you could have "msg" as the base, args[0] as "cheeseburger" and args[1] as "Panwaffle". That would be /msg cheeseburger PanWaffle.
     
  6. Offline

    es359

    Example using a switch statement.

    Code:java
    1. switch(arguments) {
    2.  
    3. case "arg1":
    4. //do arg1.
    5. break;
    6. case "arg2":
    7.  
    8. //Do arg2.
    9. break;
    10.  
    11. default:
    12. player.sendMessage("Incorrect arguments, or to many arguments.");
    13.  
    14. }
     
  7. Googlelover1234 When I did
    Code:java
    1. package me.TheGamesHawk2001.Commands;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Main extends JavaPlugin {
    10.  
    11. public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
    12. if(cmd.getName().equalsIgnoreCase("commands")) {
    13. sender.sendMessage(ChatColor.RED + "You did just /commands");
    14. if(args[0].equalsIgnoreCase("help")) {
    15. sender.sendMessage(ChatColor.RED + "You just did /commands help");
    16. if(args[0].equalsIgnoreCase("list")) {
    17. sender.sendMessage(ChatColor.RED + "You just did /commands list");
    18. }
    19. }
    20. }
    21. return true;
    22. }
    23. }


    It ran all of the args when I typed only /commands help etc
     
  8. Offline

    fireblast709

    es359 Do note that Minecraft and Bukkit are (still) compiled with Java 6.
    TheGamesHawk2001 you are nesting your commands incorrectly (they shouldn't be nested at all)
     
  9. Offline

    es359

    I've used switch statements in several of my plugins...?
     
  10. Offline

    Watto

    TheGamesHawk2001

    He literally spelled it out for you..
    Your if statements are nested.. If you don't know what that is look at your bracket placement and see if you notice anything.
     
  11. Idk but I tried this:

    Code:java
    1. package me.TheGamesHawk2001.FakeOp;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class Main extends JavaPlugin {
    11.  
    12. public boolean onCommand(CommandSender sender, Command cmd, String commandLable, String[] args) {
    13.  
    14. if (!(sender instanceof Player)) {
    15. sender.sendMessage( ChatColor.AQUA + "The Console Cant Use This Command");
    16.  
    17. }
    18.  
    19. Player player = (Player) sender;
    20.  
    21. if(cmd.getName().equalsIgnoreCase("FakeOp")) {
    22. if (!(sender instanceof Player)) {
    23. sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "You have to be a player to use this command");
    24. return true;
    25. }
    26. if (args.length == 0) {
    27. player.sendMessage(ChatColor.YELLOW + "You cannot fakeop yourself. You have to be op to use it!");
    28. return true;
    29. }else if(args.length == 1);
    30. if(Bukkit.getPlayer(args[0]) == null) {
    31. player.sendMessage(ChatColor.RED + "Player " + ChatColor.ITALIC + args[0] + "" + ChatColor.RESET + "" + ChatColor.RED + "Could not be found or is not online.");
    32. return true;
    33. }else if (args.length == 0) {
    34. if (cmd.getName().equalsIgnoreCase("help"));
    35. sender.sendMessage(ChatColor.RED + "Type /fakeop <player> <reload>");
    36. return true;
    37. }
    38. Player targetPlayer = Bukkit.getPlayer(args[0]);
    39. player.sendMessage(ChatColor.GRAY + "You have FakeOpped " + args[0]);
    40.  
    41. }
    42. return true;
    43. }
    44. }


    and when I type /fakeop help, it doesnt register.

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  12. Offline

    mythbusterma

    es359 fireblast709

    He's saying that you should compile your plugins against Java 1.6, if you want everyone to be able to use it (personally, I think this is stupid because if you haven't upgraded to Java 7 yet, something is very wrong with you). But using Strings in switch statements is something that only Java 1.7+ supports, and if you compile your plugins against Java 1.7, people still running legacy software won't be able to run your plugin. However I feel they need to upgrade and get over it.
     
    es359 likes this.
  13. Offline

    Watto

    TheGamesHawk2001

    I think you need to learn Java before working on Bukkit because your problem is a super easy fix.

    Your problem in the second code is when the player runs /fakeop it returns true, which means the if statement after that won't be run.. Does that help?
     
  14. Watto But I don't want it to run all the other code when I type /fakeop since that has its own stuff to run when you type /fakeop and other args have their own stuff too.


    Edit. This is my new one

    Code:java
    1. package me.TheGamesHawk2001.Commands;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Main extends JavaPlugin {
    10.  
    11. public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
    12. if(cmd.getName().equalsIgnoreCase("commands")) {
    13. sender.sendMessage(ChatColor.RED + "You did just /commands");
    14. }
    15. if(args[0].equalsIgnoreCase("help")) {
    16. sender.sendMessage(ChatColor.RED + "You just did /commands help");
    17. }
    18. return true;
    19. }
    20. }


    Is this right. I actually have no clue what I'm doing with args.
     
  15. Offline

    Watto

    No it's not. It will work but it's a terrible way of doing it.

    Look at it this way with your new code, you have to ensure that /commands is being typed before you check for the 'help' argument

    Just look at it logically, if you have more than one command, and lets say for example that command is '/hello' if that user types /hello help it's going to display that help message.
    You need to put the arguments check inside of /commands if statement.
     
  16. Watto so I put it like this?

    Code:java
    1. package me.TheGamesHawk2001.Commands;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Main extends JavaPlugin {
    10.  
    11. public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
    12. if(cmd.getName().equalsIgnoreCase("commands")) {
    13. sender.sendMessage(ChatColor.RED + "You did just /commands");
    14. if(args[0].equalsIgnoreCase("help")) {
    15. sender.sendMessage(ChatColor.RED + "You just did /commands help");
    16. }
    17. }
    18. return true;
    19. }
    20. }
     
  17. TheGamesHawk2001
    That should work fine. Although you will receive both of the messages if you were to execute the "/commands help" command. Also you should check the array length before trying to access an element inside it.
    Code:java
    1. if(args.length == 1) { // only 1 argument in the command
    2. if(args[0] etc) {
    3.  
    4. }
    5. }
     
    Watto likes this.
  18. Assist How would I make it so when you type /commands it displays a msg. Then /commands member a different and /commands donator for another message?
     
  19. TheGamesHawk2001
    You would create new args checks for them, like this:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
    2. if (cmd.getName().equalsIgnoreCase("commands")) {
    3. if (args.length == 0) { // they typed "/commands" only without any args
    4. sender.sendMessage(ChatColor.RED + "You did just /commands");
    5. } else if (args.length == 1) {
    6. if (args[0].equalsIgnoreCase("member")) {
    7. sender.sendMessage(ChatColor.RED + "You just did /commands member");
    8. } else if (args[0].equalsIgnoreCase("donator")) {
    9. sender.sendMessage(ChatColor.RED + "You just did /commands donator");
    10. }
    11. }
    12. }
    13.  
    14. return false;
    15. }
     
  20. Assist Thanks, What do I do for plugin.yml?
     
  21. TheGamesHawk2001
    Make a new section called "commands" and add your command in it.
    Code:
    commands:
      commands:
        description: a command
    It's a bit confusing because your command is called "commands" and the section where you put your commands in is also called "commands".
     
  22. Assist What about the separate arguments. Is that a separate command?
     
  23. Offline

    Watto

    @TheGamesHawk200

    No, your base command is 'commands' and since they are arguments you don't need to make a separate command.
     
  24. Watto Okay, I have an issue! When ever I type /commands, It runs it fine, but it says /commands in chat when I type it, with any arg. Thanks :)

    Example http://i.imgur.com/f62gpd0.png
     
  25. Offline

    Watto

    TheGamesHawk2001

    Your onCommand requires a boolean return. The return method is whether or not the command was successful, so set the return to true.
     
  26. Watto

    Code:java
    1. package me.TheGamesHawk2001.Commands;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Main extends JavaPlugin {
    10.  
    11. public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
    12.  
    13. if (cmd.getName().equalsIgnoreCase("commands")) {
    14. if (args.length == 0) { // they typed "/commands" only without any args
    15. sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Groups:");
    16. sender.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "Member:");
    17. sender.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "Iron:");
    18. sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Gold:");
    19. sender.sendMessage(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "Diamond:");
    20. sender.sendMessage(ChatColor.YELLOW + "" + ChatColor.BOLD + "Emerald:");
    21. sender.sendMessage(ChatColor.WHITE + "" + ChatColor.BOLD + "Chasma:");
    22.  
    23. } else if (args.length == 1) {
    24. if (args[0].equalsIgnoreCase("member")) {
    25. sender.sendMessage(ChatColor.GRAY + "Member has access to the following commands:");
    26. sender.sendMessage(ChatColor.DARK_PURPLE + "Help, Upgrade, Help, Kit, Warp.");
    27.  
    28. } else if (args[0].equalsIgnoreCase("Iron")) {
    29. sender.sendMessage(ChatColor.RED + "You just did /commands donator");
    30.  
    31. } else if (args[0].equalsIgnoreCase("Gold")) {
    32. sender.sendMessage(ChatColor.RED + "You just did /commands donator");
    33.  
    34. } else if (args[0].equalsIgnoreCase("Diamond")) {
    35. sender.sendMessage(ChatColor.RED + "You just did /commands donator");
    36.  
    37. } else if (args[0].equalsIgnoreCase("Emerald")) {
    38. sender.sendMessage(ChatColor.RED + "You just did /commands donator");
    39.  
    40. } else if (args[0].equalsIgnoreCase("Chasma")) {
    41. sender.sendMessage(ChatColor.RED + "You just did /commands donator");
    42. }
    43. }
    44. }
    45.  
    46. return false;
    47. }
    48. }



    It does doesn't it? and it's not red..
     
  27. Assist I changed the command to /carrot. Still doesn't work (has /carrot at the bottom)
     
Thread Status:
Not open for further replies.

Share This Page