Resource [Tutorial] Giving/Removing player permissions!

Discussion in 'Spigot Plugin Development' started by VenomEater552, Mar 7, 2015.

  1. Note: This was created on March 7th, 2015. There may be some incorrect information so always be sure to check your work as you always should.

    Hello spigot! I felt as if I should give back to the community, so here is a tutorial on permissions!

    Interested in giving players permissions within your plugin? Have you found out that the bukkit tutorial is too confusing?
    Look no further. I'm going to show you how to give players permissions within your bukkit/spigot plugin.

    First, you're going to want to make a HashMap that will store our PermissionAttachments we give to our players. This will hold information for the permissions we are going to set.

    Code (Text):
    HashMap<UUID, PermissionAttachment> perms = new HashMap<UUID, PermissionAttachment>();
    Then, you want to get the player. Obviously, this will be important.

    Code (Text):
    Player player = /* Player here */
    We then want to make a PermissionAttachment and add the player and their attachment to the hashmap.

    Code (Text):
    PermissionAttachment attachment = player.addAttachment(/* class that extends JavaPlugin here */);
    perms.put(player.getUniqueId(), attachment);
    Now that we have the player and their PermissionAttachment, we can set and unset permissions.

    Code (Text):
    PermissionAttachment pperms = perms.get(player.getUniqueId());
    pperms.setPermission("", true);
    If you want to remove a permission, do not setPermission to false. This is different from removing the permission. Do as follows.

    Code (Text):
    That should be about it. You can implement this into your code however you want. The best way to store permissions to add for players is generally to add a string list in a config, and get each string from the string list and set it for the player.

    Thank you for reading, and I hope this helped anyone who needed a tutorial on this. :)

    Pastebin tutorial:
    #1 VenomEater552, Mar 7, 2015
    Last edited: Oct 14, 2016
    • Useful x 18
    • Like x 9
    • Informative x 7
    • Agree x 1
    • Winner x 1
    • Agree Agree x 2
  2. Thx ill keep this in favorites!
    • Agree Agree x 3
  3. Is there a way i can reload the permissions attachment, without reloading the server ?
  4. Winner ! + bump this is cool thanks for your effort to repost it here because some of spigot member is lazy to go at google .
    • Optimistic Optimistic x 1
  5. Bootiful, best thing on this is you can just call Player#hasPermission(String) and done, no 3th party libs checking.
  6. bump so this thread would be keep :p
  7. This will be good for the newbies, its very nice of you to make this tutorial/
  8. lol @MaxGenre
    You won't have to bump on a tutorial.. People will find it when they search for it.
  9. because without bumping it would be archived automatically
  10. Nope. Just don't bump lol. When they search on google Spigot Tutorial removing permissions or something they get this Tutorial and when they reply it will be bumped (so on top of this forum).
  11. ok . : D Then
  12. Yes, does the server reload the permissions when it is changed?
    If not, how would I reload the permissions attachment?
  13. Rbt


    It depends on what you are doing. If you want to reset the permissions on reload, just empty it out or declare it as a new list. If you want to save it, export it to config.
  14. Hi ! I just would like to add the permission* to player when something, but I can't find the code line to add this permission to someone (this permission isn't in my plugin)
    • Funny Funny x 2
  15. Check when the player changes a world, and then add/remove permissions from them.
    • Winner Winner x 1