I've been developing for a CraftBukkit Minecraft server. This gives me a good API to work with. This API includes the onCommand method, which allows me to implement new commands.
I've just been trying to do that, but when I send a command such as /example arg0, for some reason I can't compare with arg0 from my code. I'm trying to check for when args[0] == "on", yet although args and args[0] are both defined and I set args[0] to "on", the comparison still returns false.
private CBPlugin pluginReference;
public NerfTntExecutor(CBPlugin plugin) {
// (...)
pluginReference = plugin;
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender.hasPermission("command.op.nerftnt")) {
if(args.length > 0) {
// (...)
sender.sendMessage(args[0]);
sender.sendMessage(args[0].getClass().getName());
if(args[0] == "on")
sender.sendMessage("true");
return true;
}
else {
sender.sendMessage("Not enough arguments!");
return false;
}
}
else {
pluginReference.getLogger().info("No permission");
return false;
}
}
The sender.sendMessage calls display the data given back to the person who sent the command. In this case, args[0] is displayed as "on" and args[0].getClass().getName() gets shown as java.lang.String - the type of the argument. However, the next line if(args[0] == "on") is false.
Why is this? I seem to be comparing a String to another String, and they both have the same contents. Why should a comparison of the two return false?