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,11 +249,13 @@ 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 (possibleNodes != null && !possibleNodes.isEmpty()) {
if (node.getPermission().equals("*") || node.getPermission().equals("'*'")) { if (node.getPermission().equals("*") || node.getPermission().equals("'*'")) {
if (possibleNodes != null && plugin.getConfiguration().getApplyWildcards()) { if (plugin.getConfiguration().getApplyWildcards()) {
possibleNodes.forEach(n -> perms.put(n, true)); possibleNodes.forEach(n -> perms.put(n, true));
} }
} }
}
perms.put(node.getPermission(), node.getValue()); perms.put(node.getPermission(), node.getValue());
@ -263,12 +265,14 @@ public abstract class PermissionHolder {
.forEach(s -> perms.put(s, node.getValue())); .forEach(s -> perms.put(s, node.getValue()));
} }
if (possibleNodes != null && !possibleNodes.isEmpty()) {
if (plugin.getConfiguration().getApplyWildcards()) { if (plugin.getConfiguration().getApplyWildcards()) {
node.resolveWildcard(possibleNodes).stream() node.resolveWildcard(possibleNodes).stream()
.filter(s -> !perms.containsKey(s)) .filter(s -> !perms.containsKey(s))
.forEach(s -> perms.put(s, node.getValue())); .forEach(s -> perms.put(s, node.getValue()));
} }
} }
}
return Collections.unmodifiableMap(perms); return Collections.unmodifiableMap(perms);
} }

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(".")) {
if (map.containsKey("'*'")) { int endIndex = node.lastIndexOf('.');
return Tristate.fromBoolean(map.get("'*'")); if (endIndex == -1) {
} break;
String node = "";
Iterable<String> permParts = Splitter.on('.').split(permission);
for (String s : permParts) {
if (node.equals("")) {
node = s;
} else {
node = node + "." + s;
} }
node = node.substring(0, endIndex);
if (!isEmpty(node)) {
if (map.containsKey(node + ".*")) { if (map.containsKey(node + ".*")) {
return Tristate.fromBoolean(map.get(node + ".*")); return Tristate.fromBoolean(map.get(node + ".*"));
} }
} }
}
if (map.containsKey("'*'")) {
return Tristate.fromBoolean(map.get("'*'"));
}
if (map.containsKey("*")) {
return Tristate.fromBoolean(map.get("*"));
}
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;
} }
node = node.substring(0, endIndex);
if (!isEmpty(node)) {
if (map.containsKey(node)) { if (map.containsKey(node)) {
return me.lucko.luckperms.api.Tristate.fromBoolean(map.get(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