IncomingRpcMethod.java

net.minecraft.server.jsonrpc.IncomingRpcMethod

信息

  • 全限定名:net.minecraft.server.jsonrpc.IncomingRpcMethod
  • 类型:public interface
  • 包:net.minecraft.server.jsonrpc
  • 源码路径:src/main/java/net/minecraft/server/jsonrpc/IncomingRpcMethod.java
  • 起始行号:L20
  • 职责:

    TODO

字段/常量

内部类/嵌套类型

  • net.minecraft.server.jsonrpc.IncomingRpcMethod.Attributes

    • 类型: record
    • 修饰符: public
    • 源码定位: L39
    • 说明:

      TODO

  • net.minecraft.server.jsonrpc.IncomingRpcMethod.IncomingRpcMethodBuilder

    • 类型: class
    • 修饰符: public static
    • 源码定位: L42
    • 说明:

      TODO

  • net.minecraft.server.jsonrpc.IncomingRpcMethod.Method

    • 类型: record
    • 修饰符: public
    • 源码定位: L117
    • 说明:

      TODO

  • net.minecraft.server.jsonrpc.IncomingRpcMethod.ParameterlessMethod

    • 类型: record
    • 修饰符: public
    • 源码定位: L166
    • 说明:

      TODO

  • net.minecraft.server.jsonrpc.IncomingRpcMethod.ParameterlessRpcMethodFunction

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

      TODO

  • net.minecraft.server.jsonrpc.IncomingRpcMethod.RpcMethodFunction

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

      TODO

构造器

方法

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

MethodInfo<Params,Result> info() @ L21

  • 方法名:info
  • 源码定位:L21
  • 返回类型:MethodInfo<Params,Result>
  • 修饰符:package-private

参数:

说明:

TODO

IncomingRpcMethod.Attributes attributes() @ L23

  • 方法名:attributes
  • 源码定位:L23
  • 返回类型:IncomingRpcMethod.Attributes
  • 修饰符:package-private

参数:

说明:

TODO

JsonElement apply(MinecraftApi minecraftApi, JsonElement paramsJson, ClientInfo clientInfo) @ L25

  • 方法名:apply
  • 源码定位:L25
  • 返回类型:JsonElement
  • 修饰符:package-private

参数:

  • minecraftApi: MinecraftApi
  • paramsJson: JsonElement
  • clientInfo: ClientInfo

说明:

TODO

static <Result> IncomingRpcMethod.IncomingRpcMethodBuilder<Void,Result> method(IncomingRpcMethod.ParameterlessRpcMethodFunction<Result> function) @ L27

  • 方法名:method
  • 源码定位:L27
  • 返回类型: IncomingRpcMethod.IncomingRpcMethodBuilder<Void,Result>
  • 修饰符:static

参数:

  • function: IncomingRpcMethod.ParameterlessRpcMethodFunction

说明:

TODO

static <Params,Result> IncomingRpcMethod.IncomingRpcMethodBuilder<Params,Result> method(IncomingRpcMethod.RpcMethodFunction<Params,Result> function) @ L31

  • 方法名:method
  • 源码定位:L31
  • 返回类型:<Params,Result> IncomingRpcMethod.IncomingRpcMethodBuilder<Params,Result>
  • 修饰符:static

参数:

  • function: IncomingRpcMethod.RpcMethodFunction<Params,Result>

说明:

TODO

static <Result> IncomingRpcMethod.IncomingRpcMethodBuilder<Void,Result> method(Function<MinecraftApi,Result> supplier) @ L35

  • 方法名:method
  • 源码定位:L35
  • 返回类型: IncomingRpcMethod.IncomingRpcMethodBuilder<Void,Result>
  • 修饰符:static

参数:

  • supplier: Function<MinecraftApi,Result>

说明:

TODO

代码

public interface IncomingRpcMethod<Params, Result> {
    MethodInfo<Params, Result> info();
 
    IncomingRpcMethod.Attributes attributes();
 
    JsonElement apply(MinecraftApi minecraftApi, @Nullable JsonElement paramsJson, ClientInfo clientInfo);
 
