RegistriesDatapackGenerator .java

net.minecraft.data.registries.RegistriesDatapackGenerator (class): members: forReloadableLayer, forWorldLayer, writeOps, registryData, registries, output.

net.minecraft.data.registriesclass99 linescommon3.5 KB

Implements

API Surface (4)

RegistriesDatapackGenerator forWorldLayer(final PackOutput output, final CompletableFuture<HolderLookup.Provider> registryContents)
DataProvider forReloadableLayer(final PackOutput output, final CompletableFuture<HolderLookup.Provider> registryContents)
CompletableFuture<?> run(final CachedOutput cache)
String getName()

Fields & Constants (6)

output
name
registryData
registries
writeOps
codec

String literals (4)

"world"
"reloadable"
"Couldn't generate file '"
"Registries for "

Full Source

RegistriesDatapackGenerator.java99 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.data.registries;
2
3
import com.google.gson.JsonElement;
4
import com.mojang.serialization.DynamicOps;
5
import com.mojang.serialization.Encoder;
6
import com.mojang.serialization.JsonOps;
7
import java.nio.file.Path;
8
import java.util.Collection;
9
import java.util.Optional;
10
import java.util.concurrent.CompletableFuture;
11
import net.minecraft.core.HolderLookup;
12
import net.minecraft.core.Registry;
13
import net.minecraft.data.CachedOutput;
14
import net.minecraft.data.DataProvider;
15
import net.minecraft.data.PackOutput;
16
import net.minecraft.resources.RegistryDataLoader;
17
import net.minecraft.resources.ResourceKey;
18
19
/**
20
 * Access widened by fabric-data-generation-api-v1 to extendable
21
 */
22
public class RegistriesDatapackGenerator implements DataProvider {
23
	private final PackOutput output;
24
	private final String name;
25
	private final Collection<RegistryDataLoader.RegistryData<?>> registryData;
26
	private final CompletableFuture<HolderLookup.Provider> registries;
27
28
	public RegistriesDatapackGenerator(
29
		final PackOutput output,
30
		final String name,
31
		final Collection<RegistryDataLoader.RegistryData<?>> registryData,
32
		final CompletableFuture<HolderLookup.Provider> registryContents
33
	) {
34
		this.name = name;
35
		this.registryData = registryData;
36
		this.registries = registryContents;
37
		this.output = output;
38
	}
39
40
	public static RegistriesDatapackGenerator forWorldLayer(final PackOutput output, final CompletableFuture<HolderLookup.Provider> registryContents) {
41
		return new RegistriesDatapackGenerator(output, "world", RegistryDataLoader.WORLD_REGISTRIES, registryContents);
42
	}
43
44
	public static DataProvider forReloadableLayer(final PackOutput output, final CompletableFuture<HolderLookup.Provider> registryContents) {
45
		return new RegistriesDatapackGenerator(output, "reloadable", RegistryDataLoader.RELOADABLE_REGISTRIES, registryContents);
46
	}
47
48
	@Override
49
	public CompletableFuture<?> run(final CachedOutput cache) {
50
		return this.registries
51
			.thenCompose(
52
				access -> {
53
					DynamicOps<JsonElement> registryOps = access.createSerializationContext(JsonOps.INSTANCE);
54
					return CompletableFuture.allOf(
55
						this.registryData
56
							.stream()
57
							.flatMap(v -> this.dumpRegistryCap(cache, access, registryOps, (RegistryDataLoader.RegistryData<?>)v).stream())
58
							.toArray(CompletableFuture[]::new)
59
					);
60
				}
61
			);
62
	}
63
64
	private <T> Optional<CompletableFuture<?>> dumpRegistryCap(
65
		final CachedOutput cache, final HolderLookup.Provider registries, final DynamicOps<JsonElement> writeOps, final RegistryDataLoader.RegistryData<T> v
66
	) {
67
		ResourceKey<? extends Registry<T>> registryKey = v.key();
68
		return registries.lookup(registryKey)
69
			.map(
70
				registry -> {
71
					PackOutput.PathProvider pathProvider = this.output.createRegistryElementsPathProvider(registryKey);
72
					return CompletableFuture.allOf(
73
						registry.listElements()
74
							.map(e -> dumpValue(pathProvider.json(e.key().identifier()), cache, writeOps, v.elementCodec(), e.value()))
75
							.toArray(CompletableFuture[]::new)
76
					);
77
				}
78
			);
79
	}
80
81
	private static <E> CompletableFuture<?> dumpValue(
82
		final Path path, final CachedOutput cache, final DynamicOps<JsonElement> ops, final Encoder<E> codec, final E value
83
	) {
84
		return codec.encodeStart(ops, value)
85
			.mapOrElse(
86
				result -> DataProvider.saveStable(cache, result, path),
87
				error -> CompletableFuture.failedFuture(new IllegalStateException("Couldn't generate file '" + path + "': " + error.message()))
88
			);
89
	}
90
91
	/**
92
	 * Access widened by fabric-data-generation-api-v1 to extendable
93
	 */
94
	@Override
95
	public String getName() {
96
		return "Registries for " + this.name;
97
	}
98
}
99

Decompiled Minecraft 26.3 source of RegistriesDatapackGenerator (net/minecraft/data/registries/RegistriesDatapackGenerator.java) with Mojang official mappings, stored verbatim on the site.