|
| 1 | +package dev.spiritstudios.specter.api.registry; |
| 2 | + |
| 3 | +import java.util.concurrent.CompletableFuture; |
| 4 | + |
| 5 | +import net.minecraft.registry.Registry; |
| 6 | +import net.minecraft.registry.RegistryKey; |
| 7 | +import net.minecraft.registry.RegistryWrapper; |
| 8 | + |
| 9 | +import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator; |
| 10 | +import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput; |
| 11 | +import net.fabricmc.fabric.api.datagen.v1.provider.FabricDynamicRegistryProvider; |
| 12 | + |
| 13 | +public abstract class AutomaticDynamicRegistryProvider<T> extends FabricDynamicRegistryProvider { |
| 14 | + private final RegistryKey<Registry<T>> registryKey; |
| 15 | + private final String namespace; |
| 16 | + |
| 17 | + public AutomaticDynamicRegistryProvider(FabricDataOutput output, CompletableFuture<RegistryWrapper.WrapperLookup> registriesFuture, RegistryKey<Registry<T>> registryKey, String namespace) { |
| 18 | + super(output, registriesFuture); |
| 19 | + this.registryKey = registryKey; |
| 20 | + this.namespace = namespace; |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + protected void configure(RegistryWrapper.WrapperLookup wrapperLookup, Entries entries) { |
| 25 | + RegistryWrapper<T> wrapper = wrapperLookup.getOrThrow(registryKey); |
| 26 | + |
| 27 | + wrapper.streamKeys() |
| 28 | + .filter(key -> key.getValue().getNamespace().equals(namespace)) |
| 29 | + .forEach(key -> entries.add(key, wrapper.getOrThrow(key).value())); |
| 30 | + } |
| 31 | + |
| 32 | + public static <T> FabricDataGenerator.Pack.RegistryDependentFactory<AutomaticDynamicRegistryProvider<T>> factory(RegistryKey<Registry<T>> registryKey, String namespace) { |
| 33 | + return (output, registriesFuture) -> new AutomaticDynamicRegistryProvider<>(output, registriesFuture, registryKey, namespace) { |
| 34 | + @Override |
| 35 | + public String getName() { |
| 36 | + return "Dynamic Registry Entries for " + registryKey.getValue(); |
| 37 | + } |
| 38 | + }; |
| 39 | + } |
| 40 | +} |
0 commit comments