Support creating FabricRegistryBuilder with a RegistryKey, deprecate Class based APIs. (#2905)

* Fix #846

* Update javadoc.

(cherry picked from commit f7b4d36421)
This commit is contained in:
modmuss50 2023-02-23 10:13:19 +00:00
parent e7471eb7e4
commit a383ab9763
2 changed files with 39 additions and 6 deletions

View file

@ -20,21 +20,25 @@ import java.util.EnumSet;
import com.mojang.serialization.Lifecycle;
import net.minecraft.util.Identifier;
import net.minecraft.registry.DefaultedRegistry;
import net.minecraft.registry.MutableRegistry;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.SimpleDefaultedRegistry;
import net.minecraft.registry.SimpleRegistry;
import net.minecraft.util.Identifier;
import net.fabricmc.fabric.mixin.registry.sync.RegistriesAccessor;
/**
* Used to create custom registries, with specified registry attributes.
*
* <p>See the following example for creating a {@link Registry} of String objects.
*
* <pre>
* {@code
* SimpleRegistry<String> registry = FabricRegistryBuilder.createSimple(String.class, new Identifier("registry_sync", "fabric_registry"))
* RegistryKey<Registry<String>> registryKey = RegistryKey.ofRegistry(new Identifier("modid", "registry_name"));
* Registry<String> registry = FabricRegistryBuilder.createSimple(registryKey)
* .attribute(RegistryAttribute.SYNCED)
* .buildAndRegister();
* }
@ -59,12 +63,37 @@ public final class FabricRegistryBuilder<T, R extends MutableRegistry<T>> {
/**
* Create a new {@link FabricRegistryBuilder} using a {@link SimpleRegistry}, the registry has the {@link RegistryAttribute#MODDED} attribute by default.
*
* @param registryId The registry {@link Identifier} used as the registry id
* @param registryKey The registry {@link RegistryKey}
* @param <T> The type stored in the Registry
* @return An instance of FabricRegistryBuilder
*/
public static <T> FabricRegistryBuilder<T, SimpleRegistry<T>> createSimple(RegistryKey<Registry<T>> registryKey) {
return from(new SimpleRegistry<>(registryKey, Lifecycle.stable(), false));
}
/**
* Create a new {@link FabricRegistryBuilder} using a {@link DefaultedRegistry}, the registry has the {@link RegistryAttribute#MODDED} attribute by default.
*
* @param registryKey The registry {@link RegistryKey}
* @param defaultId The default registry id
* @param <T> The type stored in the Registry
* @return An instance of FabricRegistryBuilder
*/
public static <T> FabricRegistryBuilder<T, SimpleDefaultedRegistry<T>> createDefaulted(RegistryKey<Registry<T>> registryKey, Identifier defaultId) {
return from(new SimpleDefaultedRegistry<T>(defaultId.toString(), registryKey, Lifecycle.stable(), false));
}
/**
* Create a new {@link FabricRegistryBuilder} using a {@link SimpleRegistry}, the registry has the {@link RegistryAttribute#MODDED} attribute by default.
*
* @param registryId The registry {@link Identifier} used as the registry id
* @param <T> The type stored in the Registry
* @return An instance of FabricRegistryBuilder
* @deprecated Please migrate to {@link FabricRegistryBuilder#createSimple(RegistryKey)}
*/
@Deprecated
public static <T> FabricRegistryBuilder<T, SimpleRegistry<T>> createSimple(Class<T> type, Identifier registryId) {
return from(new SimpleRegistry<T>(RegistryKey.ofRegistry(registryId), Lifecycle.stable(), false));
return createSimple(RegistryKey.ofRegistry(registryId));
}
/**
@ -74,9 +103,11 @@ public final class FabricRegistryBuilder<T, R extends MutableRegistry<T>> {
* @param defaultId The default registry id
* @param <T> The type stored in the Registry
* @return An instance of FabricRegistryBuilder
* @deprecated Please migrate to {@link FabricRegistryBuilder#createDefaulted(RegistryKey, Identifier)}
*/
@Deprecated
public static <T> FabricRegistryBuilder<T, SimpleDefaultedRegistry<T>> createDefaulted(Class<T> type, Identifier registryId, Identifier defaultId) {
return from(new SimpleDefaultedRegistry<T>(defaultId.toString(), RegistryKey.ofRegistry(registryId), Lifecycle.stable(), false));
return createDefaulted(RegistryKey.ofRegistry(registryId), defaultId);
}
private final R registry;

View file

@ -31,6 +31,7 @@ import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.SimpleRegistry;
import net.minecraft.util.Identifier;
@ -97,7 +98,8 @@ public class RegistrySyncTest implements ModInitializer {
}
}
SimpleRegistry<String> fabricRegistry = FabricRegistryBuilder.createSimple(String.class, new Identifier("registry_sync", "fabric_registry"))
RegistryKey<Registry<String>> fabricRegistryKey = RegistryKey.ofRegistry(new Identifier("registry_sync", "fabric_registry"));
SimpleRegistry<String> fabricRegistry = FabricRegistryBuilder.createSimple(fabricRegistryKey)
.attribute(RegistryAttribute.SYNCED)
.buildAndRegister();