/* * Decompiled with CFR 0.152. * * Could not load the following classes: * com.google.gson.JsonArray * com.google.gson.JsonElement * com.mojang.serialization.DynamicOps * com.mojang.serialization.JsonOps * org.jspecify.annotations.Nullable */ package net.minecraft.server.jsonrpc; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.mojang.serialization.DynamicOps; import com.mojang.serialization.JsonOps; import java.util.Locale; import java.util.function.Function; import net.minecraft.core.Registry; import net.minecraft.resources.Identifier; import net.minecraft.server.jsonrpc.api.MethodInfo; import net.minecraft.server.jsonrpc.api.ParamInfo; import net.minecraft.server.jsonrpc.api.ResultInfo; import net.minecraft.server.jsonrpc.api.Schema; import net.minecraft.server.jsonrpc.internalapi.MinecraftApi; import net.minecraft.server.jsonrpc.methods.ClientInfo; import net.minecraft.server.jsonrpc.methods.EncodeJsonRpcException; import net.minecraft.server.jsonrpc.methods.InvalidParameterJsonRpcException; import org.jspecify.annotations.Nullable; public interface IncomingRpcMethod { public MethodInfo info(); public Attributes attributes(); public JsonElement apply(MinecraftApi var1, @Nullable JsonElement var2, ClientInfo var3); public static IncomingRpcMethodBuilder method(ParameterlessRpcMethodFunction function) { return new IncomingRpcMethodBuilder(function); } public static IncomingRpcMethodBuilder method(RpcMethodFunction function) { return new IncomingRpcMethodBuilder(function); } public static IncomingRpcMethodBuilder method(Function supplier) { return new IncomingRpcMethodBuilder(supplier); } public static class IncomingRpcMethodBuilder { private String description = ""; private @Nullable ParamInfo paramInfo; private @Nullable ResultInfo resultInfo; private boolean discoverable = true; private boolean runOnMainThread = true; private @Nullable ParameterlessRpcMethodFunction parameterlessFunction; private @Nullable RpcMethodFunction parameterFunction; public IncomingRpcMethodBuilder(ParameterlessRpcMethodFunction function) { this.parameterlessFunction = function; } public IncomingRpcMethodBuilder(RpcMethodFunction function) { this.parameterFunction = function; } public IncomingRpcMethodBuilder(Function supplier) { this.parameterlessFunction = (apiService, clientInfo) -> supplier.apply(apiService); } public IncomingRpcMethodBuilder description(String description) { this.description = description; return this; } public IncomingRpcMethodBuilder response(String resultName, Schema resultSchema) { this.resultInfo = new ResultInfo(resultName, resultSchema.info()); return this; } public IncomingRpcMethodBuilder param(String paramName, Schema paramSchema) { this.paramInfo = new ParamInfo(paramName, paramSchema.info()); return this; } public IncomingRpcMethodBuilder undiscoverable() { this.discoverable = false; return this; } public IncomingRpcMethodBuilder notOnMainThread() { this.runOnMainThread = false; return this; } public IncomingRpcMethod build() { if (this.resultInfo == null) { throw new IllegalStateException("No response defined"); } Attributes attributes = new Attributes(this.runOnMainThread, this.discoverable); MethodInfo methodInfo = new MethodInfo(this.description, this.paramInfo, this.resultInfo); if (this.parameterlessFunction != null) { return new ParameterlessMethod(methodInfo, attributes, this.parameterlessFunction); } if (this.parameterFunction != null) { if (this.paramInfo == null) { throw new IllegalStateException("No param schema defined"); } return new Method(methodInfo, attributes, this.parameterFunction); } throw new IllegalStateException("No method defined"); } public IncomingRpcMethod register(Registry> methodRegistry, String key) { return this.register(methodRegistry, Identifier.withDefaultNamespace(key)); } private IncomingRpcMethod register(Registry> methodRegistry, Identifier id) { return Registry.register(methodRegistry, id, this.build()); } } @FunctionalInterface public static interface ParameterlessRpcMethodFunction { public Result apply(MinecraftApi var1, ClientInfo var2); } @FunctionalInterface public static interface RpcMethodFunction { public Result apply(MinecraftApi var1, Params var2, ClientInfo var3); } public record Method(MethodInfo info, Attributes attributes, RpcMethodFunction function) implements IncomingRpcMethod { @Override public JsonElement apply(MinecraftApi minecraftApi, @Nullable JsonElement paramsJson, ClientInfo clientInfo) { JsonElement paramsJsonElement; if (paramsJson == null || !paramsJson.isJsonArray() && !paramsJson.isJsonObject()) { throw new InvalidParameterJsonRpcException("Expected params as array or named"); } if (this.info.params().isEmpty()) { throw new IllegalArgumentException("Method defined as having parameters without describing them"); } 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); } Object params = this.info.params().get().schema().codec().parse((DynamicOps)JsonOps.INSTANCE, (Object)paramsJsonElement).getOrThrow(InvalidParameterJsonRpcException::new); Result result = this.function.apply(minecraftApi, params, clientInfo); if (this.info.result().isEmpty()) { throw new IllegalStateException("No result codec defined"); } return (JsonElement)this.info.result().get().schema().codec().encodeStart((DynamicOps)JsonOps.INSTANCE, result).getOrThrow(EncodeJsonRpcException::new); } } public record ParameterlessMethod(MethodInfo info, Attributes attributes, ParameterlessRpcMethodFunction supplier) implements IncomingRpcMethod { @Override public JsonElement apply(MinecraftApi minecraftApi, @Nullable JsonElement paramsJson, ClientInfo clientInfo) { if (!(paramsJson == null || paramsJson.isJsonArray() && paramsJson.getAsJsonArray().isEmpty())) { throw new InvalidParameterJsonRpcException("Expected no params, or an empty array"); } if (this.info.params().isPresent()) { throw new IllegalArgumentException("Parameterless method unexpectedly has parameter description"); } Result result = this.supplier.apply(minecraftApi, clientInfo); if (this.info.result().isEmpty()) { throw new IllegalStateException("No result codec defined"); } return (JsonElement)this.info.result().get().schema().codec().encodeStart((DynamicOps)JsonOps.INSTANCE, result).getOrThrow(InvalidParameterJsonRpcException::new); } } public record Attributes(boolean runOnMainThread, boolean discoverable) { } }