| 1 | package net.minecraft.server.jsonrpc;
|
| 2 |
|
| 3 | import com.google.gson.JsonArray;
|
| 4 | import com.google.gson.JsonElement;
|
| 5 | import com.google.gson.JsonObject;
|
| 6 | import java.util.List;
|
| 7 | import net.minecraft.resources.Identifier;
|
| 8 | import net.minecraft.util.GsonHelper;
|
| 9 | import org.jspecify.annotations.Nullable;
|
| 10 |
|
| 11 | public class JsonRPCUtils {
|
| 12 | public static final String JSON_RPC_VERSION = "2.0";
|
| 13 | public static final String OPEN_RPC_VERSION = "1.3.2";
|
| 14 |
|
| 15 | public static JsonObject createSuccessResult(final JsonElement id, final JsonElement result) {
|
| 16 | JsonObject response = new JsonObject();
|
| 17 | response.addProperty("jsonrpc", "2.0");
|
| 18 | response.add("id", id);
|
| 19 | response.add("result", result);
|
| 20 | return response;
|
| 21 | }
|
| 22 |
|
| 23 | public static JsonObject createRequest(final @Nullable Integer id, final Identifier method, final List<JsonElement> params) {
|
| 24 | return createRequest(id, method.toString(), params);
|
| 25 | }
|
| 26 |
|
| 27 | public static JsonObject createRequest(final @Nullable Integer id, final String method, final List<JsonElement> params) {
|
| 28 | JsonObject request = new JsonObject();
|
| 29 | request.addProperty("jsonrpc", "2.0");
|
| 30 | if (id != null) {
|
| 31 | request.addProperty("id", id);
|
| 32 | }
|
| 33 |
|
| 34 | request.addProperty("method", method);
|
| 35 | if (!params.isEmpty()) {
|
| 36 | JsonArray jsonArray = new JsonArray(params.size());
|
| 37 |
|
| 38 | for (JsonElement param : params) {
|
| 39 | jsonArray.add(param);
|
| 40 | }
|
| 41 |
|
| 42 | request.add("params", jsonArray);
|
| 43 | }
|
| 44 |
|
| 45 | return request;
|
| 46 | }
|
| 47 |
|
| 48 | public static JsonObject createError(final JsonElement id, final String message, final int errorCode, final @Nullable String data) {
|
| 49 | JsonObject errorResponse = new JsonObject();
|
| 50 | errorResponse.addProperty("jsonrpc", "2.0");
|
| 51 | errorResponse.add("id", id);
|
| 52 | JsonObject error = new JsonObject();
|
| 53 | error.addProperty("code", errorCode);
|
| 54 | error.addProperty("message", message);
|
| 55 | if (data != null && !data.isBlank()) {
|
| 56 | error.addProperty("data", data);
|
| 57 | }
|
| 58 |
|
| 59 | errorResponse.add("error", error);
|
| 60 | return errorResponse;
|
| 61 | }
|
| 62 |
|
| 63 | public static @Nullable JsonElement getRequestId(final JsonObject jsonObject) {
|
| 64 | return jsonObject.get("id");
|
| 65 | }
|
| 66 |
|
| 67 | public static @Nullable String getMethodName(final JsonObject jsonObject) {
|
| 68 | return GsonHelper.getAsString(jsonObject, "method", null);
|
| 69 | }
|
| 70 |
|
| 71 | public static @Nullable JsonElement getParams(final JsonObject jsonObject) {
|
| 72 | return jsonObject.get("params");
|
| 73 | }
|
| 74 |
|
| 75 | public static @Nullable JsonElement getResult(final JsonObject jsonObject) {
|
| 76 | return jsonObject.get("result");
|
| 77 | }
|
| 78 |
|
| 79 | public static @Nullable JsonObject getError(final JsonObject jsonObject) {
|
| 80 | return GsonHelper.getAsJsonObject(jsonObject, "error", null);
|
| 81 | }
|
| 82 | }
|
| 83 |
|