Item unique identifiers

Discussion in 'Plugin Development' started by iSexyChocobo, Sep 17, 2014.

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

    iSexyChocobo

    Heyo.. I'd like to create a random number ( optionally with the letters a-f) , make sure it hasn't been used already and then store it in an YML.. After that I'd like to split it up into color codes. Lets say the number was 123456789 , then it would be split up to the string &1&2&3&4&5&6&7&8&9

    I will be able to create the random number.. but I don't know how to store it in the yml and then check if it's unique.. I can't split it up to color codes either.

    Please help :)
     
  2. Offline

    fireblast709

    Create a random int and convert that to a hex String
     
  3. iSexyChocobo
    To see if it has been used already, can't you just check if the config contains the number? E.g. if you store them like this
    Code:
    path:
      to:
        numbers:
        - 123456789
        - 234567891
    you can check if the integerlist contains the number with this
    Code:
    int number = 123456789;
     
    List<Integer> integerList = configObject.getIntegerList("path.to.numbers");
    boolean unique = !integerList.contains(number);
    
    As for splitting up into color codes:
    Code:
    int number = 123456789;
    String s = "" + number;
     
    StringBuilder builder = new StringBuilder();
     
    for (int i = 0; i < s.length(); i++) {
        builder.append("&" + s.charAt(i));
    }
     
    String result = builder.toString();
    result in this case would be:
    Code:
    &1&2&3&4&5&6&7&8&9
     
    iSexyChocobo likes this.
  4. Offline

    fireblast709

    Assist if you want to guarantee uniqueness, just start with 0 and increment after each use :p
     
  5. Offline

    iSexyChocobo

    Assist

    Awesome, thanks for the helpful reply!

    How would I go on doing it the other way, let's say the number is &1&2&3&4&5&6&7&8&9

    and I want it translated to 123456789 ?
     
  6. iSexyChocobo
    You can simply use replaceAll() on the string to remove them:
    Code:
    String string = ...;
    string = string.replaceAll("&", "");
    this will replace all "&" with "", aka empty.
     
  7. Offline

    Garris0n

    The replaceAll() method takes regex, and this has nothing to do with regex. You should use replace() instead.
     
    ChipDev likes this.
  8. Garris0n
    True, but they both work the same. I'm just used to using replaceAll() even when not using regex.
     
  9. Offline

    Garris0n

    They don't work the same, one takes regex. For one, that means you have to escape any accidental regex (use Pattern.quote() for this, by the way). I'd also assume that replace() is more efficient, but I can't confirm that. Either way, as much as I love consistency, using replace() instead of replaceAll() is necessary.
     
  10. Garris0n
    I meant that they both work the same in this case, where you need to replace a single character number or a symbol. Using replace() here is not necessary, and probably never is as replaceAll() serves the same functionality but with more features. Of course this is just my preference.
     
  11. Offline

    Garris0n

    Using replaceAll() is the thing that is not necessary. It's for regex. You're not using regex. Escaping and then parsing regex instead of just using the method that accepts a normal string in the first place is ridiculous, and you're teaching impressionable new developers that it's a good idea, which is going to end up with them using replaceAll() for everything and coming here complaining because they tried to use replaceAll() with a string that contains a regex character and it didn't work.
     
  12. Offline

    teej107

  13. teej107
    Thanks, although I'm already familiar with the methods.

    Garris0n
    I'm not saying that it's a good idea to use replaceAll over replace when you don't have a regex, I'm just saying that it's unnecessary, especially IN THIS CASE. And as I said, it's MY preference. I find no need to use replace over replaceAll in any case.

    I don't really get why you're making such a big deal out of this. The new developers who may come across the regex problem will then learn from it, and the ones who know about it, know how to avoid it.
     
  14. Offline

    Garris0n

    Your preference is objectively wrong. The method replaceAll() is meant to take regex, and using it when you should be using replace() is incorrect. Doing this on a forum and suggesting to new developers that it's okay is significantly worse.
    New developers tend to assume replaceAll() replaces "everything" and replace() replaces the first, as replaceAll() has a strange and misleading name. You should be setting a good example, and you're not.
     
    Assist likes this.
  15. Garris0n
    I aim to set a good example, but sometimes preferences take over the reality. I think this has gone too far already, you've made some good points here and what I'm doing is wrong, however I'm going to continue using it myself as it causes no harm to me, but I will attempt to provide the correct method in the future. Thanks and sorry.
     
  16. Offline

    iSexyChocobo

    Assist Garris0n

    Thanks for clarifying that :) While your argument may not have done anything for the better, the fact that the point was brought up certainly did!

    One last question (I think :p)
    How would I detect that a string contains a sequence of 9 numbers (in a row) ?

    example,
    Hello123456789Hello and 123456789 and so on.. would return true
    while Hello12Hello3456789 and Hello12345678Hello and so on.. would return false
     
  17. iSexyChocobo
    Perhaps loop through the characters of the string 1 by 1, then increment an integer if the character is a number, if not, reset the integer to 0. Once done, check if the integer is (more or) equal to 9.
     
Thread Status:
Not open for further replies.

Share This Page