| 1 | package net.minecraft.server.packs.metadata.pack;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.DataResult;
|
| 7 | import com.mojang.serialization.MapCodec;
|
| 8 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 9 | import java.util.ArrayList;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Locale;
|
| 12 | import java.util.Optional;
|
| 13 | import java.util.function.BiFunction;
|
| 14 | import net.minecraft.server.packs.PackType;
|
| 15 | import net.minecraft.util.ExtraCodecs;
|
| 16 | import net.minecraft.util.InclusiveRange;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 | import org.slf4j.Logger;
|
| 19 |
|
| 20 | public record PackFormat(int major, int minor) implements Comparable<PackFormat> {
|
| 21 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 22 | public static final Codec<PackFormat> BOTTOM_CODEC = fullCodec(0);
|
| 23 | public static final Codec<PackFormat> TOP_CODEC = fullCodec(Integer.MAX_VALUE);
|
| 24 |
|
| 25 | private static Codec<PackFormat> fullCodec(final int defaultMinor) {
|
| 26 | return ExtraCodecs.compactListCodec(ExtraCodecs.NON_NEGATIVE_INT, ExtraCodecs.NON_NEGATIVE_INT.listOf(1, 256))
|
| 27 | .xmap(
|
| 28 | list -> list.size() > 1 ? of(list.getFirst(), list.get(1)) : of(list.getFirst(), defaultMinor),
|
| 29 | pf -> pf.minor != defaultMinor ? List.of(pf.major(), pf.minor()) : List.of(pf.major())
|
| 30 | );
|
| 31 | }
|
| 32 |
|
| 33 | public static <ResultType, HolderType extends PackFormat.IntermediaryFormatHolder> DataResult<List<ResultType>> validateHolderList(
|
| 34 | final List<HolderType> list, final int lastPreMinorVersion, final BiFunction<HolderType, InclusiveRange<PackFormat>, ResultType> constructor
|
| 35 | ) {
|
| 36 | int minVersion = list.stream()
|
| 37 | .map(PackFormat.IntermediaryFormatHolder::format)
|
| 38 | .mapToInt(PackFormat.IntermediaryFormat::effectiveMinMajorVersion)
|
| 39 | .min()
|
| 40 | .orElse(Integer.MAX_VALUE);
|
| 41 | List<ResultType> result = new ArrayList<>(list.size());
|
| 42 |
|
| 43 | for (HolderType entry : list) {
|
| 44 | PackFormat.IntermediaryFormat format = entry.format();
|
| 45 | if (format.min().isEmpty() && format.max().isEmpty() && format.supported().isEmpty()) {
|
| 46 | LOGGER.warn("Unknown or broken overlay entry {}", entry);
|
| 47 | } else {
|
| 48 | DataResult<InclusiveRange<PackFormat>> entryResult = format.validate(
|
| 49 | lastPreMinorVersion, false, minVersion <= lastPreMinorVersion, "Overlay \"" + entry + "\"", "formats"
|
| 50 | );
|
| 51 | if (!entryResult.isSuccess()) {
|
| 52 | return DataResult.error(entryResult.error().get()::message);
|
| 53 | }
|
| 54 |
|
| 55 | result.add(constructor.apply(entry, entryResult.getOrThrow()));
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | return DataResult.success(List.copyOf(result));
|
| 60 | }
|
| 61 |
|
| 62 | @VisibleForTesting
|
| 63 | public static int lastPreMinorVersion(final PackType type) {
|
| 64 | return switch (type) {
|
| 65 | case CLIENT_RESOURCES -> 64;
|
| 66 | case SERVER_DATA -> 81;
|
| 67 | };
|
| 68 | }
|
| 69 |
|
| 70 | public static MapCodec<InclusiveRange<PackFormat>> packCodec(final PackType type) {
|
| 71 | int lastPreMinorVersion = lastPreMinorVersion(type);
|
| 72 | return PackFormat.IntermediaryFormat.PACK_CODEC
|
| 73 | .flatXmap(
|
| 74 | intermediaryFormat -> intermediaryFormat.validate(lastPreMinorVersion, true, false, "Pack", "supported_formats"),
|
| 75 | range -> DataResult.success(PackFormat.IntermediaryFormat.fromRange((InclusiveRange<PackFormat>)range, lastPreMinorVersion))
|
| 76 | );
|
| 77 | }
|
| 78 |
|
| 79 | public static PackFormat of(final int major, final int minor) {
|
| 80 | return new PackFormat(major, minor);
|
| 81 | }
|
| 82 |
|
| 83 | public static PackFormat of(final int major) {
|
| 84 | return new PackFormat(major, 0);
|
| 85 | }
|
| 86 |
|
| 87 | public InclusiveRange<PackFormat> minorRange() {
|
| 88 | return new InclusiveRange<>(this, of(this.major, Integer.MAX_VALUE));
|
| 89 | }
|
| 90 |
|
| 91 | public int compareTo(final PackFormat other) {
|
| 92 | int majorDiff = Integer.compare(this.major(), other.major());
|
| 93 | return majorDiff != 0 ? majorDiff : Integer.compare(this.minor(), other.minor());
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public String toString() {
|
| 98 | return this.minor == Integer.MAX_VALUE ? String.format(Locale.ROOT, "%d.*", this.major()) : String.format(Locale.ROOT, "%d.%d", this.major(), this.minor());
|
| 99 | }
|
| 100 |
|
| 101 | public record IntermediaryFormat(Optional<PackFormat> min, Optional<PackFormat> max, Optional<Integer> format, Optional<InclusiveRange<Integer>> supported) {
|
| 102 | private static final MapCodec<PackFormat.IntermediaryFormat> PACK_CODEC = RecordCodecBuilder.mapCodec(
|
| 103 | i -> i.group(
|
| 104 | PackFormat.BOTTOM_CODEC.optionalFieldOf("min_format").forGetter(PackFormat.IntermediaryFormat::min),
|
| 105 | PackFormat.TOP_CODEC.optionalFieldOf("max_format").forGetter(PackFormat.IntermediaryFormat::max),
|
| 106 | Codec.INT.optionalFieldOf("pack_format").forGetter(PackFormat.IntermediaryFormat::format),
|
| 107 | InclusiveRange.codec(Codec.INT).optionalFieldOf("supported_formats").forGetter(PackFormat.IntermediaryFormat::supported)
|
| 108 | )
|
| 109 | .apply(i, PackFormat.IntermediaryFormat::new)
|
| 110 | );
|
| 111 | public static final MapCodec<PackFormat.IntermediaryFormat> OVERLAY_CODEC = RecordCodecBuilder.mapCodec(
|
| 112 | i -> i.group(
|
| 113 | PackFormat.BOTTOM_CODEC.optionalFieldOf("min_format").forGetter(PackFormat.IntermediaryFormat::min),
|
| 114 | PackFormat.TOP_CODEC.optionalFieldOf("max_format").forGetter(PackFormat.IntermediaryFormat::max),
|
| 115 | InclusiveRange.codec(Codec.INT).optionalFieldOf("formats").forGetter(PackFormat.IntermediaryFormat::supported)
|
| 116 | )
|
| 117 | .apply(i, (min, max, formats) -> new PackFormat.IntermediaryFormat(min, max, min.map(PackFormat::major), formats))
|
| 118 | );
|
| 119 |
|
| 120 | public static PackFormat.IntermediaryFormat fromRange(final InclusiveRange<PackFormat> range, final int lastPreMinorVersion) {
|
| 121 | InclusiveRange<Integer> majorRange = range.map(PackFormat::major);
|
| 122 | return new PackFormat.IntermediaryFormat(
|
| 123 | Optional.of(range.minInclusive()),
|
| 124 | Optional.of(range.maxInclusive()),
|
| 125 | majorRange.isValueInRange(lastPreMinorVersion) ? Optional.of(majorRange.minInclusive()) : Optional.empty(),
|
| 126 | majorRange.isValueInRange(lastPreMinorVersion) ? Optional.of(new InclusiveRange<>(majorRange.minInclusive(), majorRange.maxInclusive())) : Optional.empty()
|
| 127 | );
|
| 128 | }
|
| 129 |
|
| 130 | public int effectiveMinMajorVersion() {
|
| 131 | if (this.min.isPresent()) {
|
| 132 | return this.supported.isPresent() ? Math.min(this.min.get().major(), this.supported.get().minInclusive()) : this.min.get().major();
|
| 133 | } else {
|
| 134 | return this.supported.isPresent() ? this.supported.get().minInclusive() : Integer.MAX_VALUE;
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | public DataResult<InclusiveRange<PackFormat>> validate(
|
| 139 | final int lastPreMinorVersion, final boolean hasPackFormatField, final boolean requireOldField, final String context, final String oldFieldName
|
| 140 | ) {
|
| 141 | if (this.min.isPresent() != this.max.isPresent()) {
|
| 142 | return DataResult.error(() -> context + " missing field, must declare both min_format and max_format");
|
| 143 | } else if (requireOldField && this.supported.isEmpty()) {
|
| 144 | return DataResult.error(
|
| 145 | () -> context + " missing required field " + oldFieldName + ", must be present in all overlays for any overlays to work across game versions"
|
| 146 | );
|
| 147 | } else if (this.min.isPresent()) {
|
| 148 | return this.validateNewFormat(lastPreMinorVersion, hasPackFormatField, requireOldField, context, oldFieldName);
|
| 149 | } else if (this.supported.isPresent()) {
|
| 150 | return this.validateOldFormat(lastPreMinorVersion, hasPackFormatField, context, oldFieldName);
|
| 151 | } else if (hasPackFormatField && this.format.isPresent()) {
|
| 152 | int mainFormat = this.format.get();
|
| 153 | return mainFormat > lastPreMinorVersion
|
| 154 | ? DataResult.error(
|
| 155 | () -> context + " declares support for version newer than " + lastPreMinorVersion + ", but is missing mandatory fields min_format and max_format"
|
| 156 | )
|
| 157 | : DataResult.success(new InclusiveRange<>(PackFormat.of(mainFormat)));
|
| 158 | } else {
|
| 159 | return DataResult.error(() -> context + " could not be parsed, missing format version information");
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | private DataResult<InclusiveRange<PackFormat>> validateNewFormat(
|
| 164 | final int lastPreMinorVersion, final boolean hasPackFormatField, final boolean requireOldField, final String context, final String oldFieldName
|
| 165 | ) {
|
| 166 | int majorMin = this.min.get().major();
|
| 167 | int majorMax = this.max.get().major();
|
| 168 | if (this.min.get().compareTo(this.max.get()) > 0) {
|
| 169 | return DataResult.error(() -> context + " min_format (" + this.min.get() + ") is greater than max_format (" + this.max.get() + ")");
|
| 170 | }
|
| 171 |
|
| 172 | if (majorMin > lastPreMinorVersion && !requireOldField) {
|
| 173 | if (this.supported.isPresent()) {
|
| 174 | return DataResult.error(
|
| 175 | () -> context
|
| 176 | + " key "
|
| 177 | + oldFieldName
|
| 178 | + " is deprecated starting from pack format "
|
| 179 | + (lastPreMinorVersion + 1)
|
| 180 | + ". Remove "
|
| 181 | + oldFieldName
|
| 182 | + " from your pack.mcmeta."
|
| 183 | );
|
| 184 | }
|
| 185 |
|
| 186 | if (hasPackFormatField && this.format.isPresent()) {
|
| 187 | String packFormatError = this.validatePackFormatForRange(majorMin, majorMax);
|
| 188 | if (packFormatError != null) {
|
| 189 | return DataResult.error(() -> packFormatError);
|
| 190 | }
|
| 191 | }
|
| 192 | } else {
|
| 193 | if (!this.supported.isPresent()) {
|
| 194 | return DataResult.error(
|
| 195 | () -> context
|
| 196 | + " declares support for format "
|
| 197 | + majorMin
|
| 198 | + ", but game versions supporting formats 15 to "
|
| 199 | + lastPreMinorVersion
|
| 200 | + " require a "
|
| 201 | + oldFieldName
|
| 202 | + " field. Add \""
|
| 203 | + oldFieldName
|
| 204 | + "\": ["
|
| 205 | + majorMin
|
| 206 | + ", "
|
| 207 | + lastPreMinorVersion
|
| 208 | + "] or require a version greater or equal to "
|
| 209 | + (lastPreMinorVersion + 1)
|
| 210 | + ".0."
|
| 211 | );
|
| 212 | }
|
| 213 |
|
| 214 | InclusiveRange<Integer> oldSupportedVersions = this.supported.get();
|
| 215 | if (oldSupportedVersions.minInclusive() != majorMin) {
|
| 216 | return DataResult.error(
|
| 217 | () -> context
|
| 218 | + " version declaration mismatch between "
|
| 219 | + oldFieldName
|
| 220 | + " (from "
|
| 221 | + oldSupportedVersions.minInclusive()
|
| 222 | + ") and min_format ("
|
| 223 | + this.min.get()
|
| 224 | + ")"
|
| 225 | );
|
| 226 | }
|
| 227 |
|
| 228 | if (oldSupportedVersions.maxInclusive() != majorMax && oldSupportedVersions.maxInclusive() != lastPreMinorVersion) {
|
| 229 | return DataResult.error(
|
| 230 | () -> context
|
| 231 | + " version declaration mismatch between "
|
| 232 | + oldFieldName
|
| 233 | + " (up to "
|
| 234 | + oldSupportedVersions.maxInclusive()
|
| 235 | + ") and max_format ("
|
| 236 | + this.max.get()
|
| 237 | + ")"
|
| 238 | );
|
| 239 | }
|
| 240 |
|
| 241 | if (hasPackFormatField) {
|
| 242 | if (!this.format.isPresent()) {
|
| 243 | return DataResult.error(
|
| 244 | () -> context
|
| 245 | + " declares support for formats up to "
|
| 246 | + lastPreMinorVersion
|
| 247 | + ", but game versions supporting formats 15 to "
|
| 248 | + lastPreMinorVersion
|
| 249 | + " require a pack_format field. Add \"pack_format\": "
|
| 250 | + majorMin
|
| 251 | + " or require a version greater or equal to "
|
| 252 | + (lastPreMinorVersion + 1)
|
| 253 | + ".0."
|
| 254 | );
|
| 255 | }
|
| 256 |
|
| 257 | String packFormatError = this.validatePackFormatForRange(majorMin, majorMax);
|
| 258 | if (packFormatError != null) {
|
| 259 | return DataResult.error(() -> packFormatError);
|
| 260 | }
|
| 261 | }
|
| 262 | }
|
| 263 |
|
| 264 | return DataResult.success(new InclusiveRange<>(this.min.get(), this.max.get()));
|
| 265 | }
|
| 266 |
|
| 267 | private DataResult<InclusiveRange<PackFormat>> validateOldFormat(
|
| 268 | final int lastPreMinorVersion, final boolean hasPackFormatField, final String context, final String oldFieldName
|
| 269 | ) {
|
| 270 | InclusiveRange<Integer> oldSupportedVersions = this.supported.get();
|
| 271 | int min = oldSupportedVersions.minInclusive();
|
| 272 | int max = oldSupportedVersions.maxInclusive();
|
| 273 | if (max > lastPreMinorVersion) {
|
| 274 | return DataResult.error(
|
| 275 | () -> context + " declares support for version newer than " + lastPreMinorVersion + ", but is missing mandatory fields min_format and max_format"
|
| 276 | );
|
| 277 | }
|
| 278 |
|
| 279 | if (hasPackFormatField) {
|
| 280 | if (!this.format.isPresent()) {
|
| 281 | return DataResult.error(
|
| 282 | () -> context
|
| 283 | + " declares support for formats up to "
|
| 284 | + lastPreMinorVersion
|
| 285 | + ", but game versions supporting formats 15 to "
|
| 286 | + lastPreMinorVersion
|
| 287 | + " require a pack_format field. Add \"pack_format\": "
|
| 288 | + min
|
| 289 | + " or require a version greater or equal to "
|
| 290 | + (lastPreMinorVersion + 1)
|
| 291 | + ".0."
|
| 292 | );
|
| 293 | }
|
| 294 |
|
| 295 | String packFormatError = this.validatePackFormatForRange(min, max);
|
| 296 | if (packFormatError != null) {
|
| 297 | return DataResult.error(() -> packFormatError);
|
| 298 | }
|
| 299 | }
|
| 300 |
|
| 301 | return DataResult.success(new InclusiveRange<>(min, max).map(PackFormat::of));
|
| 302 | }
|
| 303 |
|
| 304 | private @Nullable String validatePackFormatForRange(final int min, final int max) {
|
| 305 | int mainFormat = this.format.get();
|
| 306 | if (mainFormat < min || mainFormat > max) {
|
| 307 | return "Pack declared support for versions " + min + " to " + max + " but declared main format is " + mainFormat;
|
| 308 | } else {
|
| 309 | return mainFormat < 15
|
| 310 | ? "Multi-version packs cannot support minimum version of less than 15, since this will leave versions in range unable to load pack."
|
| 311 | : null;
|
| 312 | }
|
| 313 | }
|
| 314 | }
|
| 315 |
|
| 316 | public interface IntermediaryFormatHolder {
|
| 317 | PackFormat.IntermediaryFormat format();
|
| 318 | }
|
| 319 | }
|
| 320 |
|