如何在else/if语句中忽略args[1]。

huangapple go评论86阅读模式
英文:

How to ignore args[1] in the else/if

问题

我试图为我的服务器创建一个虚假插件,但我不知道如何解决游戏中的这个错误:
"尝试执行此命令时发生内部错误"
这是因为我试图使用args[],我可以使用args[0]没有问题,但是,如果我放入args[1],就会出现这个错误(如果我在args[1]中不写任何内容,但如果我移除args[1]并且不在args[0]中写任何内容,它就可以工作)
我的代码示例:

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
    Player p = (Player)sender;
    String error = "§4§l404!";
    String noPerm = error + " §cYou don't have permission!";
    
    if(p.hasPermission("perm.fake")) {
        if(cmd.getLabel().equalsIgnoreCase("fake")) {
            if(args[0].equalsIgnoreCase("reset")) {
                p.setCustomName(p.getName());
                p.setCustomNameVisible(false);
                p.setDisplayName(p.getName());
                p.setPlayerListName(p.getName());
            } else if(args[0].length() == 0) {
                p.sendMessage(error + "§cYou need to specify a fake!");
            } else if(args[1].length() == 0) { //这里是我需要帮助的部分
                p.setCustomName(args[0].replace('&', '§'));
                p.setCustomNameVisible(true);
                p.setDisplayName(p.getCustomName()); //如果我在args[1]中不写任何内容,这部分不起作用
                p.setPlayerListName(p.getCustomName());
            } else if(args[1].equalsIgnoreCase("r")) {
                //这里有一段长代码,如果我在命令中写入字符"r",它会起作用
            }
        }
    } else {
        p.sendMessage(noPerm);
    }
    return false;
}

我只希望如果我在args[1]中不写任何内容,命令会设置一个没有特殊字符的虚假,而不是我放在"r"中的任何特殊字符。

英文:

I trying to do a Fake plugin to my server, but I don't know how to solve this error in game:
"An internal error occurred while attempting to perform this command"
It's because I trying to use args[], I can use args[0] with no problems, but, if I put args[1], this error occurs (If I don't write anything in args[1], but if I remove args[1] and don't write anything in args[0] it works)
An example of my code:

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
Player p = (Player)sender;
String error = "§4§l404!";
String noPerm = error + " §cYou don't have permission!";
if(p.hasPermission("perm.fake")) {
if(cmd.getLabel().equalsIgnoreCase("fake")) {
if(args[0].equalsIgnoreCase("reset")) {
p.setCustomName(p.getName());
p.setCustomNameVisible(false);
p.setDisplayName(p.getName());
p.setPlayerListName(p.getName());
} else if(args[0].length() == 0) {
p.sendMessage(error + "§cYou need to specify a fake!");
} else if(args[1].length() == 0) { **//Here is the part I need help**
p.setCustomName(args[0].replace('&', '§'));
p.setCustomNameVisible(true);
p.setDisplayName(p.getCustomName()); **//These part don't works if I dont write anything in args[1]**
p.setPlayerListName(p.getCustomName());
} else if(args[1].equalsIgnoreCase("r")) {
**//Here have a long code, it works if I write the char "r" in command**
}
}
} else {
p.sendMessage(noPerm);
}
return false;
}

I only want if I don't write anything in the args[1] the command sets the fake without anything special that I put in "r"

答案1

得分: 0

第一个解决方案: 更改条件语句
不要检查 args[1] 是否等于 0,因为这将触发异常,而是检查数组长度是否小于 2

} else if (args.length < 2) { **//这是我需要帮助的部分**

第二个解决方案: 检查参数数量
如果您想强制用户至少输入两个参数到命令中,您可以在函数的开头添加以下代码:

if (args.length < 2) {
    sender.sendMessage(ChatColor.RED + "您必须使用两个参数!");
    return false;
}

希望这有所帮助!

英文:

If you do not write at least two arguments then trying to access args[1] will result in ArrayOutOfBoundsException as the array does only contain one String (args[0]). To prevent that you have two workarounds:

First solution: Change the if condition
Instead of checking if args[1] equals 0 which will trigger an exception check if the array length is smaller than 2.

} else if(args.length &lt; 2) { **//Here is the part I need help**

Second solution: Check the number of arguments.
If you want to force the user to input at least two arguments to the command you can just add at the beginning of your function:

if(args.length &lt; 2) {
    sender.sendMessage(ChatColor.RED + &quot;You must use two arguments!&quot;);
    return false;
}

Let me know if it helps!

huangapple
  • 本文由 发表于 2020年8月8日 01:56:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63306983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定