Ignoring Arguments.

Discussion in 'Plugin Development' started by Evangon, Oct 23, 2011.

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

    Evangon

    How would I ignore an oncommand argument like args0?
    I tried || args0 == null, args0.equals(""), but none of them work.
     
  2. Offline

    thehutch

    what do you mean basically a command like /me which has no args?
     
  3. Offline

    coldandtired

    In all your posts you keep calling it args0! args is an Array of strings that gets passed to your onCommand() method. args[0] is the first argument, args[1] the second, args[2] the third, etc.

    args.length() will tell you how many arguments there are. 0 means /command, 1 means /command something, 2 means /command something seomething, etc.
     
  4. Offline

    thehutch

    like he said but in a more visual representation this is what he is trying to say
    Both of these are the same thing:
    Code:
    /command args1 args2 args3 args4 etc...
    
    /command args[0] args[1] args[2] args[3] etc...
     
  5. Offline

    Forairan

    If you're trying to just capture all commands in your plugin, you can't do that - Bukkit will only call commands that you've registered in your plugin.yml file.
     
  6. Offline

    Evangon

    @coldandtired
    args[0] is the proper way. 0 is the first argument as in /asdf argument
    What I mean is
    In my onCommand, I have String[] args0 (using args0[0])
    I want to be able to ignore args0[0] because one of my plugin keeps erroring when I don't have an argument.
     
  7. Offline

    coldandtired

    Can't you just post what command you're trying to use, your onCommand() code, and what you expect to happen?
     
  8. Offline

    Evangon

    Code:
    09:07:28 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'fake
    about' in plugin FakeLog v0.2A
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:16
    3)
            at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:3
    55)
            at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.
    java:757)
            at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:722)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:715)
            at net.minecraft.server.Packet3Chat.a(Packet3Chat.java:33)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:92)
            at org.getspout.spout.SpoutNetServerHandler.a(SpoutNetServerHandler.java
    :500)
            at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
            at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:471)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:374)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:417)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at logging.loginlogout.onCommand(loginlogout.java:27)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
            ... 13 more
    
    Code:
     public boolean onCommand(CommandSender sender, Command command, String label, String[] args0) {
      Server server = getServer();
      String fake = args0[0].toString();
      if(command.getName().equalsIgnoreCase("fakelogout")){
     // Fine here.
    }
      } else if(command.getName().equalsIgnoreCase("fakelogin")){ 
    // Fine here.
      } else if(command.getName().equalsIgnoreCase("fakeabout") && args0.length == 0){  // RIGHT HERE
        sender.sendMessage("FakeLogin/Logout by Evangon");
        sender.sendMessage("Server owner has been notified that you know about this.");
        log.info(sender.getName() + " knows FakeLogin/Logout exists. Proceed the caution.");
      } else if(command.getName().equalsIgnoreCase("userstatus")){
     //Fine here.
      }
      return true;
     }
    
     
  9. if (args.length == 1) {
    // DO stuff there
    }
    else {
    sender.sendMessage(ChatColor.RED + "You must have an argument");
    }
     
  10. Offline

    Evangon

    I want to ignore it, not force one to be there. And I modified it ( if(args0.legnth < 0) ) and still got the same error.
     
  11. Offline

    coldandtired

    args.length will never be less than zero, so that would never work.

    You don't need to do anything to ignore arguments. if (command.getName().equalsIgnoreCase("fakeabout")) will catch /fakeabout, and /fakeabout something.
     
  12. This is the line causing the NPE
    String fake = args0[0].toString();
    If you don't enter anything, thats going to be null. And you don't need to toString()
     
  13. Offline

    Evangon

    If you notice, I never use fake :/
    I forgot to remove it after I found out how to use arguments.
     
  14. Okay, then make sure you remove it :) Besides that, I don't see a problem :/
     
  15. Offline

    Evangon

    I got it working; I need returns at the "else"s.
    Case closed :3
     
  16. Add [Solved] to the title and edit the OP to have to solution
     
Thread Status:
Not open for further replies.

Share This Page