Solved Clear all permissions from a player

Discussion in 'Spigot Plugin Development' started by Kingbluesapphire, Apr 22, 2016.

  1. For a permissions plugin I'm making i use,

    Code (Text):
        public static void addPermission(Player player, String permission){
            HashMap<String, PermissionAttachment> perms = new HashMap<String, PermissionAttachment>();
            PermissionAttachment attachment = player.addAttachment(ServerCore.getPlugin());
            perms.put(player.getName(), attachment);
            PermissionAttachment pperms = perms.get(player.getName());
            pperms.setPermission(permission, true);
        }
    to add a permission to a player... But i need to know how to remove ALL permissions from a player after i add some

    I tried using

    Code (Text):
        public static void removePermission(Player player, String permission){
            HashMap<String, PermissionAttachment> perms = new HashMap<String, PermissionAttachment>();
            PermissionAttachment attachment = player.addAttachment(ServerCore.getPlugin());
            perms.put(player.getName(), attachment);
            perms.get(player.getName()).unsetPermission(permission);
        }
    to remove permissions but that only removes one and i would like to remove all the permissions they have anyone know how?
     
  2. have a for loop iterate over an array of strings.

    String[] allperms = {"test.perm1", "test.perm2", "test.perm3"}; // or, if you have such a method, getAllPerms()

    for(String perm : allperms){
    if(p.hasPermission(perm){
    instance?.removePermission(p, perm);
    }
    }
     
  3. Im looking for a way in bukkit to get all perms they have to remove. I pretty much want to clear all their perms
     
  4. I think there is a getEffectivePermissions method or something similar in the player class, try iterating over that and removing the players permissions.
     
  5. I tried:

    Code (Text):
        public static void removeAllPerms(Player player){
            HashMap<String, PermissionAttachment> perms = new HashMap<String, PermissionAttachment>();
            PermissionAttachment attachment = player.addAttachment(ServerCore.getPlugin());
            perms.put(player.getName(), attachment);
            for(PermissionAttachmentInfo permss : player.getEffectivePermissions()){
            perms.get(player.getName()).unsetPermission(permss.toString());
            }
        }
    and it didnt work... But if i relog it works
     
  6. This may not help due to I can't think of it off the top of my head:

    Isn't there a way in Permissionable to update the player?
     
  7. I don't know what class/methods to look under for this but i'd expect there to be something to "refresh" or "update" the player
     
  8. So i started using:

    Code (Text):
        static HashMap<String, PermissionAttachment> perms = new HashMap<String, PermissionAttachment>();

        public static void addPermission(Player player, String permission){
            PermissionAttachment attachment = player.addAttachment(ServerCore.getPlugin());
            //attachment.setPermission(permission , true);
            perms.put(player.getName(), attachment);
            PermissionAttachment pperms = perms.get(player.getName());
            pperms.setPermission(permission, true);
        }

        public static void removePermission(Player player, String permission){
            perms.get(player.getName()).unsetPermission(permission);
        }

        public static void removeAllPerms(Player player){
            for(PermissionAttachmentInfo s : player.getEffectivePermissions()){
                Bukkit.getServer().broadcastMessage(s.getPermission());
                removePermission(player, s.getPermission());
            }
        }
    And an error comes up for "perms.get(player.getName()).unsetPermission(permission);" when i use the removeAllPerms method
     
  9. What is the error? I'm not sure looping over a set is safe while modifying it.

    Maybe something like this...

    Code (Text):

        public void removePermissions(Player player) {
            // Clone into a new Set
            Set<PermissionAttachmentInfo> permissions = new HashSet<PermissionAttachmentInfo>(player.getEffectivePermissions());
           
            // Loop over and remove attachments
            for (PermissionAttachmentInfo permission : permissions) {
                player.removeAttachment(permission.getAttachment());
            }
        }
     
     
  10. I tried it but it returned this error:
    "Caused by: java.lang.IllegalArgumentException: Attachment cannot be null"
     
  11. Did some research for you, something like this should work.

    Code (Text):
        // Use this method to get the PermissionAttachment to add/remove your permissions
        private static Map<UUID, PermissionAttachment> permissionMap = new HashMap<UUID, PermissionAttachment>();
        public static PermissionAttachment getPermissionAttachment(Player player) {
            if ( ! permissionMap.containsKey(player.getUniqueId())) {
                permissionMap.put(player.getUniqueId(), player.addAttachment(YourPlugin.get()));  // Your JavaPlugin instance
            }
         
            PermissionAttachment attachment = permissionMap.get(player.getUniqueId());
         
            return attachment;
        }
     
        public void removePermissions(Player player) {
            // Clone into a new Set
            Set<PermissionAttachmentInfo> permissions = new HashSet<PermissionAttachmentInfo>(player.getEffectivePermissions());
                 
            // Loop over and remove attachments
            for (PermissionAttachmentInfo permissionInfo : permissions) {
                String permission = permissionInfo.getPermission();
             
                getPermissionAttachment(player).unsetPermission(permission);
            }
        }
     
  12. Thanks so much it worked perfectly. :D
     
    • Like Like x 1
  13. Update:

    So I've been using and haven't noticed any errors, when i add a permission to a player it works but it doesn't remove a players permissions i noticed which is a problem for demoting players.

    I'm using:

    Code (Text):
    private static Map<UUID, PermissionAttachment> permissionMap = new HashMap<UUID, PermissionAttachment>();

        public static void addPermission(Player player, String permission){
            getPermissionAttachment(player).setPermission(permission, true);
        }

        public static PermissionAttachment getPermissionAttachment(Player player) {
            if ( ! permissionMap.containsKey(player.getUniqueId())) {
                permissionMap.put(player.getUniqueId(), player.addAttachment(ServerCore.getPlugin()));
            }
            PermissionAttachment attachment = permissionMap.get(player.getUniqueId());
            return attachment;
        }

        public static void removePermissions(Player player) {
            Set<PermissionAttachmentInfo> permissions = new HashSet<PermissionAttachmentInfo>(player.getEffectivePermissions());    
            for (PermissionAttachmentInfo permissionInfo : permissions) {
                String permission = permissionInfo.getPermission();
                getPermissionAttachment(player).unsetPermission(permission);
            }
        }
    It's not giving an error in the console it just never removes the players permission
     
  14. Ive seen this code before..
     
  15. I mean it's 2 posts up...
     
  16. No, I mean i've seen this exact code in a video..
     
  17. I had to do this myself not too long ago, its done by using setPermission

    Code (Text):

    String permission = "some.perm";
    Boolean permissionValue = true;

    // blah blah blah.. get attachment

    attachment.setPermission(permission, permissionValue);
     
    So to give them the permission, change the permissionValue to true. To remove the permission, change the permissionValue to false.

    Hope that makes sense :)
     
  18. This seems to work it removes the permissions... but now if the player re-logs it doesn't add the permissions back, even if i change they rank (Re add the permissions/ add differnent ones) it doesnt add them...

    I'm using

    Code (Text):
    public static void removePermissions(Player player) {
            Set<PermissionAttachmentInfo> permissions = new HashSet<PermissionAttachmentInfo>(player.getEffectivePermissions());    
            for (PermissionAttachmentInfo permissionInfo : permissions) {
                String permission = permissionInfo.getPermission();
                getPermissionAttachment(player).unsetPermission(permission);
                getPermissionAttachment(player).setPermission(permission, false);
            }
        }
     
        public static void addPermission(Player player, String permission){
            getPermissionAttachment(player).setPermission(permission, true);
        }
    To apply / remove it but its not applying...

    Edit: Doesn't work until i reload the server
     
  19. You don't need to use unsetPermission
     

Share This Page