Search in sources :

Example 1 with PermissionAttachment

use of org.bukkit.permissions.PermissionAttachment in project Towny by ElgarL.

the class TownyPerms method assignPermissions.

/**
	 * Register a specific residents permissions with Bukkit.
	 * 
	 * @param resident
	 */
public static void assignPermissions(Resident resident, Player player) {
    PermissionAttachment playersAttachment = null;
    if (resident == null) {
        try {
            resident = TownyUniverse.getDataSource().getResident(player.getName());
        } catch (NotRegisteredException e) {
            // failed to get resident
            e.printStackTrace();
            return;
        }
    } else {
        player = BukkitTools.getPlayer(resident.getName());
    }
    if ((player == null) || !player.isOnline()) {
        attachments.remove(resident.getName());
        return;
    }
    TownyWorld World = null;
    try {
        World = TownyUniverse.getDataSource().getWorld(player.getLocation().getWorld().getName());
    } catch (NotRegisteredException e) {
        // World not registered with Towny.
        e.printStackTrace();
        return;
    }
    if (attachments.containsKey(resident.getName())) {
        playersAttachment = attachments.get(resident.getName());
    } else
        playersAttachment = BukkitTools.getPlayer(resident.getName()).addAttachment(plugin);
    try {
        synchronized (playersAttachment) {
            @SuppressWarnings("unchecked") Map<String, Boolean> orig = (Map<String, Boolean>) permissions.get(playersAttachment);
            /*
				 * Clear the map (faster than removing the attachment and
				 * recalculating)
				 */
            orig.clear();
            if (World.isUsingTowny()) {
                /*
					 * Fill with the fresh perm nodes
					 */
                orig.putAll(TownyPerms.getResidentPerms(resident));
            // System.out.print("Perms set for: " + resident.getName());
            }
            /*
				 * Tell bukkit to update it's permissions
				 */
            playersAttachment.getPermissible().recalculatePermissions();
        }
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    /*
		 * Store the attachment for future reference
		 */
    attachments.put(resident.getName(), playersAttachment);
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) PermissionAttachment(org.bukkit.permissions.PermissionAttachment) TownyWorld(com.palmergames.bukkit.towny.object.TownyWorld) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with PermissionAttachment

use of org.bukkit.permissions.PermissionAttachment in project Jobs by GamingMesh.

the class PermissionHandler method recalculatePermissions.

public void recalculatePermissions(JobsPlayer jPlayer) {
    Player player = plugin.getServer().getPlayer(jPlayer.getPlayerUUID());
    if (player == null)
        return;
    boolean changed = false;
    // remove old permissions
    String permName = "jobs.players." + player.getName();
    Permission permission = plugin.getServer().getPluginManager().getPermission(permName);
    if (permission != null) {
        plugin.getServer().getPluginManager().removePermission(permission);
        changed = true;
    }
    // Permissions should only apply if we have permission to use jobs in this world
    if (hasWorldPermission(player, player.getWorld().getName())) {
        List<JobProgression> progression = jPlayer.getJobProgression();
        // calculate new permissions
        HashMap<String, Boolean> permissions = new HashMap<String, Boolean>();
        if (progression.size() == 0) {
            Job job = Jobs.getNoneJob();
            if (job != null) {
                for (JobPermission perm : job.getPermissions()) {
                    if (perm.getLevelRequirement() <= 0) {
                        if (perm.getValue()) {
                            permissions.put(perm.getNode(), true);
                        } else {
                            /*
                                 * If the key exists, don't put a false node in
                                 * This is in case we already have a true node there
                                 */
                            if (!permissions.containsKey(perm.getNode())) {
                                permissions.put(perm.getNode(), false);
                            }
                        }
                    }
                }
            }
        } else {
            for (JobProgression prog : progression) {
                for (JobPermission perm : prog.getJob().getPermissions()) {
                    if (prog.getLevel() >= perm.getLevelRequirement()) {
                        /*
                             * If the key exists, don't put a false node in
                             * This is in case we already have a true node there
                             */
                        if (perm.getValue()) {
                            permissions.put(perm.getNode(), true);
                        } else {
                            if (!permissions.containsKey(perm.getNode())) {
                                permissions.put(perm.getNode(), false);
                            }
                        }
                    }
                }
            }
        }
        // add new permissions (if applicable)
        if (permissions.size() > 0) {
            plugin.getServer().getPluginManager().addPermission(new Permission(permName, PermissionDefault.FALSE, permissions));
            changed = true;
        }
    }
    // If the permissions changed, recalculate them
    if (!changed)
        return;
    // find old attachment
    PermissionAttachment attachment = null;
    for (PermissionAttachmentInfo pai : player.getEffectivePermissions()) {
        if (pai.getAttachment() != null && pai.getAttachment().getPlugin() instanceof JobsPlugin) {
            attachment = pai.getAttachment();
        }
    }
    // create if attachment doesn't exist
    if (attachment == null) {
        attachment = player.addAttachment(plugin);
        attachment.setPermission(permName, true);
    }
    // recalculate!
    player.recalculatePermissions();
}
Also used : Player(org.bukkit.entity.Player) JobsPlayer(com.gamingmesh.jobs.container.JobsPlayer) HashMap(java.util.HashMap) JobPermission(com.gamingmesh.jobs.container.JobPermission) JobProgression(com.gamingmesh.jobs.container.JobProgression) Permission(org.bukkit.permissions.Permission) JobPermission(com.gamingmesh.jobs.container.JobPermission) PermissionAttachmentInfo(org.bukkit.permissions.PermissionAttachmentInfo) Job(com.gamingmesh.jobs.container.Job) PermissionAttachment(org.bukkit.permissions.PermissionAttachment)

Aggregations

HashMap (java.util.HashMap)2 PermissionAttachment (org.bukkit.permissions.PermissionAttachment)2 Job (com.gamingmesh.jobs.container.Job)1 JobPermission (com.gamingmesh.jobs.container.JobPermission)1 JobProgression (com.gamingmesh.jobs.container.JobProgression)1 JobsPlayer (com.gamingmesh.jobs.container.JobsPlayer)1 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)1 TownyWorld (com.palmergames.bukkit.towny.object.TownyWorld)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Player (org.bukkit.entity.Player)1 Permission (org.bukkit.permissions.Permission)1 PermissionAttachmentInfo (org.bukkit.permissions.PermissionAttachmentInfo)1