Fix RegistryObject not working if created after registry events (#8383)

This commit is contained in:
Take Weiland authored and GitHub committed 2022-02-08 13:05:38 -08:00
1 parent 2765c0d8d9
commit b25d9562f2
3 files changed
+74 -4

No files matched your search

@@ -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<T extends IForgeRegistryEntry<? super T>> impl
ObjectHolderRegistry.addHandler(pred ->
{
if (pred.test(registry.getRegistryName()))
this.value = registry.containsKey(this.name) ? (T)registry.getValue(this.name) : null;
this.updateReference((IForgeRegistry<? extends T>) registry);
});
this.updateReference(((IForgeRegistry<? extends T>) registry));
}
@SuppressWarnings("unchecked")
@@ -99,9 +100,15 @@ public final class RegistryObject<T extends IForgeRegistryEntry<? super T>> 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<? extends T>) registry);
}
});
IForgeRegistry<V> 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<? extends T>) registry));
}
}
/**
@@ -117,9 +124,10 @@ public final class RegistryObject<T extends IForgeRegistryEntry<? super T>> impl
return ret;
}
@Deprecated(since = "1.18.1") // TODO: make package-private
public void updateReference(IForgeRegistry<? extends T> registry)
{
this.value = registry.getValue(getId());
this.value = registry.containsKey(this.name) ? registry.getValue(getId()) : null;
}
public ResourceLocation getId()
@@ -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());
}
}
+2
View File
@@ -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