com.mojang.brigadier.Command Java Examples

The following examples show how to use com.mojang.brigadier.Command. 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 vote down vote up
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: CommandContextTest.java    From brigadier with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testEquals() throws Exception {
    final Object otherSource = new Object();
    final Command<Object> command = mock(Command.class);
    final Command<Object> otherCommand = mock(Command.class);
    final CommandNode<Object> rootNode = mock(CommandNode.class);
    final CommandNode<Object> otherRootNode = mock(CommandNode.class);
    final CommandNode<Object> node = mock(CommandNode.class);
    final CommandNode<Object> otherNode = mock(CommandNode.class);
    new EqualsTester()
        .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, rootNode, 0).build(""), new CommandContextBuilder<>(dispatcher, source, rootNode, 0).build(""))
        .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, otherRootNode, 0).build(""), new CommandContextBuilder<>(dispatcher, source, otherRootNode, 0).build(""))
        .addEqualityGroup(new CommandContextBuilder<>(dispatcher, otherSource, rootNode, 0).build(""), new CommandContextBuilder<>(dispatcher, otherSource, rootNode, 0).build(""))
        .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withCommand(command).build(""), new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withCommand(command).build(""))
        .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withCommand(otherCommand).build(""), new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withCommand(otherCommand).build(""))
        .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withArgument("foo", new ParsedArgument<>(0, 1, 123)).build("123"), new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withArgument("foo", new ParsedArgument<>(0, 1, 123)).build("123"))
        .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withNode(node, StringRange.between(0, 3)).withNode(otherNode, StringRange.between(4, 6)).build("123 456"), new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withNode(node, StringRange.between(0, 3)).withNode(otherNode, StringRange.between(4, 6)).build("123 456"))
        .addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withNode(otherNode, StringRange.between(0, 3)).withNode(node, StringRange.between(4, 6)).build("123 456"), new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withNode(otherNode, StringRange.between(0, 3)).withNode(node, StringRange.between(4, 6)).build("123 456"))
        .testEquals();
}
 
