MappedRegistry .java

net.minecraft.core.MappedRegistry (class): members: unregisteredIntrusiveHolders, bindAllTagsToEmpty, registrationInfos, frozenTags, allTags, TagSet, toId.

net.minecraft.coreclass509 linescommon15.6 KB

Implements

API Surface (52)

Stream<HolderSet.Named<T>> listTags()
ResourceKey<? extends Registry<T>> key()
String toString()
Holder.Reference<T> register(final ResourceKey<T> key, final T value, final RegistrationInfo registrationInfo)
Optional<ResourceKey<T>> getResourceKey(final T thing)
int getId(final @Nullable T thing)
Optional<Holder.Reference<T>> get(final int id)
Optional<Holder.Reference<T>> get(final Identifier id)
Optional<Holder.Reference<T>> get(final ResourceKey<T> id)
Optional<Holder.Reference<T>> getAny()
Holder<T> wrapAsHolder(final T value)
int size()
Optional<RegistrationInfo> registrationInfo(final ResourceKey<T> element)
Lifecycle registryLifecycle()
Iterator<T> iterator()
Set<Identifier> keySet()
Set<ResourceKey<T>> registryKeySet()
Set<Entry<ResourceKey<T>, T>> entrySet()
Stream<Holder.Reference<T>> listElements()
Stream<HolderSet.Named<T>> getTags()
boolean isEmpty()
Optional<Holder.Reference<T>> getRandom(final RandomSource random)
boolean containsKey(final Identifier key)
boolean containsKey(final ResourceKey<T> key)
DataComponentLookup<T> componentLookup()
Registry<T> freeze()
Holder.Reference<T> createIntrusiveHolder(final T value)
Optional<HolderSet.Named<T>> get(final TagKey<T> id)
void bindTags(final Map<TagKey<T>, List<Holder<T>>> pendingTags)
void bindAllTagsToEmpty()
HolderGetter<T> createRegistrationLookup()
Optional<Holder.Reference<T>> get(final ResourceKey<T> id)
Holder.Reference<T> getOrThrow(final ResourceKey<T> id)
Optional<HolderSet.Named<T>> get(final TagKey<T> id)
HolderSet.Named<T> getOrThrow(final TagKey<T> id)
boolean canSerialize(final HolderOwner<T> owner)
Registry.PendingTags<T> prepareTagReload(final TagLoader.LoadResult<T> tags)
HolderLookup.RegistryLookup<T> parent()
Optional<HolderSet.Named<T>> get(final TagKey<T> id)
Stream<HolderSet.Named<T>> listTags()
ResourceKey<? extends Registry<? extends T>> key()
int size()
HolderLookup.RegistryLookup<T> lookup()
void apply()
boolean isBound()
Optional<HolderSet.Named<T>> get(final TagKey<T> id)
void forEach(final BiConsumer<? super TagKey<T>, ? super HolderSet.Named<T>> action)
Stream<HolderSet.Named<T>> getTags()
boolean isBound()
Optional<HolderSet.Named<T>> get(final TagKey<T> id)
void forEach(final BiConsumer<? super TagKey<T>, ? super HolderSet.Named<T>> action)
Stream<HolderSet.Named<T>> getTags()

Fields & Constants (13)

key
byId
toId
byLocation
byKey
byValue
registrationInfos
registryLifecycle
frozenTags
allTags
componentLookup
frozen
unregisteredIntrusiveHolders

String literals (21)

"Registry["
"Registry is already frozen"
"Registry is already frozen (trying to add key "
"Adding duplicate key '"
"' to registry"
"Adding duplicate value '"
"Missing intrusive holder for "
"Registry not frozen yet"
"Unbound values in registry "
"Some intrusive holders were not registered: "
"Tags already present before freezing"
"Unbound tags in registry "
"This registry can't create intrusive holders"
"Can't create named set "
" containing value "
" from outside registry "
"Found direct holder "
" value in tag "
"Invalid method used for tag loading"
"Tags not bound, trying to access "
"Tags not bound"

Full Source

