HolderLookup .java

net.minecraft.core.HolderLookup (interface): members: RegistryLookup, listElementIds, listTagIds, listElements, listTags, Provider. strings: " not found".

net.minecraft.coreinterface137 linescommonextends HolderGetter<T>4.3 KB

API Surface (5)

Stream<ResourceKey<? extends Registry<?>>> listRegistryKeys()
<T> Optional<HolderLookup.RegistryLookup<T>> lookup(final ResourceKey<? extends Registry<? extends T>> key)
HolderLookup.RegistryLookup<T> parent()
Optional<Holder.Reference<T>> get(final ResourceKey<T> id)
Stream<Holder.Reference<T>> listElements()

String literals (2)

"Registry "
" not found"

Full Source

HolderLookup.java137 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.core;
2
3
import com.mojang.serialization.DynamicOps;
4
import com.mojang.serialization.Lifecycle;
5
import java.util.Map;
6
import java.util.Optional;
7
import java.util.function.Predicate;
8
import java.util.stream.Collectors;
9
import java.util.stream.Stream;
10
import net.minecraft.resources.RegistryOps;
11
import net.minecraft.resources.ResourceKey;
12
import net.minecraft.tags.TagKey;
13
import net.minecraft.world.flag.FeatureElement;
14
import net.minecraft.world.flag.FeatureFlagSet;
15
16
public interface HolderLookup<T> extends HolderGetter<T> {
17
	Stream<Holder.Reference<T>> listElements();
18
19
	default Stream<ResourceKey<T>> listElementIds() {
20
		return this.listElements().map(Holder.Reference::key);
21
	}
22
23
	Stream<HolderSet.Named<T>> listTags();
24
25
	default Stream<TagKey<T>> listTagIds() {
26
		return this.listTags().map(HolderSet.Named::key);
27
	}
28
29
	interface Provider extends HolderGetter.Provider {
30
		Stream<ResourceKey<? extends Registry<?>>> listRegistryKeys();
31
32
		default Stream<HolderLookup.RegistryLookup<?>> listRegistries() {
33
			return this.listRegistryKeys().map(this::lookupOrThrow);
34
		}
35
36
		@Override
37
		<T> Optional<? extends HolderLookup.RegistryLookup<T>> lookup(final ResourceKey<? extends Registry<? extends T>> key);
38
39
		default <T> HolderLookup.RegistryLookup<T> lookupOrThrow(final ResourceKey<? extends Registry<? extends T>> key) {
40
			return this.lookup(key).orElseThrow(() -> new IllegalStateException("Registry " + key.identifier() + " not found"));
41
		}
42
43
		default <V> RegistryOps<V> createSerializationContext(final DynamicOps<V> parent) {
44
			return RegistryOps.create(parent, this);
45
		}
46
47
		static HolderLookup.Provider create(final Stream<HolderLookup.RegistryLookup<?>> lookups) {
48
			final Map<ResourceKey<? extends Registry<?>>, HolderLookup.RegistryLookup<?>> map = lookups.collect(
49
				Collectors.toUnmodifiableMap(HolderLookup.RegistryLookup::key, e -> e)
50
			);
51
			return new HolderLookup.Provider() {
52
				@Override
53
				public Stream<ResourceKey<? extends Registry<?>>> listRegistryKeys() {
54
					return map.keySet().stream();
55
				}
56
57
				@Override
58
				public <T> Optional<HolderLookup.RegistryLookup<T>> lookup(final ResourceKey<? extends Registry<? extends T>> key) {
59
					return Optional.ofNullable((HolderLookup.RegistryLookup<T>)map.get(key));
60
				}
61
			};
62
		}
63
64
		default Lifecycle allRegistriesLifecycle() {
65
			return this.listRegistries().map(HolderLookup.RegistryLookup::registryLifecycle).reduce(Lifecycle.stable(), Lifecycle::add);
66
		}
67
	}
68
69
	interface RegistryLookup<T> extends HolderLookup<T> {
70
		ResourceKey<? extends Registry<? extends T>> key();
71
72
		Lifecycle registryLifecycle();
73
74
		default HolderLookup.RegistryLookup<T> filterFeatures(final FeatureFlagSet enabledFeatures) {
75
			return FeatureElement.FILTERED_REGISTRIES.contains(this.key()) ? this.filterElements(t -> ((FeatureElement)t).isEnabled(enabledFeatures)) : this;
76
		}
77
78
		default HolderLookup.RegistryLookup<T> filterElements(final Predicate<T> filter) {
79
			return new HolderLookup.RegistryLookup.Delegate<T>() {
80
				@Override
81
				public HolderLookup.RegistryLookup<T> parent() {
82
					return RegistryLookup.this;
83
				}
84
85
				@Override
86
				public Optional<Holder.Reference<T>> get(final ResourceKey<T> id) {
87
					return this.parent().get(id).filter(holder -> filter.test(holder.value()));
88
				}
89
90
				@Override
91
				public Stream<Holder.Reference<T>> listElements() {
92
					return this.parent().listElements().filter(e -> filter.test(e.value()));
93
				}
94
			};
95
		}
96
97
		interface Delegate<T> extends HolderLookup.RegistryLookup<T> {
98
			HolderLookup.RegistryLookup<T> parent();
99
100
			@Override
101
			default ResourceKey<? extends Registry<? extends T>> key() {
102
				return this.parent().key();
103
			}
104
105
			@Override
106
			default Lifecycle registryLifecycle() {
107
				return this.parent().registryLifecycle();
108
			}
109
110
			@Override
111
			default Optional<Holder.Reference<T>> get(final ResourceKey<T> id) {
112
				return this.parent().get(id);
113
			}
114
115
			@Override
116
			default Stream<Holder.Reference<T>> listElements() {
117
				return this.parent().listElements();
118
			}
119
120
			@Override
121
			default Optional<HolderSet.Named<T>> get(final TagKey<T> id) {
122
				return this.parent().get(id);
123
			}
124
125
			@Override
126
			default Stream<HolderSet.Named<T>> listTags() {
127
				return this.parent().listTags();
128
			}
129
130
			@Override
131
			default boolean canSerialize(final HolderOwner<T> owner) {
132
				return this.parent().canSerialize(owner);
133
			}
134
		}
135
	}
136
}
137

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