/* * Copyright (c) Forge Development LLC and contributors * SPDX-License-Identifier: LGPL-2.1-only */ package net.minecraftforge.registries; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import com.google.common.collect.Sets; import com.mojang.serialization.Lifecycle; import net.minecraft.core.RegistrationInfo; import net.minecraft.core.WritableRegistry; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.core.Registry; import net.minecraftforge.fml.ModLoader; import net.minecraftforge.registries.ForgeRegistry.Snapshot; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.ApiStatus; public class RegistryManager { private static final Logger LOGGER = LogManager.getLogger(); public static final RegistryManager ACTIVE = new RegistryManager("ACTIVE"); public static final RegistryManager VANILLA = new RegistryManager("VANILLA"); public static final RegistryManager FROZEN = new RegistryManager("FROZEN"); private static Set vanillaRegistryKeys = Set.of(); BiMap> registries = HashBiMap.create(); private Map> registryView = Collections.unmodifiableMap(registries); private Set persisted = Sets.newHashSet(); private Set synced = Sets.newHashSet(); private Map legacyNames = new HashMap<>(); private final String name; RegistryManager() { this("STAGING"); } public RegistryManager(String name) { this.name = name; } public String getName() { return this.name; } boolean isStaging() { return "STAGING".equals(this.name); } @SuppressWarnings("unchecked") public ForgeRegistry getRegistry(ResourceLocation key) { return (ForgeRegistry)this.registries.get(key); } public ForgeRegistry getRegistry(ResourceKey> key) { return getRegistry(key.location()); } public ResourceLocation getName(IForgeRegistry reg) { return this.registries.inverse().get(reg); } public Map> getRegistries() { return this.registryView; } public static Set getVanillaRegistryKeys() { return vanillaRegistryKeys; } public ResourceLocation updateLegacyName(ResourceLocation legacyName) { ResourceLocation originalName = legacyName; while (getRegistry(legacyName) == null) { legacyName = legacyNames.get(legacyName); if (legacyName == null) return originalName; } return legacyName; } public ForgeRegistry getRegistry(ResourceLocation key, RegistryManager other) { if (!this.registries.containsKey(key)) { ForgeRegistry ot = other.getRegistry(key); if (ot == null) return null; this.registries.put(key, ot.copy(this)); if (other.persisted.contains(key)) this.persisted.add(key); if (other.synced.contains(key)) this.synced.add(key); other.legacyNames.entrySet().stream() .filter(e -> e.getValue().equals(key)) .forEach(e -> addLegacyName(e.getKey(), e.getValue())); } return getRegistry(key); } ForgeRegistry createRegistry(ResourceLocation name, RegistryBuilder builder) { if (registries.containsKey(name)) throw new IllegalArgumentException("Attempted to register a registry for " + name + " but it already exists"); ForgeRegistry reg = new ForgeRegistry(this, name, builder); registries.put(name, reg); if (builder.getSaveToDisc()) this.persisted.add(name); if (builder.getSync()) this.synced.add(name); for (ResourceLocation legacyName : builder.getLegacyNames()) addLegacyName(legacyName, name); return getRegistry(name); } static void registerToRootRegistry(ForgeRegistry forgeReg) { injectForgeRegistry(forgeReg, BuiltInRegistries.REGISTRY); } @SuppressWarnings("unchecked") private static void injectForgeRegistry(ForgeRegistry forgeReg, Registry> rootRegistry) { WritableRegistry> registry = (WritableRegistry>) rootRegistry; Registry wrapper = forgeReg.getWrapper(); if (wrapper != null) registry.register(forgeReg.getRegistryKey(), wrapper, RegistrationInfo.BUILT_IN); } public static void postNewRegistryEvent() { NewRegistryEvent event = new NewRegistryEvent(); DataPackRegistryEvent.NewRegistry dataPackEvent = new DataPackRegistryEvent.NewRegistry(); vanillaRegistryKeys = Set.copyOf(BuiltInRegistries.REGISTRY.keySet()); ModLoader.get().postEventWrapContainerInModOrder(event); ModLoader.get().postEventWrapContainerInModOrder(dataPackEvent); event.fill(); dataPackEvent.process(); } private void addLegacyName(ResourceLocation legacyName, ResourceLocation name) { if (this.legacyNames.containsKey(legacyName)) throw new IllegalArgumentException("Legacy name conflict for registry " + name + ", upgrade path must be linear: " + legacyName); this.legacyNames.put(legacyName, name); } public Map takeSnapshot(boolean savingToDisc) { Map ret = new HashMap<>(); var keys = savingToDisc ? this.persisted : this.synced; keys.forEach(name -> ret.put(name, getRegistry(name).makeSnapshot())); return ret; } //Public for testing only @ApiStatus.Internal public void clean() { this.persisted.clear(); this.synced.clear(); this.registries.clear(); } @ApiStatus.Internal public static List getRegistryNamesForSyncToClient() { return ACTIVE.registries.keySet().stream().filter(ACTIVE.synced::contains).toList(); } }