Skip to content

Commit

Permalink
Brigadier quick recode + clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqzn committed Oct 6, 2024
1 parent d47e22a commit e25c73a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 171 deletions.
98 changes: 59 additions & 39 deletions brigadier/src/main/java/dev/velix/imperat/BaseBrigadierManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import com.mojang.brigadier.Message;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import com.mojang.brigadier.tree.LiteralCommandNode;
import dev.velix.imperat.command.Command;
import dev.velix.imperat.command.Description;
import dev.velix.imperat.command.parameters.CommandParameter;
Expand All @@ -22,8 +26,8 @@
import java.util.List;

import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;

@SuppressWarnings("unchecked")
public abstract non-sealed class BaseBrigadierManager<S extends Source> implements BrigadierManager<S> {

protected final Imperat<S> dispatcher;
Expand All @@ -34,63 +38,70 @@ protected BaseBrigadierManager(Imperat<S> dispatcher) {
}

@Override
public <CN extends com.mojang.brigadier.tree.CommandNode<?>> @NotNull CN parseCommandIntoNode(@NotNull Command<S> command) {
public @NotNull <T> LiteralCommandNode<T> parseCommandIntoNode(@NotNull Command<S> command) {
var tree = command.tree();
var root = tree.getRoot();
return convertRoot(root).toInternalNode();
return this.<T>convertRoot(root).build();
}

private BrigadierNode convertRoot(CommandNode<S> root) {
BrigadierNode bRoot = BrigadierNode.create(literal(root.getData().name()));

bRoot.withExecution(dispatcher, this)
.withRequirement((obj) -> {
var source = wrapCommandSource(obj);
return root.getData().isIgnoringACPerms()
|| dispatcher.getPermissionResolver().hasPermission(source, root.getData().permission());
}
);
@SuppressWarnings("unchecked")
private <T> LiteralArgumentBuilder<T> convertRoot(CommandNode<S> root) {
LiteralArgumentBuilder<T> builder = (LiteralArgumentBuilder<T>) literal(root.getData().name())
.requires((obj) -> {
var source = wrapCommandSource(obj);
return root.getData().isIgnoringACPerms()
|| dispatcher.getPermissionResolver().hasPermission(source, root.getData().permission());
});
executor(builder);

for (var child : root.getChildren()) {
bRoot.addChild(convertNode(root, root, child));
builder.then(convertNode(root, root, child));
}
return bRoot;
return builder;
}

private BrigadierNode convertNode(CommandNode<S> root, ParameterNode<?, ?> parent, ParameterNode<S, ?> node) {
BrigadierNode child = BrigadierNode.create(node instanceof CommandNode<?> ? literal(node.getData().name()) : argument(node.getData().name(), getArgumentType(node.getData())));
child.withExecution(dispatcher, this)
.withRequirement((obj) -> {
var permissionResolver = dispatcher.getPermissionResolver();
var source = wrapCommandSource(obj);
private <T> com.mojang.brigadier.tree.CommandNode<T> convertNode(CommandNode<S> root, ParameterNode<?, ?> parent, ParameterNode<S, ?> node) {

boolean isIgnoringAC = root.getData().isIgnoringACPerms();
if (parent != root && parent instanceof CommandNode<?> parentCmdNode) {
isIgnoringAC = isIgnoringAC && parentCmdNode.getData().isIgnoringACPerms();
}
if (node instanceof CommandNode<?> commandNode) {
isIgnoringAC = isIgnoringAC && commandNode.getData().isIgnoringACPerms();
}
if (isIgnoringAC) {
return true;
}
boolean hasParentPerm = permissionResolver.hasPermission(source, parent.getData().permission());
boolean hasNodePerm = permissionResolver.hasPermission(source, node.getData().permission());
ArgumentBuilder<T, ?> childBuilder = node instanceof CommandNode<?> ?
LiteralArgumentBuilder.literal(node.getData().name())
: RequiredArgumentBuilder.argument(node.getData().name(), getArgumentType(node.getData()));

return (hasParentPerm && hasNodePerm);
});
childBuilder.requires((obj) -> {
var permissionResolver = dispatcher.getPermissionResolver();
var source = wrapCommandSource(obj);

boolean isIgnoringAC = root.getData().isIgnoringACPerms();
if (parent != root && parent instanceof CommandNode<?> parentCmdNode) {
isIgnoringAC = isIgnoringAC && parentCmdNode.getData().isIgnoringACPerms();
}
if (node instanceof CommandNode<?> commandNode) {
isIgnoringAC = isIgnoringAC && commandNode.getData().isIgnoringACPerms();
}
if (isIgnoringAC) {
return true;
}
boolean hasParentPerm = permissionResolver.hasPermission(source, parent.getData().permission());
boolean hasNodePerm = permissionResolver.hasPermission(source, node.getData().permission());

return (hasParentPerm && hasNodePerm);
});

executor(childBuilder);
if (!(node instanceof CommandNode<?>)) {
child.suggest(createSuggestionProvider(root.getData(), node.getData()));
((RequiredArgumentBuilder<T, ?>) childBuilder).suggests(
createSuggestionProvider(root.getData(), node.getData())
);
}

for (var innerChild : node.getChildren()) {
child.addChild(convertNode(root, node, innerChild));
childBuilder.then(convertNode(root, node, innerChild));
}
return child;

return childBuilder.build();
}


private @NotNull SuggestionProvider<?> createSuggestionProvider(
private @NotNull <T> SuggestionProvider<T> createSuggestionProvider(
Command<S> command,
CommandParameter<S> parameter
) {
Expand Down Expand Up @@ -131,6 +142,15 @@ private BrigadierNode convertNode(CommandNode<S> root, ParameterNode<?, ?> paren
};
}

private void executor(ArgumentBuilder<?, ?> builder) {
builder.executes((context) -> {
String input = context.getInput();
S sender = this.wrapCommandSource(context.getSource());
dispatcher.dispatch(sender, input);
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
});
}

private String[] processedInput(final String input) {
String result = input;
if (result.charAt(0) == '/')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode;
import dev.velix.imperat.command.Command;
import dev.velix.imperat.command.parameters.CommandParameter;
import dev.velix.imperat.context.Source;
import org.jetbrains.annotations.NotNull;

/**
* A class that manages parsing {@link Command}
* into brigadier {@link BrigadierNode}
* into brigadier {@link CommandNode}
*
* @param <S> the command-source type
*/
Expand Down Expand Up @@ -55,6 +56,6 @@ public sealed interface BrigadierManager<S extends Source> permits BaseBrigadier
*
* @return the parsed node
*/
<CN extends CommandNode<?>> CN parseCommandIntoNode(Command<S> command);
<T> LiteralCommandNode<T> parseCommandIntoNode(Command<S> command);

}
71 changes: 0 additions & 71 deletions brigadier/src/main/java/dev/velix/imperat/BrigadierNode.java

This file was deleted.

59 changes: 0 additions & 59 deletions brigadier/src/main/java/dev/velix/imperat/NodeModifier.java

This file was deleted.

0 comments on commit e25c73a

Please sign in to comment.