Make more specific wildcards take priority over less specific ones

This commit is contained in:
Luck 2016-09-19 20:36:52 +01:00
parent f52a106165
commit 6ffa6720be
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
5 changed files with 82 additions and 37 deletions

View File

@ -249,9 +249,11 @@ public abstract class PermissionHolder {
Map<String, Boolean> perms = new HashMap<>(); Map<String, Boolean> perms = new HashMap<>();
for (Node node : getAllNodesFiltered(context)) { for (Node node : getAllNodesFiltered(context)) {
if (node.getPermission().equals("*") || node.getPermission().equals("'*'")) { if (possibleNodes != null && !possibleNodes.isEmpty()) {
if (possibleNodes != null && plugin.getConfiguration().getApplyWildcards()) { if (node.getPermission().equals("*") || node.getPermission().equals("'*'")) {
possibleNodes.forEach(n -> perms.put(n, true)); if (plugin.getConfiguration().getApplyWildcards()) {
possibleNodes.forEach(n -> perms.put(n, true));
}
} }
} }
@ -263,10 +265,12 @@ public abstract class PermissionHolder {
.forEach(s -> perms.put(s, node.getValue())); .forEach(s -> perms.put(s, node.getValue()));
} }
if (plugin.getConfiguration().getApplyWildcards()) { if (possibleNodes != null && !possibleNodes.isEmpty()) {
node.resolveWildcard(possibleNodes).stream() if (plugin.getConfiguration().getApplyWildcards()) {
.filter(s -> !perms.containsKey(s)) node.resolveWildcard(possibleNodes).stream()
.forEach(s -> perms.put(s, node.getValue())); .filter(s -> !perms.containsKey(s))
.forEach(s -> perms.put(s, node.getValue()));
}
} }
} }

View File

@ -30,14 +30,14 @@ import java.util.Comparator;
@NoArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor(access = AccessLevel.PRIVATE)
public class PriorityComparator implements Comparator<Node> { public class PriorityComparator implements Comparator<Node> {
private static final PriorityComparator instance = new PriorityComparator(); private static final PriorityComparator INSTANCE = new PriorityComparator();
public static Comparator<Node> get() { public static Comparator<Node> get() {
return instance; return INSTANCE;
} }
public static Comparator<Node> reverse() { public static Comparator<Node> reverse() {
return instance.reversed(); return INSTANCE.reversed();
} }
@Override @Override

View File

@ -277,7 +277,6 @@ public class Node implements me.lucko.luckperms.api.Node {
return false; return false;
} }
// TODO move this method to resolve time
@Override @Override
public List<String> resolveWildcard(List<String> possibleNodes) { public List<String> resolveWildcard(List<String> possibleNodes) {
if (!isWildcard() || possibleNodes == null) { if (!isWildcard() || possibleNodes == null) {

View File

@ -22,7 +22,6 @@
package me.lucko.luckperms.utils; package me.lucko.luckperms.utils;
import com.google.common.base.Splitter;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -87,36 +86,54 @@ public class PermissionCalculator {
} }
@AllArgsConstructor @AllArgsConstructor
public static class WildcardProcessor implements PermissionCalculator.PermissionProcessor { public static class WildcardProcessor implements PermissionProcessor {
@Getter @Getter
private final Map<String, Boolean> map; private final Map<String, Boolean> map;
@Override @Override
public Tristate hasPermission(String permission) { public Tristate hasPermission(String permission) {
if (map.containsKey("*")) { String node = permission;
return Tristate.fromBoolean(map.get("*"));
while (node.contains(".")) {
int endIndex = node.lastIndexOf('.');
if (endIndex == -1) {
break;
}
node = node.substring(0, endIndex);
if (!isEmpty(node)) {
if (map.containsKey(node + ".*")) {
return Tristate.fromBoolean(map.get(node + ".*"));
}
}
} }
if (map.containsKey("'*'")) { if (map.containsKey("'*'")) {
return Tristate.fromBoolean(map.get("'*'")); return Tristate.fromBoolean(map.get("'*'"));
} }
String node = ""; if (map.containsKey("*")) {
Iterable<String> permParts = Splitter.on('.').split(permission); return Tristate.fromBoolean(map.get("*"));
for (String s : permParts) {
if (node.equals("")) {
node = s;
} else {
node = node + "." + s;
}
if (map.containsKey(node + ".*")) {
return Tristate.fromBoolean(map.get(node + ".*"));
}
} }
return Tristate.UNDEFINED; return Tristate.UNDEFINED;
} }
private static boolean isEmpty(String s) {
if (s.equals("")) {
return true;
}
char[] chars = s.toCharArray();
for (char c : chars) {
if (c != '.') {
return false;
}
}
return true;
}
} }
public interface PermissionProcessor { public interface PermissionProcessor {

View File

@ -97,29 +97,54 @@ public class LuckPermsUserSubject extends LuckPermsSubject {
// TODO proper implementation. // TODO proper implementation.
@AllArgsConstructor @AllArgsConstructor
private static class SpongeWildcardProcessor implements PermissionCalculator.PermissionProcessor { public static class SpongeWildcardProcessor implements PermissionCalculator.PermissionProcessor {
@Getter @Getter
private final Map<String, Boolean> map; private final Map<String, Boolean> map;
@Override @Override
public me.lucko.luckperms.api.Tristate hasPermission(String permission) { public me.lucko.luckperms.api.Tristate hasPermission(String permission) {
String node = ""; String node = permission;
Iterable<String> permParts = Splitter.on('.').split(permission);
for (String s : permParts) { while (node.contains(".")) {
if (node.equals("")) { int endIndex = node.lastIndexOf('.');
node = s; if (endIndex == -1) {
} else { break;
node = node + "." + s;
} }
if (map.containsKey(node)) { node = node.substring(0, endIndex);
return me.lucko.luckperms.api.Tristate.fromBoolean(map.get(node)); if (!isEmpty(node)) {
if (map.containsKey(node)) {
return me.lucko.luckperms.api.Tristate.fromBoolean(map.get(node));
}
} }
} }
if (map.containsKey("'*'")) {
return me.lucko.luckperms.api.Tristate.fromBoolean(map.get("'*'"));
}
if (map.containsKey("*")) {
return me.lucko.luckperms.api.Tristate.fromBoolean(map.get("*"));
}
return me.lucko.luckperms.api.Tristate.UNDEFINED; return me.lucko.luckperms.api.Tristate.UNDEFINED;
} }
private static boolean isEmpty(String s) {
if (s.equals("")) {
return true;
}
char[] chars = s.toCharArray();
for (char c : chars) {
if (c != '.') {
return false;
}
}
return true;
}
} }
@AllArgsConstructor @AllArgsConstructor