Skip to content

Commit

Permalink
Return syntax instead of no permission in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Feb 21, 2024
1 parent 23de304 commit b225229
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 26 deletions.
96 changes: 71 additions & 25 deletions cloud-core/src/main/java/org/incendo/cloud/CommandTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,7 @@ private CommandTree(final @NonNull CommandManager<C> commandManager) {
final PermissionResult permissionResult = this.determinePermissionResult(commandContext.sender(), root);
if (permissionResult.denied()) {
return CompletableFutures.failedFuture(
new NoPermissionException(
permissionResult,
commandContext.sender(),
this.getComponentChain(root)
)
this.noPermissionOrSyntax(permissionResult, commandContext.sender(), root)
);
}

Expand Down Expand Up @@ -328,11 +324,7 @@ private CommandTree(final @NonNull CommandManager<C> commandManager) {
);
if (check.denied()) {
return CompletableFutures.failedFuture(
new NoPermissionException(
check,
commandContext.sender(),
this.getComponentChain(root)
)
this.noPermissionOrSyntax(check, commandContext.sender(), root)
);
}
return CompletableFuture.completedFuture(root.command());
Expand All @@ -349,6 +341,72 @@ private CommandTree(final @NonNull CommandManager<C> commandManager) {
});
}

@SuppressWarnings({"unchecked", "rawtypes"})
private Exception noPermissionOrSyntax(
final PermissionResult permissionResult,
final C sender,
final CommandNode<C> root
) {
final boolean convert = this.commandManager.settings().get(ManagerSetting.CONVERT_NO_PERMISSION_TO_SYNTAX_EXCEPTION);
if (!convert) {
return new NoPermissionException(
permissionResult,
sender,
this.getComponentChain(root)
);
}

if (this.childPermitted(root, sender)) {
return new InvalidSyntaxException(
this.commandManager.commandSyntaxFormatter().apply(sender, (List) this.getComponentChain(root), root),
sender, this.getComponentChain(root)
);
}

final @Nullable List<CommandNode<C>> parentChain = this.permittedParentChain(root, sender);
if (parentChain != null) {
return new InvalidSyntaxException(
this.commandManager.commandSyntaxFormatter().apply(
sender,
parentChain.stream().map(CommandNode::component)
.filter(Objects::nonNull).collect(Collectors.toList()),
root
),
sender, this.getComponentChain(root)
);
}

return new NoPermissionException(
permissionResult,
sender,
this.getComponentChain(root)
);
}

private boolean childPermitted(CommandNode<C> node, C sender) {
if (this.determinePermissionResult(sender, node).allowed()) {
return true;
}
for (final CommandNode<C> child : node.children()) {
if (this.childPermitted(child, sender)) {
return true;
}
}
return false;
}

private @Nullable List<CommandNode<C>> permittedParentChain(CommandNode<C> node, C sender) {
final @Nullable CommandNode<C> parent = node.parent();
if (parent != null) {
if (this.determinePermissionResult(sender, parent).allowed()) {
return this.getChain(parent);
} else {
return this.permittedParentChain(parent, sender);
}
}
return null;
}

private @Nullable CompletableFuture<@Nullable Command<C>> attemptParseUnambiguousChild(
final @NonNull List<@NonNull CommandComponent<C>> parsedArguments,
final @NonNull CommandContext<C> commandContext,
Expand Down Expand Up @@ -380,11 +438,7 @@ private CommandTree(final @NonNull CommandManager<C> commandManager) {
final PermissionResult childCheck = this.determinePermissionResult(sender, child);
if (!commandInput.isEmpty() && childCheck.denied()) {
return CompletableFutures.failedFuture(
new NoPermissionException(
childCheck,
sender,
this.getComponentChain(child)
)
this.noPermissionOrSyntax(childCheck, sender, child)
);
}

Expand Down Expand Up @@ -447,11 +501,7 @@ private CommandTree(final @NonNull CommandManager<C> commandManager) {
return CompletableFuture.completedFuture(command);
}
return CompletableFutures.failedFuture(
new NoPermissionException(
check,
sender,
this.getComponentChain(root)
)
this.noPermissionOrSyntax(check, sender, root)
);
} else {
// The child is not a leaf, but may have an intermediary executor, attempt to use it
Expand All @@ -476,11 +526,7 @@ private CommandTree(final @NonNull CommandManager<C> commandManager) {
}

return CompletableFutures.failedFuture(
new NoPermissionException(
check,
sender,
this.getComponentChain(root)
)
this.noPermissionOrSyntax(check, sender, root)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,8 @@ public enum ManagerSetting implements Setting {
* and code inspecting the command tree may need to be adjusted.
*/
@API(status = API.Status.EXPERIMENTAL)
LIBERAL_FLAG_PARSING
LIBERAL_FLAG_PARSING,

@API(status = API.Status.EXPERIMENTAL)
CONVERT_NO_PERMISSION_TO_SYNTAX_EXCEPTION
}

0 comments on commit b225229

Please sign in to comment.