Best way to check for highest permission ?

Discussion in 'Spigot Plugin Development' started by Drawethree, Jun 3, 2018.

  1. Hey guys, what's the best way to check for player's highest permission ? Like party.size.5
     
  2. Code (Text):
    int playerAmount;
    for(int count = 0; count <= 99; ++count) // I used 99 but that should be the max amount possible
    {
        if(playerAmount != null
        && !player.hasPermission("party.size."+count.toString())
            break;
        if(player.hasPermission("party.size."+count.toString())
            playerAmount = count;

    }
    this is how I would do it.
     
  3. You can use Player#getEffectivePermissions to get all permissions and then filter them out and fin the highest.

    Something along the lines of this should work:
    Code (Java):
    player.getEffectivePermissions().stream().filter(pai -> pai.getPermission().startsWith("party.size.")).mapToInt(pai -> Integer.parseInt(pai.getPermission().substring(11))).max().orElse(0);
    may contain errors, did this without an IDE

    Will return the highest party size value the player currently has, or, if the player doesn't have any, zero.
     
    • Creative Creative x 1
  4. I use LuckPerms as permissions plugin, because you can set a meta and then easily access it within code.
     
  5. I do it this way as well, except I iterate from highest to lowest and break out of the loop as soon as a match is found.
     
  6. You should consider changing your method.
    If for example a player had the permission for size 2 and 5 then he would get the size 2 set to him. Re-read your method and you will understand why. So going from top to bottom is much wiser.
     
  7. never actually made any plugins that need this, hence the way i "would" do it. But yeah that is a valid point.