Get Second Command?

Discussion in 'Plugin Development' started by mrrabbit, May 15, 2011.

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

    mrrabbit

    I am trying to make a plugin. And to make that I need to know how to get a second command (sub-command). Can someone tell me the easiest way to get the second command. I want to make it like this.

    A player types:

    Code:
    /<command> <second command>
    And the <second command> converts into a string. And then I can use the string. I know that I on some way most split the command by space. But I cant get it to work :(
     
  2. Offline

    halvors

    args[1] if you use onCommand().
     
  3. Offline

    mrrabbit

    Thanks, but why aint this working then? :

    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
            String[] split = args;
            String commandName = command.getName().toLowerCase();
                if (sender instanceof Player) {
                    Player player = (Player) sender;
    
                    // If using the command /MyDesc
                    if(commandLabel.equalsIgnoreCase("startnew")){
                        System.out.println(args[1]);
                    }
                }
                return true;
            }
    
    error code:

    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'star
    tnew' in plugin FirstOne v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:37)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:85
    )
            at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:2
    78)
            at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.
    java:699)
            at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:664)
    
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:658)
            at net.minecraft.server.Packet3Chat.a(Packet3Chat.java:32)
            at net.minecraft.server.NetworkManager.a(NetworkManager.java:196)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:75)
            at net.minecraft.server.NetworkListenThread.a(SourceFile:100)
            at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:372)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:287)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:394)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
            at com.bukkit.TryToGuess.NumberOne.NumberOne.onCommand(NumberOne.java:58
    )
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:35)
            ... 12 more
    13:12:31 [INFO] Connection reset
     
  4. Offline

    halvors

    Here is a example code, hope you understand it :)

    Code:
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (sender instanceof Player) {
            Player player = (Player)sender;
    
    	if (args.length == 0) {
                System.out.println(args[0]);
    
                return true;
            } else {
                String subCommand = args[0];
    
                if (subCommand.equalsIgnoreCase("argumentcommandhere")) {
                    // Do something here.
    
                    return true;
                }
            }
        }
    
        return false;
    }
    
     
  5. Offline

    Kekec852

    because array starts at 0 and to at 1
     
  6. Offline

    mrrabbit

    Thanks for your patiens :D But I cant get it to work. How do i do if I... let say, wants to make a plugin that when you write:

    Code:
    /iwrote Hej 
    A broadcast Message run that broadcasts.

    Code:
    Playername wrote Hej
    How do I do that? I knows how to broadcast and get playername but how do i load "Hej" into a string-variable? Can someone give me an example? Thanks for everything so far :)
     
  7. Offline

    halvors

    That would be:
    Code:
    @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (sender instanceof Player) {
            Player player = (Player)sender;
    
            if (args.length == 0) {
                // Main command
    
                 return true;
            } else {
                String subCommand = args[0];
    
                if (subCommand.equalsIgnoreCase("iwrote")) {
                    player.sendMessage(player.getName() + " wrote " + args[1]);
                    return true;
                }
            }
        }
    
        return false;
    }
    
     
  8. Offline

    mrrabbit

    The only thing that happens when I type:

    Code:
    /iwrote hej
    Is that it sends information from the plugin.yml, it say like this into the chat:

    Code:
    /iwrote <subcommand>
    How should the plugin.yml file be. I have it like this for the moment:

    Code:
    name: FirstOne
    main: com.bukkit.TryToGuess.NumberOne.NumberOne
    version: 1.0
    commands:
      iwrote:
        description: Writes out text
        usage: /<command> <subcommand>
    
    What is wrong? :/
     
  9. Offline

    halvors

    Do you use a CommandExecutor?
     
  10. Offline

    cjc343

    Code:
    @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
         if (sender instanceof Player) {
             Player player = (Player)sender;
              if (args.length == 0) {
                 // Main command
                   return true;
             } else {
                 String subCommand = args[0]; // subCommand now contains the first argument to the /iwrote command
                  if (subCommand.equalsIgnoreCase("iwrote")) { // if subcommand is iwrote (so /iwrote iwrote )
                      player.sendMessage(player.getName() + " wrote " + args[1]); // send the player a message with the second argument in "/iwrote iwrote ____"
                     return true;
                 }
             }
         }
          return false; // send the yml stuff to the sender
     }
    
    I commented up the code. Note that if you use the command '/iwrote iwrote' with no args[1] this will cause a NPE.
     
  11. Offline

    mrrabbit

    Okey, thanks :D Now its working. But I dont understand why I have to type:

    Code:
    /iwrote iwrote hej
    instead of:

    Code:
    /iwrote hej
    Thanks anyway. Thanks!:D
     
  12. Offline

    cjc343

    That's just how the code is written. You can change the code.
     
Thread Status:
Not open for further replies.

Share This Page