    static <Result> IncomingRpcMethod.IncomingRpcMethodBuilder<Void, Result> method(IncomingRpcMethod.ParameterlessRpcMethodFunction<Result> function) {
        return new IncomingRpcMethod.IncomingRpcMethodBuilder<>(function);
    }
 
    static <Params, Result> IncomingRpcMethod.IncomingRpcMethodBuilder<Params, Result> method(IncomingRpcMethod.RpcMethodFunction<Params, Result> function) {
        return new IncomingRpcMethod.IncomingRpcMethodBuilder<>(function);
    }
 
    static <Result> IncomingRpcMethod.IncomingRpcMethodBuilder<Void, Result> method(Function<MinecraftApi, Result> supplier) {
        return new IncomingRpcMethod.IncomingRpcMethodBuilder<>(supplier);
    }
 
    public record Attributes(boolean runOnMainThread, boolean discoverable) {
    }
 
    public static class IncomingRpcMethodBuilder<Params, Result> {
        private String description = "";
        private @Nullable ParamInfo<Params> paramInfo;
        private @Nullable ResultInfo<Result> resultInfo;
        private boolean discoverable = true;
        private boolean runOnMainThread = true;
        private IncomingRpcMethod.@Nullable ParameterlessRpcMethodFunction<Result> parameterlessFunction;
        private IncomingRpcMethod.@Nullable RpcMethodFunction<Params, Result> parameterFunction;
 
        public IncomingRpcMethodBuilder(IncomingRpcMethod.ParameterlessRpcMethodFunction<Result> function) {
            this.parameterlessFunction = function;
        }
 
        public IncomingRpcMethodBuilder(IncomingRpcMethod.RpcMethodFunction<Params, Result> function) {
            this.parameterFunction = function;
        }
 
        public IncomingRpcMethodBuilder(Function<MinecraftApi, Result> supplier) {
            this.parameterlessFunction = (apiService, clientInfo) -> supplier.apply(apiService);
        }
 
        public IncomingRpcMethod.IncomingRpcMethodBuilder<Params, Result> description(String description) {
            this.description = description;
            return this;
        }
 
        public IncomingRpcMethod.IncomingRpcMethodBuilder<Params, Result> response(String resultName, Schema<Result> resultSchema) {
            this.resultInfo = new ResultInfo<>(resultName, resultSchema.info());
            return this;
        }
 
        public IncomingRpcMethod.IncomingRpcMethodBuilder<Params, Result> param(String paramName, Schema<Params> paramSchema) {
            this.paramInfo = new ParamInfo<>(paramName, paramSchema.info());
            return this;
        }
 
        public IncomingRpcMethod.IncomingRpcMethodBuilder<Params, Result> undiscoverable() {
            this.discoverable = false;
            return this;
        }
 
        public IncomingRpcMethod.IncomingRpcMethodBuilder<Params, Result> notOnMainThread() {
            this.runOnMainThread = false;
            return this;
        }
 
        public IncomingRpcMethod<Params, Result> build() {
            if (this.resultInfo == null) {
                throw new IllegalStateException("No response defined");
            } else {
                IncomingRpcMethod.Attributes attributes = new IncomingRpcMethod.Attributes(this.runOnMainThread, this.discoverable);
                MethodInfo<Params, Result> methodInfo = new MethodInfo<>(this.description, this.paramInfo, this.resultInfo);
                if (this.parameterlessFunction != null) {
                    return new IncomingRpcMethod.ParameterlessMethod<>(methodInfo, attributes, this.parameterlessFunction);
                } else if (this.parameterFunction != null) {
                    if (this.paramInfo == null) {
                        throw new IllegalStateException("No param schema defined");
                    } else {
                        return new IncomingRpcMethod.Method<>(methodInfo, attributes, this.parameterFunction);
                    }
                } else {
                    throw new IllegalStateException("No method defined");
                }
            }
        }
 
        public IncomingRpcMethod<?, ?> register(Registry<IncomingRpcMethod<?, ?>> methodRegistry, String key) {
            return this.register(methodRegistry, Identifier.withDefaultNamespace(key));
        }
 
