ResourceMetadata .java

net.minecraft.server.packs.resources.ResourceMetadata (interface): members: getTypedSections, getTypedSection, EMPTY_SUPPLIER, fromJsonStream, MapBased.

net.minecraft.server.packs.resourcesinterface79 linesserver2.7 KB

API Surface (3)

<T> Optional<T> getSection(final MetadataSectionType<T> serializer)
<T> Optional<T> getSection(final MetadataSectionType<T> serializer)
<T> Optional<T> getSection(final MetadataSectionType<T> serializer)

Fields & Constants (2)

EMPTY
EMPTY_SUPPLIER

Code Graph: Dependencies (2)

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

Full Source

ResourceMetadata.java79 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.packs.resources;
2
3
import com.google.gson.JsonElement;
4
import com.google.gson.JsonObject;
5
import com.google.gson.JsonParseException;
6
import com.mojang.serialization.JsonOps;
7
import java.io.BufferedReader;
8
import java.io.IOException;
9
import java.io.InputStream;
10
import java.io.InputStreamReader;
11
import java.nio.charset.StandardCharsets;
12
import java.util.Collection;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.Optional;
16
import java.util.stream.Collectors;
17
import net.minecraft.server.packs.metadata.MetadataSectionType;
18
import net.minecraft.util.GsonHelper;
19
20
public interface ResourceMetadata {
21
	ResourceMetadata EMPTY = new ResourceMetadata() {
22
		@Override
23
		public <T> Optional<T> getSection(final MetadataSectionType<T> serializer) {
24
			return Optional.empty();
25
		}
26
	};
27
	IoSupplier<ResourceMetadata> EMPTY_SUPPLIER = () -> EMPTY;
28
29
	static ResourceMetadata fromJsonStream(final InputStream inputStream) throws IOException {
30
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
31
			final JsonObject metadata = GsonHelper.parse(reader);
32
			return new ResourceMetadata() {
33
				@Override
34
				public <T> Optional<T> getSection(final MetadataSectionType<T> serializer) {
35
					String name = serializer.name();
36
					JsonElement rawSection = metadata.get(name);
37
					if (rawSection != null) {
38
						T section = serializer.codec().parse(JsonOps.INSTANCE, rawSection).getOrThrow(JsonParseException::new);
39
						return Optional.of(section);
40
					} else {
41
						return Optional.empty();
42
					}
43
				}
44
			};
45
		}
46
	}
47
48
	<T> Optional<T> getSection(MetadataSectionType<T> serializer);
49
50
	default <T> Optional<MetadataSectionType.WithValue<T>> getTypedSection(final MetadataSectionType<T> type) {
51
		return this.getSection(type).map(type::withValue);
52
	}
53
54
	static <T> ResourceMetadata of(final MetadataSectionType<T> k, final T v) {
55
		return new ResourceMetadata.MapBased(Map.of(k, v));
56
	}
57
58
	static <T1, T2> ResourceMetadata of(final MetadataSectionType<T1> k1, final T1 v1, final MetadataSectionType<T2> k2, final T2 v2) {
59
		return new ResourceMetadata.MapBased(Map.of(k1, v1, k2, (T1)v2));
60
	}
61
62
	default List<MetadataSectionType.WithValue<?>> getTypedSections(final Collection<MetadataSectionType<?>> types) {
63
		return types.stream().map(this::getTypedSection).flatMap(Optional::stream).collect(Collectors.toUnmodifiableList());
64
	}
65
66
	class MapBased implements ResourceMetadata {
67
		private final Map<MetadataSectionType<?>, ?> values;
68
69
		private MapBased(final Map<MetadataSectionType<?>, ?> values) {
70
			this.values = values;
71
		}
72
73
		@Override
74
		public <T> Optional<T> getSection(final MetadataSectionType<T> serializer) {
75
			return Optional.ofNullable((T)this.values.get(serializer));
76
		}
77
	}
78
}
79

Decompiled Minecraft 26.3 source of ResourceMetadata (net/minecraft/server/packs/resources/ResourceMetadata.java) with Mojang official mappings, stored verbatim on the site.