| 1 | package net.minecraft.server.jsonrpc;
|
| 2 |
|
| 3 | import com.google.gson.JsonArray;
|
| 4 | import com.google.gson.JsonElement;
|
| 5 | import com.mojang.serialization.JsonOps;
|
| 6 | import java.util.Locale;
|
| 7 | import java.util.function.Function;
|
| 8 | import net.minecraft.core.Registry;
|
| 9 | import net.minecraft.resources.Identifier;
|
| 10 | import net.minecraft.server.jsonrpc.api.MethodInfo;
|
| 11 | import net.minecraft.server.jsonrpc.api.ParamInfo;
|
| 12 | import net.minecraft.server.jsonrpc.api.ResultInfo;
|
| 13 | import net.minecraft.server.jsonrpc.api.Schema;
|
| 14 | import net.minecraft.server.jsonrpc.internalapi.MinecraftApi;
|
| 15 | import net.minecraft.server.jsonrpc.methods.ClientInfo;
|
| 16 | import net.minecraft.server.jsonrpc.methods.EncodeJsonRpcException;
|
| 17 | import net.minecraft.server.jsonrpc.methods.InvalidParameterJsonRpcException;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 |
|
| 20 | public interface IncomingRpcMethod<Params, Result> {
|
| 21 | MethodInfo<Params, Result> info();
|
| 22 |
|
| 23 | IncomingRpcMethod.Attributes attributes();
|
| 24 |
|
| 25 | JsonElement apply(MinecraftApi minecraftApi, @Nullable JsonElement paramsJson, ClientInfo clientInfo);
|
| 26 |
|
| 27 | static <Result> IncomingRpcMethod.IncomingRpcMethodBuilder<Void, Result> method(final IncomingRpcMethod.ParameterlessRpcMethodFunction<Result> function) {
|
| 28 | return new IncomingRpcMethod.IncomingRpcMethodBuilder<>(function);
|
| 29 | }
|
| 30 |
|
| 31 | static <Params, Result> IncomingRpcMethod.IncomingRpcMethodBuilder<Params, Result> method(final IncomingRpcMethod.RpcMethodFunction<Params, Result> function) {
|
| 32 | return new IncomingRpcMethod.IncomingRpcMethodBuilder<>(function);
|
| 33 | }
|
| 34 |
|
| 35 | static <Result> IncomingRpcMethod.IncomingRpcMethodBuilder<Void, Result> method(final Function<MinecraftApi, Result> supplier) {
|
| 36 | return new IncomingRpcMethod.IncomingRpcMethodBuilder<>(supplier);
|
| 37 | }
|
| 38 |
|
| 39 | record Attributes(boolean runOnMainThread, boolean discoverable, boolean allowPreServerInit) {
|
| 40 | }
|
| 41 |
|
| 42 | class IncomingRpcMethodBuilder<Params, Result> {
|
| 43 | private String description = "";
|
| 44 | private @Nullable ParamInfo<Params> paramInfo;
|
| 45 | private @Nullable ResultInfo<Result> resultInfo;
|
| 46 | private boolean discoverable = true;
|
| 47 | private boolean runOnMainThread = true;
|
| 48 | private IncomingRpcMethod.@Nullable ParameterlessRpcMethodFunction<Result> parameterlessFunction;
|
| 49 | private IncomingRpcMethod.@Nullable RpcMethodFunction<Params, Result> parameterFunction;
|
| 50 | private boolean allowPreServerInit = false;
|
| 51 |
|
| 52 | public IncomingRpcMethodBuilder(final IncomingRpcMethod.ParameterlessRpcMethodFunction<Result> function) {
|
| 53 | this.parameterlessFunction = function;
|
| 54 | }
|
| 55 |
|
| 56 | public IncomingRpcMethodBuilder(final IncomingRpcMethod.RpcMethodFunction<Params, Result> function) {
|
| 57 | this.parameterFunction = function;
|
| 58 | }
|
| 59 |
|
| 60 | public IncomingRpcMethodBuilder(final Function<MinecraftApi, Result> supplier) {
|
| 61 | this.parameterlessFunction = (apiService, var2) -> supplier.apply(apiService);
|
| 62 | }
|
| 63 |
|
| 64 | public IncomingRpcMethod.IncomingRpcMethodBuilder<Params, Result> description(final String description) {
|
| 65 | this.description = description;
|
| 66 | return this;
|
| 67 | }
|
| 68 |
|
| 69 | public IncomingRpcMethod.IncomingRpcMethodBuilder<Params, Result> response(final String resultName, final Schema<Result> resultSchema) {
|
| 70 | this.resultInfo = new ResultInfo<>(resultName, resultSchema.info());
|
| 71 | return this;
|
| 72 | }
|
| 73 |
|
| 74 | public IncomingRpcMethod.IncomingRpcMethodBuilder<Params, Result> param(final String paramName, final Schema<Params> paramSchema) {
|
| 75 | this.paramInfo = new ParamInfo<>(paramName, paramSchema.info());
|
| 76 | return this;
|
| 77 | }
|
| 78 |
|
| 79 | public IncomingRpcMethod.IncomingRpcMethodBuilder<Params, Result> undiscoverable() {
|
| 80 | this.discoverable = false;
|
| 81 | return this;
|
| 82 | }
|
| 83 |
|
| 84 | public IncomingRpcMethod.IncomingRpcMethodBuilder<Params, Result> notOnMainThread() {
|
| 85 | this.runOnMainThread = false;
|
| 86 | return this;
|
| 87 | }
|
| 88 |
|
| 89 | public IncomingRpcMethod.IncomingRpcMethodBuilder<Params, Result> allowPreServerInit() {
|
| 90 | this.allowPreServerInit = true;
|
| 91 | return this;
|
| 92 | }
|
| 93 |
|
| 94 | public IncomingRpcMethod<Params, Result> build() {
|
| 95 | if (this.resultInfo == null) {
|
| 96 | throw new IllegalStateException("No response defined");
|
| 97 | }
|
| 98 |
|
| 99 | IncomingRpcMethod.Attributes attributes = new IncomingRpcMethod.Attributes(this.runOnMainThread, this.discoverable, this.allowPreServerInit);
|
| 100 | MethodInfo<Params, Result> methodInfo = new MethodInfo<>(this.description, this.paramInfo, this.resultInfo);
|
| 101 | if (this.parameterlessFunction != null) {
|
| 102 | return new IncomingRpcMethod.ParameterlessMethod<>(methodInfo, attributes, this.parameterlessFunction);
|
| 103 | }
|
| 104 |
|
| 105 | if (this.parameterFunction != null) {
|
| 106 | if (this.paramInfo == null) {
|
| 107 | throw new IllegalStateException("No param schema defined");
|
| 108 | } else {
|
| 109 | return new IncomingRpcMethod.Method<>(methodInfo, attributes, this.parameterFunction);
|
| 110 | }
|
| 111 | } else {
|
| 112 | throw new IllegalStateException("No method defined");
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | public IncomingRpcMethod<?, ?> register(final Registry<IncomingRpcMethod<?, ?>> methodRegistry, final String key) {
|
| 117 | return this.register(methodRegistry, Identifier.withDefaultNamespace(key));
|
| 118 | }
|
| 119 |
|
| 120 | private IncomingRpcMethod<?, ?> register(final Registry<IncomingRpcMethod<?, ?>> methodRegistry, final Identifier id) {
|
| 121 | return Registry.register(methodRegistry, id, this.build());
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | record Method<Params, Result>(
|
| 126 | MethodInfo<Params, Result> info, IncomingRpcMethod.Attributes attributes, IncomingRpcMethod.RpcMethodFunction<Params, Result> function
|
| 127 | ) implements IncomingRpcMethod<Params, Result> {
|
| 128 | @Override
|
| 129 | public JsonElement apply(final MinecraftApi minecraftApi, final @Nullable JsonElement paramsJson, final ClientInfo clientInfo) {
|
| 130 | if (paramsJson != null && (paramsJson.isJsonArray() || paramsJson.isJsonObject())) {
|
| 131 | if (this.info.params().isEmpty()) {
|
| 132 | throw new IllegalArgumentException("Method defined as having parameters without describing them");
|
| 133 | }
|
| 134 |
|
| 135 | JsonElement paramsJsonElement;
|
| 136 | if (paramsJson.isJsonObject()) {
|
| 137 | String parameterName = this.info.params().get().name();
|
| 138 | JsonElement jsonElement = paramsJson.getAsJsonObject().get(parameterName);
|
| 139 | if (jsonElement == null) {
|
| 140 | throw new InvalidParameterJsonRpcException(String.format(Locale.ROOT, "Params passed by-name, but expected param [%s] does not exist", parameterName));
|
| 141 | }
|
| 142 |
|
| 143 | paramsJsonElement = jsonElement;
|
| 144 | } else {
|
| 145 | JsonArray jsonArray = paramsJson.getAsJsonArray();
|
| 146 | if (jsonArray.isEmpty() || jsonArray.size() > 1) {
|
| 147 | throw new InvalidParameterJsonRpcException("Expected exactly one element in the params array");
|
| 148 | }
|
| 149 |
|
| 150 | paramsJsonElement = jsonArray.get(0);
|
| 151 | }
|
| 152 |
|
| 153 | Params params = this.info.params().get().schema().codec().parse(JsonOps.INSTANCE, paramsJsonElement).getOrThrow(InvalidParameterJsonRpcException::new);
|
| 154 | Result result = this.function.apply(minecraftApi, params, clientInfo);
|
| 155 | if (this.info.result().isEmpty()) {
|
| 156 | throw new IllegalStateException("No result codec defined");
|
| 157 | } else {
|
| 158 | return this.info.result().get().schema().codec().encodeStart(JsonOps.INSTANCE, result).getOrThrow(EncodeJsonRpcException::new);
|
| 159 | }
|
| 160 | } else {
|
| 161 | throw new InvalidParameterJsonRpcException("Expected params as array or named");
|
| 162 | }
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | record ParameterlessMethod<Params, Result>(
|
| 167 | MethodInfo<Params, Result> info, IncomingRpcMethod.Attributes attributes, IncomingRpcMethod.ParameterlessRpcMethodFunction<Result> supplier
|
| 168 | ) implements IncomingRpcMethod<Params, Result> {
|
| 169 | @Override
|
| 170 | public JsonElement apply(final MinecraftApi minecraftApi, final @Nullable JsonElement paramsJson, final ClientInfo clientInfo) {
|
| 171 | if (paramsJson == null || paramsJson.isJsonArray() && paramsJson.getAsJsonArray().isEmpty()) {
|
| 172 | if (this.info.params().isPresent()) {
|
| 173 | throw new IllegalArgumentException("Parameterless method unexpectedly has parameter description");
|
| 174 | } else {
|
| 175 | Result result = this.supplier.apply(minecraftApi, clientInfo);
|
| 176 | if (this.info.result().isEmpty()) {
|
| 177 | throw new IllegalStateException("No result codec defined");
|
| 178 | } else {
|
| 179 | return this.info.result().get().schema().codec().encodeStart(JsonOps.INSTANCE, result).getOrThrow(InvalidParameterJsonRpcException::new);
|
| 180 | }
|
| 181 | }
|
| 182 | } else {
|
| 183 | throw new InvalidParameterJsonRpcException("Expected no params, or an empty array");
|
| 184 | }
|
| 185 | }
|
| 186 | }
|
| 187 |
|
| 188 | @FunctionalInterface
|
| 189 | interface ParameterlessRpcMethodFunction<Result> {
|
| 190 | Result apply(MinecraftApi api, ClientInfo clientInfo);
|
| 191 | }
|
| 192 |
|
| 193 | @FunctionalInterface
|
| 194 | interface RpcMethodFunction<Params, Result> {
|
| 195 | Result apply(MinecraftApi api, Params params, ClientInfo clientInfo);
|
| 196 | }
|
| 197 | }
|
| 198 |
|