Problème de plugin Java Minecraft-Ne répond pas à l'instruction if?
Je fais donc un plugin de rachat de code simple pour un serveur Minecraft. Ce qui est bizarre, c'est que lorsque je tape /rachète (le code valide), rien ne se passe, bien que ce soit censé le faire... Le code valide est le code a entré dans la configuration des plugins par l'utilisateur.
Voici mon code...
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;
}
Le problème se produit sur "if (args[0] = = validCode1 && hasUsed == null)". Le code qui est censé se produire si ces deux choses vérifient, ne se produit pas et je ne sais pas pourquoi.
1 answers
Assurez-vous d'utiliser equals()
lors de la comparaison de chaînes. L'utilisation de commandChosen1 == "xp"
compare les références de chaîne et non les valeurs; utilisez commandChosen1.equals("xp")
ou si vous préférez "xp".equals(commandChosen1)
.
Aussi,
Bien qu'il soit possible d'utiliser un this.getConfig().getString()
avec une valeur de clé qui contient des espaces, cela peut rendre les fichiers de configuration difficiles à lire et encombrés. Chaque fois que je conçois des plugins, je conçois ma configuration.yml en tant que tel
VoteGUI:
message: 'hello'
, puis exécutez un this.getConfig().getString("VoteGUI.message");
Pour le vôtre, je suggérerais quelque chose comme ça
Promo-Codes:
validCode1: 'insert code here'
validCode2: 'insert code here'
validCode3: 'insert code here'
Puis mettre dans votre onCommand
méthode:
String validCode1 = this.getConfig().getString("Promo-Codes.validCode1");
String validCode2 = this.getConfig().getString("Promo-Codes.validCode2");
String validCode3 = this.getConfig().getString("Promo-Codes.validCode3");
Si cela ne résout pas le problème, copiez et collez l'exception levée à partir de la console et je peux être d'une aide supplémentaire