Example #3
Source File: FabricClientSparkPlugin.java    From spark with GNU General Public License v3.0 6 votes vote down vote up
private void checkCommandRegistered() {
    CommandDispatcher<CommandSource> dispatcher = getPlayerCommandDispatcher();
    if (dispatcher == null) {
        return;
    }

    try {
        if (dispatcher != this.dispatcher) {
            this.dispatcher = dispatcher;
            registerCommands(this.dispatcher, c -> Command.SINGLE_SUCCESS, this, "sparkc", "sparkclient");
            FabricSparkGameHooks.INSTANCE.setChatSendCallback(this::onClientChat);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #4
Source File: FabricSparkPlugin.java    From spark with GNU General Public License v3.0 6 votes vote down vote up
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 #5
Source File: FabricServerSparkPlugin.java    From spark with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int run(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
    String[] args = processArgs(context);
    if (args == null) {
        return 0;
    }

    this.platform.executeCommand(new FabricCommandSender(context.getSource().getPlayer(), this), args);
    return Command.SINGLE_SUCCESS;
}
 
Example #6
Source File: ForgeClientSparkPlugin.java    From spark with GNU General Public License v3.0 5 votes vote down vote up
private void checkCommandRegistered() {
    CommandDispatcher<ISuggestionProvider> dispatcher = getPlayerCommandDispatcher();
    if (dispatcher == null) {
        return;
    }

    try {
        if (dispatcher != this.dispatcher) {
            this.dispatcher = dispatcher;
            registerCommands(this.dispatcher, context -> Command.SINGLE_SUCCESS, this, "sparkc", "sparkclient");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #7
Source File: ForgeServerSparkPlugin.java    From spark with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int run(CommandContext<CommandSource> context) throws CommandSyntaxException {
    String[] args = processArgs(context);
    if (args == null)
        return 0;
    this.platform.executeCommand(new ForgeCommandSender(context.getSource().source, this), args);
    return Command.SINGLE_SUCCESS;
}
 
Example #8
Source File: ArgumentCommandNodeTest.java    From brigadier with MIT License 5 votes vote down vote up
@Test
public void testEquals() throws Exception {
    @SuppressWarnings("unchecked") final Command<Object> command = (Command<Object>) mock(Command.class);

    new EqualsTester()
        .addEqualityGroup(
            argument("foo", integer()).build(),
            argument("foo", integer()).build()
        )
        .addEqualityGroup(
            argument("foo", integer()).executes(command).build(),
            argument("foo", integer()).executes(command).build()
        )
        .addEqualityGroup(
            argument("bar", integer(-100, 100)).build(),
            argument("bar", integer(-100, 100)).build()
        )
        .addEqualityGroup(
            argument("foo", integer(-100, 100)).build(),
            argument("foo", integer(-100, 100)).build()
        )
        .addEqualityGroup(
            argument("foo", integer()).then(
                argument("bar", integer())
            ).build(),
            argument("foo", integer()).then(
                argument("bar", integer())
            ).build()
        )
        .testEquals();
}
 
Example #9
Source File: LiteralCommandNodeTest.java    From brigadier with MIT License 5 votes vote down vote up
@Test
public void testEquals() throws Exception {
    @SuppressWarnings("unchecked") final Command<Object> command = mock(Command.class);

    new EqualsTester()
        .addEqualityGroup(
            literal("foo").build(),
            literal("foo").build()
        )
        .addEqualityGroup(
            literal("bar").executes(command).build(),
            literal("bar").executes(command).build()
        )
        .addEqualityGroup(
            literal("bar").build(),
            literal("bar").build()
        )
        .addEqualityGroup(
            literal("foo").then(
                literal("bar")
            ).build(),
            literal("foo").then(
                literal("bar")
            ).build()
        )
        .testEquals();
}
 
Example #10
Source File: VariableResolver.java    From Chimera with MIT License 5 votes vote down vote up
public VariableResolver(Environment environment) {
    super(environment);
    command = specialize(Command.class, sender);
    argumentType = types.erasure(elements.getTypeElement(ArgumentType.class.getName()).asType());
    requirement = specialize(Predicate.class, sender);
    suggestions = specialize(SuggestionProvider.class, sender);
}
 
Example #11
Source File: MutableTest.java    From Chimera with MIT License 5 votes vote down vote up
@ParameterizedTest
@MethodSource("mutables")
void setCommand(Mutable<CommandSender> mutable) {
    Command command = val -> 0;
    
    mutable.setCommand(command);
    
    assertEquals(command, ((CommandNode<?>) mutable).getCommand());
    if (mutable instanceof Aliasable<?>) {
        assertEquals(command, ((Aliasable<CommandSender>) mutable).aliases().get(0).getCommand());
    }
}
 
Example #12
Source File: ConfigCommand.java    From BoundingBoxOutlineReloaded with MIT License 5 votes vote down vote up
private static <T> void buildSetSettingCommand(LiteralArgumentBuilder<CommandSource> command,
                                               Setting<T> setting, ArgumentType<T> argument, Class<T> clazz) {
    Command<CommandSource> setSettingCommand = context -> {
        setting.set(context.getArgument(VALUE, clazz));
        if (CommandHelper.lastNodeIsLiteral(context, SAVE)) {
            ConfigManager.saveConfig();
        }
        return 0;
    };
    command.then(Commands.argument(VALUE, argument)
            .executes(setSettingCommand)
            .then(Commands.literal(SAVE)
                    .executes(setSettingCommand)));
}
 
Example #13
Source File: CommandNode.java    From brigadier with MIT License 5 votes vote down vote up
protected CommandNode(final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier, final boolean forks) {
    this.command = command;
    this.requirement = requirement;
    this.redirect = redirect;
    this.modifier = modifier;
    this.forks = forks;
}
 
Example #14
Source File: CommandContext.java    From brigadier with MIT License 5 votes vote down vote up
public CommandContext(final S source, final String input, final Map<String, ParsedArgument<S, ?>> arguments, final Command<S> command, final CommandNode<S> rootNode, final List<ParsedCommandNode<S>> nodes, final StringRange range, final CommandContext<S> child, final RedirectModifier<S> modifier, boolean forks) {
    this.source = source;
    this.input = input;
    this.arguments = arguments;
    this.command = command;
    this.rootNode = rootNode;
    this.nodes = nodes;
    this.range = range;
    this.child = child;
    this.modifier = modifier;
    this.forks = forks;
}
 
Example #15
Source File: PlayerCommand.java    From fabric-carpet with MIT License 4 votes vote down vote up
private static Command<ServerCommandSource> manipulation(Consumer<EntityPlayerActionPack> action)
{
    return c -> manipulate(c, action);
}
 
Example #16
Source File: ArgumentBuilder.java    From brigadier with MIT License 4 votes vote down vote up
public Command<S> getCommand() {
    return command;
}
 
Example #17
Source File: ArgumentBuilder.java    From brigadier with MIT License 4 votes vote down vote up
public T executes(final Command<S> command) {
    this.command = command;
    return getThis();
}
 
Example #18
Source File: CommandContext.java    From brigadier with MIT License 4 votes vote down vote up
public Command<S> getCommand() {
    return command;
}
 
Example #19
Source File: CommandContextBuilder.java    From brigadier with MIT License 4 votes vote down vote up
public Command<S> getCommand() {
    return command;
}
 
Example #20
Source File: CommandContextBuilder.java    From brigadier with MIT License 4 votes vote down vote up
public CommandContextBuilder<S> withCommand(final Command<S> command) {
    this.command = command;
    return this;
}
 
Example #21
Source File: ArgumentCommandNode.java    From brigadier with MIT License 4 votes vote down vote up
public ArgumentCommandNode(final String name, final ArgumentType<T> type, final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier, final boolean forks, final SuggestionProvider<S> customSuggestions) {
    super(command, requirement, redirect, modifier, forks);
    this.name = name;
    this.type = type;
    this.customSuggestions = customSuggestions;
}
 
Example #22
Source File: LiteralCommandNode.java    From brigadier with MIT License 4 votes vote down vote up
public LiteralCommandNode(final String literal, final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier, final boolean forks) {
    super(command, requirement, redirect, modifier, forks);
    this.literal = literal;
}
 
Example #23
Source File: CommandNode.java    From brigadier with MIT License 4 votes vote down vote up
public Command<S> getCommand() {
    return command;
}
 
Example #24
Source File: Mapper.java    From Chimera with MIT License 4 votes vote down vote up
@VisibleForOverride
protected Command<R> execution(CommandNode<T> command) {
    return (Command<R>) NONE;
}
 
Example #25
Source File: Nodes.java    From Chimera with MIT License 4 votes vote down vote up
public B executes(Execution<T> command) {
    return executes((Command<T>) command);
}
 
Example #26
Source File: CommandAPIHandler.java    From 1.13-Command-API with Apache License 2.0 4 votes vote down vote up
protected void fixPermissions() {
	/*
	 * Makes permission checks more "Bukkit" like and less "Vanilla Minecraft" like
	 */
	try {

		// Get the command map to find registered commands
		SimpleCommandMap map = nms.getSimpleCommandMap();
		Field f = getField(SimpleCommandMap.class, "knownCommands");
		Map<String, org.bukkit.command.Command> knownCommands = (Map<String, org.bukkit.command.Command>) f
				.get(map);

		CommandAPIMain.getLog().info("Linking permissions to commands:");
		permissionsToFix.forEach((cmdName, perm) -> {

			if (perm.equals(CommandPermission.NONE)) {
				if (CommandAPIMain.getConfiguration().hasVerboseOutput()) {
					CommandAPIMain.getLog().info("NONE -> /" + cmdName);
				}
				// Set the command permission to empty string (Minecraft standard for "no
				// permission required")
				if (nms.isVanillaCommandWrapper(knownCommands.get(cmdName))) {
					knownCommands.get(cmdName).setPermission("");
				}
				if (nms.isVanillaCommandWrapper(knownCommands.get("minecraft:" + cmdName))) {
					knownCommands.get(cmdName).setPermission("");
				}
			} else {
				if (perm.getPermission() != null) {
					if (CommandAPIMain.getConfiguration().hasVerboseOutput()) {
						CommandAPIMain.getLog().info(perm.getPermission() + " -> /" + cmdName);
					}
					// Set the command permission to the (String) permission node
					if (nms.isVanillaCommandWrapper(knownCommands.get(cmdName))) {
						knownCommands.get(cmdName).setPermission(perm.getPermission());
					}
					if (nms.isVanillaCommandWrapper(knownCommands.get("minecraft:" + cmdName))) {
						knownCommands.get(cmdName).setPermission(perm.getPermission());
					}
				} else {
					// Dafaq?
				}
			}
		});
	} catch (IllegalAccessException | IllegalArgumentException e) {
		e.printStackTrace();
	}
}
 
Example #27
Source File: Mutable.java    From Chimera with MIT License votes vote down vote up
public void setCommand(Command<T> command); 
Example #28
Source File: Mutable.java    From Chimera with MIT License votes vote down vote up
public Command<T> getCommand();