Command arguments confusion

Discussion in 'Plugin Development' started by Rehmedy, Jun 16, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Rehmedy

    If I wanted to make a plugin with the commands /chunk, /chunk claim, /chunk flag, /chunk flag pvp, and /chunk flag build, Would this be the code? I am confused if it would mess up with this:
    Code:
    package com.github.rehmedy.Chunk;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class ChunkCommandExecutor implements CommandExecutor{
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if(label.equalsIgnoreCase("chunk")) {
      Player player = (Player) sender;
      if (args[0].equalsIgnoreCase("claim")) {
      //code here
      }
      if (args[0].equalsIgnoreCase("flag")) {
      //code here
      }
      if (args[1].equalsIgnoreCase("pvp")) {
      //code here
      }
      if (args[1].equalsIgnoreCase("build")) {
      //code here
      }
      else {
      helpMessage(player);
      }
    }
    return false;
    }
     
    public void helpMessage(Player player) {
      //help messages
    }
     
    }
    ?

    ok, I changed the code to this:
    Code:
    package com.github.rehmedy.Chunk;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class ChunkCommandExecutor implements CommandExecutor{
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if(label.equalsIgnoreCase("chunk")) {
      Player player = (Player) sender;
      if (args[0] == null) {
          //helpMessage code here
      }
      else if (args[0].equalsIgnoreCase("claim")) {
      //code here
      }
      else if (args[0].equalsIgnoreCase("flag")) {
      //code here
      }
      if (args[1] == null) {
          // "/chunk flag" code here
      }
      else if (args[1].equalsIgnoreCase("pvp")) {
      //code here
      }
      else if (args[1].equalsIgnoreCase("build")) {
      //code here
      }
      else {
      helpMessage(player);
      }
    }
    return false;
    }
     
    public void helpMessage(Player player) {
      //help messages
    }
     
    }
    Would that be right?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  2. Offline

    r0306

    Rehmedy
    You should use
    Code:
    if (args.length == 0) {
          //"/chunk" code here
      }
    Code:
    if (args.length == 1) {
    else if (args[0].equalsIgnoreCase("claim")) {
      //code here
      }
      else if (args[0].equalsIgnoreCase("flag")) {
      //code here
      }
    else {
    helpMessage(player);
    }
    }
    Code:
    if (args.length == 2) {
    else if (args[1].equalsIgnoreCase("pvp")) {
      //code here
      }
      else if (args[1].equalsIgnoreCase("build")) {
      //code here
      }
    else {
    helpMessage(player);
    }
    }
    Code:
    else {
    helpMessage(player);
    }
     
  3. Offline

    Rehmedy

    in different .class files? Or in the same file?

    Also, I don't want the help message being showed for EVERY incorrect argument that is put in, any way I can make seperate help messages for each argument that is messed up?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  4. Offline

    FurmigaHumana

Thread Status:
Not open for further replies.

Share This Page