| 1 | package net.minecraft.data.info;
|
| 2 |
|
| 3 | import com.google.gson.JsonElement;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.DynamicOps;
|
| 7 | import com.mojang.serialization.Encoder;
|
| 8 | import com.mojang.serialization.JsonOps;
|
| 9 | import com.mojang.serialization.MapCodec;
|
| 10 | import java.nio.file.Path;
|
| 11 | import java.util.ArrayList;
|
| 12 | import java.util.List;
|
| 13 | import java.util.Optional;
|
| 14 | import java.util.concurrent.CompletableFuture;
|
| 15 | import net.minecraft.core.HolderLookup;
|
| 16 | import net.minecraft.core.registries.Registries;
|
| 17 | import net.minecraft.data.CachedOutput;
|
| 18 | import net.minecraft.data.DataProvider;
|
| 19 | import net.minecraft.data.PackOutput;
|
| 20 | import net.minecraft.resources.Identifier;
|
| 21 | import net.minecraft.resources.ResourceKey;
|
| 22 | import net.minecraft.world.level.biome.Biome;
|
| 23 | import net.minecraft.world.level.biome.Climate;
|
| 24 | import net.minecraft.world.level.biome.MultiNoiseBiomeSourceParameterList;
|
| 25 | import org.slf4j.Logger;
|
| 26 |
|
| 27 | /**
|
| 28 | * Access widened by fabric-data-generation-api-v1 to extendable
|
| 29 | */
|
| 30 | public class BiomeParametersDumpReport implements DataProvider {
|
| 31 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 32 | private final Path topPath;
|
| 33 | private final CompletableFuture<HolderLookup.Provider> registries;
|
| 34 | private static final MapCodec<ResourceKey<Biome>> ENTRY_CODEC = ResourceKey.codec(Registries.BIOME).fieldOf("biome");
|
| 35 | private static final Codec<Climate.ParameterList<ResourceKey<Biome>>> CODEC = Climate.ParameterList.codec(ENTRY_CODEC).fieldOf("biomes").codec();
|
| 36 |
|
| 37 | public BiomeParametersDumpReport(final PackOutput output, final CompletableFuture<HolderLookup.Provider> registries) {
|
| 38 | this.topPath = output.getOutputFolder(PackOutput.Target.REPORTS).resolve("biome_parameters");
|
| 39 | this.registries = registries;
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public CompletableFuture<?> run(final CachedOutput cache) {
|
| 44 | return this.registries
|
| 45 | .thenCompose(
|
| 46 | registryAccess -> {
|
| 47 | DynamicOps<JsonElement> registryOps = registryAccess.createSerializationContext(JsonOps.INSTANCE);
|
| 48 | List<CompletableFuture<?>> result = new ArrayList<>();
|
| 49 | MultiNoiseBiomeSourceParameterList.knownPresets()
|
| 50 | .forEach(
|
| 51 | (preset, parameterList) -> result.add(
|
| 52 | dumpValue(this.createPath(preset.id()), cache, registryOps, CODEC, (Climate.ParameterList<ResourceKey<Biome>>)parameterList)
|
| 53 | )
|
| 54 | );
|
| 55 | return CompletableFuture.allOf(result.toArray(CompletableFuture[]::new));
|
| 56 | }
|
| 57 | );
|
| 58 | }
|
| 59 |
|
| 60 | private static <E> CompletableFuture<?> dumpValue(
|
| 61 | final Path path, final CachedOutput cache, final DynamicOps<JsonElement> ops, final Encoder<E> codec, final E value
|
| 62 | ) {
|
| 63 | Optional<JsonElement> result = codec.encodeStart(ops, value).resultOrPartial(e -> LOGGER.error("Couldn't serialize element {}: {}", path, e));
|
| 64 | return result.isPresent() ? DataProvider.saveStable(cache, result.get(), path) : CompletableFuture.completedFuture(null);
|
| 65 | }
|
| 66 |
|
| 67 | private Path createPath(final Identifier element) {
|
| 68 | return element.withSuffix(".json").resolveAgainst(this.topPath);
|
| 69 | }
|
| 70 |
|
| 71 | /**
|
| 72 | * Access widened by fabric-data-generation-api-v1 to extendable
|
| 73 | */
|
| 74 | @Override
|
| 75 | public String getName() {
|
| 76 | return "Biome Parameters";
|
| 77 | }
|
| 78 | }
|
| 79 |
|