com.mojang.brigadier.tree.LiteralCommandNode Java Examples
The following examples show how to use
com.mojang.brigadier.tree.LiteralCommandNode.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ForgeSparkPlugin.java From spark with GNU General Public License v3.0 | 7 votes |
public static <T> void registerCommands(CommandDispatcher<T> dispatcher, Command<T> executor, SuggestionProvider<T> suggestor, String... aliases) { if (aliases.length == 0) { return; } String mainName = aliases[0]; LiteralArgumentBuilder<T> command = LiteralArgumentBuilder.<T>literal(mainName) .executes(executor) .then(RequiredArgumentBuilder.<T, String>argument("args", StringArgumentType.greedyString()) .suggests(suggestor) .executes(executor) ); LiteralCommandNode<T> node = dispatcher.register(command); for (int i = 1; i < aliases.length; i++) { dispatcher.register(LiteralArgumentBuilder.<T>literal(aliases[i]).redirect(node)); } }
Example #2
Source File: BrigadierRemover.java From multiconnect with MIT License | 6 votes |
@SuppressWarnings("unchecked") public boolean remove() { if (thisNode == null || parentNode == null) { return false; } Map<String, CommandNode<S>> parentChildren; Map<String, LiteralCommandNode<S>> parentLiterals; Map<String, ArgumentCommandNode<S, ?>> parentArguments; try { parentChildren = (Map<String, CommandNode<S>>) CHILDREN_FIELD.get(parentNode); parentLiterals = thisNode instanceof LiteralCommandNode ? (Map<String, LiteralCommandNode<S>>) LITERALS_FIELD.get(parentNode) : null; parentArguments = thisNode instanceof ArgumentCommandNode ? (Map<String, ArgumentCommandNode<S, ?>>) ARGUMENTS_FIELD.get(parentNode) : null; } catch (ReflectiveOperationException e) { throw new AssertionError(e); } parentChildren.remove(thisNode.getName()); if (thisNode instanceof LiteralCommandNode) { parentLiterals.remove(thisNode.getName()); } else if (thisNode instanceof ArgumentCommandNode) { parentArguments.remove(thisNode.getName()); } return true; }
Example #3
Source File: FabricSparkPlugin.java From spark with GNU General Public License v3.0 | 6 votes |
public static <T> void registerCommands(CommandDispatcher<T> dispatcher, Command<T> executor, SuggestionProvider<T> suggestor, String... aliases) { if (aliases.length == 0) { return; } String mainName = aliases[0]; LiteralArgumentBuilder<T> command = LiteralArgumentBuilder.<T>literal(mainName) .executes(executor) .then(RequiredArgumentBuilder.<T, String>argument("args", StringArgumentType.greedyString()) .suggests(suggestor) .executes(executor) ); LiteralCommandNode<T> node = dispatcher.register(command); for (int i = 1; i < aliases.length; i++) { dispatcher.register(LiteralArgumentBuilder.<T>literal(aliases[i]).redirect(node)); } }
Example #4
Source File: BackendPlaySessionHandler.java From Velocity with MIT License | 6 votes |
@Override public boolean handle(AvailableCommands commands) { // Inject commands from the proxy. for (String command : server.getCommandManager().getAllRegisteredCommands()) { if (!server.getCommandManager().hasPermission(serverConn.getPlayer(), command)) { continue; } LiteralCommandNode<Object> root = LiteralArgumentBuilder.literal(command) .then(RequiredArgumentBuilder.argument("args", StringArgumentType.greedyString()) .suggests(new ProtocolSuggestionProvider("minecraft:ask_server")) .build()) .executes((ctx) -> 0) .build(); commands.getRootNode().addChild(root); } return false; }
Example #5
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 6 votes |
@Test public void getCompletionSuggestions_redirect_lots() throws Exception { final LiteralCommandNode<Object> loop = subject.register(literal("redirect")); subject.register( literal("redirect") .then( literal("loop") .then( argument("loop", integer()) .redirect(loop) ) ) ); final Suggestions result = subject.getCompletionSuggestions(subject.parse("redirect loop 1 loop 02 loop 003 ", source)).join(); assertThat(result.getRange(), equalTo(StringRange.at(33))); assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.at(33), "loop")))); }
Example #6
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 6 votes |
@Test public void getCompletionSuggestions_movingCursor_redirect() throws Exception { final LiteralCommandNode<Object> actualOne = subject.register(literal("actual_one") .then(literal("faz")) .then(literal("fbz")) .then(literal("gaz")) ); final LiteralCommandNode<Object> actualTwo = subject.register(literal("actual_two")); subject.register(literal("redirect_one").redirect(actualOne)); subject.register(literal("redirect_two").redirect(actualOne)); testSuggestions("redirect_one faz ", 0, StringRange.at(0), "actual_one", "actual_two", "redirect_one", "redirect_two"); testSuggestions("redirect_one faz ", 9, StringRange.between(0, 9), "redirect_one", "redirect_two"); testSuggestions("redirect_one faz ", 10, StringRange.between(0, 10), "redirect_one"); testSuggestions("redirect_one faz ", 12, StringRange.at(0)); testSuggestions("redirect_one faz ", 13, StringRange.at(13), "faz", "fbz", "gaz"); testSuggestions("redirect_one faz ", 14, StringRange.between(13, 14), "faz", "fbz"); testSuggestions("redirect_one faz ", 15, StringRange.between(13, 15), "faz"); testSuggestions("redirect_one faz ", 16, StringRange.at(0)); testSuggestions("redirect_one faz ", 17, StringRange.at(0)); }
Example #7
Source File: LiteralArgumentBuilderTest.java From brigadier with MIT License | 5 votes |
@Test public void testBuildWithChildren() throws Exception { builder.then(argument("bar", integer())); builder.then(argument("baz", integer())); final LiteralCommandNode<Object> node = builder.build(); assertThat(node.getChildren(), hasSize(2)); }
Example #8
Source File: LiteralArgumentBuilderTest.java From brigadier with MIT License | 5 votes |
@Test public void testBuildWithExecutor() throws Exception { final LiteralCommandNode<Object> node = builder.executes(command).build(); assertThat(node.getLiteral(), is("foo")); assertThat(node.getCommand(), is(command)); }
Example #9
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 5 votes |
@Test public void getCompletionSuggestions_execute_simulation_partial() throws Exception { final LiteralCommandNode<Object> execute = subject.register(literal("execute")); subject.register( literal("execute") .then( literal("as") .then(literal("bar").redirect(execute)) .then(literal("baz").redirect(execute)) ) .then( literal("store") .then( argument("name", word()) .redirect(execute) ) ) .then( literal("run") .executes(c -> 0) ) ); final ParseResults<Object> parse = subject.parse("execute as bar as ", source); final Suggestions result = subject.getCompletionSuggestions(parse).join(); assertThat(result.getRange(), equalTo(StringRange.at(18))); assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.at(18), "bar"), new Suggestion(StringRange.at(18), "baz")))); }
Example #10
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 5 votes |
@Test public void getCompletionSuggestions_execute_simulation() throws Exception { final LiteralCommandNode<Object> execute = subject.register(literal("execute")); subject.register( literal("execute") .then( literal("as") .then( argument("name", word()) .redirect(execute) ) ) .then( literal("store") .then( argument("name", word()) .redirect(execute) ) ) .then( literal("run") .executes(c -> 0) ) ); final ParseResults<Object> parse = subject.parse("execute as Dinnerbone as", source); final Suggestions result = subject.getCompletionSuggestions(parse).join(); assertThat(result.isEmpty(), is(true)); }
Example #11
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 5 votes |
@Test public void getCompletionSuggestions_redirectPartial_withInputOffset() throws Exception { final LiteralCommandNode<Object> actual = subject.register(literal("actual").then(literal("sub"))); subject.register(literal("redirect").redirect(actual)); final ParseResults<Object> parse = subject.parse(inputWithOffset("/redirect s", 1), source); final Suggestions result = subject.getCompletionSuggestions(parse).join(); assertThat(result.getRange(), equalTo(StringRange.between(10, 11))); assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.between(10, 11), "sub")))); }
Example #12
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 5 votes |
@Test public void getCompletionSuggestions_redirectPartial() throws Exception { final LiteralCommandNode<Object> actual = subject.register(literal("actual").then(literal("sub"))); subject.register(literal("redirect").redirect(actual)); final ParseResults<Object> parse = subject.parse("redirect s", source); final Suggestions result = subject.getCompletionSuggestions(parse).join(); assertThat(result.getRange(), equalTo(StringRange.between(9, 10))); assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.between(9, 10), "sub")))); }
Example #13
Source File: CommandSuggestionsTest.java From brigadier with MIT License | 5 votes |
@Test public void getCompletionSuggestions_redirect() throws Exception { final LiteralCommandNode<Object> actual = subject.register(literal("actual").then(literal("sub"))); subject.register(literal("redirect").redirect(actual)); final ParseResults<Object> parse = subject.parse("redirect ", source); final Suggestions result = subject.getCompletionSuggestions(parse).join(); assertThat(result.getRange(), equalTo(StringRange.at(9))); assertThat(result.getList(), equalTo(Lists.newArrayList(new Suggestion(StringRange.at(9), "sub")))); }
Example #14
Source File: CommandDispatcherTest.java From brigadier with MIT License | 5 votes |
@Test public void testFindNodeExists() { final LiteralCommandNode<Object> bar = literal("bar").build(); subject.register(literal("foo").then(bar)); assertThat(subject.findNode(Lists.newArrayList("foo", "bar")), is(bar)); }
Example #15
Source File: CommandDispatcherTest.java From brigadier with MIT License | 5 votes |
@Test public void testGetPath() { final LiteralCommandNode<Object> bar = literal("bar").build(); subject.register(literal("foo").then(bar)); assertThat(subject.getPath(bar), equalTo(Lists.newArrayList("foo", "bar"))); }
Example #16
Source File: CommandDispatcherTest.java From brigadier with MIT License | 5 votes |
@SuppressWarnings("unchecked") @Test public void testExecuteRedirected() throws Exception { final RedirectModifier<Object> modifier = mock(RedirectModifier.class); final Object source1 = new Object(); final Object source2 = new Object(); when(modifier.apply(argThat(hasProperty("source", is(source))))).thenReturn(Lists.newArrayList(source1, source2)); final LiteralCommandNode<Object> concreteNode = subject.register(literal("actual").executes(command)); final LiteralCommandNode<Object> redirectNode = subject.register(literal("redirected").fork(subject.getRoot(), modifier)); final String input = "redirected actual"; final ParseResults<Object> parse = subject.parse(input, source); assertThat(parse.getContext().getRange().get(input), equalTo("redirected")); assertThat(parse.getContext().getNodes().size(), is(1)); assertThat(parse.getContext().getRootNode(), equalTo(subject.getRoot())); assertThat(parse.getContext().getNodes().get(0).getRange(), equalTo(parse.getContext().getRange())); assertThat(parse.getContext().getNodes().get(0).getNode(), is(redirectNode)); assertThat(parse.getContext().getSource(), is(source)); final CommandContextBuilder<Object> parent = parse.getContext().getChild(); assertThat(parent, is(notNullValue())); assertThat(parent.getRange().get(input), equalTo("actual")); assertThat(parent.getNodes().size(), is(1)); assertThat(parse.getContext().getRootNode(), equalTo(subject.getRoot())); assertThat(parent.getNodes().get(0).getRange(), equalTo(parent.getRange())); assertThat(parent.getNodes().get(0).getNode(), is(concreteNode)); assertThat(parent.getSource(), is(source)); assertThat(subject.execute(parse), is(2)); verify(command).run(argThat(hasProperty("source", is(source1)))); verify(command).run(argThat(hasProperty("source", is(source2)))); }
Example #17
Source File: CommandDispatcherTest.java From brigadier with MIT License | 5 votes |
@SuppressWarnings("unchecked") @Test public void testExecuteRedirectedMultipleTimes() throws Exception { final LiteralCommandNode<Object> concreteNode = subject.register(literal("actual").executes(command)); final LiteralCommandNode<Object> redirectNode = subject.register(literal("redirected").redirect(subject.getRoot())); final String input = "redirected redirected actual"; final ParseResults<Object> parse = subject.parse(input, source); assertThat(parse.getContext().getRange().get(input), equalTo("redirected")); assertThat(parse.getContext().getNodes().size(), is(1)); assertThat(parse.getContext().getRootNode(), is(subject.getRoot())); assertThat(parse.getContext().getNodes().get(0).getRange(), equalTo(parse.getContext().getRange())); assertThat(parse.getContext().getNodes().get(0).getNode(), is(redirectNode)); final CommandContextBuilder<Object> child1 = parse.getContext().getChild(); assertThat(child1, is(notNullValue())); assertThat(child1.getRange().get(input), equalTo("redirected")); assertThat(child1.getNodes().size(), is(1)); assertThat(child1.getRootNode(), is(subject.getRoot())); assertThat(child1.getNodes().get(0).getRange(), equalTo(child1.getRange())); assertThat(child1.getNodes().get(0).getNode(), is(redirectNode)); final CommandContextBuilder<Object> child2 = child1.getChild(); assertThat(child2, is(notNullValue())); assertThat(child2.getRange().get(input), equalTo("actual")); assertThat(child2.getNodes().size(), is(1)); assertThat(child2.getRootNode(), is(subject.getRoot())); assertThat(child2.getNodes().get(0).getRange(), equalTo(child2.getRange())); assertThat(child2.getNodes().get(0).getNode(), is(concreteNode)); assertThat(subject.execute(parse), is(42)); verify(command).run(any(CommandContext.class)); }
Example #18
Source File: CommandDispatcherTest.java From brigadier with MIT License | 5 votes |
@SuppressWarnings("unchecked") @Test public void testExecuteAmbiguiousParentSubcommandViaRedirect() throws Exception { final Command<Object> subCommand = mock(Command.class); when(subCommand.run(any())).thenReturn(100); final LiteralCommandNode<Object> real = subject.register( literal("test") .then( argument("incorrect", integer()) .executes(command) ) .then( argument("right", integer()) .then( argument("sub", integer()) .executes(subCommand) ) ) ); subject.register(literal("redirect").redirect(real)); assertThat(subject.execute("redirect 1 2", source), is(100)); verify(subCommand).run(any(CommandContext.class)); verify(command, never()).run(any()); }
Example #19
Source File: LiteralArgumentBuilder.java From brigadier with MIT License | 5 votes |
@Override public LiteralCommandNode<S> build() { final LiteralCommandNode<S> result = new LiteralCommandNode<>(getLiteral(), getCommand(), getRequirement(), getRedirect(), getRedirectModifier(), isFork()); for (final CommandNode<S> argument : getArguments()) { result.addChild(argument); } return result; }
Example #20
Source File: CommandHelper.java From BoundingBoxOutlineReloaded with MIT License | 5 votes |
static boolean lastNodeIsLiteral(CommandContext<CommandSource> context, String literal) { CommandNode lastNode = getLastNode(context); if (lastNode instanceof LiteralCommandNode) { LiteralCommandNode literalCommandNode = (LiteralCommandNode) lastNode; return literalCommandNode.getLiteral().equals(literal); } return false; }
Example #21
Source File: SpigotMap.java From Chimera with MIT License | 5 votes |
DispatcherCommand wrap(LiteralCommandNode<CommandSender> command) { var aliases = new ArrayList<String>(); if (command instanceof Aliasable<?>) { for (var alias : ((Aliasable<?>) command).aliases()) { aliases.add(alias.getName()); } } return new DispatcherCommand(command.getName(), plugin, dispatcher, command.getUsageText(), aliases); }
Example #22
Source File: SpigotMap.java From Chimera with MIT License | 5 votes |
@Override public @Nullable DispatcherCommand register(LiteralCommandNode<CommandSender> command) { // We don't need to check if map contains "prefix:command_name" since Spigot will // always override it if (map.getKnownCommands().containsKey(command.getName())) { return null; } var wrapped = wrap(command); map.register(prefix, wrapped); return wrapped; }
Example #23
Source File: LuckPermsBrigadier.java From LuckPerms with MIT License | 5 votes |
public static void register(LPBukkitPlugin plugin, Command pluginCommand) throws Exception { Commodore commodore = CommodoreProvider.getCommodore(plugin.getBootstrap()); try (InputStream is = plugin.getBootstrap().getResourceStream("luckperms.commodore")) { if (is == null) { throw new Exception("Brigadier command data missing from jar"); } LiteralCommandNode<?> commandNode = CommodoreFileFormat.parse(is); commodore.register(pluginCommand, commandNode, player -> { Sender playerAsSender = plugin.getSenderFactory().wrap(player); return plugin.getCommandManager().hasPermissionForAny(playerAsSender); }); } }
Example #24
Source File: CommandArgumentType.java From multiconnect with MIT License | 5 votes |
@Override public Collection<String> getExamples() { return dispatcher.getRoot().getChildren().stream() .filter(it -> it instanceof LiteralCommandNode) .map(CommandNode::getName) .collect(Collectors.toList()); }
Example #25
Source File: AvailableCommands.java From Velocity with MIT License | 4 votes |
private static void serializeNode(CommandNode<Object> node, ByteBuf buf, Object2IntMap<CommandNode<Object>> idMappings) { byte flags = 0; if (node.getRedirect() != null) { flags |= FLAG_IS_REDIRECT; } if (node.getCommand() != null) { flags |= FLAG_EXECUTABLE; } if (node instanceof RootCommandNode<?>) { flags |= NODE_TYPE_ROOT; } else if (node instanceof LiteralCommandNode<?>) { flags |= NODE_TYPE_LITERAL; } else if (node instanceof ArgumentCommandNode<?, ?>) { flags |= NODE_TYPE_ARGUMENT; if (((ArgumentCommandNode) node).getCustomSuggestions() != null) { flags |= FLAG_HAS_SUGGESTIONS; } } else { throw new IllegalArgumentException("Unknown node type " + node.getClass().getName()); } buf.writeByte(flags); ProtocolUtils.writeVarInt(buf, node.getChildren().size()); for (CommandNode<Object> child : node.getChildren()) { ProtocolUtils.writeVarInt(buf, idMappings.getInt(child)); } if (node.getRedirect() != null) { ProtocolUtils.writeVarInt(buf, idMappings.getInt(node.getRedirect())); } if (node instanceof ArgumentCommandNode<?, ?>) { ProtocolUtils.writeString(buf, node.getName()); ArgumentPropertyRegistry.serialize(buf, ((ArgumentCommandNode) node).getType()); if (((ArgumentCommandNode) node).getCustomSuggestions() != null) { // The unchecked cast is required, but it is not particularly relevant because we check for // a more specific type later. (Even then, we only pull out one field.) @SuppressWarnings("unchecked") SuggestionProvider<Object> provider = ((ArgumentCommandNode) node).getCustomSuggestions(); if (!(provider instanceof ProtocolSuggestionProvider)) { throw new IllegalArgumentException("Suggestion provider " + provider.getClass().getName() + " is not valid."); } ProtocolUtils.writeString(buf, ((ProtocolSuggestionProvider) provider).name); } } else if (node instanceof LiteralCommandNode<?>) { ProtocolUtils.writeString(buf, node.getName()); } }
Example #26
Source File: ParsingBenchmarks.java From brigadier with MIT License | 4 votes |
@Setup public void setup() { subject = new CommandDispatcher<>(); subject.register( literal("a") .then( literal("1") .then(literal("i").executes(c -> 0)) .then(literal("ii").executes(c -> 0)) ) .then( literal("2") .then(literal("i").executes(c -> 0)) .then(literal("ii").executes(c -> 0)) ) ); subject.register(literal("b").then(literal("1").executes(c -> 0))); subject.register(literal("c").executes(c -> 0)); subject.register(literal("d").requires(s -> false).executes(c -> 0)); subject.register( literal("e") .executes(c -> 0) .then( literal("1") .executes(c -> 0) .then(literal("i").executes(c -> 0)) .then(literal("ii").executes(c -> 0)) ) ); subject.register( literal("f") .then( literal("1") .then(literal("i").executes(c -> 0)) .then(literal("ii").executes(c -> 0).requires(s -> false)) ) .then( literal("2") .then(literal("i").executes(c -> 0).requires(s -> false)) .then(literal("ii").executes(c -> 0)) ) ); subject.register( literal("g") .executes(c -> 0) .then(literal("1").then(literal("i").executes(c -> 0))) ); final LiteralCommandNode<Object> h = subject.register( literal("h") .executes(c -> 0) .then(literal("1").then(literal("i").executes(c -> 0))) .then(literal("2").then(literal("i").then(literal("ii").executes(c -> 0)))) .then(literal("3").executes(c -> 0)) ); subject.register( literal("i") .executes(c -> 0) .then(literal("1").executes(c -> 0)) .then(literal("2").executes(c -> 0)) ); subject.register( literal("j") .redirect(subject.getRoot()) ); subject.register( literal("k") .redirect(h) ); }
Example #27
Source File: LiteralArgumentBuilderTest.java From brigadier with MIT License | 4 votes |
@Test public void testBuild() throws Exception { final LiteralCommandNode<Object> node = builder.build(); assertThat(node.getLiteral(), is("foo")); }
Example #28
Source File: HallowedCommands.java From the-hallow with MIT License | 4 votes |
private static void register(CommandDispatcher<ServerCommandSource> dispatcher) { RootCommandNode<ServerCommandSource> root = dispatcher.getRoot(); LiteralCommandNode<ServerCommandSource> baseCmd = literal("spook").executes(HallowedCommand::run).build(); LiteralCommandNode<ServerCommandSource> spooktober = literal("spooktober").executes(SpooktoberCommand::run).build(); LiteralCommandNode<ServerCommandSource> contributors = literal("contributors").executes(ContibutorsCommand::run).build(); root.addChild(baseCmd); root.addChild(spooktober); baseCmd.addChild(spooktober); baseCmd.addChild(contributors); dispatcher.register(literal("the-hallow").redirect(baseCmd)); }
Example #29
Source File: CommandDispatcher.java From brigadier with MIT License | 2 votes |
/** * Utility method for registering new commands. * * <p>This is a shortcut for calling {@link RootCommandNode#addChild(CommandNode)} after building the provided {@code command}.</p> * * <p>As {@link RootCommandNode} can only hold literals, this method will only allow literal arguments.</p> * * @param command a literal argument builder to add to this command tree * @return the node added to this tree */ public LiteralCommandNode<S> register(final LiteralArgumentBuilder<S> command) { final LiteralCommandNode<S> build = command.build(); root.addChild(build); return build; }
Example #30
Source File: Aliasable.java From Chimera with MIT License | votes |
public List<LiteralCommandNode<T>> aliases();