How to check if a player has a permission starting with some string?

Discussion in 'Plugin Development' started by Diederikmc, Feb 26, 2013.

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

    Diederikmc

    two questions:
    How to check if a player has a permission starting with some string?
    And how to get the full permission if the player has a permission that starts with that string?

    I know how to check if a player has a permission and know Java, but I can't find out how to get a half permission. I hope someone can tell me how to do this. Or is it just not possible?
     
  2. Offline

    ZeusAllMighty11

    Well there's a method hasPermission(String perm) that returns a boolean

    And I don't know how to complete the permissions
     
  3. Offline

    Tirelessly

    Loop through player.getEffectivePermissions() and use startsWith()
     
  4. Offline

    Diederikmc

    Can you post a good example? I'm developing plugins now for 1,5 week, I know the basics of Java but I don't know how to "loop through" a set and get the strings I want with startsWith().
     
  5. Loop is basic Java, for() and while() are loops that can trigger the code inside their code block more than once.

    Code:
        public boolean matchPermission(Player player, String match)
        {
            for(PermissionAttachmentInfo permission : player.getEffectivePermissions())
            {
                if(permission.getPermission().startsWith(match) && permission.getValue())
                    return true;
            }
            
            return false;
        }
    The getValue() thing you might want to consider how you want it to be, I never used this code so you should test stuff out.
     
  6. I don't know what code you already have but I'd suggest creating a something.* permission rather than searching for all permissions and looking for one that starts with something.

    In your plugin.yml you could do something like this:
    Code:
    permissions:
      example.one:
        description: Command one!
      example.two:
        description: Command two!
      example..admin.one:
        description: Admin command one!
      example.*:
        description: Gives access to all non-admin example commands
        default: op
        children:
          example.one: true
          example.two: true
    If you need do need to check that they have a base permission node before continuing (Which I don't really see a need for unless it's a very specific corner case) then you should follow the method Digi has posted above.
     
  7. Offline

    Diederikmc

    Thanks, I will try it.


    I'm not stupid! XD I know how that works, but I want to check if a player has a permission like this nameoftheplugin.command.amounttouseit(1-100)
    So first I want to check if It begins with nameoftheplugin.command.
    And than I want to get that number after it(Already know how to strip the number of the permission)

    How could I get the permission that starts with some string as string?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  8. The "permission.getPermission()" method returns the permission string because I use startsWith() on it.... so use that in return and change return type to String.

    You really need to learn how to look for methods, if using Eclipse you can just type permissions. and press Ctrl+Space, that will print all available methods/vars for that pointer's class.
     
Thread Status:
Not open for further replies.

Share This Page