tutorial:command_exceptions
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:command_exceptions [2022/08/08 02:16] – created (migrated from [[tutorial:commands]]) solidblock | tutorial:command_exceptions [2022/09/18 13:38] – nexus-dino | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ======= 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. | 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. | ||
Line 7: | Line 7: | ||
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: | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
dispatcher.register(literal(" | dispatcher.register(literal(" | ||
.executes(ctx -> { | .executes(ctx -> { | ||
- | Random random = new Random(); | + | Random random = Random.create(); |
+ | |||
+ | if(random.nextBoolean()) { // If heads succeed. | ||
+ | ctx.getSource().sendMessage(Text.translatable(" | ||
+ | return Command.SINGLE_SUCCESS; | ||
+ | } | ||
+ | |||
+ | throw new SimpleCommandExceptionType(new TranslatableText(" | ||
+ | })); | ||
+ | </ | ||
+ | |||
+ | <code java [enable_line_numbers=" | ||
+ | DynamicCommandExceptionType used_name = new DynamicCommandExceptionType(name -> { | ||
+ | return new LiteralText(" | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | For 1.19 and above: | ||
+ | <code java [enable_line_numbers=" | ||
+ | dispatcher.register(literal(" | ||
+ | .executes(ctx -> { | ||
+ | Random random = Random.create(); | ||
if(random.nextBoolean()) { // If heads succeed. | if(random.nextBoolean()) { // If heads succeed. |
tutorial/command_exceptions.txt · Last modified: 2024/04/15 06:43 by solidblock