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:37] – 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(" |
| })); | })); | ||
| </ | </ | ||
| + | :!: '' | ||
| - | For 1.19 and above: | + | ===== Using CommandSyntaxException ===== |
| - | <code java [enable_line_numbers="true"]> | + | |
| - | dispatcher.register(literal(" | + | If you use '' |
| - | .executes(ctx -> { | + | |
| - | Random random = Random.create(); | + | <code java> |
| - | + | public class ExampleMod implements ModInitializer { | |
| - | if(random.nextBoolean()) { // If heads succeed. | + | public static final SimpleCommandExceptionType COIN_FLIP_TAILS |
| - | ctx.getSource().sendMessage(Text.translatable(" | + | |
| + | public void onInitialize() { | ||
| + | CommandRegistrationCallback.EVENT.register((dispatcher, | ||
| + | .executes(ctx -> { | ||
| + | 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.1663508270.txt.gz · Last modified: 2022/09/18 13:37 by nexus-dino