Allow group weights as permission nodes - resolves #73

This commit is contained in:
Luck 2016-12-12 19:30:42 +00:00
parent 053066f1a7
commit 41c3df41b4
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B

View File

@ -71,6 +71,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
@ -290,15 +291,19 @@ public abstract class PermissionHolder {
);
TreeSet<Map.Entry<Integer, Node>> sortedParents = new TreeSet<>(Util.META_COMPARATOR.reversed());
Map<String, Integer> 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));
Group group = plugin.getGroupManager().getIfLoaded(node.getGroupName());
if (group != null) {
OptionalInt weight = group.getWeight();
if (weight.isPresent()) {
sortedParents.add(Maps.immutableEntry(weight.getAsInt(), node));
continue;
}
}
sortedParents.add(Maps.immutableEntry(0, node));
}
for (Map.Entry<Integer, Node> e : sortedParents) {
Node parent = e.getValue();
Group group = plugin.getGroupManager().getIfLoaded(parent.getGroupName());
@ -513,15 +518,19 @@ public abstract class PermissionHolder {
);
TreeSet<Map.Entry<Integer, Node>> sortedParents = new TreeSet<>(Util.META_COMPARATOR.reversed());
Map<String, Integer> 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));
Group group = plugin.getGroupManager().getIfLoaded(node.getGroupName());
if (group != null) {
OptionalInt weight = group.getWeight();
if (weight.isPresent()) {
sortedParents.add(Maps.immutableEntry(weight.getAsInt(), node));
continue;
}
}
sortedParents.add(Maps.immutableEntry(0, node));
}
for (Map.Entry<Integer, Node> e : sortedParents) {
Node parent = e.getValue();
Group group = plugin.getGroupManager().getIfLoaded(parent.getGroupName());
@ -1090,6 +1099,26 @@ public abstract class PermissionHolder {
return getPermissions(false).stream().filter(Node::isMeta).collect(Collectors.toSet());
}
public OptionalInt getWeight() {
OptionalInt weight = OptionalInt.empty();
try {
weight = getNodes().stream()
.filter(n -> n.getPermission().startsWith("group.weight."))
.map(n -> n.getPermission().substring("group.weight.".length()))
.mapToInt(Integer::parseInt)
.max();
} catch (Exception ignored) {}
if (!weight.isPresent()) {
Integer w = plugin.getConfiguration().getGroupWeights().get(getObjectName().toLowerCase());
if (w != null) {
weight = OptionalInt.of(w);
}
}
return weight;
}
/**
* Get a {@link List} of all of the groups the holder inherits, on all servers
*