About permission attachment

Discussion in 'Spigot Plugin Development' started by Lifeonblack, Jun 5, 2017.

  1. Hello so I am trying to make a permission attachment and then I had this scratch now I don't have the permission I debug it in config it said the permission insert successfully I placed the insert debug message to insertpermission method so here is the code

    Code (Java):

    package org.lifeonblack;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.UUID;

    import org.bukkit.entity.Player;
    import org.bukkit.permissions.PermissionAttachment;

    public class PermissionsManager {
     
        private PermissionsManager () { }
     
        static PermissionsManager pmanager = new PermissionsManager();
        ConfigAccessor ca = ConfigAccessor.getInstance();
        private Map<UUID, PermissionAttachment> permissionsDataMap = new HashMap<UUID, PermissionAttachment>();
     
        public static PermissionsManager getInstance() {
            return pmanager;
        }
     
        public PermissionAttachment getPermissionData(Player player) {
            if(permissionsDataMap.containsKey(player.getUniqueId())) {
                return permissionsDataMap.get(player.getUniqueId());
            }
            PermissionAttachment permissionAttachment = player.addAttachment(MyPermissions.getInstance());
            permissionsDataMap.put(player.getUniqueId(), permissionAttachment);
            return permissionsDataMap.get(player.getUniqueId());
        }
     
        public void InsertPermission(Player player, String permission) {
            getPermissionData(player).setPermission(permission, true);
        }
     
        public void clear(Player player) {
            permissionsDataMap.remove(player.getUniqueId());
        }
     
        public void reload(Player player) {
            List<String> permissions;
         
            if(ca.getConfig().contains("data." + player.getName())) {
                permissions = ca.getConfig().getStringList("data." + player.getName() + ".permissions");
                return;
            }
            ca.getConfig().set("data." + player.getName() + ".uuid", player.getUniqueId().toString());
            ca.getConfig().set("data." + player.getName() + ".permissions", new ArrayList<String>());
            ca.save();
         
            permissions = ca.getConfig().getStringList("data." + player.getName() + ".permissions");
            for(String permission : permissions) {
                InsertPermission(player, permission);
            }
        }
     
     

    }
     

    Code (Java):

    package org.lifeonblack;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;

    public class PlayerListener implements Listener {
        ConfigAccessor ca = ConfigAccessor.getInstance();

        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
         
            PermissionsManager.getInstance().reload(player);
        }
     
        @EventHandler
        public void onQuit(PlayerQuitEvent e) {
            Player player = e.getPlayer();
         
            if(ca.getConfig().contains("data." + player.getName())) {
                PermissionsManager.getInstance().clear(player);
                return;
            }
        }
    }
     

    // data
    Code (Java):

    data:
      LifeonBlack:
        uuid: 529eb5b0-d942-487a-83fa-e8e45555c351
        permissions:
         - staffalarm.command
     
     
    #1 Lifeonblack, Jun 5, 2017
    Last edited: Jun 5, 2017