2

So I'm making a simple code redemption plugin for a Minecraft server. What's weird is when I type /redeem (the valid code), nothing happens, although it's supposed to... The valid code is the a code entered into the plugins configuration by the user.

Here's my 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;
}

The problem occurs on "if (args[0] == validCode1 && hasUsed == null)". The code that's supposed to happen if both those things check out, doesn't happen and I have no clue why.

4Dimensions
  • 83
  • 1
  • 10
  • 1
    use `.equals()` instead of `==`. – IQV Jan 30 '17 at 14:24
  • 3
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – rmlan Jan 30 '17 at 14:26

1 Answers1

1

Make sure to use equals() when comparing Strings. Using commandChosen1 == "xp" compares string references not values; use commandChosen1.equals("xp") or if you prefer "xp".equals(commandChosen1).

Also,

While it is possible to use a this.getConfig().getString()with a key value that contains spaces, it can make configuration files hard to read and cluttered. Whenever I design plugins I'll design my config.yml as such

VoteGUI:
  message: 'hello'

and then run a this.getConfig().getString("VoteGUI.message");

For yours I'd suggest something like this

Promo-Codes:
    validCode1: 'insert code here'
    validCode2: 'insert code here'
    validCode3: 'insert code here'

and then put this in your onCommand method:

String validCode1 = this.getConfig().getString("Promo-Codes.validCode1");
    String validCode2 = this.getConfig().getString("Promo-Codes.validCode2");
    String validCode3 = this.getConfig().getString("Promo-Codes.validCode3");

If this does not resolve the issue, copy and paste the exception being thrown from the console and I may be of further assistance

Frelling
  • 3,287
  • 24
  • 27
Christian
  • 52
  • 1
  • 13