From bfba8d571bc26811a7207198fffdab34361242b9 Mon Sep 17 00:00:00 2001 From: Gadse Date: Sat, 26 Sep 2020 11:29:26 +0200 Subject: [PATCH] Fix max jobs, respect negated permissions, add cumulative option --- .../gamingmesh/jobs/PermissionManager.java | 36 ++++++++++--------- .../jobs/Placeholders/Placeholder.java | 2 +- .../com/gamingmesh/jobs/PlayerManager.java | 2 +- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/PermissionManager.java b/src/main/java/com/gamingmesh/jobs/PermissionManager.java index 10dcce5f..1ca7ee9b 100644 --- a/src/main/java/com/gamingmesh/jobs/PermissionManager.java +++ b/src/main/java/com/gamingmesh/jobs/PermissionManager.java @@ -20,6 +20,7 @@ package com.gamingmesh.jobs; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; import org.bukkit.Bukkit; import org.bukkit.World; @@ -119,7 +120,11 @@ public class PermissionManager { return getMaxPermission(jPlayer, perm, false); } - public Double getMaxPermission(JobsPlayer jPlayer, String perm, boolean force) { + public Double getMaxPermission(JobsPlayer jPlayer, String perm, boolean force) { + return getMaxPermission(jPlayer, perm, force, true); + } + + public Double getMaxPermission(JobsPlayer jPlayer, String perm, boolean force, boolean cumulative) { if (jPlayer == null || jPlayer.getPlayer() == null) return 0D; @@ -138,23 +143,22 @@ public class PermissionManager { return 0D; } - Double amount = 0D; - for (String uno : permissions.keySet()) { - if (uno.startsWith(perm)) { - double t = 0d; - try { - t = Double.parseDouble(uno.replace(perm, "")); - } catch (NumberFormatException e) { - } + double amount = 0D; - if (uno.startsWith("jobs.max")) { - if (amount == 0D || t > amount) { - amount = t; - } - } else { - amount += t; + for (Map.Entry permission : permissions.entrySet()) { + if (!permission.getKey().startsWith(perm) || !permission.getValue()) + continue; + + try { + double temp = Double.parseDouble(permission.getKey().replace(perm, "")); + + if (cumulative) + amount += temp; + else if (temp > amount) + amount = temp; + + } catch (NumberFormatException ignored) { } - } } return amount; diff --git a/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java b/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java index b3ba8e60..a385f15b 100644 --- a/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java +++ b/src/main/java/com/gamingmesh/jobs/Placeholders/Placeholder.java @@ -554,7 +554,7 @@ public class Placeholder { return convert(true); case maxjobs: - int max = Jobs.getPermissionManager().getMaxPermission(user, "jobs.max").intValue(); + int max = Jobs.getPermissionManager().getMaxPermission(user, "jobs.max", false, false).intValue(); max = max == 0 ? Jobs.getGCManager().getMaxJobs() : max; return Integer.toString(max); diff --git a/src/main/java/com/gamingmesh/jobs/PlayerManager.java b/src/main/java/com/gamingmesh/jobs/PlayerManager.java index ebbf0917..a994569d 100644 --- a/src/main/java/com/gamingmesh/jobs/PlayerManager.java +++ b/src/main/java/com/gamingmesh/jobs/PlayerManager.java @@ -783,7 +783,7 @@ public class PlayerManager { * @return True if he have permission */ public boolean getJobsLimit(JobsPlayer jPlayer, short currentCount) { - int max = Jobs.getPermissionManager().getMaxPermission(jPlayer, "jobs.max").intValue(); + int max = Jobs.getPermissionManager().getMaxPermission(jPlayer, "jobs.max", false, false).intValue(); max = max == 0 ? Jobs.getGCManager().getMaxJobs() : max; return max > currentCount; }