Solved "Codes"

Discussion in 'Plugin Development' started by darthteddy1, Jan 29, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    darthteddy1

    Hello, I was wondering if there was a way, for me to be able to generate, let's say 8 character "codes" (numbers and letters) that I could give to a player and so on and so forth. The only trouble I'm having, is

    1) Actually generating a random "code"

    2) Authenticating a "code" so players don't just choose random numbers and letters

    Example of a code "a9w9dfq3"

    Any help?
     
  2. Offline

    Zombie_Striker

    @darthteddy1
    Lets say the codes consist of 4 letters and 2 numbers. Here's what you would do.
    1. Create a collection or array of letters/numbers (If you want it to be more secure, add lower case letters and upper case letters to the letters array)
    2. Create an instance of random
    3. Create a character by using "char c1 = letters.get(random.nextInt(letters.size))"
    4. Do this for all 6 chars.
    5. Turn the chars into one string.
    As for authenticating the code, I do not know how you would go about this. You can create a "try" counter, that after 3 times failing to guess the password, the user will no longer be allowed to enter that account.
     
  3. Offline

    Konato_K

  4. Offline

    darthteddy1

    thanks for the making a code, but to clear up for everyone, it's not for logging into accounts, it's for redeeming things :p
     
  5. Offline

    WolfMage1

    Code:
    char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    StringBuilder sb = new StringBuilder();
    Random random = new Random();
    for (int i = 0; i < 5; i++) {
        char c = chars[random.nextInt(chars.length)];
        sb.append(c);
    }
    Then just put it in a hashmap storing their uuid and the "code" see if it's the exact same then do what you want to do from there
     
  6. Offline

    WolfMage1

  7. Offline

    Xerox262

    @darthteddy1 Use RandomStringUtils as @Assist said (It's included in the bukkit api libraries), then use a HashMap to store what item/items they receive, a HashMap to store how many attempts a player has taken (Don't add an attempt if they get it right, otherwise they can't redeem more than a set amount and clear it after an amount of time), then to validate the code you just do HashMap#containsKey(String);
     
  8. Offline

    darthteddy1

    Thanks everyone, I got it! (Saving using a Hashmap: <UUID, String>)
     
Thread Status:
Not open for further replies.

Share This Page