CompilableString.java

net.minecraft.util.CompilableString

信息

  • 全限定名:net.minecraft.util.CompilableString
  • 类型:public class
  • 包:net.minecraft.util
  • 源码路径:src/main/java/net/minecraft/util/CompilableString.java
  • 起始行号:L12
  • 职责:

    TODO

字段/常量

  • source

    • 类型: String
    • 修饰符: private final
    • 源码定位: L13
    • 说明:

      TODO

  • compiled

    • 类型: T
    • 修饰符: private final
    • 源码定位: L14
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.util.CompilableString.CommandParserHelper
    • 类型: class
    • 修饰符: public abstract static
    • 源码定位: L48
    • 说明:

      TODO

构造器

private CompilableString(String source, T compiled) @ L16

  • 构造器名:CompilableString
  • 源码定位:L16
  • 修饰符:private

参数:

  • source: String
  • compiled: T

说明:

TODO

方法

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

public static <T> Codec<CompilableString<T>> codec(Function<String,DataResult<T>> compiler) @ L21

  • 方法名:codec
  • 源码定位:L21
  • 返回类型: Codec<CompilableString>
  • 修饰符:public static

参数:

  • compiler: Function<String,DataResult>

说明:

TODO

public String source() @ L25

  • 方法名:source
  • 源码定位:L25
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

public T compiled() @ L29

  • 方法名:compiled
  • 源码定位:L29
  • 返回类型:T
  • 修饰符:public

参数:

说明:

TODO

public boolean equals(Object o) @ L33

  • 方法名:equals
  • 源码定位:L33
  • 返回类型:boolean
  • 修饰符:public

参数:

  • o: Object

说明:

TODO

public int hashCode() @ L38

  • 方法名:hashCode
  • 源码定位:L38
  • 返回类型:int
  • 修饰符:public

参数:

说明:

TODO

public String toString() @ L43

  • 方法名:toString
  • 源码定位:L43
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

代码

public class CompilableString<T> {
    private final String source;
    private final T compiled;
 
    private CompilableString(String source, T compiled) {
        this.source = source;
        this.compiled = compiled;
    }
 
    public static <T> Codec<CompilableString<T>> codec(Function<String, DataResult<T>> compiler) {
        return Codec.STRING.comapFlatMap(s -> compiler.apply(s).map(compiled -> new CompilableString<>(s, (T)compiled)), CompilableString::source);
    }
 
    public String source() {
        return this.source;
    }
 
    public T compiled() {
        return this.compiled;
    }
 
    @Override
    public boolean equals(Object o) {
        return o instanceof CompilableString<?> that && Objects.equals(this.source, that.source);
    }
 
    @Override
    public int hashCode() {
        return this.source.hashCode();
    }
 
    @Override
    public String toString() {
        return this.source;
    }
 
    public abstract static class CommandParserHelper<T> implements Function<String, DataResult<T>> {
        private static final DynamicCommandExceptionType TRAILING_DATA = new DynamicCommandExceptionType(
            commandAndRemainder -> Component.translatableEscape("command.trailing_data", commandAndRemainder)
        );
 
        public final DataResult<T> apply(String contents) {
            StringReader reader = new StringReader(contents);
 
            try {
                T result = this.parse(reader);
                if (reader.canRead()) {
                    String parsed = reader.getString().substring(0, reader.getCursor());
                    String leftovers = reader.getString().substring(reader.getCursor());
                    throw TRAILING_DATA.create(parsed + "[" + leftovers + "]");
                } else {
                    return DataResult.success(result);
                }
            } catch (CommandSyntaxException var6) {
                return DataResult.error(() -> this.errorMessage(contents, var6));
            }
        }
 
        protected abstract T parse(StringReader reader) throws CommandSyntaxException;
 
        protected abstract String errorMessage(String original, CommandSyntaxException exception);
    }
}

引用的其他类

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