Co-authored-by: Max <maxh2709@gmail.com>
This commit is contained in:
Josiah Glosson
2024-01-09 15:11:07 -06:00
committed by GitHub
parent e986c607c3
commit 3483bc44e3
5 changed files with 40 additions and 3 deletions

View File

@@ -78,6 +78,12 @@ public final class RegistrarManager {
return this.provider.builder((Class<T>) typeGetter.getClass().getComponentType(), registryId);
}
@SafeVarargs
public final <T> RegistrarBuilder<T> builderDefaulted(ResourceLocation registryId, ResourceLocation defaultId, T... typeGetter) {
if (typeGetter.length != 0) throw new IllegalStateException("array must be empty!");
return this.provider.builderDefaulted((Class<T>) typeGetter.getClass().getComponentType(), registryId, defaultId);
}
/**
* Registers a non-synced dynamic registry.
*
@@ -174,6 +180,8 @@ public final class RegistrarManager {
<T> RegistrarBuilder<T> builder(Class<T> type, ResourceLocation registryId);
<T> RegistrarBuilder<T> builderDefaulted(Class<T> type, ResourceLocation registryId, ResourceLocation defaultId);
<T> void registerDynamicRegistry(ResourceKey<Registry<T>> key, Codec<T> dataCodec);
<T> void registerDynamicRegistrySynced(ResourceKey<Registry<T>> key, Codec<T> dataCodec, Codec<T> networkCodec);

View File

@@ -97,6 +97,11 @@ public class RegistrarManagerImpl {
return new RegistrarBuilderWrapper<>(modId, FabricRegistryBuilder.createSimple(type, registryId));
}
@Override
public <T> RegistrarBuilder<T> builderDefaulted(Class<T> type, ResourceLocation registryId, ResourceLocation defaultId) {
return new RegistrarBuilderWrapper<>(modId, FabricRegistryBuilder.createDefaulted(type, registryId, defaultId));
}
@Override
public <T> void registerDynamicRegistry(ResourceKey<Registry<T>> key, Codec<T> dataCodec) {
DynamicRegistries.register(key, dataCodec);
@@ -133,9 +138,9 @@ public class RegistrarManagerImpl {
public static class RegistrarBuilderWrapper<T> implements RegistrarBuilder<T> {
private final String modId;
private FabricRegistryBuilder<T, MappedRegistry<T>> builder;
private FabricRegistryBuilder<T, ? extends MappedRegistry<T>> builder;
public RegistrarBuilderWrapper(String modId, FabricRegistryBuilder<T, MappedRegistry<T>> builder) {
public RegistrarBuilderWrapper(String modId, FabricRegistryBuilder<T, ? extends MappedRegistry<T>> builder) {
this.modId = modId;
this.builder = builder;
}

View File

@@ -181,6 +181,12 @@ public class RegistrarManagerImpl {
.setName(registryId), registryId);
}
@Override
public <T> RegistrarBuilder<T> builderDefaulted(Class<T> type, ResourceLocation registryId, ResourceLocation defaultId) {
return new RegistryBuilderWrapper<>(this, new net.minecraftforge.registries.RegistryBuilder<>()
.setName(registryId).setDefaultKey(defaultId), registryId);
}
@Override
public <T> void registerDynamicRegistry(ResourceKey<Registry<T>> key, Codec<T> dataCodec) {
if (newDynamicRegistries == null) {

View File

@@ -143,6 +143,11 @@ public class RegistrarManagerImpl {
return new RegistryBuilderWrapper<>(this, new RegistryBuilder<>(ResourceKey.createRegistryKey(registryId)));
}
@Override
public <T> RegistrarBuilder<T> builderDefaulted(Class<T> type, ResourceLocation registryId, ResourceLocation defaultId) {
return new RegistryBuilderWrapper<>(this, new RegistryBuilder<T>(ResourceKey.createRegistryKey(registryId)).defaultKey(defaultId));
}
@Override
public <T> void registerDynamicRegistry(ResourceKey<Registry<T>> key, Codec<T> dataCodec) {
if (newDynamicRegistries == null) {

View File

@@ -75,9 +75,18 @@ public class TestRegistries {
public TestInt(int value) {
this.value = value;
}
@Override
public String toString() {
return Integer.toString(value);
}
}
public static final Registrar<TestInt> INTS = RegistrarManager.get(TestMod.MOD_ID).<TestInt>builder(new ResourceLocation(TestMod.MOD_ID, "ints"))
public static final Registrar<TestInt> INTS = RegistrarManager.get(TestMod.MOD_ID)
.<TestInt>builderDefaulted(
new ResourceLocation(TestMod.MOD_ID, "ints"),
new ResourceLocation(TestMod.MOD_ID, "test_no_int")
)
.syncToClients()
.build();
public static final DeferredRegister<CreativeModeTab> TABS = DeferredRegister.create(TestMod.MOD_ID, Registries.CREATIVE_MODE_TAB);
@@ -101,6 +110,7 @@ public class TestRegistries {
.bucketItemSupplier(() -> TestRegistries.TEST_FLUID_BUCKET)
.color(0xFF0000);
public static final RegistrySupplier<TestInt> TEST_NO_INT = INTS.register(new ResourceLocation(TestMod.MOD_ID, "test_no_int"), () -> new TestInt(0));
public static final RegistrySupplier<TestInt> TEST_INT = INTS.register(new ResourceLocation(TestMod.MOD_ID, "test_int"), () -> new TestInt(1));
public static final RegistrySupplier<TestInt> TEST_INT_2 = INTS.register(new ResourceLocation(TestMod.MOD_ID, "test_int_2"), () -> new TestInt(2));
@@ -223,5 +233,8 @@ public class TestRegistries {
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")));
});
}
}