From 5361b1e87b1903909db3b035172f0cd0638314eb Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 5 Nov 2016 09:04:26 +0000 Subject: [PATCH] Implement group weights --- bukkit/src/main/resources/config.yml | 5 +++++ bungee/src/main/resources/config.yml | 5 +++++ .../common/config/AbstractConfiguration.java | 9 +++++++++ .../luckperms/common/config/LPConfiguration.java | 2 ++ .../luckperms/common/core/PermissionHolder.java | 15 ++++++++++++++- .../me/lucko/luckperms/sponge/SpongeConfig.java | 5 ++++- sponge/src/main/resources/luckperms.conf | 6 ++++++ 7 files changed, 45 insertions(+), 2 deletions(-) diff --git a/bukkit/src/main/resources/config.yml b/bukkit/src/main/resources/config.yml index d2bbc409..85bb4ba2 100644 --- a/bukkit/src/main/resources/config.yml +++ b/bukkit/src/main/resources/config.yml @@ -67,6 +67,11 @@ apply-regex: true # If set to true, LuckPerms will detect and expand shorthand node patterns. apply-shorthand: true +# Define special group weights for this server. +# Default is just 0. +group-weight: +# admin: 10 + diff --git a/bungee/src/main/resources/config.yml b/bungee/src/main/resources/config.yml index da8716e8..bc13167d 100644 --- a/bungee/src/main/resources/config.yml +++ b/bungee/src/main/resources/config.yml @@ -77,6 +77,11 @@ apply-regex: true # If set to true, LuckPerms will detect and expand shorthand node patterns. apply-shorthand: true +# Define special group weights for this server. +# Default is just 0. +group-weight: +# admin: 10 + diff --git a/common/src/main/java/me/lucko/luckperms/common/config/AbstractConfiguration.java b/common/src/main/java/me/lucko/luckperms/common/config/AbstractConfiguration.java index 994cf806..4c05ea09 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/AbstractConfiguration.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/AbstractConfiguration.java @@ -59,6 +59,7 @@ public abstract class AbstractConfiguration implement private boolean applyingWildcards; private boolean applyingRegex; private boolean applyingShorthand; + private Map groupWeights; private boolean logNotify; private boolean opsEnabled; private boolean commandsAllowOp; @@ -109,6 +110,14 @@ public abstract class AbstractConfiguration implement applyingWildcards = getBoolean("apply-wildcards", true); applyingRegex = getBoolean("apply-regex", true); applyingShorthand = getBoolean("apply-shorthand", true); + Map weights = getMap("group-weight", Collections.emptyMap()); + ImmutableMap.Builder mb = ImmutableMap.builder(); + for (Map.Entry e : weights.entrySet()) { + try { + mb.put(e.getKey().toLowerCase(), Integer.parseInt(e.getValue())); + } catch (NumberFormatException ignored) {} + } + groupWeights = mb.build(); logNotify = getBoolean("log-notify", true); autoOp = getBoolean("auto-op", false); opsEnabled = !isAutoOp() && getBoolean("enable-ops", true); diff --git a/common/src/main/java/me/lucko/luckperms/common/config/LPConfiguration.java b/common/src/main/java/me/lucko/luckperms/common/config/LPConfiguration.java index ab1f6f73..fa395aee 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/LPConfiguration.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/LPConfiguration.java @@ -62,6 +62,8 @@ public interface LPConfiguration { boolean isApplyingShorthand(); + Map getGroupWeights(); + boolean isLogNotify(); boolean isOpsEnabled(); diff --git a/common/src/main/java/me/lucko/luckperms/common/core/PermissionHolder.java b/common/src/main/java/me/lucko/luckperms/common/core/PermissionHolder.java index e6b6a3c8..acc0f536 100644 --- a/common/src/main/java/me/lucko/luckperms/common/core/PermissionHolder.java +++ b/common/src/main/java/me/lucko/luckperms/common/core/PermissionHolder.java @@ -25,6 +25,7 @@ package me.lucko.luckperms.common.core; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSortedSet; +import com.google.common.collect.Maps; import lombok.AccessLevel; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -37,6 +38,7 @@ import me.lucko.luckperms.api.event.events.*; import me.lucko.luckperms.common.LuckPermsPlugin; import me.lucko.luckperms.common.api.internal.GroupLink; import me.lucko.luckperms.common.api.internal.PermissionHolderLink; +import me.lucko.luckperms.common.commands.Util; import me.lucko.luckperms.common.groups.Group; import me.lucko.luckperms.common.utils.Cache; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; @@ -263,7 +265,18 @@ public abstract class PermissionHolder { !node.shouldApplyWithContext(contexts, false) ); - for (Node parent : parents) { + TreeSet> sortedParents = new TreeSet<>(Util.getMetaComparator().reversed()); + Map weights = plugin.getConfiguration().getGroupWeights(); + for (Node node : parents) { + if (weights.containsKey(node.getGroupName().toLowerCase())) { + sortedParents.add(Maps.immutableEntry(weights.get(node.getGroupName().toLowerCase()), node)); + } else { + sortedParents.add(Maps.immutableEntry(0, node)); + } + } + + for (Map.Entry e : sortedParents) { + Node parent = e.getValue(); Group group = plugin.getGroupManager().get(parent.getGroupName()); if (group == null) { continue; diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeConfig.java b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeConfig.java index 290ef7e8..ce290442 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeConfig.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeConfig.java @@ -34,6 +34,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -118,6 +119,7 @@ class SpongeConfig extends AbstractConfiguration { return node.getChildrenList().stream().map(n -> (String) n.getKey()).collect(Collectors.toList()); } + @SuppressWarnings("unchecked") @Override protected Map getMap(String path, Map def) { ConfigurationNode node = getNode(path); @@ -125,6 +127,7 @@ class SpongeConfig extends AbstractConfiguration { return def; } - return node.getChildrenList().stream().collect(Collectors.toMap(n -> (String) n.getKey(), ConfigurationNode::getString)); + Map m = (Map) node.getValue(Collections.emptyMap()); + return m.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, v -> v.getValue().toString())); } } diff --git a/sponge/src/main/resources/luckperms.conf b/sponge/src/main/resources/luckperms.conf index 40d1e32d..c62a4d90 100644 --- a/sponge/src/main/resources/luckperms.conf +++ b/sponge/src/main/resources/luckperms.conf @@ -75,6 +75,12 @@ apply-regex=true # If set to true, LuckPerms will detect and expand shorthand node patterns. apply-shorthand=true +# Define special group weights for this server. +# Default is just 0. +group-weight { +# admin=10 +} +