OutgoingRpcMethod .java

net.minecraft.server.jsonrpc.OutgoingRpcMethod (interface): members: OutgoingRpcMethodBuilder, ParmeterlessNotification, notificationWithParams, Method.

net.minecraft.server.jsonrpcinterface152 linesserver5.8 KB

API Surface (7)

Result decodeResult(final JsonElement result)
OutgoingRpcMethod.OutgoingRpcMethodBuilder<Params, Result> description(final String description)
OutgoingRpcMethod.OutgoingRpcMethodBuilder<Params, Result> response(final String resultName, final Schema<Result> resultSchema)
OutgoingRpcMethod.OutgoingRpcMethodBuilder<Params, Result> param(final String paramName, final Schema<Params> paramSchema)
OutgoingRpcMethod.OutgoingRpcMethodBuilder<Params, Result> allowPreServerInit()
Holder.Reference<OutgoingRpcMethod<Params, Result>> register(final String key)
Result decodeResult(final JsonElement result)

Fields & Constants (1)

NOTIFICATION_PREFIX

String literals (3)

"notification/"
"Method defined as having no parameters"
"Method defined as having no result"

Full Source

OutgoingRpcMethod.java152 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.jsonrpc;
2
3
import com.google.gson.JsonElement;
4
import com.mojang.serialization.JsonOps;
5
import net.minecraft.core.Holder;
6
import net.minecraft.core.Registry;
7
import net.minecraft.core.registries.BuiltInRegistries;
8
import net.minecraft.resources.Identifier;
9
import net.minecraft.server.jsonrpc.api.MethodInfo;
10
import net.minecraft.server.jsonrpc.api.ParamInfo;
11
import net.minecraft.server.jsonrpc.api.ResultInfo;
12
import net.minecraft.server.jsonrpc.api.Schema;
13
import org.jspecify.annotations.Nullable;
14
15
public interface OutgoingRpcMethod<Params, Result> {
16
	String NOTIFICATION_PREFIX = "notification/";
17
18
	MethodInfo<Params, Result> info();
19
20
	OutgoingRpcMethod.Attributes attributes();
21
22
	default @Nullable JsonElement encodeParams(final Params params) {
23
		return null;
24
	}
25
26
	default @Nullable Result decodeResult(final JsonElement result) {
27
		return null;
28
	}
29
30
	static OutgoingRpcMethod.OutgoingRpcMethodBuilder<Void, Void> notification() {
31
		return new OutgoingRpcMethod.OutgoingRpcMethodBuilder<>(OutgoingRpcMethod.ParmeterlessNotification::new);
32
	}
33
34
	static <Params> OutgoingRpcMethod.OutgoingRpcMethodBuilder<Params, Void> notificationWithParams() {
35
		return new OutgoingRpcMethod.OutgoingRpcMethodBuilder<>(OutgoingRpcMethod.Notification::new);
36
	}
37
38
	static <Result> OutgoingRpcMethod.OutgoingRpcMethodBuilder<Void, Result> request() {
39
		return new OutgoingRpcMethod.OutgoingRpcMethodBuilder<>(OutgoingRpcMethod.ParameterlessMethod::new);
40
	}
41
42
	static <Params, Result> OutgoingRpcMethod.OutgoingRpcMethodBuilder<Params, Result> requestWithParams() {
43
		return new OutgoingRpcMethod.OutgoingRpcMethodBuilder<>(OutgoingRpcMethod.Method::new);
44
	}
45
46
	record Attributes(boolean discoverable, boolean allowPreServerInit) {
47
	}
48
49
	@FunctionalInterface
50
	interface Factory<Params, Result> {
51
		OutgoingRpcMethod<Params, Result> create(MethodInfo<Params, Result> info, OutgoingRpcMethod.Attributes attributes);
52
	}
53
54
	record Method<Params, Result>(MethodInfo<Params, Result> info, OutgoingRpcMethod.Attributes attributes) implements OutgoingRpcMethod<Params, Result> {
55
		@Override
56
		public @Nullable JsonElement encodeParams(final Params params) {
57
			if (this.info.params().isEmpty()) {
58
				throw new IllegalStateException("Method defined as having no parameters");
59
			} else {
60
				return this.info.params().get().schema().codec().encodeStart(JsonOps.INSTANCE, params).getOrThrow();
61
			}
62
		}
63
64
		@Override
65
		public Result decodeResult(final JsonElement result) {
66
			if (this.info.result().isEmpty()) {
67
				throw new IllegalStateException("Method defined as having no result");
68
			} else {
69
				return this.info.result().get().schema().codec().parse(JsonOps.INSTANCE, result).getOrThrow();
70
			}
71
		}
72
	}
73
74
	record Notification<Params>(MethodInfo<Params, Void> info, OutgoingRpcMethod.Attributes attributes) implements OutgoingRpcMethod<Params, Void> {
75
		@Override
76
		public @Nullable JsonElement encodeParams(final Params params) {
77
			if (this.info.params().isEmpty()) {
78
				throw new IllegalStateException("Method defined as having no parameters");
79
			} else {
80
				return this.info.params().get().schema().codec().encodeStart(JsonOps.INSTANCE, params).getOrThrow();
81
			}
82
		}
83
	}
84
85
	class OutgoingRpcMethodBuilder<Params, Result> {
86
		public static final OutgoingRpcMethod.Attributes DEFAULT_ATTRIBUTES = new OutgoingRpcMethod.Attributes(true, false);
87
		private final OutgoingRpcMethod.Factory<Params, Result> method;
88
		private String description = "";
89
		private @Nullable ParamInfo<Params> paramInfo;
90
		private @Nullable ResultInfo<Result> resultInfo;
91
		private boolean allowPreServerInit = false;
92
93
		public OutgoingRpcMethodBuilder(final OutgoingRpcMethod.Factory<Params, Result> method) {
94
			this.method = method;
95
		}
96
97
		public OutgoingRpcMethod.OutgoingRpcMethodBuilder<Params, Result> description(final String description) {
98
			this.description = description;
99
			return this;
100
		}
101
102
		public OutgoingRpcMethod.OutgoingRpcMethodBuilder<Params, Result> response(final String resultName, final Schema<Result> resultSchema) {
103
			this.resultInfo = new ResultInfo<>(resultName, resultSchema);
104
			return this;
105
		}
106
107
		public OutgoingRpcMethod.OutgoingRpcMethodBuilder<Params, Result> param(final String paramName, final Schema<Params> paramSchema) {
108
			this.paramInfo = new ParamInfo<>(paramName, paramSchema);
109
			return this;
110
		}
111
112
		public OutgoingRpcMethod.OutgoingRpcMethodBuilder<Params, Result> allowPreServerInit() {
113
			this.allowPreServerInit = true;
114
			return this;
115
		}
116
117
		private OutgoingRpcMethod<Params, Result> build() {
118
			MethodInfo<Params, Result> methodInfo = new MethodInfo<>(this.description, this.paramInfo, this.resultInfo);
119
			OutgoingRpcMethod.Attributes attributes;
120
			if (this.allowPreServerInit) {
121
				attributes = new OutgoingRpcMethod.Attributes(DEFAULT_ATTRIBUTES.discoverable(), true);
122
			} else {
123
				attributes = DEFAULT_ATTRIBUTES;
124
			}
125
126
			return this.method.create(methodInfo, attributes);
127
		}
128
129
		public Holder.Reference<OutgoingRpcMethod<Params, Result>> register(final String key) {
130
			return this.register(Identifier.withDefaultNamespace("notification/" + key));
131
		}
132
133
		private Holder.Reference<OutgoingRpcMethod<Params, Result>> register(final Identifier id) {
134
			return Registry.registerForHolder(BuiltInRegistries.OUTGOING_RPC_METHOD, id, this.build());
135
		}
136
	}
137
138
	record ParameterlessMethod<Result>(MethodInfo<Void, Result> info, OutgoingRpcMethod.Attributes attributes) implements OutgoingRpcMethod<Void, Result> {
139
		@Override
140
		public Result decodeResult(final JsonElement result) {
141
			if (this.info.result().isEmpty()) {
142
				throw new IllegalStateException("Method defined as having no result");
143
			} else {
144
				return this.info.result().get().schema().codec().parse(JsonOps.INSTANCE, result).getOrThrow();
145
			}
146
		}
147
	}
148
149
	record ParmeterlessNotification(MethodInfo<Void, Void> info, OutgoingRpcMethod.Attributes attributes) implements OutgoingRpcMethod<Void, Void> {
150
	}
151
}
152

Decompiled Minecraft 26.3 source of OutgoingRpcMethod (net/minecraft/server/jsonrpc/OutgoingRpcMethod.java) with Mojang official mappings, stored verbatim on the site.