| 1 | package net.minecraft.data.info;
|
| 2 |
|
| 3 | import com.google.gson.JsonElement;
|
| 4 | import com.google.gson.JsonObject;
|
| 5 | import com.mojang.serialization.JsonOps;
|
| 6 | import java.nio.file.Path;
|
| 7 | import java.util.ArrayList;
|
| 8 | import java.util.List;
|
| 9 | import java.util.concurrent.CompletableFuture;
|
| 10 | import net.minecraft.core.HolderLookup;
|
| 11 | import net.minecraft.core.component.DataComponentPatch;
|
| 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.RegistryOps;
|
| 18 |
|
| 19 | /**
|
| 20 | * Access widened by fabric-data-generation-api-v1 to extendable
|
| 21 | */
|
| 22 | public class RegistryComponentsReport implements DataProvider {
|
| 23 | private final PackOutput output;
|
| 24 | private final CompletableFuture<HolderLookup.Provider> registries;
|
| 25 |
|
| 26 | public RegistryComponentsReport(final PackOutput output, final CompletableFuture<HolderLookup.Provider> registries) {
|
| 27 | this.output = output;
|
| 28 | this.registries = registries;
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | public CompletableFuture<?> run(final CachedOutput cache) {
|
| 33 | return this.registries
|
| 34 | .thenCompose(
|
| 35 | registries -> {
|
| 36 | RegistryOps<JsonElement> registryOps = registries.createSerializationContext(JsonOps.INSTANCE);
|
| 37 | List<CompletableFuture<?>> writes = new ArrayList<>();
|
| 38 | BuiltInRegistries.DATA_COMPONENT_INITIALIZERS
|
| 39 | .build(registries)
|
| 40 | .forEach(
|
| 41 | pendingComponents -> {
|
| 42 | PackOutput.PathProvider registryPathProvider = this.output.createRegistryComponentPathProvider(pendingComponents.key());
|
| 43 | pendingComponents.forEach(
|
| 44 | (element, components) -> {
|
| 45 | if (!components.isEmpty()) {
|
| 46 | Identifier elementId = element.key().identifier();
|
| 47 | Path elementPath = registryPathProvider.json(elementId);
|
| 48 | DataComponentPatch patch = DataComponentPatch.builder().set(components).build();
|
| 49 | JsonObject root = new JsonObject();
|
| 50 | root.add(
|
| 51 | "components",
|
| 52 | DataComponentPatch.CODEC
|
| 53 | .encodeStart(registryOps, patch)
|
| 54 | .getOrThrow(err -> new IllegalStateException("Failed to encode components for item " + elementId + ": " + err))
|
| 55 | );
|
| 56 | writes.add(DataProvider.saveStable(cache, root, elementPath));
|
| 57 | }
|
| 58 | }
|
| 59 | );
|
| 60 | }
|
| 61 | );
|
| 62 | return CompletableFuture.allOf(writes.toArray(CompletableFuture[]::new));
|
| 63 | }
|
| 64 | );
|
| 65 | }
|
| 66 |
|
| 67 | /**
|
| 68 | * Access widened by fabric-data-generation-api-v1 to extendable
|
| 69 | */
|
| 70 | @Override
|
| 71 | public String getName() {
|
| 72 | return "Default Components";
|
| 73 | }
|
| 74 | }
|
| 75 |
|