[Urgent] Args[] Help!

Discussion in 'Plugin Development' started by Ape101, Oct 23, 2013.

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

    Ape101

    Hello
    I am trying to get the integer argument from a command, and if my plugin finds the strings that match that number in the config, clear them.

    My code:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){
    2. Player p = null;
    3. if ((sender instanceof Player))
    4. {
    5. p = (Player)sender;
    6. }
    7. {
    8.  
    9. if (!p.hasPermission("plugin.clearpoint"))
    10. {
    11. p.sendMessage(ChatColor.RED + " You do not have permission.");
    12. }
    13.  
    14. if (args.length == 1)
    15. {
    16. p.sendMessage(ChatColor.RED + " Error: Too many Arguments");
    17. return true;
    18. }
    19.  
    20. if (args.length == 0){
    21. int Tutnumber = Integer.parseInt(args[0]);
    22. plugin.getConfig().set("Point." + Tutnumber + ".Message", "");
    23. plugin.getConfig().set("Point." + Tutnumber + ".Location.world", "");
    24. plugin.getConfig().set("Point." + Tutnumber + ".Location.x", "");
    25. plugin.getConfig().set("Point." + Tutnumber + ".Location.y", "");
    26. plugin.getConfig().set("Point." + Tutnumber + ".Location.z", "");
    27. plugin.getConfig().set("Point." + Tutnumber + ".Location.yaw","");
    28. plugin.getConfig().set("Point." + Tutnumber + ".Location.pitch", "");
    29.  
    30.  
    31. TutPlus.getInstance().saveConfig();
    32.  
    33. p.sendMessage(ChatColor.DARK_AQUA + " Tutorial Point number " +ChatColor.RED + Tutnumber +ChatColor.DARK_AQUA+ " cleared!");
    34. plugin.reloadConfig();
    35. return true;
    36. }
    37. p.sendMessage(ChatColor.RED + " Unknown argument: " + args[0] + "Make sure you are using an Integer!");
    38. return true;
    39. }
    40. }
    41. {
    42.  
    43.  
    44.  


    I know this command is completely wrong, so if anyone could re format it and add in the integer stuff i would love you forever! :D thanks guys

    Updated my code a little bit, still not working :(

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

    EvilKanoa

    You have two problems (that I can see).

    One, you check to make sure it's a player using the command. If it is, you set "p" to the sender, otherwise you do nothing. You need to change it to something like this:
    Code:java
    1.  
    2. Player p = null;
    3. if (sender instanceof Player) {
    4. p = (Player) sender;
    5. } else {
    6. sender.sendMessage("Player only command!");
    7. return true; //This will exit the method.
    8. }

    Basically, if it isn't a player calling the command (read: console), it will send the sender a message and exit.

    Two, you want this command to have 1 argument based on this code
    Code:java
    1. int int Tutnumber = Integer.parseInt(args[0]);
    (computers start counting at 0 instead of 1, so think of args[0] as args[0 + 1].) But that code is inside your if statement that makes sure there is NO arguments, the first if statement (args.length == 1) should read
    Code:java
    1. if (args.length != 1) {
    and the second statement (args.length == 0) should read
    Code:java
    1. if (args.length == 1) {
     
  3. Offline

    Pizza371

    EvilKanoa why would you go ahead and define a player as null to just redefine it a second later? :p
     
  4. Offline

    Jalau

    It's sometimes needed if you define the object in brackets but you want to use it behind the brackets like this:


    int i = null;
    try {
    i = Integer.parseInt(args[0]
    }
    catch {
    player.sendMessage("Invalid Number")
    }

    And do here you need i!
     
  5. Offline

    1Rogue

    Alternatively you can just do:
    Code:java
    1. Player p;
    2. if (sender instanceof Player) {
    3. p = (Player) sender;
    4. } else {
    5. sender.sendMessage("Player-only command");
    6. return true;
    7. }


    But I don't see the point of this being a user-only command? Why couldn't the console use it?
     
  6. Offline

    Ape101

    1Rogue thats true, i might change that, and thanks for the help EvilKanoa That worked :D One question, any ideas on how i would be able to send a message saying "No point INTEGER found" if the command doesn't find it in the config?
     
Thread Status:
Not open for further replies.

Share This Page