Fix DynamicRegistryManager being initialized too early (#1660)

This commit is contained in:
deirn 2021-08-24 23:44:25 +07:00 committed by GitHub
parent 647b9e3efb
commit c2ece0401d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 17 deletions

View file

@ -17,9 +17,7 @@
package net.fabricmc.fabric.impl.tag.extension; package net.fabricmc.fabric.impl.tag.extension;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.function.Supplier; import java.util.function.Supplier;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
@ -50,7 +48,6 @@ public final class TagFactoryImpl<T> implements TagFactory<T> {
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger();
public static final Map<RegistryKey<? extends Registry<?>>, RequiredTagList<?>> TAG_LISTS = new HashMap<>(); public static final Map<RegistryKey<? extends Registry<?>>, RequiredTagList<?>> TAG_LISTS = new HashMap<>();
public static final Set<RequiredTagList<?>> DYNAMICS = new HashSet<>();
public static <T> TagFactory<T> of(Supplier<TagGroup<T>> tagGroupSupplier) { public static <T> TagFactory<T> of(Supplier<TagGroup<T>> tagGroupSupplier) {
return new TagFactoryImpl<>(tagGroupSupplier); return new TagFactoryImpl<>(tagGroupSupplier);
@ -68,11 +65,6 @@ public final class TagFactoryImpl<T> implements TagFactory<T> {
} else { } else {
tagList = RequiredTagListRegistry.register(registryKey, dataType); tagList = RequiredTagListRegistry.register(registryKey, dataType);
TAG_LISTS.put(registryKey, tagList); TAG_LISTS.put(registryKey, tagList);
// Check whether the registry dynamic.
if (DynamicRegistryManagerAccessor.getInfos().containsKey(registryKey)) {
DYNAMICS.add(tagList);
}
} }
return of(tagList::getGroup); return of(tagList::getGroup);
@ -91,14 +83,16 @@ public final class TagFactoryImpl<T> implements TagFactory<T> {
Stopwatch stopwatch = Stopwatch.createStarted(); Stopwatch stopwatch = Stopwatch.createStarted();
int loadedTags = 0; int loadedTags = 0;
for (RequiredTagList<?> tagList : DYNAMICS) { for (RequiredTagList<?> tagList : TAG_LISTS.values()) {
RegistryKey<? extends Registry<?>> registryKey = tagList.getRegistryKey(); if (isDynamic(tagList)) {
Registry<?> registry = registryManager.get(registryKey); RegistryKey<? extends Registry<?>> registryKey = tagList.getRegistryKey();
TagGroupLoader<?> tagGroupLoader = new TagGroupLoader<>(registry::getOrEmpty, tagList.getDataType()); Registry<?> registry = registryManager.get(registryKey);
TagGroup<?> tagGroup = tagGroupLoader.load(resourceManager); TagGroupLoader<?> tagGroupLoader = new TagGroupLoader<>(registry::getOrEmpty, tagList.getDataType());
((FabricTagManagerHooks) ServerTagManagerHolder.getTagManager()).fabric_addTagGroup(registryKey, tagGroup); TagGroup<?> tagGroup = tagGroupLoader.load(resourceManager);
tagList.updateTagManager(ServerTagManagerHolder.getTagManager()); ((FabricTagManagerHooks) ServerTagManagerHolder.getTagManager()).fabric_addTagGroup(registryKey, tagGroup);
loadedTags += tagGroup.getTags().size(); tagList.updateTagManager(ServerTagManagerHolder.getTagManager());
loadedTags += tagGroup.getTags().size();
}
} }
if (loadedTags > 0) { if (loadedTags > 0) {
@ -106,6 +100,10 @@ public final class TagFactoryImpl<T> implements TagFactory<T> {
} }
} }
public static boolean isDynamic(RequiredTagList<?> tagList) {
return DynamicRegistryManagerAccessor.getInfos().containsKey(tagList.getRegistryKey());
}
private final Supplier<TagGroup<T>> tagGroupSupplier; private final Supplier<TagGroup<T>> tagGroupSupplier;
private TagFactoryImpl(Supplier<TagGroup<T>> tagGroupSupplier) { private TagFactoryImpl(Supplier<TagGroup<T>> tagGroupSupplier) {

View file

@ -37,7 +37,7 @@ public abstract class MixinTagManagerLoader {
@Inject(method = "method_33179", at = @At("HEAD"), cancellable = true) @Inject(method = "method_33179", at = @At("HEAD"), cancellable = true)
private void method_33179(ResourceManager resourceManager, Executor executor, List<?> list, RequiredTagList<?> requiredTagList, CallbackInfo ci) { private void method_33179(ResourceManager resourceManager, Executor executor, List<?> list, RequiredTagList<?> requiredTagList, CallbackInfo ci) {
// Don't load dynamic registry tags now, we need to load them after the dynamic registry. // Don't load dynamic registry tags now, we need to load them after the dynamic registry.
if (TagFactoryImpl.DYNAMICS.contains(requiredTagList)) { if (TagFactoryImpl.isDynamic(requiredTagList)) {
ci.cancel(); ci.cancel();
} }
} }