diff --git a/src/main/java/net/minecraftforge/registries/RegistryObject.java b/src/main/java/net/minecraftforge/registries/RegistryObject.java index fb5b3d6452..cf95efe831 100644 --- a/src/main/java/net/minecraftforge/registries/RegistryObject.java +++ b/src/main/java/net/minecraftforge/registries/RegistryObject.java @@ -1,6 +1,6 @@ /* * Minecraft Forge - * Copyright (c) 2016-2021. + * Copyright (c) 2016-2022. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -76,8 +76,9 @@ public final class RegistryObject> impl ObjectHolderRegistry.addHandler(pred -> { if (pred.test(registry.getRegistryName())) - this.value = registry.containsKey(this.name) ? (T)registry.getValue(this.name) : null; + this.updateReference((IForgeRegistry) registry); }); + this.updateReference(((IForgeRegistry) registry)); } @SuppressWarnings("unchecked") @@ -99,9 +100,15 @@ public final class RegistryObject> impl throw new IllegalStateException("Unable to find registry for type " + baseType.getName() + " for mod \"" + modid + "\". Check the 'caused by' to see futher stack.", callerStack); } if (pred.test(registry.getRegistryName())) - RegistryObject.this.value = registry.containsKey(RegistryObject.this.name) ? (T)registry.getValue(RegistryObject.this.name) : null; + RegistryObject.this.updateReference((IForgeRegistry) registry); } }); + IForgeRegistry registry = RegistryManager.ACTIVE.getRegistry(baseType); + // allow registry to be null, this might be for a custom registry that does not exist yet + if (registry != null) + { + this.updateReference(((IForgeRegistry) registry)); + } } /** @@ -117,9 +124,10 @@ public final class RegistryObject> impl return ret; } + @Deprecated(since = "1.18.1") // TODO: make package-private public void updateReference(IForgeRegistry registry) { - this.value = registry.getValue(getId()); + this.value = registry.containsKey(this.name) ? registry.getValue(getId()) : null; } public ResourceLocation getId() diff --git a/src/test/java/net/minecraftforge/debug/RegistryObjectTest.java b/src/test/java/net/minecraftforge/debug/RegistryObjectTest.java new file mode 100644 index 0000000000..e3e40fbfa6 --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/RegistryObjectTest.java @@ -0,0 +1,60 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2022. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.debug; + +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.level.block.Block; +import net.minecraftforge.eventbus.api.IEventBus; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.registries.ForgeRegistries; +import net.minecraftforge.registries.RegistryObject; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +/** + * Checks that {@link RegistryObject} works correctly, specifically that get() functions immediately + * after construction, if registries are already populated. + */ +@Mod(RegistryObjectTest.MODID) +public class RegistryObjectTest +{ + + static final String MODID = "registry_object_test"; + + private static final boolean ENABLED = true; + + private static final Logger LOGGER = LogManager.getLogger(); + + public RegistryObjectTest() + { + if (!ENABLED) return; + + IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus(); + modBus.addListener(this::commonSetup); + } + + public void commonSetup(FMLCommonSetupEvent event) + { + LOGGER.info("Stone 1: {}", RegistryObject.of(new ResourceLocation("minecraft", "stone"), ForgeRegistries.BLOCKS).get()); + LOGGER.info("Stone 2: {}", RegistryObject.of(new ResourceLocation("minecraft", "stone"), Block.class, MODID).get()); + } +} diff --git a/src/test/resources/META-INF/mods.toml b/src/test/resources/META-INF/mods.toml index 30d6a8a5c5..f7d78c7456 100644 --- a/src/test/resources/META-INF/mods.toml +++ b/src/test/resources/META-INF/mods.toml @@ -192,5 +192,7 @@ license="LGPL v2.1" modId="client_command_test" [[mods]] modId="registry_codec_test" +[[mods]] + modId="registry_object_test" # ADD ABOVE THIS LINE