        private IncomingRpcMethod<?, ?> register(Registry<IncomingRpcMethod<?, ?>> methodRegistry, Identifier id) {
            return Registry.register(methodRegistry, id, this.build());
        }
    }
 
    public record Method<Params, Result>(
        MethodInfo<Params, Result> info, IncomingRpcMethod.Attributes attributes, IncomingRpcMethod.RpcMethodFunction<Params, Result> function
    ) implements IncomingRpcMethod<Params, Result> {
        @Override
        public JsonElement apply(MinecraftApi minecraftApi, @Nullable JsonElement paramsJson, ClientInfo clientInfo) {
            if (paramsJson != null && (paramsJson.isJsonArray() || paramsJson.isJsonObject())) {
                if (this.info.params().isEmpty()) {
                    throw new IllegalArgumentException("Method defined as having parameters without describing them");
                } else {
                    JsonElement paramsJsonElement;
                    if (paramsJson.isJsonObject()) {
                        String parameterName = this.info.params().get().name();
                        JsonElement jsonElement = paramsJson.getAsJsonObject().get(parameterName);
                        if (jsonElement == null) {
                            throw new InvalidParameterJsonRpcException(
                                String.format(Locale.ROOT, "Params passed by-name, but expected param [%s] does not exist", parameterName)
                            );
                        }
 
                        paramsJsonElement = jsonElement;
                    } else {
                        JsonArray jsonArray = paramsJson.getAsJsonArray();
                        if (jsonArray.isEmpty() || jsonArray.size() > 1) {
                            throw new InvalidParameterJsonRpcException("Expected exactly one element in the params array");
                        }
 
                        paramsJsonElement = jsonArray.get(0);
                    }
 
                    Params params = this.info
                        .params()
                        .get()
                        .schema()
                        .codec()
                        .parse(JsonOps.INSTANCE, paramsJsonElement)
                        .getOrThrow(InvalidParameterJsonRpcException::new);
                    Result result = this.function.apply(minecraftApi, params, clientInfo);
                    if (this.info.result().isEmpty()) {
                        throw new IllegalStateException("No result codec defined");
                    } else {
                        return this.info.result().get().schema().codec().encodeStart(JsonOps.INSTANCE, result).getOrThrow(EncodeJsonRpcException::new);
                    }
                }
            } else {
                throw new InvalidParameterJsonRpcException("Expected params as array or named");
            }
        }
    }
 
    public record ParameterlessMethod<Params, Result>(
        MethodInfo<Params, Result> info, IncomingRpcMethod.Attributes attributes, IncomingRpcMethod.ParameterlessRpcMethodFunction<Result> supplier
    ) implements IncomingRpcMethod<Params, Result> {
        @Override
        public JsonElement apply(MinecraftApi minecraftApi, @Nullable JsonElement paramsJson, ClientInfo clientInfo) {
            if (paramsJson == null || paramsJson.isJsonArray() && paramsJson.getAsJsonArray().isEmpty()) {
                if (this.info.params().isPresent()) {
                    throw new IllegalArgumentException("Parameterless method unexpectedly has parameter description");
                } else {
                    Result result = this.supplier.apply(minecraftApi, clientInfo);
                    if (this.info.result().isEmpty()) {
                        throw new IllegalStateException("No result codec defined");
                    } else {
                        return this.info
                            .result()
                            .get()
                            .schema()
                            .codec()
                            .encodeStart(JsonOps.INSTANCE, result)
                            .getOrThrow(InvalidParameterJsonRpcException::new);
                    }
                }
            } else {
                throw new InvalidParameterJsonRpcException("Expected no params, or an empty array");
            }
        }
    }
 
    @FunctionalInterface
    public interface ParameterlessRpcMethodFunction<Result> {
        Result apply(MinecraftApi api, ClientInfo clientInfo);
    }
 
    @FunctionalInterface
    public interface RpcMethodFunction<Params, Result> {
        Result apply(MinecraftApi api, Params params, ClientInfo clientInfo);
    }
}

引用的其他类