zh_cn:tutorial:command_argument_types
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
zh_cn:tutorial:command_argument_types [2023/04/20 10:57] – created solidblock | zh_cn:tutorial:command_argument_types [2023/04/20 11:11] – fix type solidblock | ||
---|---|---|---|
Line 42: | Line 42: | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | 最终,我们检票我们的参数是否正确,并解析我们的参数,如果解析失败则抛出异常。 | + | 最终,我们检查我们的参数是否正确,并解析我们的参数,如果解析失败则抛出异常。 |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
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: | ||
/** | /** | ||
- | | + | |
*/ | */ | ||
public class UuidArgumentType implements ArgumentType< | public class UuidArgumentType implements ArgumentType< | ||
Line 88: | Line 87: | ||
public static <S> UUID getUuid(String name, CommandContext< | public static <S> UUID getUuid(String name, CommandContext< | ||
- | // Note that you should assume the CommandSource wrapped inside of the CommandContext | + | // 注意你应该假设 |
- | // If you need to access the ServerCommandSource | + | // 如果你需要访问 |
return context.getArgument(name, | return context.getArgument(name, | ||
} | } | ||
- | private static final Collection< | + | private static final Collection< |
- | | + | " |
- | | + | " |
- | | + | " |
- | | + | ); |
@Override | @Override | ||
public UUID parse(StringReader reader) throws CommandSyntaxException { | public UUID parse(StringReader reader) throws CommandSyntaxException { | ||
- | int argBeginning = reader.getCursor(); | + | 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 | + | // 现在我们检查参数的内容,直到命令行的末尾(即 |
- | // command line (when '' | + | // 否则我们检查到空格的位置,即表示下一个参数要开始了 |
- | // 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(); |
- | reader.skip(); | + | |
} | } | ||
- | + | | |
- | // 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, | String uuidString = reader.getString().substring(argBeginning, | ||
try { | try { | ||
- | UUID uuid = UUID.fromString(uuidString); | + | 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 在由字符串生成时,可能会抛出异常,因此我们捕获这个异常,并将其包装成 |
- | // and repackage it into a CommandSyntaxException | + | // 创建时会带有环境,告诉 |
- | // Create with context tells Brigadier | + | // 尽管应该使用正常的 |
- | // where the command failed at. | + | |
- | // Though normal | + | |
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< | public Collection< | ||
- | // Brigadier | + | // 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