Help with getting arguments in a command

Discussion in 'Plugin Development' started by Mattz, Dec 27, 2012.

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

    Mattz

    I'm trying to get a player's name in a command and store it in a variable, along with a string. What am I doing wrong here?

    Main class:
    Code:text
    1. package net.thepvp.quests;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class QuestsAPI extends JavaPlugin {
    6.  
    7. @Override
    8. public void onEnable() {
    9. getLogger().info("Plugin is now active!");
    10. getCommand("notice").setExecutor(new Commands(this));
    11. }
    12.  
    13. @Override
    14. public void onDisable() {
    15. getLogger().info("Plugin is now inactive!");
    16. }
    17. }
    18.  


    Commands.java:
    Code:text
    1. package net.thepvp.quests;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9.  
    10. public class Commands implements CommandExecutor {
    11.  
    12. String notificationPrefix = ChatColor.GRAY + "[" + ChatColor.RED + "!" + ChatColor.GRAY + "] " + ChatColor.BLUE;
    13. @SuppressWarnings("unused")
    14. private QuestsAPI plugin;
    15.  
    16. public Commands(QuestsAPI plugin) {
    17. this.plugin = plugin;
    18. }
    19.  
    20. public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
    21. if(cmd.getName().equalsIgnoreCase("notice")) {
    22. if(args.length == 0) {
    23. sender.sendMessage(notificationPrefix + "Try /notice [player] [message]");
    24. } else if(args.length == 1) {
    25. sender.sendMessage(notificationPrefix + "Did you specify a target player and a message?");
    26. } else if(args.length == 2) {
    27. String message = "";
    28. Player toSendNotice = Bukkit.getPlayerExact(args[1]);
    29.  
    30. //Extracts the message
    31. for (int i = 1; i < args.length; i++) {
    32. message = message + args[I] + " ";[/I]
    33. [I] sender.sendMessage("Not working!");[/I]
    34. [I] }[/I]
    35.  
    36. [I] if(toSendNotice.isOnline()) {[/I]
    37. [I] toSendNotice.sendMessage(notificationPrefix + message);[/I]
    38. [I] } else {[/I]
    39. [I] sender.sendMessage(notificationPrefix + "You cannot send notifications to offline players!");[/I]
    40. [I] }[/I]
    41.  
    42.  
    43. [I] } else {[/I]
    44.  
    45. [I] }[/I]
    46. [I] return true;[/I]
    47. [I] }[/I]
    48. [I] return false;[/I]
    49. [I] }[/I]
    50. [I]}[/I]
    51. [I]
    [/I]

    Thanks for any help!
     
  2. Offline

    Nashor

    Here is something important to note:
    /label args[0] args[1] args[2] etc.
    In your case, the label is notice, args[0] is the player name and args[1] is the message. You mixed it up a bit which is why it does not work.
     
  3. Offline

    MP5K

    // didn't see the part of the code im sorry
     
  4. Offline

    Mattz

    I tried changing it to args[0], but it didn't make any difference...

    It already does support spaces?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
Thread Status:
Not open for further replies.

Share This Page