Skip to content

Commit

Permalink
Use BrigadierUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Jan 12, 2024
1 parent 4c78782 commit 4f1b5fe
Showing 1 changed file with 2 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
import cloud.commandframework.brigadier.CloudBrigadierCommand;
import cloud.commandframework.internal.CommandRegistrationHandler;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.mojang.brigadier.tree.RootCommandNode;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -47,6 +45,8 @@
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;

import static cloud.commandframework.brigadier.util.BrigadierUtil.buildRedirect;

/**
* A registration handler for NeoForge.
*
Expand Down Expand Up @@ -86,41 +86,6 @@ protected final void registerCommand(final Command<C> command, final CommandDisp
}
}

/**
* Returns a literal node that redirects its execution to
* the given destination node.
*
* <p>This method is taken from MIT licensed code in the Velocity project, see
* <a href="https://github.com/VelocityPowered/Velocity/blob/b88c573eb11839a95bea1af947b0c59a5956368b/proxy/src/main/java/com/velocitypowered/proxy/util/BrigadierUtils.java#L33">
* Velocity's BrigadierUtils class</a></p>
*
* @param alias the command alias
* @param destination the destination node
* @param <S> brig sender type
* @return the built node
*/
private static <S> LiteralCommandNode<S> buildRedirect(
final @NonNull String alias,
final @NonNull CommandNode<S> destination
) {
// Redirects only work for nodes with children, but break the top argument-less command.
// Manually adding the root command after setting the redirect doesn't fix it.
// (See https://github.com/Mojang/brigadier/issues/46) Manually clone the node instead.
final LiteralArgumentBuilder<S> builder = LiteralArgumentBuilder
.<S>literal(alias)
.requires(destination.getRequirement())
.forward(
destination.getRedirect(),
destination.getRedirectModifier(),
destination.isFork()
)
.executes(destination.getCommand());
for (final CommandNode<S> child : destination.getChildren()) {
builder.then(child);
}
return builder.build();
}

static class Client<C> extends NeoForgeCommandRegistrationHandler<C> {

private final Set<Command<C>> registeredCommands = ConcurrentHashMap.newKeySet();
Expand Down

0 comments on commit 4f1b5fe

Please sign in to comment.