tutorial:command_exceptions
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
tutorial:command_exceptions [2022/08/08 02:16] – created (migrated from [[tutorial:commands]]) solidblock | tutorial:command_exceptions [2024/04/15 06:43] (current) – solidblock | ||
---|---|---|---|
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. | ||
- | 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. | ||
+ | ===== Using CommandException ===== | ||
<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 = 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(Text.translatable(" | + | 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 | + | |
- | | + | <code java> |
- | }); | + | public class ExampleMod implements ModInitializer { |
+ | public static final SimpleCommandExceptionType COIN_FLIP_TAILS | ||
+ | @Override | ||
+ | public void onInitialize() | ||
+ | | ||
+ | .executes(ctx -> { | ||
+ | Random random = ctx.getSource().getWorld().getRandom(); | ||
+ | |||
+ | if (random.nextBoolean()) { // If heads succeed. | ||
+ | ctx.getSource().sendFeedback(() -> Text.translatable("coin.flip.heads"), true); | ||
+ | | ||
+ | | ||
+ | |||
+ | throw COIN_FLIP_TAILS.create(); | ||
+ | }))); | ||
+ | } | ||
+ | } | ||
</ | </ | ||
- | There are more Dynamic exception types which each take a different amount of arguments into account ('' | + | Sometimes you may use '' |
+ | <code java> | ||
+ | public class ExampleMod implements ModInitializer { | ||
+ | public static final DynamicCommandExceptionType COIN_FLIP_TAILS = new DynamicCommandExceptionType(o -> Text.translatable(" | ||
+ | @Override | ||
+ | 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; | ||
+ | } | ||
+ | |||
+ | 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 some vanilla exceptions. Some can be found in '' |
tutorial/command_exceptions.1659925012.txt.gz · Last modified: 2022/08/08 02:16 by solidblock