将args合并为Bukkit中的一句话?

时间:2014-03-10 19:02:50

标签: java command minecraft bukkit

我遇到的问题似乎很简单。我如何从命令中获得一个句子?

如果用户输入了/command i want to get this with spaces minus the command并按了回车,我需要这样做:

myString = "i want to get this with spaces minus the command";

我该怎么做?一个循环?那么最快,最有效的方法是什么?

感谢。这就是我到目前为止所做的:

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (cmd.getName().equalsIgnoreCase("command"))
    {   
        if(args.length > 0)
        {
            sender.sendMessage(ChatColor.RED + "/command <message>");
        }
        else
        {
            if (!(sender instanceof Player)) {
                sender.sendMessage("This command can only be run by a player.");
            } 
            else 
            {
                Player player = (Player) sender;
                // Check to make sure nothing is null or empty
                if(args[0].equals(" ") || args[0].equals("") || args[0] == null)
                {
                    // Do command !!
                }
                else
                {
                    player.sendMessage(ChatColor.RED + "Please enter a <message>");
                }
            }
        }
        return true;
    }
    return false;
}

4 个答案:

答案 0 :(得分:1)

代码:

String myString = "/command this is the text we want to grab right here";  
myString = myString.subStr( myString.indexOf( ' ' ) + 1);  

说明:

因此,如果您有 String myString =“/命令这是我们想要的”,那么您可以使用.subStr()分隔第一个单词。 subStr()有两个版本,一个有一个参数,它是起点,给你到结尾的字符串,另一个有两个参数,它们是起点和终点,它们给你这些位置之间的字符串。

由于我们从特定位置到结尾获取字符串,因此我们使用第一个方法和一个参数。那个参数是什么?我们知道我们想要的第一个字符就在/ after之后的空格之后。所以它是行中第一个空格之后的位置。我们如何获得第一个空间的位置? Easy,String有一个内置的方法 indexOf(),它可以接收一个String或一个char,并返回我们传入的第一次出现的位置。 / p>

我们将char''传递给indexOf(char c),因为我们想要找到第一个空格的位置。因为一旦我们知道第一个空间的位置,我们就可以在那之后找到一个并且具有我们所需要的开始位置。然后我们将它传递给subStr(int location)以获取从该位置到结尾的字符串,这就是我们想要的。

所以这一切听起来都非常复杂,但代码是不言自明的:

String myString = "/command this is the text we want to grab right here";  
myString = myString.subStr( myString.indexOf( ' ' ) + 1);

答案 1 :(得分:1)

你可以通过使用for循环来轻松地获取/command之后的所有字符串,这些字符串都作为参数处理。这是一个例子:

String myString = ""; //we're going to store the arguments here    

for(int i = 0; i < args.length; i++){ //loop threw all the arguments
    String arg = args[i] + " "; //get the argument, and add a space so that the words get spaced out
    myString = myString + arg; //add the argument to myString
}

现在你有了你的字符串,你可以用它做任何事情,比如将消息发送给命令发送者:

sender.sendMessage(myString);

上面代码的简短解释是,首先,我们循环抛出所有参数(/command之后的所有参数,然后,我们在参数的末尾添加一个空格,最后,我们将参数放入字符串myString ......这是一个实现的例子:

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {       
    if(cmd.getName().equalsIgnoreCase("command")){
        String myString = "";

        for(int i = 0; i < args.length(); i++){
            String arg = args[i] + " ";
            myString = myString + arg;
        }

        sender.sendMessage(myString); //send the message to the command sender.
    }
}

您还可以使用以下命令检查命令是否包含参数:

if(args.length != 0){

使用命令的一个例子是,假设玩家输入/command bar foo。然后播放器将收到消息bar foo

答案 2 :(得分:0)

您可以使用String Builder轻松完成此操作。

public String getArgs(String[] args, int num){ //You can use a method if you want
    StringBuilder sb = new StringBuilder(); //We make a String Builder
    String prefix = ""; //We're going to store the space here
    for(int i = num; i < args.length; i++) { //We get all the arguments with a for loop
        sb.append(prefix); //We apply the space
        prefix = " "; //We save the space in our prefix string
        sb.append(args[i]); //We add the argument
    }
    return sb.toString(); //We return the message
}

public String getArgs(String[] args, int num){ //You can use a method if you want
    StringBuilder sb = new StringBuilder(); //We make a String Builder
    for(int i = num; i < args.length; i++) { //We get all the arguments with a for loop
        sb.append(args[i]).append(" "); //We add the argument and the space
    }
    return sb.toString().trim(); //We return the message
}

我最喜欢第二种方法:3

&#34; int i = num&#34;是我们想要开始全部获取它们的参数的数量,例如: 你有这样的命令:/ announce add message(message)。

消息从args [2]开始,所以我们会这样做:getArgs(args, 2)

答案 3 :(得分:0)

如果你想在拥有一个多字参数之前有一个单词参数,你可能希望从某个参数开始遍历每个参数。

for(int i = 1; args.length > 1; i++){
    String argsString = args[i] + " ";
}

要将所有给出的参数作为一个参数,您只需执行此操作:

String argsString = args.toString();