MappedRegistry.java509 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.core;
2
3
import com.google.common.collect.ImmutableMap;
4
import com.google.common.collect.Iterators;
5
import com.google.common.collect.ImmutableMap.Builder;
6
import com.mojang.serialization.Lifecycle;
7
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
8
import it.unimi.dsi.fastutil.objects.ObjectList;
9
import it.unimi.dsi.fastutil.objects.Reference2IntMap;
10
import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap;
11
import java.util.ArrayList;
12
import java.util.Collections;
13
import java.util.HashMap;
14
import java.util.IdentityHashMap;
15
import java.util.Iterator;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.Objects;
19
import java.util.Optional;
20
import java.util.Set;
21
import java.util.Map.Entry;
22
import java.util.function.BiConsumer;
23
import java.util.stream.Stream;
24
import net.minecraft.core.component.DataComponentLookup;
25
import net.minecraft.resources.Identifier;
26
import net.minecraft.resources.ResourceKey;
27
import net.minecraft.tags.TagKey;
28
import net.minecraft.tags.TagLoader;
29
import net.minecraft.util.RandomSource;
30
import net.minecraft.util.Util;
31
import org.jspecify.annotations.Nullable;
32
33
public class MappedRegistry<T> implements WritableRegistry<T> {
34
	private final ResourceKey<? extends Registry<T>> key;
35
	private final ObjectList<Holder.Reference<T>> byId = new ObjectArrayList<>(256);
36
	private final Reference2IntMap<T> toId = Util.make(new Reference2IntOpenHashMap<>(), t -> t.defaultReturnValue(-1));
37
	private final Map<Identifier, Holder.Reference<T>> byLocation = new HashMap<>();
38
	private final Map<ResourceKey<T>, Holder.Reference<T>> byKey = new HashMap<>();
39
	private final Map<T, Holder.Reference<T>> byValue = new IdentityHashMap<>();
40
	private final Map<ResourceKey<T>, RegistrationInfo> registrationInfos = new IdentityHashMap<>();
41
	private Lifecycle registryLifecycle;
42
	private final Map<TagKey<T>, HolderSet.Named<T>> frozenTags = new IdentityHashMap<>();
43
	private MappedRegistry.TagSet<T> allTags = MappedRegistry.TagSet.unbound();
44
	private @Nullable DataComponentLookup<T> componentLookup;
45
	private boolean frozen;
46
	private @Nullable Map<T, Holder.Reference<T>> unregisteredIntrusiveHolders;
47
48
	@Override
49
	public Stream<HolderSet.Named<T>> listTags() {
50
		return this.getTags();
51
	}
52
53
	public MappedRegistry(final ResourceKey<? extends Registry<T>> key, final Lifecycle lifecycle) {
54
		this(key, lifecycle, false);
55
	}
56
57
	public MappedRegistry(final ResourceKey<? extends Registry<T>> key, final Lifecycle initialLifecycle, final boolean intrusiveHolders) {
58
		this.key = key;
59
		this.registryLifecycle = initialLifecycle;
60
		if (intrusiveHolders) {
61
			this.unregisteredIntrusiveHolders = new IdentityHashMap<>();
62
		}
63
	}
64
65
	@Override
66
	public ResourceKey<? extends Registry<T>> key() {
67
		return this.key;
68
	}
69
70
	@Override
71
	public String toString() {
72
		return "Registry[" + this.key + " (" + this.registryLifecycle + ")]";
73
	}
74
75
	private void validateWrite() {
76
		if (this.frozen) {
77
			throw new IllegalStateException("Registry is already frozen");
78
		}
79
	}
80
81
	private void validateWrite(final ResourceKey<T> key) {
82
		if (this.frozen) {
83
			throw new IllegalStateException("Registry is already frozen (trying to add key " + key + ")");
84
		}
85
	}
86
87
	@Override
88
	public Holder.Reference<T> register(final ResourceKey<T> key, final T value, final RegistrationInfo registrationInfo) {
89
		this.validateWrite(key);
90
		Objects.requireNonNull(key);
91
		Objects.requireNonNull(value);
92
		if (this.byLocation.containsKey(key.identifier())) {
93
			throw (IllegalStateException)Util.pauseInIde(new IllegalStateException("Adding duplicate key '" + key + "' to registry"));
94
		}
95
96
		if (this.byValue.containsKey(value)) {
97
			throw (IllegalStateException)Util.pauseInIde(new IllegalStateException("Adding duplicate value '" + value + "' to registry"));
98
		}
99
100
		Holder.Reference<T> holder;
101
		if (this.unregisteredIntrusiveHolders != null) {
102
			holder = this.unregisteredIntrusiveHolders.remove(value);
103
			if (holder == null) {
104
				throw new AssertionError("Missing intrusive holder for " + key + ":" + value);
105
			}
106
107
			holder.bindKey(key);
108
		} else {
109
			holder = this.byKey.computeIfAbsent(key, k -> Holder.Reference.createStandAlone(this, (ResourceKey<T>)k));
110
		}
111
112
		this.byKey.put(key, holder);
113
		this.byLocation.put(key.identifier(), holder);
114
		this.byValue.put(value, holder);
115
		int newId = this.byId.size();
116
		this.byId.add(holder);
117
		this.toId.put(value, newId);
118
		this.registrationInfos.put(key, registrationInfo);
119
		this.registryLifecycle = this.registryLifecycle.add(registrationInfo.lifecycle());
120
		return holder;
121
	}
122
123
	@Override
124
	public @Nullable Identifier getKey(final T thing) {
125
		Holder.Reference<T> holder = this.byValue.get(thing);
126
		return holder != null ? holder.key().identifier() : null;
127
	}
128
129
	@Override
130
	public Optional<ResourceKey<T>> getResourceKey(final T thing) {
131
		return Optional.ofNullable(this.byValue.get(thing)).map(Holder.Reference::key);
132
	}
133
134
	@Override
135
	public int getId(final @Nullable T thing) {
136
		return this.toId.getInt(thing);
137
	}
138
139
	@Override
140
	public @Nullable T getValue(final @Nullable ResourceKey<T> key) {
141
		return getValueFromNullable(this.byKey.get(key));
142
	}
143
144
	@Override
145
	public @Nullable T byId(final int id) {
146
		return id >= 0 && id < this.byId.size() ? this.byId.get(id).value() : null;
147
	}
148
149
	@Override
150
	public Optional<Holder.Reference<T>> get(final int id) {
151
		return id >= 0 && id < this.byId.size() ? Optional.ofNullable(this.byId.get(id)) : Optional.empty();
152
	}
153
154
	@Override
155
	public Optional<Holder.Reference<T>> get(final Identifier id) {
156
		return Optional.ofNullable(this.byLocation.get(id));
157
	}
158
159
	@Override
160
	public Optional<Holder.Reference<T>> get(final ResourceKey<T> id) {
161
		return Optional.ofNullable(this.byKey.get(id));
162
	}
163
164
	@Override
165
	public Optional<Holder.Reference<T>> getAny() {
166
		return this.byId.isEmpty() ? Optional.empty() : Optional.of(this.byId.getFirst());
167
	}
168
169
	@Override
170
	public Holder<T> wrapAsHolder(final T value) {
171
		Holder.Reference<T> existingHolder = this.byValue.get(value);
172
		return existingHolder != null ? existingHolder : Holder.direct(value);
173
	}
174
175
	private Holder.Reference<T> getOrCreateHolderOrThrow(final ResourceKey<T> key) {
176
		return this.byKey.computeIfAbsent(key, id -> {
177
			if (this.unregisteredIntrusiveHolders != null) {
178
				throw new IllegalStateException("This registry can't create new holders without value");
179
			}
180
181
			this.validateWrite((ResourceKey<T>)id);
182
			return Holder.Reference.createStandAlone(this, (ResourceKey<T>)id);
183
		});
184
	}
185
186
	@Override
187
	public int size() {
188
		return this.byKey.size();
189
	}
190
191
	@Override
192
	public Optional<RegistrationInfo> registrationInfo(final ResourceKey<T> element) {
193
		return Optional.ofNullable(this.registrationInfos.get(element));
194
	}
195
196
	@Override
197
	public Lifecycle registryLifecycle() {
198
		return this.registryLifecycle;
199
	}
200
201
	@Override
202
	public Iterator<T> iterator() {
203
		return Iterators.transform(this.byId.iterator(), Holder::value);
204
	}
205
206
	@Override
207
	public @Nullable T getValue(final @Nullable Identifier key) {
208
		Holder.Reference<T> result = this.byLocation.get(key);
209
		return getValueFromNullable(result);
210
	}
211
212
	private static <T> @Nullable T getValueFromNullable(final Holder.@Nullable Reference<T> result) {
213
		return result != null ? result.value() : null;
214
	}
215
216
	@Override
217
	public Set<Identifier> keySet() {
218
		return Collections.unmodifiableSet(this.byLocation.keySet());
219
	}
220
221
	@Override
222
	public Set<ResourceKey<T>> registryKeySet() {
223
		return Collections.unmodifiableSet(this.byKey.keySet());
224
	}
225
226
	@Override
227
	public Set<Entry<ResourceKey<T>, T>> entrySet() {
228
		return Collections.unmodifiableSet(Util.mapValuesLazy(this.byKey, Holder::value).entrySet());
229
	}
230
231
	@Override
232
	public Stream<Holder.Reference<T>> listElements() {
233
		return this.byId.stream();
234
	}
235
236
	@Override
237
	public Stream<HolderSet.Named<T>> getTags() {
238
		return this.allTags.getTags();
239
	}
240
241
	private HolderSet.Named<T> getOrCreateTagForRegistration(final TagKey<T> tag) {
242
		return this.frozenTags.computeIfAbsent(tag, this::createTag);
243
	}
244
245
	private HolderSet.Named<T> createTag(final TagKey<T> tag) {
246
		return new HolderSet.Named<>(this, tag);
247
	}
248
249
	@Override
250
	public boolean isEmpty() {
251
		return this.byKey.isEmpty();
252
	}
253
254
	@Override
255
	public Optional<Holder.Reference<T>> getRandom(final RandomSource random) {
256
		return Util.getRandomSafe(this.byId, random);
257
	}
258
259
	@Override
260
	public boolean containsKey(final Identifier key) {
261
		return this.byLocation.containsKey(key);
262
	}
263
264
	@Override
265
	public boolean containsKey(final ResourceKey<T> key) {
266
		return this.byKey.containsKey(key);
267
	}
268
269
	@Override
270
	public DataComponentLookup<T> componentLookup() {
271
		return Objects.requireNonNull(this.componentLookup, "Registry not frozen yet");
272
	}
273
274
	@Override
275
	public Registry<T> freeze() {
276
		if (this.frozen) {
277
			return this;
278
		}
279
280
		this.frozen = true;
281
		this.byValue.forEach((value, holder) -> holder.bindValue((T)value));
282
		List<Identifier> unboundEntries = this.byKey.entrySet().stream().filter(e -> !e.getValue().isBound()).map(e -> e.getKey().identifier()).sorted().toList();
283
		if (!unboundEntries.isEmpty()) {
284
			throw new IllegalStateException("Unbound values in registry " + this.key() + ": " + unboundEntries);
285
		}
286
287
		if (this.unregisteredIntrusiveHolders != null) {
288
			if (!this.unregisteredIntrusiveHolders.isEmpty()) {
289
				throw new IllegalStateException("Some intrusive holders were not registered: " + this.unregisteredIntrusiveHolders.values());
290
			}
291
292
			this.unregisteredIntrusiveHolders = null;
293
		}
294
295
		if (this.allTags.isBound()) {
296
			throw new IllegalStateException("Tags already present before freezing");
297
		}
298
299
		List<Identifier> unboundTags = this.frozenTags.entrySet().stream().filter(e -> !e.getValue().isBound()).map(e -> e.getKey().location()).sorted().toList();
300
		if (!unboundTags.isEmpty()) {
301
			throw new IllegalStateException("Unbound tags in registry " + this.key() + ": " + unboundTags);
302
		}
303
304
		this.componentLookup = new DataComponentLookup<>(this.byId);
305
		this.allTags = MappedRegistry.TagSet.fromMap(this.frozenTags);
306
		this.refreshTagsInHolders();
307
		return this;
308
	}
309
310
	@Override
311
	public Holder.Reference<T> createIntrusiveHolder(final T value) {
312
		if (this.unregisteredIntrusiveHolders == null) {
313
			throw new IllegalStateException("This registry can't create intrusive holders");
314
		}
315
316
		this.validateWrite();
317
		return this.unregisteredIntrusiveHolders.computeIfAbsent(value, v -> Holder.Reference.createIntrusive(this, (T)v));
318
	}
319
320
	@Override
321
	public Optional<HolderSet.Named<T>> get(final TagKey<T> id) {
322
		return this.allTags.get(id);
323
	}
324
325
	private Holder.Reference<T> validateAndUnwrapTagElement(final TagKey<T> id, final Holder<T> value) {
326
		if (!value.canSerializeIn(this)) {
327
			throw new IllegalStateException("Can't create named set " + id + " containing value " + value + " from outside registry " + this);
328
		} else if (value instanceof Holder.Reference<T> reference) {
329
			return reference;
330
		} else {
331
			throw new IllegalStateException("Found direct holder " + value + " value in tag " + id);
332
		}
333
	}
334
335
	@Override
336
	public void bindTags(final Map<TagKey<T>, List<Holder<T>>> pendingTags) {
337
		this.validateWrite();
338
		pendingTags.forEach((id, values) -> this.getOrCreateTagForRegistration((TagKey<T>)id).bind((List<Holder<T>>)values));
339
	}
340
341
	private void refreshTagsInHolders() {
342
		Map<Holder.Reference<T>, List<TagKey<T>>> tagsForElement = new IdentityHashMap<>();
343
		this.byKey.values().forEach(h -> tagsForElement.put((Holder.Reference<T>)h, new ArrayList<>()));
344
		this.allTags.forEach((id, values) -> {
345
			for (Holder<T> value : values) {
346
				Holder.Reference<T> reference = this.validateAndUnwrapTagElement((TagKey<T>)id, value);
347
				tagsForElement.get(reference).add((TagKey<T>)id);
348
			}
349
		});
350
		tagsForElement.forEach(Holder.Reference::bindTags);
351
	}
352
353
	public void bindAllTagsToEmpty() {
354
		this.validateWrite();
355
		this.frozenTags.values().forEach(e -> e.bind(List.of()));
356
	}
357
358
	@Override
359
	public HolderGetter<T> createRegistrationLookup() {
360
		this.validateWrite();
361
		return new HolderGetter<T>() {
362
			@Override
363
			public Optional<Holder.Reference<T>> get(final ResourceKey<T> id) {
364
				return Optional.of(this.getOrThrow(id));
365
			}
366
367
			@Override
368
			public Holder.Reference<T> getOrThrow(final ResourceKey<T> id) {
369
				return MappedRegistry.this.getOrCreateHolderOrThrow(id);
370
			}
371
372
			@Override
373
			public Optional<HolderSet.Named<T>> get(final TagKey<T> id) {
374
				return Optional.of(this.getOrThrow(id));
375
			}
376
377
			@Override
378
			public HolderSet.Named<T> getOrThrow(final TagKey<T> id) {
379
				return MappedRegistry.this.getOrCreateTagForRegistration(id);
380
			}
381
382
			@Override
383
			public boolean canSerialize(final HolderOwner<T> owner) {
384
				return MappedRegistry.this.canSerialize(owner);
385
			}
386
		};
387
	}
388
389
	@Override
390
	public Registry.PendingTags<T> prepareTagReload(final TagLoader.LoadResult<T> tags) {
391
		if (!this.frozen) {
392
			throw new IllegalStateException("Invalid method used for tag loading");
393
		}
394
395
		Builder<TagKey<T>, HolderSet.Named<T>> pendingTagsBuilder = ImmutableMap.builder();
396
		final Map<TagKey<T>, List<Holder<T>>> pendingContents = new HashMap<>();
397
		tags.tags().forEach((id, contents) -> {
398
			HolderSet.Named<T> tagToAdd = this.frozenTags.get(id);
399
			if (tagToAdd == null) {
400
				tagToAdd = this.createTag((TagKey<T>)id);
401
			}
402
403
			pendingTagsBuilder.put((TagKey<T>)id, tagToAdd);
404
			pendingContents.put((TagKey<T>)id, List.copyOf(contents));
405
		});
406
		final ImmutableMap<TagKey<T>, HolderSet.Named<T>> pendingTags = pendingTagsBuilder.build();
407
		final HolderLookup.RegistryLookup<T> patchedHolder = new HolderLookup.RegistryLookup.Delegate<T>() {
408
			@Override
409
			public HolderLookup.RegistryLookup<T> parent() {
410
				return MappedRegistry.this;
411
			}
412
413
			@Override
414
			public Optional<HolderSet.Named<T>> get(final TagKey<T> id) {
415
				return Optional.ofNullable(pendingTags.get(id));
416
			}
417
418
			@Override
419
			public Stream<HolderSet.Named<T>> listTags() {
420
				return pendingTags.values().stream();
421
			}
422
		};
423
		return new Registry.PendingTags<T>() {
424
			@Override
425
			public ResourceKey<? extends Registry<? extends T>> key() {
426
				return MappedRegistry.this.key();
427
			}
428
429
			@Override
430
			public int size() {
431
				return pendingContents.size();
432
			}
433
434
			@Override
435
			public HolderLookup.RegistryLookup<T> lookup() {
436
				return patchedHolder;
437
			}
438
439
			@Override
440
			public void apply() {
441
				pendingTags.forEach((id, tag) -> {
442
					List<Holder<T>> values = pendingContents.getOrDefault(id, List.of());
443
					tag.bind(values);
444
				});
445
				MappedRegistry.this.allTags = MappedRegistry.TagSet.fromMap(pendingTags);
446
				MappedRegistry.this.refreshTagsInHolders();
447
			}
448
		};
449
	}
450
451
	private interface TagSet<T> {
452
		static <T> MappedRegistry.TagSet<T> unbound() {
453
			return new MappedRegistry.TagSet<T>() {
454
				@Override
455
				public boolean isBound() {
456
					return false;
457
				}
458
459
				@Override
460
				public Optional<HolderSet.Named<T>> get(final TagKey<T> id) {
461
					throw new IllegalStateException("Tags not bound, trying to access " + id);
462
				}
463
464
				@Override
465
				public void forEach(final BiConsumer<? super TagKey<T>, ? super HolderSet.Named<T>> action) {
466
					throw new IllegalStateException("Tags not bound");
467
				}
468
469
				@Override
470
				public Stream<HolderSet.Named<T>> getTags() {
471
					throw new IllegalStateException("Tags not bound");
472
				}
473
			};
474
		}
475
476
		static <T> MappedRegistry.TagSet<T> fromMap(final Map<TagKey<T>, HolderSet.Named<T>> tags) {
477
			return new MappedRegistry.TagSet<T>() {
478
				@Override
479
				public boolean isBound() {
480
					return true;
481
				}
482
483
				@Override
484
				public Optional<HolderSet.Named<T>> get(final TagKey<T> id) {
485
					return Optional.ofNullable(tags.get(id));
486
				}
487
488
				@Override
489
				public void forEach(final BiConsumer<? super TagKey<T>, ? super HolderSet.Named<T>> action) {
490
					tags.forEach(action);
491
				}
492
493
				@Override
494
				public Stream<HolderSet.Named<T>> getTags() {
495
					return tags.values().stream();
496
				}
497
			};
498
		}
499
500
		boolean isBound();
501
502
		Optional<HolderSet.Named<T>> get(TagKey<T> id);
503
504
		void forEach(BiConsumer<? super TagKey<T>, ? super HolderSet.Named<T>> action);
505
506
		Stream<HolderSet.Named<T>> getTags();
507
	}
508
}
509

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