1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-29 14:05:25 +01:00
Jobs/com/gamingmesh/jobs/PermissionHandler.java

250 lines
7.4 KiB
Java
Raw Normal View History

2015-08-21 15:10:08 +02:00
/**
* Jobs Plugin for Bukkit
* Copyright (C) 2011 Zak Ford <zak.j.ford@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.gamingmesh.jobs;
import java.util.HashMap;
import java.util.List;
2016-06-10 17:25:40 +02:00
import java.util.Map.Entry;
2015-08-21 15:10:08 +02:00
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.PluginManager;
import com.gamingmesh.jobs.container.Job;
import com.gamingmesh.jobs.container.JobConditions;
2015-08-21 15:10:08 +02:00
import com.gamingmesh.jobs.container.JobPermission;
import com.gamingmesh.jobs.container.JobProgression;
import com.gamingmesh.jobs.container.JobsPlayer;
public class PermissionHandler {
private Jobs plugin;
2015-08-21 15:10:08 +02:00
public PermissionHandler(Jobs plugin) {
2015-09-04 16:37:36 +02:00
this.plugin = plugin;
}
2015-08-21 15:10:08 +02:00
2015-09-04 16:37:36 +02:00
public void recalculatePermissions(JobsPlayer jPlayer) {
2015-08-21 15:10:08 +02:00
2015-09-04 16:37:36 +02:00
if (jPlayer == null)
return;
2015-08-21 15:10:08 +02:00
2016-08-05 12:28:31 +02:00
Player player = jPlayer.getPlayer();
if (player == null)
return;
2015-08-21 15:10:08 +02:00
2015-09-04 16:37:36 +02:00
boolean changed = false;
2015-08-21 15:10:08 +02:00
2015-09-04 16:37:36 +02:00
// remove old permissions
String permName = "jobs.players." + player.getName();
2016-06-25 14:51:21 +02:00
Permission permission = this.plugin.getServer().getPluginManager().getPermission(permName);
2015-09-04 16:37:36 +02:00
if (permission != null) {
2016-06-25 14:51:21 +02:00
this.plugin.getServer().getPluginManager().removePermission(permission);
2015-09-04 16:37:36 +02:00
changed = true;
}
2015-08-21 15:10:08 +02:00
2015-09-04 16:37:36 +02:00
// 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.isValue()) {
2015-09-04 16:37:36 +02:00
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);
}
}
2015-08-21 15:10:08 +02:00
}
2015-09-04 16:37:36 +02:00
}
for (JobConditions Condition : job.getConditions()) {
boolean ok = true;
2016-06-10 17:25:40 +02:00
for (String oneReq : Condition.getRequiredPerm()) {
if (!player.hasPermission(oneReq)) {
ok = false;
break;
2016-06-10 17:25:40 +02:00
}
}
2016-06-10 17:25:40 +02:00
for (Entry<String, Integer> oneReq : Condition.getRequiredJobs().entrySet()) {
String jobName = oneReq.getKey();
int jobLevel = oneReq.getValue();
boolean found = false;
for (JobProgression oneJob : jPlayer.getJobProgression()) {
if (oneJob.getJob().getName().equalsIgnoreCase(jobName))
found = true;
if (oneJob.getJob().getName().equalsIgnoreCase(jobName) && oneJob.getLevel() < jobLevel) {
ok = false;
break;
}
}
if (!found)
2016-06-10 17:25:40 +02:00
ok = false;
}
if (!ok)
continue;
2016-06-10 17:25:40 +02:00
for (Entry<String, Boolean> one : Condition.getPerformPerm().entrySet()) {
String perm = one.getKey();
boolean node = one.getValue();
if (node) {
permissions.put(perm, 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)) {
permissions.put(perm, false);
}
}
}
}
2015-08-21 15:10:08 +02:00
}
2015-09-04 16:37:36 +02:00
} 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.isValue()) {
2015-09-04 16:37:36 +02:00
permissions.put(perm.getNode(), true);
} else {
if (!permissions.containsKey(perm.getNode())) {
permissions.put(perm.getNode(), false);
}
}
2015-08-21 15:10:08 +02:00
}
2015-09-04 16:37:36 +02:00
}
for (JobConditions Condition : prog.getJob().getConditions()) {
boolean ok = true;
2016-06-10 17:25:40 +02:00
for (String oneReq : Condition.getRequiredPerm()) {
if (!player.hasPermission(oneReq)) {
ok = false;
break;
2016-06-10 17:25:40 +02:00
}
}
2016-06-10 17:25:40 +02:00
for (Entry<String, Integer> oneReq : Condition.getRequiredJobs().entrySet()) {
String jobName = oneReq.getKey();
int jobLevel = oneReq.getValue();
boolean found = false;
for (JobProgression oneJob : jPlayer.getJobProgression()) {
if (oneJob.getJob().getName().equalsIgnoreCase(jobName))
found = true;
if (oneJob.getJob().getName().equalsIgnoreCase(jobName) && oneJob.getLevel() < jobLevel) {
ok = false;
break;
}
}
if (!found)
2016-06-10 17:25:40 +02:00
ok = false;
}
if (!ok)
continue;
2016-06-10 17:25:40 +02:00
for (Entry<String, Boolean> one : Condition.getPerformPerm().entrySet()) {
String perm = one.getKey();
boolean node = one.getValue();
if (node) {
permissions.put(perm, 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)) {
permissions.put(perm, false);
}
}
}
}
2015-08-21 15:10:08 +02:00
}
2015-09-04 16:37:36 +02:00
}
2015-08-21 15:10:08 +02:00
2015-09-04 16:37:36 +02:00
// add new permissions (if applicable)
if (permissions.size() > 0) {
2016-06-25 14:51:21 +02:00
this.plugin.getServer().getPluginManager().addPermission(new Permission(permName, PermissionDefault.FALSE, permissions));
2015-09-04 16:37:36 +02:00
changed = true;
}
}
// If the permissions changed, recalculate them
if (!changed)
return;
2015-08-21 15:10:08 +02:00
2015-09-04 16:37:36 +02:00
// find old attachment
PermissionAttachment attachment = null;
for (PermissionAttachmentInfo pai : player.getEffectivePermissions()) {
if (pai.getAttachment() != null && pai.getAttachment().getPlugin() instanceof Jobs) {
2015-09-04 16:37:36 +02:00
attachment = pai.getAttachment();
}
2015-08-21 15:10:08 +02:00
}
2015-09-04 16:37:36 +02:00
// create if attachment doesn't exist
if (attachment == null) {
2016-06-25 14:51:21 +02:00
attachment = player.addAttachment(this.plugin);
2015-09-04 16:37:36 +02:00
attachment.setPermission(permName, true);
2015-08-21 15:10:08 +02:00
}
2015-09-04 16:37:36 +02:00
// recalculate!
player.recalculatePermissions();
}
public void registerPermissions() {
2016-06-25 14:51:21 +02:00
PluginManager pm = this.plugin.getServer().getPluginManager();
for (World world : this.plugin.getServer().getWorlds()) {
2015-09-04 16:37:36 +02:00
if (pm.getPermission("jobs.world." + world.getName().toLowerCase()) == null)
pm.addPermission(new Permission("jobs.world." + world.getName().toLowerCase(), PermissionDefault.TRUE));
}
for (Job job : Jobs.getJobs()) {
if (pm.getPermission("jobs.join." + job.getName().toLowerCase()) == null)
pm.addPermission(new Permission("jobs.join." + job.getName().toLowerCase(), PermissionDefault.TRUE));
}
}
/**
* Check World permissions
*/
public boolean hasWorldPermission(Player player, String world) {
if (!player.hasPermission("jobs.use")) {
return false;
2015-08-21 15:10:08 +02:00
}
2016-06-25 15:27:01 +02:00
return player.hasPermission("jobs.world." + world.toLowerCase());
2015-09-04 16:37:36 +02:00
}
2015-08-21 15:10:08 +02:00
}