User Tools

Site Tools


zh_cn:tutorial:command_argument_types

Differences

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

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
zh_cn:tutorial:command_argument_types [2023/04/20 10:57] – created solidblockzh_cn:tutorial:command_argument_types [2023/04/20 11:11] – fix type solidblock
Line 42: Line 42:
 <code java [enable_line_numbers="true"]>String uuidString = reader.getString().substring(argBeginning, reader.getCursor());</code> <code java [enable_line_numbers="true"]>String uuidString = reader.getString().substring(argBeginning, reader.getCursor());</code>
  
-最终,我们检我们的参数是否正确,并解析我们的参数,如果解析失败则抛出异常。+最终,我们检我们的参数是否正确,并解析我们的参数,如果解析失败则抛出异常。
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
Line 73: Line 73:
 import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
 import net.minecraft.text.Text; import net.minecraft.text.Text;
-import net.minecraft.util.SystemUtil; 
  
 import java.util.ArrayList; import java.util.ArrayList;
Line 80: Line 79:
  
 /** /**
- Represents an ArgumentType that will return a UUID.+ 代表一个返回 UUID 的参数类型。
  */  */
 public class UuidArgumentType implements ArgumentType<UUID> { public class UuidArgumentType implements ArgumentType<UUID> {
Line 88: Line 87:
  
     public static <S> UUID getUuid(String name, CommandContext<S> context) {     public static <S> UUID getUuid(String name, CommandContext<S> context) {
-        // Note that you should assume the CommandSource wrapped inside of the CommandContext will always be a generic type. +        // 注意你应该假设 CommandContext 中包含的 CommandSource 是一个泛型类型。 
-        // If you need to access the ServerCommandSource make sure you verify the source is a server command source before casting.+        // 如果你需要访问 ServerCommandSource,确保你在强转之前验证了命令源。
         return context.getArgument(name, UUID.class);         return context.getArgument(name, UUID.class);
     }     }
  
-    private static final Collection<String> EXAMPLES = SystemUtil.consume(new ArrayList<>(), list -> { +    private static final Collection<String> EXAMPLES = List.of
-        list.add("765e5d33-c991-454f-8775-b6a7a394c097"); // i509VCB: Username The_1_gamers +        "765e5d33-c991-454f-8775-b6a7a394c097"// i509VCB: 用户名 The_1_gamers 
-        list.add("069a79f4-44e9-4726-a5be-fca90e38aaf5"); // Notch +        "069a79f4-44e9-4726-a5be-fca90e38aaf5"// Notch 
-        list.add("61699b2e-d327-4a01-9f1e-0ea8c3f06bc6"); // Dinnerbone +        "61699b2e-d327-4a01-9f1e-0ea8c3f06bc6"  // Dinnerbone 
-    });+    );
  
     @Override     @Override
     public UUID parse(StringReader reader) throws CommandSyntaxException {     public UUID parse(StringReader reader) throws CommandSyntaxException {
-        int argBeginning = reader.getCursor(); // The starting position of the cursor is at the beginning of the argument.+        int argBeginning = reader.getCursor(); // 指针的开始位置,是参数的开始位置。
         if (!reader.canRead()) {         if (!reader.canRead()) {
             reader.skip();             reader.skip();
         }         }
  
-        // Now we check the contents of the argument till either we hit the end of the +        // 现在我们检查参数的内容,直到命令行的末尾(即 ''canRead'' 返回了 false 
-        // command line (when ''canRead'' becomes false) +        // 否则我们检查到空格的位置,即表示下一个参数要开始了 
-        // Otherwise we go till reach reach a space, which signifies the next argument +        while (reader.canRead() && reader.peek() != ' ') { // peek”提供了当前指针位置的字符。 
-        while (reader.canRead() && reader.peek() != ' ') { // peek provides the character at the current cursor position. +            reader.skip(); // 告诉 StringReader,将其指针移动到下一个位置。
-            reader.skip(); // Tells the StringReader to move it's cursor to the next position.+
         }         }
- +         
-        // Now we substring the specific part we want to see using the starting cursor +        // 现在我们使用当前的指针位置,取出我们需要看到的特定部分子字符串,在下一个参数开始的位置结束。
-        // position and the ends where the next argument starts.+
         String uuidString = reader.getString().substring(argBeginning, reader.getCursor());         String uuidString = reader.getString().substring(argBeginning, reader.getCursor());
         try {         try {
-            UUID uuid = UUID.fromString(uuidString); // Now our actual logic.+            UUID uuid = UUID.fromString(uuidString); // 我们的正常逻辑。
             return uuid;             return uuid;
-            // And we return our type, in this case the parser will consider this +            // 我们返回我们的类型,在这个例子中,解析器会认为类型已经正确解析,并继续。
-            // argument to have parsed properly and then move on.+
         } catch (Exception ex) {         } catch (Exception ex) {
-            // UUIDs can throw an exception when made by a string, so we catch the exception +            // UUID 在由字符串生成时,可能会抛出异常,因此我们捕获这个异常,并将其包装成 CommandSyntaxException 类型。 
-            // and repackage it into a CommandSyntaxException type. +            // 创建时会带有环境,告诉 Brigadier,根据这个环境来告诉玩家,命令在哪里解析失败了。 
-            // Create with context tells Brigadier to supply some context to tell the user +            // 尽管应该使用正常的 create 方法。
-            // where the command failed at. +
-            // Though normal create method could be used.+
             throw new SimpleCommandExceptionType(Text.literal(ex.getMessage())).createWithContext(reader);             throw new SimpleCommandExceptionType(Text.literal(ex.getMessage())).createWithContext(reader);
         }         }
Line 133: Line 127:
     @Override     @Override
     public Collection<String> getExamples() {     public Collection<String> getExamples() {
-        // Brigadier has support to show examples for what the argument should look like, +        // Brigadier 支持显示示例,表示这个命令应该像是什么样子,这应该包含一个集合, 
-        // this should contain a Collection of only the argument this type will return. +        // 集合的内容是此类型能够返回的参数。 
-        // This is mainly used to calculate ambiguous commands which share the exact same +        // 这主要用于计算共享了同一个的难懂的命令。
         return EXAMPLES;         return EXAMPLES;
     }     }
zh_cn/tutorial/command_argument_types.txt · Last modified: 2024/04/15 07:24 by solidblock