mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2025-01-15 04:41:33 +01:00
Include info about wildcards in permission check command output
This commit is contained in:
parent
c904ede972
commit
33e412f663
@ -42,6 +42,14 @@ public class WildcardProcessor extends AbstractPermissionProcessor implements Pe
|
||||
private static final String ROOT_WILDCARD = "*";
|
||||
private static final String ROOT_WILDCARD_WITH_QUOTES = "'*'";
|
||||
|
||||
public static boolean isRootWildcard(String permission) {
|
||||
return ROOT_WILDCARD.equals(permission) || ROOT_WILDCARD_WITH_QUOTES.equals(permission);
|
||||
}
|
||||
|
||||
public static boolean isWildcardPermission(String permission) {
|
||||
return isRootWildcard(permission) || (permission.endsWith(WILDCARD_SUFFIX) && permission.length() > 2);
|
||||
}
|
||||
|
||||
private Map<String, TristateResult> wildcardPermissions = Collections.emptyMap();
|
||||
private TristateResult rootWildcardState = TristateResult.UNDEFINED;
|
||||
|
||||
|
@ -25,6 +25,7 @@
|
||||
|
||||
package me.lucko.luckperms.common.commands.generic.permission;
|
||||
|
||||
import me.lucko.luckperms.common.calculator.processor.WildcardProcessor;
|
||||
import me.lucko.luckperms.common.calculator.result.TristateResult;
|
||||
import me.lucko.luckperms.common.command.CommandResult;
|
||||
import me.lucko.luckperms.common.command.abstraction.CommandException;
|
||||
@ -35,8 +36,10 @@ import me.lucko.luckperms.common.command.spec.CommandSpec;
|
||||
import me.lucko.luckperms.common.command.tabcomplete.TabCompleter;
|
||||
import me.lucko.luckperms.common.command.tabcomplete.TabCompletions;
|
||||
import me.lucko.luckperms.common.command.utils.ArgumentList;
|
||||
import me.lucko.luckperms.common.config.ConfigKeys;
|
||||
import me.lucko.luckperms.common.locale.Message;
|
||||
import me.lucko.luckperms.common.model.PermissionHolder;
|
||||
import me.lucko.luckperms.common.node.AbstractNode;
|
||||
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
||||
import me.lucko.luckperms.common.query.QueryOptionsImpl;
|
||||
import me.lucko.luckperms.common.sender.Sender;
|
||||
@ -48,12 +51,14 @@ import net.luckperms.api.context.ImmutableContextSet;
|
||||
import net.luckperms.api.model.PermissionHolder.Identifier;
|
||||
import net.luckperms.api.node.Node;
|
||||
import net.luckperms.api.node.metadata.types.InheritanceOriginMetadata;
|
||||
import net.luckperms.api.node.types.PermissionNode;
|
||||
import net.luckperms.api.node.types.RegexPermissionNode;
|
||||
import net.luckperms.api.query.QueryOptions;
|
||||
import net.luckperms.api.util.Tristate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class PermissionCheck extends GenericChildCommand {
|
||||
public PermissionCheck() {
|
||||
@ -72,21 +77,19 @@ public class PermissionCheck extends GenericChildCommand {
|
||||
// accumulate nodes
|
||||
List<Node> own = new ArrayList<>();
|
||||
List<Node> inherited = new ArrayList<>();
|
||||
List<Node> wildcards = new ArrayList<>();
|
||||
|
||||
List<Node> resolved = target.resolveInheritedNodes(QueryOptionsImpl.DEFAULT_NON_CONTEXTUAL);
|
||||
for (Node n : resolved) {
|
||||
if (!matches(node, n)) {
|
||||
continue;
|
||||
if (matches(node, n, plugin)) {
|
||||
if (isInherited(n, target)) {
|
||||
inherited.add(n);
|
||||
} else {
|
||||
own.add(n);
|
||||
}
|
||||
}
|
||||
|
||||
Identifier origin = n.getMetadata(InheritanceOriginMetadata.KEY)
|
||||
.map(InheritanceOriginMetadata::getOrigin)
|
||||
.orElse(null);
|
||||
|
||||
if (origin == null || target.getIdentifier().equals(origin)) {
|
||||
own.add(n);
|
||||
} else {
|
||||
inherited.add(n);
|
||||
if (matchesWildcard(node, n, plugin)) {
|
||||
wildcards.add(n);
|
||||
}
|
||||
}
|
||||
|
||||
@ -96,7 +99,7 @@ public class PermissionCheck extends GenericChildCommand {
|
||||
Message.PERMISSION_CHECK_INFO_NOT_DIRECTLY.send(sender, target, node);
|
||||
} else {
|
||||
for (Node n : own) {
|
||||
Message.PERMISSION_CHECK_INFO_DIRECTLY.send(sender, target, node, Tristate.of(n.getValue()), n.getContexts());
|
||||
Message.PERMISSION_CHECK_INFO_DIRECTLY.send(sender, target, n.getKey(), Tristate.of(n.getValue()), n.getContexts());
|
||||
}
|
||||
}
|
||||
if (inherited.isEmpty()) {
|
||||
@ -104,7 +107,15 @@ public class PermissionCheck extends GenericChildCommand {
|
||||
} else {
|
||||
for (Node n : inherited) {
|
||||
String origin = n.metadata(InheritanceOriginMetadata.KEY).getOrigin().getName();
|
||||
Message.PERMISSION_CHECK_INFO_INHERITED.send(sender, target, node, Tristate.of(n.getValue()), n.getContexts(), origin);
|
||||
Message.PERMISSION_CHECK_INFO_INHERITED.send(sender, target, n.getKey(), Tristate.of(n.getValue()), n.getContexts(), origin);
|
||||
}
|
||||
}
|
||||
for (Node n : wildcards) {
|
||||
if (isInherited(n, target)) {
|
||||
String origin = n.metadata(InheritanceOriginMetadata.KEY).getOrigin().getName();
|
||||
Message.PERMISSION_CHECK_INFO_INHERITED.send(sender, target, n.getKey(), Tristate.of(n.getValue()), n.getContexts(), origin);
|
||||
} else {
|
||||
Message.PERMISSION_CHECK_INFO_DIRECTLY.send(sender, target, n.getKey(), Tristate.of(n.getValue()), n.getContexts());
|
||||
}
|
||||
}
|
||||
|
||||
@ -149,9 +160,65 @@ public class PermissionCheck extends GenericChildCommand {
|
||||
.complete(args);
|
||||
}
|
||||
|
||||
private static boolean matches(String permission, Node node) {
|
||||
return node.getKey().equals(permission) ||
|
||||
node.resolveShorthand().contains(permission) ||
|
||||
node instanceof RegexPermissionNode && ((RegexPermissionNode) node).getPattern().map(p -> p.matcher(permission).matches()).orElse(false);
|
||||
private static boolean isInherited(Node n, PermissionHolder target) {
|
||||
Identifier origin = n.getMetadata(InheritanceOriginMetadata.KEY)
|
||||
.map(InheritanceOriginMetadata::getOrigin)
|
||||
.orElse(null);
|
||||
|
||||
return origin != null && !target.getIdentifier().equals(origin);
|
||||
}
|
||||
|
||||
private static boolean matchesWildcard(String permission, Node node, LuckPermsPlugin plugin) {
|
||||
if (plugin.getConfiguration().get(ConfigKeys.APPLYING_WILDCARDS)) {
|
||||
if (node instanceof PermissionNode && ((PermissionNode) node).isWildcard()) {
|
||||
String key = node.getKey();
|
||||
if (WildcardProcessor.isRootWildcard(key)) {
|
||||
return true;
|
||||
} else {
|
||||
// luckperms.* becomes luckperms.
|
||||
String wildcardBody = key.substring(0, key.length() - 1);
|
||||
if (permission.startsWith(wildcardBody)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (plugin.getConfiguration().get(ConfigKeys.APPLYING_WILDCARDS_SPONGE)) {
|
||||
String key = node.getKey();
|
||||
|
||||
int endIndex = key.lastIndexOf(AbstractNode.NODE_SEPARATOR);
|
||||
if (endIndex > 0) {
|
||||
String wildcardBody = key.substring(0, endIndex);
|
||||
if (permission.startsWith(wildcardBody)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean matches(String permission, Node node, LuckPermsPlugin plugin) {
|
||||
if (node.getKey().equals(permission)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (plugin.getConfiguration().get(ConfigKeys.APPLYING_SHORTHAND)) {
|
||||
if (node.resolveShorthand().contains(permission)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (plugin.getConfiguration().get(ConfigKeys.APPLYING_REGEX)) {
|
||||
if (node instanceof RegexPermissionNode) {
|
||||
Pattern pattern = ((RegexPermissionNode) node).getPattern().orElse(null);
|
||||
if (pattern != null && pattern.matcher(permission).matches()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -56,7 +56,6 @@ public abstract class AbstractNode<N extends ScopedNode<N, B>, B extends NodeBui
|
||||
*/
|
||||
public static final char NODE_SEPARATOR = '.';
|
||||
public static final String NODE_SEPARATOR_STRING = String.valueOf(NODE_SEPARATOR);
|
||||
public static final int NODE_SEPARATOR_CODE = Character.getNumericValue(NODE_SEPARATOR);
|
||||
|
||||
// node attributes
|
||||
protected final String key;
|
||||
|
@ -50,7 +50,7 @@ public class Permission extends AbstractNode<PermissionNode, PermissionNode.Buil
|
||||
|
||||
public Permission(String permission, boolean value, long expireAt, ImmutableContextSet contexts, Map<NodeMetadataKey<?>, Object> metadata) {
|
||||
super(permission, value, expireAt, contexts, metadata);
|
||||
this.wildcardLevel = permission.endsWith(WildcardProcessor.WILDCARD_SUFFIX) ? permission.chars().filter(num -> num == NODE_SEPARATOR_CODE).sum() : -1;
|
||||
this.wildcardLevel = WildcardProcessor.isWildcardPermission(permission) ? permission.chars().filter(num -> num == NODE_SEPARATOR).sum() : -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user