| 1 | package net.minecraft.server.jsonrpc.api;
|
| 2 |
|
| 3 | import com.mojang.datafixers.kinds.App;
|
| 4 | import com.mojang.datafixers.util.Either;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.DataResult;
|
| 7 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 8 | import com.mojang.serialization.codecs.RecordCodecBuilder.Mu;
|
| 9 | import java.net.URI;
|
| 10 | import java.util.ArrayList;
|
| 11 | import java.util.HashMap;
|
| 12 | import java.util.List;
|
| 13 | import java.util.Map;
|
| 14 | import java.util.Optional;
|
| 15 | import java.util.UUID;
|
| 16 | import java.util.Map.Entry;
|
| 17 | import java.util.function.Supplier;
|
| 18 | import java.util.stream.Collectors;
|
| 19 | import java.util.stream.Stream;
|
| 20 | import net.minecraft.core.UUIDUtil;
|
| 21 | import net.minecraft.server.jsonrpc.methods.BanlistService;
|
| 22 | import net.minecraft.server.jsonrpc.methods.DiscoveryService;
|
| 23 | import net.minecraft.server.jsonrpc.methods.GameRulesService;
|
| 24 | import net.minecraft.server.jsonrpc.methods.IpBanlistService;
|
| 25 | import net.minecraft.server.jsonrpc.methods.Message;
|
| 26 | import net.minecraft.server.jsonrpc.methods.OperatorService;
|
| 27 | import net.minecraft.server.jsonrpc.methods.PlayerService;
|
| 28 | import net.minecraft.server.jsonrpc.methods.ServerStateService;
|
| 29 | import net.minecraft.server.permissions.PermissionLevel;
|
| 30 | import net.minecraft.util.ExtraCodecs;
|
| 31 | import net.minecraft.util.StringRepresentable;
|
| 32 | import net.minecraft.world.Difficulty;
|
| 33 | import net.minecraft.world.level.GameType;
|
| 34 | import net.minecraft.world.level.gamerules.GameRuleType;
|
| 35 |
|
| 36 | public record Schema<T>(
|
| 37 | Optional<URI> reference, List<String> type, Optional<Schema<?>> items, Map<String, Schema<?>> properties, List<String> enumValues, Codec<T> codec
|
| 38 | ) {
|
| 39 | public static final Codec<? extends Schema<?>> CODEC = Codec.recursive(
|
| 40 | "Schema",
|
| 41 | subCodec -> RecordCodecBuilder.create(
|
| 42 | i -> i.group(
|
| 43 | (App<Mu<? extends Schema<?>>, Optional<URI>>)ReferenceUtil.REFERENCE_CODEC.optionalFieldOf("$ref").forGetter(Schema::reference),
|
| 44 | (App<Mu<? extends Schema<?>>, List<String>>)ExtraCodecs.compactListCodec(Codec.STRING).optionalFieldOf("type", List.of()).forGetter(Schema::type),
|
| 45 | (App<Mu<? extends Schema<?>>, Optional<? extends Schema<?>>>)subCodec.optionalFieldOf("items").forGetter(Schema::items),
|
| 46 | (App<Mu<? extends Schema<?>>, Map<String, ? extends Schema<?>>>)Codec.unboundedMap(Codec.STRING, subCodec)
|
| 47 | .optionalFieldOf("properties", Map.of())
|
| 48 | .forGetter(Schema::properties),
|
| 49 | (App<Mu<? extends Schema<?>>, List<String>>)Codec.STRING.listOf().optionalFieldOf("enum", List.of()).forGetter(Schema::enumValues)
|
| 50 | )
|
| 51 | .apply(i, (ref, type, items, properties, enumValues) -> null)
|
| 52 | )
|
| 53 | )
|
| 54 | .validate(schema -> schema == null ? DataResult.error(() -> "Should not deserialize schema") : DataResult.success(schema));
|
| 55 | private static final List<SchemaComponent<?>> SCHEMA_REGISTRY = new ArrayList<>();
|
| 56 | public static final Schema<Boolean> BOOL_SCHEMA = ofType("boolean", Codec.BOOL);
|
| 57 | public static final Schema<Integer> INT_SCHEMA = ofType("integer", Codec.INT);
|
| 58 | public static final Schema<Either<Boolean, Integer>> BOOL_OR_INT_SCHEMA = ofTypes(List.of("boolean", "integer"), Codec.either(Codec.BOOL, Codec.INT));
|
| 59 | public static final Schema<Float> NUMBER_SCHEMA = ofType("number", Codec.FLOAT);
|
| 60 | public static final Schema<String> STRING_SCHEMA = ofType("string", Codec.STRING);
|
| 61 | public static final Schema<UUID> UUID_SCHEMA = ofType("string", UUIDUtil.CODEC);
|
| 62 | public static final Schema<DiscoveryService.DiscoverResponse> DISCOVERY_SCHEMA = ofType("string", DiscoveryService.DiscoverResponse.CODEC.codec());
|
| 63 | public static final SchemaComponent<Difficulty> DIFFICULTY_SCHEMA = registerSchema("difficulty", ofEnum(Difficulty::values, Difficulty.CODEC));
|
| 64 | public static final SchemaComponent<GameType> GAME_TYPE_SCHEMA = registerSchema("game_type", ofEnum(GameType::values, GameType.CODEC));
|
| 65 | public static final Schema<PermissionLevel> PERMISSION_LEVEL_SCHEMA = ofType("integer", PermissionLevel.INT_CODEC);
|
| 66 | public static final SchemaComponent<PlayerDto> PLAYER_SCHEMA = registerSchema(
|
| 67 | "player", record(PlayerDto.CODEC.codec()).withField("id", UUID_SCHEMA).withField("name", STRING_SCHEMA)
|
| 68 | );
|
| 69 | public static final SchemaComponent<DiscoveryService.DiscoverInfo> VERSION_SCHEMA = registerSchema(
|
| 70 | "version", record(DiscoveryService.DiscoverInfo.CODEC.codec()).withField("name", STRING_SCHEMA).withField("protocol", INT_SCHEMA)
|
| 71 | );
|
| 72 | public static final SchemaComponent<ServerStateService.ServerState> SERVER_STATE_SCHEMA = registerSchema(
|
| 73 | "server_state",
|
| 74 | record(ServerStateService.ServerState.CODEC)
|
| 75 | .withField("started", BOOL_SCHEMA)
|
| 76 | .withField("players", PLAYER_SCHEMA.asRef().asArray())
|
| 77 | .withField("version", VERSION_SCHEMA.asRef())
|
| 78 | );
|
| 79 | public static final Schema<GameRuleType> RULE_TYPE_SCHEMA = ofEnum(GameRuleType::values);
|
| 80 | public static final SchemaComponent<GameRulesService.GameRuleUpdate<?>> TYPED_GAME_RULE_SCHEMA = registerSchema(
|
| 81 | "typed_game_rule",
|
| 82 | record(GameRulesService.GameRuleUpdate.TYPED_CODEC)
|
| 83 | .withField("key", STRING_SCHEMA)
|
| 84 | .withField("value", BOOL_OR_INT_SCHEMA)
|
| 85 | .withField("type", RULE_TYPE_SCHEMA)
|
| 86 | );
|
| 87 | public static final SchemaComponent<GameRulesService.GameRuleUpdate<?>> UNTYPED_GAME_RULE_SCHEMA = registerSchema(
|
| 88 | "untyped_game_rule", record(GameRulesService.GameRuleUpdate.CODEC).withField("key", STRING_SCHEMA).withField("value", BOOL_OR_INT_SCHEMA)
|
| 89 | );
|
| 90 | public static final SchemaComponent<Message> MESSAGE_SCHEMA = registerSchema(
|
| 91 | "message",
|
| 92 | record(Message.CODEC).withField("literal", STRING_SCHEMA).withField("translatable", STRING_SCHEMA).withField("translatableParams", STRING_SCHEMA.asArray())
|
| 93 | );
|
| 94 | public static final SchemaComponent<ServerStateService.SystemMessage> SYSTEM_MESSAGE_SCHEMA = registerSchema(
|
| 95 | "system_message",
|
| 96 | record(ServerStateService.SystemMessage.CODEC)
|
| 97 | .withField("message", MESSAGE_SCHEMA.asRef())
|
| 98 | .withField("overlay", BOOL_SCHEMA)
|
| 99 | .withField("receivingPlayers", PLAYER_SCHEMA.asRef().asArray())
|
| 100 | );
|
| 101 | public static final SchemaComponent<PlayerService.KickDto> KICK_PLAYER_SCHEMA = registerSchema(
|
| 102 | "kick_player", record(PlayerService.KickDto.CODEC.codec()).withField("message", MESSAGE_SCHEMA.asRef()).withField("player", PLAYER_SCHEMA.asRef())
|
| 103 | );
|
| 104 | public static final SchemaComponent<OperatorService.OperatorDto> OPERATOR_SCHEMA = registerSchema(
|
| 105 | "operator",
|
| 106 | record(OperatorService.OperatorDto.CODEC.codec())
|
| 107 | .withField("player", PLAYER_SCHEMA.asRef())
|
| 108 | .withField("bypassesPlayerLimit", BOOL_SCHEMA)
|
| 109 | .withField("permissionLevel", INT_SCHEMA)
|
| 110 | );
|
| 111 | public static final SchemaComponent<IpBanlistService.IncomingIpBanDto> INCOMING_IP_BAN_SCHEMA = registerSchema(
|
| 112 | "incoming_ip_ban",
|
| 113 | record(IpBanlistService.IncomingIpBanDto.CODEC.codec())
|
| 114 | .withField("player", PLAYER_SCHEMA.asRef())
|
| 115 | .withField("ip", STRING_SCHEMA)
|
| 116 | .withField("reason", STRING_SCHEMA)
|
| 117 | .withField("source", STRING_SCHEMA)
|
| 118 | .withField("expires", STRING_SCHEMA)
|
| 119 | );
|
| 120 | public static final SchemaComponent<IpBanlistService.IpBanDto> IP_BAN_SCHEMA = registerSchema(
|
| 121 | "ip_ban",
|
| 122 | record(IpBanlistService.IpBanDto.CODEC.codec())
|
| 123 | .withField("ip", STRING_SCHEMA)
|
| 124 | .withField("reason", STRING_SCHEMA)
|
| 125 | .withField("source", STRING_SCHEMA)
|
| 126 | .withField("expires", STRING_SCHEMA)
|
| 127 | );
|
| 128 | public static final SchemaComponent<BanlistService.UserBanDto> PLAYER_BAN_SCHEMA = registerSchema(
|
| 129 | "user_ban",
|
| 130 | record(BanlistService.UserBanDto.CODEC.codec())
|
| 131 | .withField("player", PLAYER_SCHEMA.asRef())
|
| 132 | .withField("reason", STRING_SCHEMA)
|
| 133 | .withField("source", STRING_SCHEMA)
|
| 134 | .withField("expires", STRING_SCHEMA)
|
| 135 | );
|
| 136 |
|
| 137 | public static <T> Codec<Schema<T>> typedCodec() {
|
| 138 | return (Codec<Schema<T>>)CODEC;
|
| 139 | }
|
| 140 |
|
| 141 | public Schema<T> info() {
|
| 142 | return new Schema<>(
|
| 143 | this.reference,
|
| 144 | this.type,
|
| 145 | this.items.map(Schema::info),
|
| 146 | this.properties.entrySet().stream().collect(Collectors.toMap(Entry::getKey, b -> b.getValue().info())),
|
| 147 | this.enumValues,
|
| 148 | this.codec
|
| 149 | );
|
| 150 | }
|
| 151 |
|
| 152 | private static <T> SchemaComponent<T> registerSchema(final String name, final Schema<T> schema) {
|
| 153 | SchemaComponent<T> entry = new SchemaComponent<>(name, ReferenceUtil.createLocalReference(name), schema);
|
| 154 | SCHEMA_REGISTRY.add(entry);
|
| 155 | return entry;
|
| 156 | }
|
| 157 |
|
| 158 | public static List<SchemaComponent<?>> getSchemaRegistry() {
|
| 159 | return SCHEMA_REGISTRY;
|
| 160 | }
|
| 161 |
|
| 162 | public static <T> Schema<T> ofRef(final URI ref, final Codec<T> codec) {
|
| 163 | return new Schema<>(Optional.of(ref), List.of(), Optional.empty(), Map.of(), List.of(), codec);
|
| 164 | }
|
| 165 |
|
| 166 | public static <T> Schema<T> ofType(final String type, final Codec<T> codec) {
|
| 167 | return ofTypes(List.of(type), codec);
|
| 168 | }
|
| 169 |
|
| 170 | public static <T> Schema<T> ofTypes(final List<String> types, final Codec<T> codec) {
|
| 171 | return new Schema<>(Optional.empty(), types, Optional.empty(), Map.of(), List.of(), codec);
|
| 172 | }
|
| 173 |
|
| 174 | public static <E extends Enum<E> & StringRepresentable> Schema<E> ofEnum(final Supplier<E[]> values) {
|
| 175 | return ofEnum(values, StringRepresentable.fromEnum(values));
|
| 176 | }
|
| 177 |
|
| 178 | public static <E extends Enum<E> & StringRepresentable> Schema<E> ofEnum(final Supplier<E[]> values, final Codec<E> codec) {
|
| 179 | List<String> enumValues = Stream.<Enum>of((Enum[])values.get()).map(rec$ -> ((StringRepresentable)rec$).getSerializedName()).toList();
|
| 180 | return ofEnum(enumValues, codec);
|
| 181 | }
|
| 182 |
|
| 183 | public static <T> Schema<T> ofEnum(final List<String> enumValues, final Codec<T> codec) {
|
| 184 | return new Schema<>(Optional.empty(), List.of("string"), Optional.empty(), Map.of(), enumValues, codec);
|
| 185 | }
|
| 186 |
|
| 187 | public static <T> Schema<List<T>> arrayOf(final Schema<?> item, final Codec<T> codec) {
|
| 188 | return new Schema<>(Optional.empty(), List.of("array"), Optional.of(item), Map.of(), List.of(), codec.listOf());
|
| 189 | }
|
| 190 |
|
| 191 | public static <T> Schema<T> record(final Codec<T> codec) {
|
| 192 | return new Schema<>(Optional.empty(), List.of("object"), Optional.empty(), Map.of(), List.of(), codec);
|
| 193 | }
|
| 194 |
|
| 195 | private static <T> Schema<T> record(final Map<String, Schema<?>> properties, final Codec<T> codec) {
|
| 196 | return new Schema<>(Optional.empty(), List.of("object"), Optional.empty(), properties, List.of(), codec);
|
| 197 | }
|
| 198 |
|
| 199 | public Schema<T> withField(final String name, final Schema<?> field) {
|
| 200 | HashMap<String, Schema<?>> properties = new HashMap<>(this.properties);
|
| 201 | properties.put(name, field);
|
| 202 | return record(properties, this.codec);
|
| 203 | }
|
| 204 |
|
| 205 | public Schema<List<T>> asArray() {
|
| 206 | return arrayOf(this, this.codec);
|
| 207 | }
|
| 208 | }
|
| 209 |
|