Table of Contents

Command Exceptions

Brigadier supports command exceptions which can be used to end a command such as if an argument didn't parse properly or the command failed to execute, as well as including richer details of the failure.

There are two type of command exceptions:

The two main types of CommandSyntaxException are dynamic and simple exception types, of which you can call create() to create the exception to throw it. These exceptions also allow you to specify the context in which the exception was thrown using createWithContext(ImmutableStringReader), which builds the error message to point to where on the inputted command line the error occured.

Below is a coin flip command to show an example of exceptions in use.

Using CommandException

  1. dispatcher.register(literal("coinflip")
  2. .executes(ctx -> {
  3. Random random = ctx.getSource().getWorld().getRandom();
  4.  
  5. if(random.nextBoolean()) { // If heads succeed.
  6. ctx.getSource().sendFeedback(() -> Text.translatable("coin.flip.heads"), true);
  7. return Command.SINGLE_SUCCESS;
  8. }
  9.  
  10. throw new CommandException(Text.translatable("coin.flip.tails"));
  11. }));

:!: CommandException is removed since 1.20.3.

Using CommandSyntaxException

If you use CommandSyntaxException, you should add a SimpleCommandExceptionType first. It is usually stored as fields, so this example presents the whole class.

public class ExampleMod implements ModInitializer {
  public static final SimpleCommandExceptionType COIN_FLIP_TAILS = new SimpleCommandExceptionType(Text.translatable("coin.flip.tails"));
  @Override
  public void onInitialize() {
    CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(literal("coinflip")
        .executes(ctx -> {
          Random random = ctx.getSource().getWorld().getRandom();
 
          if (random.nextBoolean()) { // If heads succeed.
            ctx.getSource().sendFeedback(() -> Text.translatable("coin.flip.heads"), true);
            return Command.SINGLE_SUCCESS;
          }
 
          throw COIN_FLIP_TAILS.create();
        })));
  }
}

Sometimes you may use DynamicCommandExceptionType to accept complex exceptions. For example:

public class ExampleMod implements ModInitializer {
  public static final DynamicCommandExceptionType COIN_FLIP_TAILS = new DynamicCommandExceptionType(o -> Text.translatable("coin.flip.tails", o));
 
  @Override
  public void onInitialize() {
    CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(literal("coinflip")
        .executes(ctx -> {
          Random random = ctx.getSource().getWorld().getRandom();
 
          if (random.nextBoolean()) { // If heads succeed.
            ctx.getSource().sendFeedback(() -> Text.translatable("coin.flip.heads"), true);
            return Command.SINGLE_SUCCESS;
          }
 
          throw COIN_FLIP_TAILS.create(ctx.getSource().getDisplayName());
        })));
  }
}

There are more Dynamic exception types which each take a different amount of arguments into account (Dynamic2CommandExceptionType, Dynamic3CommandExceptionType, Dynamic4CommandExceptionType, DynamicNCommandExceptionType). You should remember that the Dynamic exceptions takes an object as an argument so you may have to cast the argument for your use.

There are some vanilla exceptions. Some can be found in CommandSyntaxException.BUILT_IN_EXCEPTIONS.<exception type>().