tutorial:command_exceptions
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:command_exceptions [2022/09/18 13:39] – nexus-dino | tutorial:command_exceptions [2024/04/15 06:43] (current) – solidblock | ||
---|---|---|---|
Line 3: | Line 3: | ||
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. | 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. | ||
- | All the exceptions | + | There are two type of command |
+ | * **'' | ||
+ | * **'' | ||
+ | |||
+ | The two main types of '' | ||
Below is a coin flip command to show an example of exceptions in use. | Below is a coin flip command to show an example of exceptions in use. | ||
- | For 1.18 and below: | + | ===== Using CommandException ===== |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
dispatcher.register(literal(" | dispatcher.register(literal(" | ||
.executes(ctx -> { | .executes(ctx -> { | ||
- | Random random = Random.create(); | + | Random random = ctx.getSource().getWorld().getRandom(); |
if(random.nextBoolean()) { // If heads succeed. | if(random.nextBoolean()) { // If heads succeed. | ||
- | ctx.getSource().sendMessage(Text.translatable(" | + | ctx.getSource().sendFeedback(() -> Text.translatable(" |
return Command.SINGLE_SUCCESS; | return Command.SINGLE_SUCCESS; | ||
} | } | ||
- | throw new SimpleCommandExceptionType(new TranslatableText(" | + | throw new CommandException(Text.translatable(" |
})); | })); | ||
</ | </ | ||
+ | :!: '' | ||
- | Though you are not just limited to a single type of exception as Brigadier also supplies Dynamic exceptions which take additional parameters for context. | + | ===== Using CommandSyntaxException ===== |
- | <code java [enable_line_numbers=" | + | If you use '' |
- | DynamicCommandExceptionType used_name = new DynamicCommandExceptionType(name -> { | + | |
- | return new LiteralText(" | + | |
- | }); | + | |
- | </ | + | |
- | For 1.19 and above: | + | <code java> |
- | <code java [enable_line_numbers="true"]> | + | public class ExampleMod implements ModInitializer { |
- | dispatcher.register(literal(" | + | public static final SimpleCommandExceptionType COIN_FLIP_TAILS |
- | .executes(ctx -> { | + | |
- | Random random = Random.create(); | + | public void onInitialize() { |
- | + | CommandRegistrationCallback.EVENT.register((dispatcher, | |
- | if(random.nextBoolean()) { // If heads succeed. | + | .executes(ctx -> { |
- | ctx.getSource().sendMessage(Text.translatable(" | + | Random random = ctx.getSource().getWorld().getRandom(); |
+ | |||
+ | if (random.nextBoolean()) { // If heads succeed. | ||
+ | ctx.getSource().sendFeedback(() -> Text.translatable(" | ||
return Command.SINGLE_SUCCESS; | return Command.SINGLE_SUCCESS; | ||
- | | + | |
- | | + | |
- | })); | + | }))); |
+ | } | ||
+ | } | ||
</ | </ | ||
- | Though | + | Sometimes |
+ | <code java> | ||
+ | public class ExampleMod implements ModInitializer { | ||
+ | public static final DynamicCommandExceptionType COIN_FLIP_TAILS = new DynamicCommandExceptionType(o -> Text.translatable(" | ||
- | <code java [enable_line_numbers="true"]> | + | @Override |
- | DynamicCommandExceptionType used_name = new DynamicCommandExceptionType(name -> { | + | public void onInitialize() { |
- | | + | CommandRegistrationCallback.EVENT.register((dispatcher, |
- | }); | + | |
+ | | ||
+ | |||
+ | 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 ('' | There are more Dynamic exception types which each take a different amount of arguments into account ('' | ||
+ | There are some vanilla exceptions. Some can be found in '' |
tutorial/command_exceptions.1663508342.txt.gz · Last modified: 2022/09/18 13:39 by nexus-dino