diff --git a/common/src/main/java/dev/architectury/registry/registries/RegistrarManager.java b/common/src/main/java/dev/architectury/registry/registries/RegistrarManager.java index cf5a8aa6..4fd91952 100644 --- a/common/src/main/java/dev/architectury/registry/registries/RegistrarManager.java +++ b/common/src/main/java/dev/architectury/registry/registries/RegistrarManager.java @@ -78,6 +78,12 @@ public final RegistrarBuilder builder(ResourceLocation registryId, T... t return this.provider.builder((Class) typeGetter.getClass().getComponentType(), registryId); } + @SafeVarargs + public final RegistrarBuilder builderDefaulted(ResourceLocation registryId, ResourceLocation defaultId, T... typeGetter) { + if (typeGetter.length != 0) throw new IllegalStateException("array must be empty!"); + return this.provider.builderDefaulted((Class) typeGetter.getClass().getComponentType(), registryId, defaultId); + } + /** * Registers a non-synced dynamic registry. * @@ -174,6 +180,8 @@ public interface RegistryProvider { RegistrarBuilder builder(Class type, ResourceLocation registryId); + RegistrarBuilder builderDefaulted(Class type, ResourceLocation registryId, ResourceLocation defaultId); + void registerDynamicRegistry(ResourceKey> key, Codec dataCodec); void registerDynamicRegistrySynced(ResourceKey> key, Codec dataCodec, Codec networkCodec); diff --git a/fabric/src/main/java/dev/architectury/registry/registries/fabric/RegistrarManagerImpl.java b/fabric/src/main/java/dev/architectury/registry/registries/fabric/RegistrarManagerImpl.java index 177ad7a2..4551901a 100644 --- a/fabric/src/main/java/dev/architectury/registry/registries/fabric/RegistrarManagerImpl.java +++ b/fabric/src/main/java/dev/architectury/registry/registries/fabric/RegistrarManagerImpl.java @@ -97,6 +97,11 @@ public RegistrarBuilder builder(Class type, ResourceLocation registryI return new RegistrarBuilderWrapper<>(modId, FabricRegistryBuilder.createSimple(type, registryId)); } + @Override + public RegistrarBuilder builderDefaulted(Class type, ResourceLocation registryId, ResourceLocation defaultId) { + return new RegistrarBuilderWrapper<>(modId, FabricRegistryBuilder.createDefaulted(type, registryId, defaultId)); + } + @Override public void registerDynamicRegistry(ResourceKey> key, Codec dataCodec) { DynamicRegistries.register(key, dataCodec); @@ -133,9 +138,9 @@ public int hashCode() { public static class RegistrarBuilderWrapper implements RegistrarBuilder { private final String modId; - private FabricRegistryBuilder> builder; + private FabricRegistryBuilder> builder; - public RegistrarBuilderWrapper(String modId, FabricRegistryBuilder> builder) { + public RegistrarBuilderWrapper(String modId, FabricRegistryBuilder> builder) { this.modId = modId; this.builder = builder; } diff --git a/minecraftforge/src/main/java/dev/architectury/registry/registries/forge/RegistrarManagerImpl.java b/minecraftforge/src/main/java/dev/architectury/registry/registries/forge/RegistrarManagerImpl.java index ace409f5..937f02c8 100644 --- a/minecraftforge/src/main/java/dev/architectury/registry/registries/forge/RegistrarManagerImpl.java +++ b/minecraftforge/src/main/java/dev/architectury/registry/registries/forge/RegistrarManagerImpl.java @@ -181,6 +181,12 @@ public RegistrarBuilder builder(Class type, ResourceLocation registryI .setName(registryId), registryId); } + @Override + public RegistrarBuilder builderDefaulted(Class type, ResourceLocation registryId, ResourceLocation defaultId) { + return new RegistryBuilderWrapper<>(this, new net.minecraftforge.registries.RegistryBuilder<>() + .setName(registryId).setDefaultKey(defaultId), registryId); + } + @Override public void registerDynamicRegistry(ResourceKey> key, Codec dataCodec) { if (newDynamicRegistries == null) { diff --git a/neoforge/src/main/java/dev/architectury/registry/registries/forge/RegistrarManagerImpl.java b/neoforge/src/main/java/dev/architectury/registry/registries/forge/RegistrarManagerImpl.java index d51fdc8b..faaa03cf 100644 --- a/neoforge/src/main/java/dev/architectury/registry/registries/forge/RegistrarManagerImpl.java +++ b/neoforge/src/main/java/dev/architectury/registry/registries/forge/RegistrarManagerImpl.java @@ -143,6 +143,11 @@ public RegistrarBuilder builder(Class type, ResourceLocation registryI return new RegistryBuilderWrapper<>(this, new RegistryBuilder<>(ResourceKey.createRegistryKey(registryId))); } + @Override + public RegistrarBuilder builderDefaulted(Class type, ResourceLocation registryId, ResourceLocation defaultId) { + return new RegistryBuilderWrapper<>(this, new RegistryBuilder(ResourceKey.createRegistryKey(registryId)).defaultKey(defaultId)); + } + @Override public void registerDynamicRegistry(ResourceKey> key, Codec dataCodec) { if (newDynamicRegistries == null) { diff --git a/testmod-common/src/main/java/dev/architectury/test/registry/TestRegistries.java b/testmod-common/src/main/java/dev/architectury/test/registry/TestRegistries.java index bd72bc84..dbd6ffbc 100644 --- a/testmod-common/src/main/java/dev/architectury/test/registry/TestRegistries.java +++ b/testmod-common/src/main/java/dev/architectury/test/registry/TestRegistries.java @@ -75,9 +75,18 @@ public static final class TestInt { public TestInt(int value) { this.value = value; } + + @Override + public String toString() { + return Integer.toString(value); + } } - public static final Registrar INTS = RegistrarManager.get(TestMod.MOD_ID).builder(new ResourceLocation(TestMod.MOD_ID, "ints")) + public static final Registrar INTS = RegistrarManager.get(TestMod.MOD_ID) + .builderDefaulted( + new ResourceLocation(TestMod.MOD_ID, "ints"), + new ResourceLocation(TestMod.MOD_ID, "test_no_int") + ) .syncToClients() .build(); public static final DeferredRegister TABS = DeferredRegister.create(TestMod.MOD_ID, Registries.CREATIVE_MODE_TAB); @@ -101,6 +110,7 @@ public TestInt(int value) { .bucketItemSupplier(() -> TestRegistries.TEST_FLUID_BUCKET) .color(0xFF0000); + public static final RegistrySupplier TEST_NO_INT = INTS.register(new ResourceLocation(TestMod.MOD_ID, "test_no_int"), () -> new TestInt(0)); public static final RegistrySupplier TEST_INT = INTS.register(new ResourceLocation(TestMod.MOD_ID, "test_int"), () -> new TestInt(1)); public static final RegistrySupplier TEST_INT_2 = INTS.register(new ResourceLocation(TestMod.MOD_ID, "test_int_2"), () -> new TestInt(2)); @@ -223,5 +233,8 @@ public static void initialize() { TEST_RECIPE_TYPE.listen(type -> { System.out.println("Registered recipe type!"); }); + INTS.listen(TEST_INT_2, i -> { + System.out.println("Non-existent int: " + INTS.get(new ResourceLocation("non_existent"))); + }); } }