[HELP] Getting an integer from a player's permission?

Discussion in 'Plugin Development' started by Tauryuu, Dec 31, 2011.

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


    For example, I would like to use the 100 part of the permission stone.iron.100 of a player. How would I do that?

    I also want to use that integer as a percentage AKA there will be more than 1 permission. (e.g. stone.iron.90, stone.iron.1, stone.iron.6, etc). How can I get it without setting up individual permissions?
  2. Offline


    You could split the string at the "." and then use the different parts. For example, consider this:

    1. String permission = "stone.iron.100";
    2. String[] parts = permission.split(".");
    3. // parts[0] = "stone"
    4. // parts[1] = "iron"
    5. // parts[2] = "100"
    6. // do other useful stuff

    Hope this helps. ;)
    Tauryuu likes this.
  3. Offline


    Thank you :D

    EDIT: @thescreem Would it be possible to get the integer's number without setting up each one individually? I want to use it as a percentage in the rest of the plugin.
  4. Offline


    you don't need to set them, by using split, it will return an array, every cell will contain part of the original string.
    In your case, spliting "stone.iron.100", will give you an array which contains the following strings :


    the location in the array is :
    parts[0] ==> "stone"
    parts[1] ==> "iron"
    parts[2] ==> "100"
  5. Offline


    No, I meant something like this:

    parts[0] = "stone"
    parts[1] = "iron"
    int a = parts[2]
    Would a get the last number (of 100)?
  6. Offline


    Well, yes and no. You'd have to parse parts[2] into an Integer because it's currently a String. To do that you use a simple method: int a = Integer.parseInt(parts[2]);

    To use even fewer lines of code, you could do:
    1. String permission = "stone.iron.100";
    2. int a = Integer.parseInt(permission.split(".")[2]);

    But if you want to do other things with the other parts of the permission, it would be more efficient to store all the split parts of the permission node in a String array, as shown above: String[] parts = permission.split(".");
  7. Offline


    Awesome. Thanks!
  8. Offline


    I didn'y say that parts[3] is equal to 100, I said that 100 is what stored in there as a string.
Thread Status:
Not open for further replies.

Share This Page