Java Minecraft插件问题-不响应if语句?
因此,我正在为Minecraft服务器制作一个简单的代码赎回插件。奇怪的是,当我输入/兑换(有效代码)时,什么都没有发生,尽管它应该。。。有效代码是用户在插件配置中输入的代码 这是我的密码Java Minecraft插件问题-不响应if语句?,java,plugins,minecraft,Java,Plugins,Minecraft,因此,我正在为Minecraft服务器制作一个简单的代码赎回插件。奇怪的是,当我输入/兑换(有效代码)时,什么都没有发生,尽管它应该。。。有效代码是用户在插件配置中输入的代码 这是我的密码 public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { //Assigns the commands chosen in config to strings
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
//Assigns the commands chosen in config to strings
String commandChosen1 = this.getConfig().getString("Command for code 1");
String commandChosen2 = this.getConfig().getString("Command for code 2");
String commandChosen3 = this.getConfig().getString("Command for code 3");
//Assigns the codes to strings
String validCode1 = this.getConfig().getString("Valid Code 1");
String validCode2 = this.getConfig().getString("Valid Code 2");
String validCode3 = this.getConfig().getString("Valid Code 3");
//If the redeem command is sent from a player
if(cmd.getName().equalsIgnoreCase("redeem") && sender instanceof Player)
{
//Casts the sender to a new player.
Player player = (Player) sender;
//Creates object hasUSed to store whether or not the player has already redeemed a code
Object hasUsed = this.getConfig().get(player.getName());
//Gives an error message of the arguments don't equal 1.
if(args.length != 1)
{
player.sendMessage(ChatColor.DARK_RED + "Please enter a valid promo code. Find them on our twitter!");
}
if(args.length == 1)
{
//If the player hasn't used the code yet and the arguments given are equal to a code then give them the reward...
if(args[0] == validCode1 && hasUsed == null)
{
this.getConfig().set(player.getName(), 1);
player.sendMessage(ChatColor.GREEN + "Promo code successfully entered!");
if(commandChosen1 == "xp")
{
Bukkit.dispatchCommand(player, commandChosen1 + getConfig().getString("XP Given") + "L" + " " + player.getName());
}
}
}
return true;
}
return false;
}
问题发生在“if(args[0]==validCode1&&hasUsed==null)”上。如果这两种情况都发生了,那么应该发生的代码没有发生,我也不知道为什么。在比较字符串时,请确保使用
equals()
。使用commandChosen1==“xp”
比较字符串引用而不是值;使用commandChosen1.equals(“xp”)
或者如果您更喜欢“xp”.equals(commandChosen1)
而且
虽然可以将this.getConfig().getString()
与包含空格的键值一起使用,但这会使配置文件难以读取和混乱。每当我设计插件时,我都会这样设计我的config.yml
VoteGUI:
message: 'hello'
然后运行一个this.getConfig().getString(“VoteGUI.message”)代码>
对于你的,我建议这样做
Promo-Codes:
validCode1: 'insert code here'
validCode2: 'insert code here'
validCode3: 'insert code here'
然后将其放入您的onCommand
方法中:
String validCode1 = this.getConfig().getString("Promo-Codes.validCode1");
String validCode2 = this.getConfig().getString("Promo-Codes.validCode2");
String validCode3 = this.getConfig().getString("Promo-Codes.validCode3");
如果这不能解决问题,请复制并粘贴从控制台引发的异常,我可能会提供进一步帮助使用.equals()
而不是=
。将字符串与.equals()
进行比较的可能重复不是Minecraft的特定要求。这对于任何纯Java代码都是如此。我知道,我只是说minecraft在使用==时对“是”和“不等于”更挑剔。这是一个不准确的语句,因为minecraft并没有规定相等,Java是。我很清楚这一点。