OperationArgument.java

net.minecraft.commands.arguments.OperationArgument

信息

  • 全限定名:net.minecraft.commands.arguments.OperationArgument
  • 类型:public class
  • 包:net.minecraft.commands.arguments
  • 源码路径:src/main/java/net/minecraft/commands/arguments/OperationArgument.java
  • 起始行号:L19
  • 实现:ArgumentType<OperationArgument.Operation>
  • 职责:

    TODO

字段/常量

  • EXAMPLES

    • 类型: Collection<String>
    • 修饰符: private static final
    • 源码定位: L20
    • 说明:

      TODO

  • ERROR_INVALID_OPERATION

    • 类型: SimpleCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L21
    • 说明:

      TODO

  • ERROR_DIVIDE_BY_ZERO

    • 类型: SimpleCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L24
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.commands.arguments.OperationArgument.Operation

    • 类型: interface
    • 修饰符: public
    • 源码定位: L93
    • 说明:

      TODO

  • net.minecraft.commands.arguments.OperationArgument.SimpleOperation

    • 类型: interface
    • 修饰符: private
    • 源码定位: L98
    • 说明:

      TODO

构造器

方法

下面的方法块按源码顺序生成。

public static OperationArgument operation() @ L26

  • 方法名:operation
  • 源码定位:L26
  • 返回类型:OperationArgument
  • 修饰符:public static

参数:

说明:

TODO

public static OperationArgument.Operation getOperation(CommandContext<CommandSourceStack> context, String name) @ L30

  • 方法名:getOperation
  • 源码定位:L30
  • 返回类型:OperationArgument.Operation
  • 修饰符:public static

参数:

  • context: CommandContext
  • name: String

说明:

TODO

public OperationArgument.Operation parse(StringReader reader) @ L34

  • 方法名:parse
  • 源码定位:L34
  • 返回类型:OperationArgument.Operation
  • 修饰符:public

参数:

  • reader: StringReader

说明:

TODO

public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) @ L48

  • 方法名:listSuggestions
  • 源码定位:L48
  • 返回类型: CompletableFuture
  • 修饰符:public

参数:

  • context: CommandContext
  • builder: SuggestionsBuilder

说明:

TODO

public Collection<String> getExamples() @ L53

  • 方法名:getExamples
  • 源码定位:L53
  • 返回类型:Collection
  • 修饰符:public

参数:

说明:

TODO

private static OperationArgument.Operation getOperation(String op) @ L58

  • 方法名:getOperation
  • 源码定位:L58
  • 返回类型:OperationArgument.Operation
  • 修饰符:private static

参数:

  • op: String

说明:

TODO

private static OperationArgument.SimpleOperation getSimpleOperation(String op) @ L66

  • 方法名:getSimpleOperation
  • 源码定位:L66
  • 返回类型:OperationArgument.SimpleOperation
  • 修饰符:private static

参数:

  • op: String

说明:

TODO

代码

public class OperationArgument implements ArgumentType<OperationArgument.Operation> {
    private static final Collection<String> EXAMPLES = Arrays.asList("=", ">", "<");
    private static final SimpleCommandExceptionType ERROR_INVALID_OPERATION = new SimpleCommandExceptionType(
        Component.translatable("arguments.operation.invalid")
    );
    private static final SimpleCommandExceptionType ERROR_DIVIDE_BY_ZERO = new SimpleCommandExceptionType(Component.translatable("arguments.operation.div0"));
 
    public static OperationArgument operation() {
        return new OperationArgument();
    }
 
    public static OperationArgument.Operation getOperation(CommandContext<CommandSourceStack> context, String name) {
        return context.getArgument(name, OperationArgument.Operation.class);
    }
 
    public OperationArgument.Operation parse(StringReader reader) throws CommandSyntaxException {
        if (!reader.canRead()) {
            throw ERROR_INVALID_OPERATION.createWithContext(reader);
        } else {
            int start = reader.getCursor();
 
            while (reader.canRead() && reader.peek() != ' ') {
                reader.skip();
            }
 
            return getOperation(reader.getString().substring(start, reader.getCursor()));
        }
    }
 
    @Override
    public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
        return SharedSuggestionProvider.suggest(new String[]{"=", "+=", "-=", "*=", "/=", "%=", "<", ">", "><"}, builder);
    }
 
    @Override
    public Collection<String> getExamples() {
        return EXAMPLES;
    }
 
    private static OperationArgument.Operation getOperation(String op) throws CommandSyntaxException {
        return (OperationArgument.Operation)(op.equals("><") ? (OperationArgument.Operation)(a, b) -> {
            int swap = a.get();
            a.set(b.get());
            b.set(swap);
        } : getSimpleOperation(op));
    }
 
    private static OperationArgument.SimpleOperation getSimpleOperation(String op) throws CommandSyntaxException {
        return switch (op) {
            case "=" -> (a, b) -> b;
            case "+=" -> Integer::sum;
            case "-=" -> (a, b) -> a - b;
            case "*=" -> (a, b) -> a * b;
            case "/=" -> (a, b) -> {
                if (b == 0) {
                    throw ERROR_DIVIDE_BY_ZERO.create();
                } else {
                    return Mth.floorDiv(a, b);
                }
            };
            case "%=" -> (a, b) -> {
                if (b == 0) {
                    throw ERROR_DIVIDE_BY_ZERO.create();
                } else {
                    return Mth.positiveModulo(a, b);
                }
            };
            case "<" -> Math::min;
            case ">" -> Math::max;
            default -> throw ERROR_INVALID_OPERATION.create();
        };
    }
 
    @FunctionalInterface
    public interface Operation {
        void apply(ScoreAccess a, ScoreAccess b) throws CommandSyntaxException;
    }
 
    @FunctionalInterface
    private interface SimpleOperation extends OperationArgument.Operation {
        int apply(int a, int b) throws CommandSyntaxException;
 
        @Override
        default void apply(ScoreAccess a, ScoreAccess b) throws CommandSyntaxException {
            a.set(this.apply(a.get(), b.get()));
        }
    }
}

引用的其他类

  • CommandSourceStack

    • 引用位置: 参数
  • SharedSuggestionProvider

    • 引用位置: 方法调用
    • 关联成员: SharedSuggestionProvider.suggest()
  • Component

    • 引用位置: 方法调用
    • 关联成员: Component.translatable()
  • Mth

    • 引用位置: 方法调用
    • 关联成员: Mth.floorDiv(), Mth.positiveModulo()