Skip to content

Commit

Permalink
Resolve #470 (#471)
Browse files Browse the repository at this point in the history
Co-authored-by: Max <[email protected]>
  • Loading branch information
Gaming32 and MaxNeedsSnacks authored Jan 9, 2024
1 parent e986c60 commit 3483bc4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public final <T> RegistrarBuilder<T> builder(ResourceLocation registryId, T... t
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.
*
Expand Down Expand Up @@ -174,6 +180,8 @@ public interface RegistryProvider {

<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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public <T> RegistrarBuilder<T> builder(Class<T> type, ResourceLocation registryI
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);
Expand Down Expand Up @@ -133,9 +138,9 @@ public int hashCode() {

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ public <T> RegistrarBuilder<T> builder(Class<T> type, ResourceLocation registryI
.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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ public <T> RegistrarBuilder<T> builder(Class<T> type, ResourceLocation registryI
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<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);
Expand All @@ -101,6 +110,7 @@ public TestInt(int value) {
.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));

Expand Down Expand Up @@ -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")));
});
}
}

0 comments on commit 3483bc4

Please sign in to comment.