zh_cn:tutorial:command_exceptions
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| zh_cn:tutorial:command_exceptions [2023/01/02 05:16] – lei64 | zh_cn:tutorial:command_exceptions [2024/04/15 06:35] (current) – solidblock | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| Brigadier 支持命令异常,这些异常可用于结束命令,例如参数未正确解析或命令未能执行,以及更丰富的错误细节。 | Brigadier 支持命令异常,这些异常可用于结束命令,例如参数未正确解析或命令未能执行,以及更丰富的错误细节。 | ||
| - | Brigadier 的所有异常都基于 | + | 命令异常有以下两种类型: |
| + | * **'' | ||
| + | * **''< | ||
| - | 下面是一个抛硬币的代码,用于显示使用中的异常示例。 | + | '' |
| - | 对于1.18及以下: | + | 下面是一个抛硬币的代码,用于显示使用中的异常示例。 |
| + | ===== 使用 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()) { // 如果是正面。 |
| - | 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(" |
| })); | })); | ||
| </ | </ | ||
| + | :!: '' | ||
| - | 你不需要局限于一种类型的异常,Brigadier还提供了动态异常,能为上下文提供额外的参数。 | + | ===== 使用 CommandSyntaxException ===== |
| - | <code java [enable_line_numbers=" | + | 如果你使用 '' |
| - | DynamicCommandExceptionType used_name = new DynamicCommandExceptionType(name -> { | + | |
| - | return new LiteralText(" | + | |
| - | }); | + | |
| - | </ | + | |
| - | 1.19及以上: | + | <code java> |
| + | public class ExampleMod implements ModInitializer { | ||
| + | public static final SimpleCommandExceptionType COIN_FLIP_TAILS = new SimpleCommandExceptionType(Text.translatable(" | ||
| + | @Override | ||
| + | public void onInitialize() { | ||
| + | CommandRegistrationCallback.EVENT.register((dispatcher, | ||
| + | .executes(ctx -> { | ||
| + | Random random = ctx.getSource().getWorld().getRandom(); | ||
| - | 静态 | + | |
| - | + | ctx.getSource().sendFeedback(() -> Text.translatable(" | |
| - | <code java [enable_line_numbers=" | + | |
| - | dispatcher.register(literal(" | + | |
| - | .executes(ctx -> { | + | |
| - | Random random = Random.create(); | + | |
| - | + | ||
| - | | + | |
| - | ctx.getSource().sendMessage(Text.translatable(" | + | |
| return Command.SINGLE_SUCCESS; | return Command.SINGLE_SUCCESS; | ||
| - | | + | |
| - | | + | |
| - | })); | + | }))); |
| + | } | ||
| + | } | ||
| </ | </ | ||
| - | 动态 | + | 有时你也会使用 '' |
| + | <code java> | ||
| + | public class ExampleMod implements ModInitializer { | ||
| + | public static final DynamicCommandExceptionType COIN_FLIP_TAILS = new DynamicCommandExceptionType(o -> Text.translatable(" | ||
| - | <code java [enable_line_numbers=" | + | @Override |
| - | DynamicCommandExceptionType used_name = new DynamicCommandExceptionType(name -> { | + | |
| - | | + | |
| - | }); | + | .executes(ctx -> { |
| - | </ | + | Random random = ctx.getSource().getWorld().getRandom(); |
| + | |||
| + | if (random.nextBoolean()) { // 如果是正面。 | ||
| + | ctx.getSource().sendFeedback(() -> Text.translatable("coin.flip.heads"), true); | ||
| + | | ||
| + | } | ||
| - | 还有更多的动态异常类型,每种类型都考虑了不同数量的参数(“Dynamic2CommandExceptionType”、“Dynamic3CommandExcessionType”、”“Dynamic4CommandExcitionType”“、”“Dynamic NCommandExclusionType”“)。你应该记住,动态异常将对象作为参数,因此你可能必须强制转换参数以供使用。 | + | throw COIN_FLIP_TAILS.create(ctx.getSource().getDisplayName()); |
| + | }))); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | 有更多的动态异常类型,可以接受不同数量的参数('' | ||
| + | 有些原版的异常,可以见于 '' | ||
zh_cn/tutorial/command_exceptions.1672636579.txt.gz · Last modified: 2023/01/02 05:16 by lei64