Include internal weight value in meta output if not already present

This commit is contained in:
Luck 2017-03-13 20:17:54 +00:00
parent aa2941fcdb
commit e9e844c1f7
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
3 changed files with 32 additions and 20 deletions

View File

@ -22,6 +22,7 @@
package me.lucko.luckperms.common.caching;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.ToString;
@ -42,9 +43,11 @@ import java.util.TreeMap;
@ToString
public class MetaHolder {
@Getter(AccessLevel.NONE)
private final Map<String, String> meta;
private final SortedMap<Integer, String> prefixes;
private final SortedMap<Integer, String> suffixes;
private int weight = 0;
private final MetaStack prefixStack;
private final MetaStack suffixStack;
@ -88,4 +91,18 @@ public class MetaHolder {
}
}
public void accumulateWeight(int weight) {
this.weight = Math.max(this.weight, weight);
}
// We can assume that if this method is being called, this holder is effectively finalized. (it's not going to accumulate more nodes)
// Therefore, it should be ok to set the weight meta key, if not already present.
public Map<String, String> getMeta() {
if (!this.meta.containsKey("weight") && this.weight != 0) {
this.meta.put("weight", String.valueOf(this.weight));
}
return this.meta;
}
}

View File

@ -460,10 +460,10 @@ public class Util {
return sb.delete(sb.length() - 6, sb.length()).toString();
}
public static class MetaComparator implements Comparator<Map.Entry<Integer, ? extends Node>> {
public static class MetaComparator implements Comparator<Map.Entry<Integer, ?>> {
@Override
public int compare(Map.Entry<Integer, ? extends Node> o1, Map.Entry<Integer, ? extends Node> o2) {
public int compare(Map.Entry<Integer, ?> o1, Map.Entry<Integer, ?> o2) {
int result = Integer.compare(o1.getKey(), o2.getKey());
return result != 0 ? result : 1;
}

View File

@ -59,7 +59,6 @@ import me.lucko.luckperms.common.utils.WeightComparator;
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
import me.lucko.luckperms.exceptions.ObjectLacksException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
@ -493,7 +492,7 @@ public abstract class PermissionHolder {
return exportNodesCache.getUnchecked(ExportNodesHolder.of(context, lowerCase));
}
public MetaHolder accumulateMeta(MetaHolder holder, List<String> excludedGroups, ExtractedContexts contexts) {
public MetaHolder accumulateMeta(MetaHolder holder, Set<String> excludedGroups, ExtractedContexts contexts) {
if (holder == null) {
holder = new MetaHolder(
plugin.getConfiguration().get(ConfigKeys.PREFIX_FORMATTING_OPTIONS).copy(),
@ -502,7 +501,7 @@ public abstract class PermissionHolder {
}
if (excludedGroups == null) {
excludedGroups = new ArrayList<>();
excludedGroups = new HashSet<>();
}
excludedGroups.add(getObjectName().toLowerCase());
@ -524,6 +523,11 @@ public abstract class PermissionHolder {
holder.accumulateNode(ln);
}
OptionalInt w = getWeight();
if (w.isPresent()) {
holder.accumulateWeight(w.getAsInt());
}
Set<Node> parents = all.stream()
.map(LocalizedNode::getNode)
.filter(Node::getValue)
@ -536,23 +540,10 @@ public abstract class PermissionHolder {
!node.shouldApplyWithContext(contexts.getContextSet(), false)
);
TreeSet<Map.Entry<Integer, Node>> sortedParents = new TreeSet<>(Util.META_COMPARATOR.reversed());
TreeSet<Map.Entry<Integer, Group>> sortedParents = new TreeSet<>(Util.META_COMPARATOR.reversed());
for (Node node : parents) {
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());
if (group == null) {
continue;
}
@ -561,7 +552,11 @@ public abstract class PermissionHolder {
continue;
}
group.accumulateMeta(holder, excludedGroups, contexts);
sortedParents.add(Maps.immutableEntry(group.getWeight().orElse(0), group));
}
for (Map.Entry<Integer, Group> e : sortedParents) {
e.getValue().accumulateMeta(holder, excludedGroups, contexts);
}
return holder;