| 1 | package net.minecraft.data.info;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.JsonOps;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.nio.file.Path;
|
| 8 | import java.util.HashMap;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.concurrent.CompletableFuture;
|
| 11 | import net.minecraft.core.Registry;
|
| 12 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 13 | import net.minecraft.data.CachedOutput;
|
| 14 | import net.minecraft.data.DataProvider;
|
| 15 | import net.minecraft.data.PackOutput;
|
| 16 | import net.minecraft.resources.Identifier;
|
| 17 | import net.minecraft.resources.RegistryDataLoader;
|
| 18 | import net.minecraft.resources.ResourceKey;
|
| 19 | import net.minecraft.util.StringRepresentable;
|
| 20 |
|
| 21 | public class DatapackStructureReport implements DataProvider {
|
| 22 | private final PackOutput output;
|
| 23 | private static final DatapackStructureReport.Entry STABLE_DYNAMIC_REGISTRY = new DatapackStructureReport.Entry(true, true, true);
|
| 24 | private static final DatapackStructureReport.Entry UNSTABLE_DYNAMIC_REGISTRY = new DatapackStructureReport.Entry(true, true, false);
|
| 25 | private static final DatapackStructureReport.Entry BUILT_IN_REGISTRY = new DatapackStructureReport.Entry(false, true, true);
|
| 26 | private static final Map<String, DatapackStructureReport.CustomPackEntry> NON_REGISTRY_ENTRIES = Map.of(
|
| 27 | "structure",
|
| 28 | new DatapackStructureReport.CustomPackEntry(DatapackStructureReport.Format.STRUCTURE, new DatapackStructureReport.Entry(true, false, true)),
|
| 29 | "function",
|
| 30 | new DatapackStructureReport.CustomPackEntry(DatapackStructureReport.Format.MCFUNCTION, new DatapackStructureReport.Entry(true, true, true))
|
| 31 | );
|
| 32 | private static final Codec<ResourceKey<? extends Registry<?>>> REGISTRY_KEY_CODEC = Identifier.CODEC
|
| 33 | .xmap(ResourceKey::createRegistryKey, ResourceKey::identifier);
|
| 34 |
|
| 35 | public DatapackStructureReport(final PackOutput output) {
|
| 36 | this.output = output;
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public CompletableFuture<?> run(final CachedOutput cache) {
|
| 41 | DatapackStructureReport.Report report = new DatapackStructureReport.Report(this.listRegistries(), NON_REGISTRY_ENTRIES);
|
| 42 | Path path = this.output.getOutputFolder(PackOutput.Target.REPORTS).resolve("datapack.json");
|
| 43 | return DataProvider.saveStable(cache, DatapackStructureReport.Report.CODEC.encodeStart(JsonOps.INSTANCE, report).getOrThrow(), path);
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public String getName() {
|
| 48 | return "Datapack Structure";
|
| 49 | }
|
| 50 |
|
| 51 | private void putIfNotPresent(
|
| 52 | final Map<ResourceKey<? extends Registry<?>>, DatapackStructureReport.Entry> output,
|
| 53 | final ResourceKey<? extends Registry<?>> key,
|
| 54 | final DatapackStructureReport.Entry entry
|
| 55 | ) {
|
| 56 | DatapackStructureReport.Entry previous = output.putIfAbsent(key, entry);
|
| 57 | if (previous != null) {
|
| 58 | throw new IllegalStateException("Duplicate entry for key " + key.identifier());
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | private Map<ResourceKey<? extends Registry<?>>, DatapackStructureReport.Entry> listRegistries() {
|
| 63 | Map<ResourceKey<? extends Registry<?>>, DatapackStructureReport.Entry> result = new HashMap<>();
|
| 64 | BuiltInRegistries.REGISTRY.forEach(entry -> this.putIfNotPresent(result, entry.key(), BUILT_IN_REGISTRY));
|
| 65 | RegistryDataLoader.WORLD_REGISTRIES.forEach(entry -> this.putIfNotPresent(result, entry.key(), UNSTABLE_DYNAMIC_REGISTRY));
|
| 66 | RegistryDataLoader.DIMENSION_REGISTRIES.forEach(entry -> this.putIfNotPresent(result, entry.key(), UNSTABLE_DYNAMIC_REGISTRY));
|
| 67 | RegistryDataLoader.RELOADABLE_REGISTRIES.forEach(entry -> this.putIfNotPresent(result, entry.key(), STABLE_DYNAMIC_REGISTRY));
|
| 68 | return result;
|
| 69 | }
|
| 70 |
|
| 71 | private record CustomPackEntry(DatapackStructureReport.Format format, DatapackStructureReport.Entry entry) {
|
| 72 | public static final Codec<DatapackStructureReport.CustomPackEntry> CODEC = RecordCodecBuilder.create(
|
| 73 | i -> i.group(
|
| 74 | DatapackStructureReport.Format.CODEC.fieldOf("format").forGetter(DatapackStructureReport.CustomPackEntry::format),
|
| 75 | DatapackStructureReport.Entry.MAP_CODEC.forGetter(DatapackStructureReport.CustomPackEntry::entry)
|
| 76 | )
|
| 77 | .apply(i, DatapackStructureReport.CustomPackEntry::new)
|
| 78 | );
|
| 79 | }
|
| 80 |
|
| 81 | private record Entry(boolean elements, boolean tags, boolean stable) {
|
| 82 | public static final MapCodec<DatapackStructureReport.Entry> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 83 | i -> i.group(
|
| 84 | Codec.BOOL.fieldOf("elements").forGetter(DatapackStructureReport.Entry::elements),
|
| 85 | Codec.BOOL.fieldOf("tags").forGetter(DatapackStructureReport.Entry::tags),
|
| 86 | Codec.BOOL.fieldOf("stable").forGetter(DatapackStructureReport.Entry::stable)
|
| 87 | )
|
| 88 | .apply(i, DatapackStructureReport.Entry::new)
|
| 89 | );
|
| 90 | public static final Codec<DatapackStructureReport.Entry> CODEC = MAP_CODEC.codec();
|
| 91 | }
|
| 92 |
|
| 93 | private enum Format implements StringRepresentable {
|
| 94 | STRUCTURE("structure"),
|
| 95 | MCFUNCTION("mcfunction");
|
| 96 |
|
| 97 | public static final Codec<DatapackStructureReport.Format> CODEC = StringRepresentable.fromEnum(DatapackStructureReport.Format::values);
|
| 98 | private final String name;
|
| 99 |
|
| 100 | Format(final String name) {
|
| 101 | this.name = name;
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public String getSerializedName() {
|
| 106 | return this.name;
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | private record Report(
|
| 111 | Map<ResourceKey<? extends Registry<?>>, DatapackStructureReport.Entry> registries, Map<String, DatapackStructureReport.CustomPackEntry> others
|
| 112 | ) {
|
| 113 | public static final Codec<DatapackStructureReport.Report> CODEC = RecordCodecBuilder.create(
|
| 114 | i -> i.group(
|
| 115 | Codec.unboundedMap(DatapackStructureReport.REGISTRY_KEY_CODEC, DatapackStructureReport.Entry.CODEC)
|
| 116 | .fieldOf("registries")
|
| 117 | .forGetter(DatapackStructureReport.Report::registries),
|
| 118 | Codec.unboundedMap(Codec.STRING, DatapackStructureReport.CustomPackEntry.CODEC).fieldOf("others").forGetter(DatapackStructureReport.Report::others)
|
| 119 | )
|
| 120 | .apply(i, DatapackStructureReport.Report::new)
|
| 121 | );
|
| 122 | }
|
| 123 | }
|
| 124 |
|