User Tools

Site Tools


zh_cn:tutorial:command_exceptions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
zh_cn:tutorial:command_exceptions [2023/02/20 05:41] solidblockzh_cn:tutorial:command_exceptions [2024/04/15 06:35] (current) solidblock
Line 3: Line 3:
 Brigadier 支持命令异常,这些异常可用于结束命令,例如参数未正确解析或命令未能执行,以及更丰富的错误细节。 Brigadier 支持命令异常,这些异常可用于结束命令,例如参数未正确解析或命令未能执行,以及更丰富的错误细节。
  
-Brigadier 的所有异常都基于 ''CommandSyntaxException''Brigadier 提供的两种主异常类型分为动态(Dynamic)和一般(Simple)你需要用 ''create()'' 抛出它。这些异常还允许您使用 ''createWithContext(ImmutableStringReader)'' 指定现异常上下文它将生成错误消息指向输的代码中出错的位置+命令异常有以下两种类型: 
 +  * **''CommandSyntaxException''**:由 Brigadier 提供,需先有一个异常类型,然后通过 ''create(...)'' 或 ''createWithContext(...)'' 创建。不属于 ''RuntimeException'',所以一旦抛,必须被适当捕获,或者添加到方法签名中。 
 +  * **''<yarn class_2164>''(自 1.20.3 被移除)**:由 Minecraft 提供,用于更少情况。属于 ''RuntimeException''直接传入 ''Text'' 作为参数
  
-下面是一个抛硬币的代码用于显示使用中的异常示例+''CommandSyntaxException'' 的两种类型包括动态和基本的类型,其中你可以调用 ''create()'' 以创建一个异常以出。这些异常也允许你指定环境,使用 ''createWithContext(ImmutableStringReader)'' 就可以,会构建出一条异常消息以指出你输入的命令中哪里出现错误
  
-对于1.18及以+面是一个抛硬币的代码,用于显示使用中的异常示例。
  
 +===== 使用 CommandException =====
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
 dispatcher.register(literal("coinflip") dispatcher.register(literal("coinflip")
     .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("coin.flip.heads"))+            ctx.getSource().sendFeedback(() -> Text.translatable("coin.flip.heads"), true);
             return Command.SINGLE_SUCCESS;             return Command.SINGLE_SUCCESS;
         }         }
  
-        throw new SimpleCommandExceptionType(new TranslatableText("coin.flip.tails")).create(); // Oh no tails, you lose.+        throw new CommandException(Text.translatable("coin.flip.tails")); 
     }));     }));
 </code> </code>
 +:!: ''CommandException'' is removed since 1.20.3.
  
-你不需要局限于一种类型的异常,Brigadier 还提供了动态异常,能为上下文提供额外的参数。+===== 使用 CommandSyntaxException =====
  
-<code java [enable_line_numbers="true"]> +如果你使用 ''CommandSyntaxException'',需要先添加一个 ''SimpleCommandExceptionType'',通常存储为字段,所以这个例子呈现一整个类。
-DynamicCommandExceptionType used_name = new DynamicCommandExceptionType(name -> { +
-    return new LiteralText("The name: " + (String) name + " has been used"); +
-}); +
-</code>+
  
-1.19及以上:+<code java> 
 +public class ExampleMod implements ModInitializer { 
 +  public static final SimpleCommandExceptionType COIN_FLIP_TAILS = new SimpleCommandExceptionType(Text.translatable("coin.flip.tails")); 
 +  @Override 
 +  public void onInitialize() { 
 +    CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(literal("coinflip"
 +        .executes(ctx -> { 
 +          Random random = ctx.getSource().getWorld().getRandom();
  
-静态 +          if (random.nextBoolean()) { // 如果是正面。 
- +            ctx.getSource().sendFeedback(() -> Text.translatable("coin.flip.heads"), true);
-<code java [enable_line_numbers="true"]> +
-dispatcher.register(literal("coinflip"+
-    .executes(ctx -> { +
-        Random random = Random.create(); +
-  +
-        if(random.nextBoolean()) { // If heads succeed. +
-            ctx.getSource().sendMessage(Text.translatable("coin.flip.heads"))+
             return Command.SINGLE_SUCCESS;             return Command.SINGLE_SUCCESS;
-        }+          }
  
-        throw new SimpleCommandExceptionType(Text.translatable("coin.flip.tails")).create(); // Oh no tails, you lose. +          throw COIN_FLIP_TAILS.create(); 
-    }));+        }))); 
 +  } 
 +}
 </code> </code>
  
-动态+有时你也会使用 ''DynamicCommandExceptionType'' 以接受复杂的异常,例如: 
 +<code java> 
 +public class ExampleMod implements ModInitializer { 
 +  public static final DynamicCommandExceptionType COIN_FLIP_TAILS = new DynamicCommandExceptionType(o -> Text.translatable("coin.flip.tails", o));
  
-<code java [enable_line_numbers="true"]> +  @Override 
-DynamicCommandExceptionType used_name = new DynamicCommandExceptionType(name -> +  public void onInitialize(
-    return Text.literal("The name: (Stringname + has been used"); +    CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(literal("coinflip"
-})+        .executes(ctx -> { 
-</code>+          Random random = ctx.getSource().getWorld().getRandom(); 
 + 
 +          if (random.nextBoolean()) { // 如果是正面。 
 +            ctx.getSource().sendFeedback(() -> Text.translatable("coin.flip.heads"), true); 
 +            return Command.SINGLE_SUCCESS
 +          }
  
-还有更多的动态异常类型,每种类型都考虑了不同数量的参数(''Dynamic2CommandExceptionType''、''Dynamic3CommandExcessionType''、''Dynamic4CommandExcitionType''、''Dynamic NCommandExclusionType'')。你应该记住,动态异常将 ''Object'' 对象作为参数,因此你可能必须强制转换参数以供使用。+          throw COIN_FLIP_TAILS.create(ctx.getSource().getDisplayName()); 
 +        }))); 
 +  } 
 +
 +</code>
  
 +有更多的动态异常类型,可以接受不同数量的参数(''Dynamic2CommandExceptionType''、''Dynamic3CommandExceptionType''、''Dynamic4CommandExceptionType''、''DynamicNCommandExceptionType'')。请记住,动态异常会接收 object 作为参数,因此你在使用时可能需要强转。
  
 +有些原版的异常,可以见于 ''CommandSyntaxException.BUILT_IN_EXCEPTIONS.//<异常类型>//()''
zh_cn/tutorial/command_exceptions.txt · Last modified: 2024/04/15 06:35 by solidblock