Args[0] question? or not working?

Discussion in 'Plugin Development' started by jusjus112, Dec 22, 2013.

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

    jusjus112

    I have an plugin called gunmaster, and i have commands with more aurguments!
    If you typed /gunmaster it does send the message, but if you typ /gunmaster help it does say the gunmaster message, and not the help messsage! Anyone know what i am doning wrong?

    i have put the commands in my plugin.yml and
    Here is my code:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]){
    2. if(!(sender instanceof Player)){
    3. sender.sendMessage(MsgType.CONSOLE + "Only players can use this command!");
    4. return true;
    5. }
    6.  
    7. Player p = (Player) sender;
    8.  
    9. if (ArenaManager.getManager().isInGame(p)) {
    10. p.sendMessage(MsgType.DENIED + "You can not use commands ingame");
    11. }
    12.  
    13. if (cmd.getName().equalsIgnoreCase("gunmaster")) {
    14. p.sendMessage(ChatColor.GOLD + "------------ " + MsgType.NORMAL + "--------------------");
    15. p.sendMessage(ChatColor.GOLD + "Developed by: " + ChatColor.GRAY + "jusjus112");
    16. p.sendMessage(ChatColor.GOLD + "Version: " + ChatColor.GRAY + " 0.5");
    17. p.sendMessage(ChatColor.GOLD + "Minecraft version: " + ChatColor.GRAY + "1.7.2");
    18. p.sendMessage(ChatColor.YELLOW + "Typ /gm help for an list of commands.");
    19. return false;
    20. }
    21. if (args.length > 0) {
    22. if (args[0].equalsIgnoreCase("help")) {
    23. p.sendMessage(MsgType.NORMAL + "/gm join"
    24. + "/gm leave"
    25. + "/gm create"
    26. + "/gm remove");
    27. return true;
    28. }
    29. return false;
    30. }
     
  2. Offline

    thepaperboy99

    I think it's because you have if(args.length > 0)

    Try

    if(args.length == 0)
     
  3. Offline

    jusjus112

    thepaperboy99
    Sorry, but i did have tried that!
    It says again the same thing under the command /gunmaster not the /gunmaster help?

    And if i put "Return true;"
    under the gunmaster command, it dont work
     
  4. Offline

    adam753

    Code:java
    1. if (cmd.getName().equalsIgnoreCase("gunmaster")) {

    When you type /gunmaster help, this statement is true, so it runs all the code after it and then returns false. Incidentally, you're then checking if args[0] is help regardless of what the command name is.
    Your code should look like this:
    Code:java
    1.  
    2. if (cmd.getName().equalsIgnoreCase("gunmaster")) {
    3. if(args.length == 0) {
    4. //. . .
    5. }
    6. else if(args.length == 1){ //Technically could just be "else", but this is better practice
    7. if(args[0].equals("help") {
    8. //. . .
    9. }
    10. }
    11. }
    12.  
     
  5. Offline

    jusjus112

    oke, it works :D
    Thanks adam753

    adam753
    I have quickly reopened this thread with one more question!
    If i typ /gumaster delete <arenaNumber>
    it does not work, but if i typed /gunmaster delete. Then he says with arena number 0
    What i doning wrong? I cant go to args[1] or sometghing. Only args[0] works!
    Code: I got the error in line 6!
    Code:java
    1. if (args.length == 1) {
    2. if (args[0].equalsIgnoreCase("delete")) {
    3. }
    4. int num = 0;
    5. try{
    6. num = Integer.parseInt(args[0]);
    7. p.sendMessage(MsgType.DENIED + "Invalid arena " + num);
    8. }
    9. ArenaManager.getManager().removeArena(num);
    10. p.sendMessage(MsgType.NORMAL + "Removed arena " + num);
    11. return true;
    12. }

    And my error:
    PHP:
    22.12 17:24:47 [ServerINFO ... 15 more
    22.12 17
    :24:47 [ServerINFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    22.12 17:24:47 [ServerINFO at gunmaster.Main.onCommand(Main.java:82)
    22.12 17:24:47 [ServerINFO Caused byjava.lang.ArrayIndexOutOfBoundsException1
    22.12 17
    :24:47 [ServerINFO at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    22.12 17:24:47 [ServerINFO at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
    22.12 17:24:47 [ServerINFO at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
    22.12 17:24:47 [ServerINFO at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
    22.12 17:24:47 [ServerINFO at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
    22.12 17:24:47 [ServerINFO at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
    22.12 17:24:47 [ServerINFO at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
    22.12 17:24:47 [ServerINFO at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
    22.12 17:24:47 [ServerINFO at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
    22.12 17:24:47 [ServerINFO at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
    22.12 17:24:47 [ServerINFO at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:837)
    22.12 17:24:47 [ServerINFO at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:886)
    22.12 17:24:47 [ServerINFO at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:968)
    22.12 17:24:47 [ServerINFO at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:528)
    22.12 17:24:47 [ServerINFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
    22.12 17:24:47 [ServerINFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    22.12 17:24:47 [ServerINFO org.bukkit.command.CommandExceptionUnhandled exception executing command 'gm' in plugin ZombieSurvival v0.1
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  6. Offline

    Garris0n

    You don't really seem to understand what the brackets do...
    It checks if the args is 0. If so it does absolutely nothing. Then it parses args[0] (which would be delete, just saying) and beacause it's "delete" nothing happens. None of your code makes any sense.
    Click this.
     
  7. Offline

    Davesan

    Yes, I agree with GarrisOn, you must understand what an array means, and what it means in case of ingame commands. The command "/gunmaster delete <area>" will result the following arguments:
    Code:java
    1. label == "gunmaster"
    2. args == String[] { "delete", "<arena>" } which means:
    3. args[0] == "delete"
    4. args[1] == "<arena>"

    args.lenth means how many record is has, so in this case args{"delete", "<arena>"}.lenth == 2

    So you will handle that this way:
    (I do my comands this way)

    Code:java
    1. if (label.toLowerCase().equalsIgnoreCase("gunmaster")) {
    2. if (args.lenth > 0) { //there are arguments after the main command
    3. if (args[0].toLowerCase().equalsIgnoreCase("delete")) { //if the first argument is "delete"
    4. if (args.length == 2) { //there are two arguments after "/gunmaster"
    5. try { int num = Inteher.parseInt(args[1]); }
    6. catch(NumberFormatException nfe) {
    7. sender.sendMessage("§4Arena id must be a number!"); //§4 is red color code
    8. return true;
    9. }
    10. ArenaManager.getManager().removeArena(num);
    11. sender.sendMessage("§2Arena #" + num + " has been successfully removed."); //§2 is green color code
    12. } else {
    13. sender.sendMessage("§7Right syntax: /gunmaster delete <arena id>"); //
    14. return true;
    15. }
    16. } else {
    17. sender.sendMessage("No gungame command like that.");
    18. sender.sendMessage("Commands:");
    19. sender.sendMessage(" /gunmaster delete <arena-id>");
    20. }
    21. } else { //there are no arguments, the command is only "/gunmaster"
    22. //Do the "/gunmaster" command
    23. }
    24. return true;
    25. }
    26. return false;


    Edit: yep so many edits, i'm not home and there are so many if methods with {} marks :p
     
Thread Status:
Not open for further replies.

Share This Page