DataProvider .java

net.minecraft.data.DataProvider (interface): members: FIXED_ORDER_FIELDS, KEY_COMPARATOR, pathGetter, saveStable, saveAll, pathProvider, Factory, value.

net.minecraft.datainterface104 linescommon4.0 KB

API Surface (0)

No public/protected methods detected (fields or constants only).

Fields & Constants (6)

FIXED_ORDER_FIELDS
KEY_COMPARATOR
LOGGER
pathProvider
pathGetter
value

String literals (2)

"parent"
"saveStable"

Code Graph: Dependencies (8)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

DataProvider.java104 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.data;
2
3
import com.google.common.hash.Hashing;
4
import com.google.common.hash.HashingOutputStream;
5
import com.google.gson.JsonElement;
6
import com.google.gson.stream.JsonWriter;
7
import com.mojang.logging.LogUtils;
8
import com.mojang.serialization.Codec;
9
import com.mojang.serialization.DynamicOps;
10
import com.mojang.serialization.JsonOps;
11
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
12
import java.io.ByteArrayOutputStream;
13
import java.io.IOException;
14
import java.io.OutputStreamWriter;
15
import java.nio.charset.StandardCharsets;
16
import java.nio.file.Path;
17
import java.util.Comparator;
18
import java.util.Map;
19
import java.util.concurrent.CompletableFuture;
20
import java.util.function.Function;
21
import java.util.function.ToIntFunction;
22
import net.minecraft.core.HolderLookup;
23
import net.minecraft.resources.Identifier;
24
import net.minecraft.resources.RegistryOps;
25
import net.minecraft.util.GsonHelper;
26
import net.minecraft.util.Util;
27
import org.slf4j.Logger;
28
29
public interface DataProvider {
30
	ToIntFunction<String> FIXED_ORDER_FIELDS = Util.make(new Object2IntOpenHashMap<>(), m -> {
31
		m.put("type", 0);
32
		m.put("parent", 1);
33
		m.defaultReturnValue(2);
34
	});
35
	Comparator<String> KEY_COMPARATOR = Comparator.comparingInt(FIXED_ORDER_FIELDS).thenComparing(e -> (String)e);
36
	Logger LOGGER = LogUtils.getLogger();
37
38
	CompletableFuture<?> run(CachedOutput cache);
39
40
	String getName();
41
42
	static <T> CompletableFuture<?> saveAll(
43
		final CachedOutput cache, final Codec<T> codec, final PackOutput.PathProvider pathProvider, final Map<Identifier, T> entries
44
	) {
45
		return saveAll(cache, codec, pathProvider::json, entries);
46
	}
47
48
	static <T, E> CompletableFuture<?> saveAll(final CachedOutput cache, final Codec<E> codec, final Function<T, Path> pathGetter, final Map<T, E> contents) {
49
		return saveAll(cache, e -> codec.encodeStart(JsonOps.INSTANCE, (E)e).getOrThrow(), pathGetter, contents);
50
	}
51
52
	static <T, E> CompletableFuture<?> saveAll(
53
		final CachedOutput cache, final Function<E, JsonElement> serializer, final Function<T, Path> pathGetter, final Map<T, E> contents
54
	) {
55
		return CompletableFuture.allOf(contents.entrySet().stream().map(entry -> {
56
			Path path = pathGetter.apply(entry.getKey());
57
			JsonElement json = serializer.apply(entry.getValue());
58
			return saveStable(cache, json, path);
59
		}).toArray(CompletableFuture[]::new));
60
	}
61
62
	static <T> CompletableFuture<?> saveStable(
63
		final CachedOutput cache, final HolderLookup.Provider registries, final Codec<T> codec, final T value, final Path path
64
	) {
65
		RegistryOps<JsonElement> ops = registries.createSerializationContext(JsonOps.INSTANCE);
66
		return saveStable(cache, ops, codec, value, path);
67
	}
68
69
	static <T> CompletableFuture<?> saveStable(final CachedOutput cache, final Codec<T> codec, final T value, final Path path) {
70
		return saveStable(cache, JsonOps.INSTANCE, codec, value, path);
71
	}
72
73
	private static <T> CompletableFuture<?> saveStable(
74
		final CachedOutput cache, final DynamicOps<JsonElement> ops, final Codec<T> codec, final T value, final Path path
75
	) {
76
		JsonElement json = codec.encodeStart(ops, value).getOrThrow();
77
		return saveStable(cache, json, path);
78
	}
79
80
	static CompletableFuture<?> saveStable(final CachedOutput cache, final JsonElement root, final Path path) {
81
		return CompletableFuture.runAsync(() -> {
82
			try {
83
				ByteArrayOutputStream bytes = new ByteArrayOutputStream();
84
				HashingOutputStream hashedBytes = new HashingOutputStream(Hashing.sha1(), bytes);
85
86
				try (JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(hashedBytes, StandardCharsets.UTF_8))) {
87
					jsonWriter.setSerializeNulls(false);
88
					jsonWriter.setIndent("  ");
89
					GsonHelper.writeValue(jsonWriter, root, KEY_COMPARATOR);
90
				}
91
92
				cache.writeIfNeeded(path, bytes.toByteArray(), hashedBytes.hash());
93
			} catch (IOException e) {
94
				LOGGER.error("Failed to save file to {}", path, e);
95
			}
96
		}, Util.backgroundExecutor().forName("saveStable"));
97
	}
98
99
	@FunctionalInterface
100
	interface Factory<T extends DataProvider> {
101
		T create(PackOutput output);
102
	}
103
}
104

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