| 1 | package net.minecraft.server.jsonrpc.methods;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.ArrayList;
|
| 7 | import java.util.HashMap;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Map;
|
| 10 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 11 | import net.minecraft.server.jsonrpc.api.MethodInfo;
|
| 12 | import net.minecraft.server.jsonrpc.api.Schema;
|
| 13 | import net.minecraft.server.jsonrpc.api.SchemaComponent;
|
| 14 |
|
| 15 | public class DiscoveryService {
|
| 16 | public static DiscoveryService.DiscoverResponse discover(final List<SchemaComponent<?>> schemaRegistry) {
|
| 17 | List<MethodInfo.Named<?, ?>> methods = new ArrayList<>(BuiltInRegistries.INCOMING_RPC_METHOD.size() + BuiltInRegistries.OUTGOING_RPC_METHOD.size());
|
| 18 | BuiltInRegistries.INCOMING_RPC_METHOD.listElements().forEach(e -> {
|
| 19 | if (e.value().attributes().discoverable()) {
|
| 20 | methods.add(e.value().info().named(e.key().identifier()));
|
| 21 | }
|
| 22 | });
|
| 23 | BuiltInRegistries.OUTGOING_RPC_METHOD.listElements().forEach(e -> {
|
| 24 | if (e.value().attributes().discoverable()) {
|
| 25 | methods.add(e.value().info().named(e.key().identifier()));
|
| 26 | }
|
| 27 | });
|
| 28 | Map<String, Schema<?>> schemas = new HashMap<>();
|
| 29 |
|
| 30 | for (SchemaComponent<?> component : schemaRegistry) {
|
| 31 | schemas.put(component.name(), component.schema().info());
|
| 32 | }
|
| 33 |
|
| 34 | DiscoveryService.DiscoverInfo discoverInfo = new DiscoveryService.DiscoverInfo("Minecraft Server JSON-RPC", "3.1.0");
|
| 35 | return new DiscoveryService.DiscoverResponse("1.3.2", discoverInfo, methods, new DiscoveryService.DiscoverComponents(schemas));
|
| 36 | }
|
| 37 |
|
| 38 | public record DiscoverComponents(Map<String, Schema<?>> schemas) {
|
| 39 | public static final MapCodec<DiscoveryService.DiscoverComponents> CODEC = typedSchema();
|
| 40 |
|
| 41 | private static MapCodec<DiscoveryService.DiscoverComponents> typedSchema() {
|
| 42 | return RecordCodecBuilder.mapCodec(
|
| 43 | i -> i.group(Codec.unboundedMap(Codec.STRING, Schema.CODEC).fieldOf("schemas").forGetter(DiscoveryService.DiscoverComponents::schemas))
|
| 44 | .apply(i, DiscoveryService.DiscoverComponents::new)
|
| 45 | );
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | public record DiscoverInfo(String title, String version) {
|
| 50 | public static final MapCodec<DiscoveryService.DiscoverInfo> CODEC = RecordCodecBuilder.mapCodec(
|
| 51 | i -> i.group(
|
| 52 | Codec.STRING.fieldOf("title").forGetter(DiscoveryService.DiscoverInfo::title),
|
| 53 | Codec.STRING.fieldOf("version").forGetter(DiscoveryService.DiscoverInfo::version)
|
| 54 | )
|
| 55 | .apply(i, DiscoveryService.DiscoverInfo::new)
|
| 56 | );
|
| 57 | }
|
| 58 |
|
| 59 | public record DiscoverResponse(
|
| 60 | String jsonRpcProtocolVersion,
|
| 61 | DiscoveryService.DiscoverInfo discoverInfo,
|
| 62 | List<MethodInfo.Named<?, ?>> methods,
|
| 63 | DiscoveryService.DiscoverComponents components
|
| 64 | ) {
|
| 65 | public static final MapCodec<DiscoveryService.DiscoverResponse> CODEC = RecordCodecBuilder.mapCodec(
|
| 66 | i -> i.group(
|
| 67 | Codec.STRING.fieldOf("openrpc").forGetter(DiscoveryService.DiscoverResponse::jsonRpcProtocolVersion),
|
| 68 | DiscoveryService.DiscoverInfo.CODEC.codec().fieldOf("info").forGetter(DiscoveryService.DiscoverResponse::discoverInfo),
|
| 69 | Codec.list(MethodInfo.Named.CODEC).fieldOf("methods").forGetter(DiscoveryService.DiscoverResponse::methods),
|
| 70 | DiscoveryService.DiscoverComponents.CODEC.codec().fieldOf("components").forGetter(DiscoveryService.DiscoverResponse::components)
|
| 71 | )
|
| 72 | .apply(i, DiscoveryService.DiscoverResponse::new)
|
| 73 | );
|
| 74 | }
|
| 75 | }
|
| 76 |
|