From 90dd782bcf851a29fe9b59ce77206ca8349e2b4f Mon Sep 17 00:00:00 2001 From: DBotThePony <dbotthepony@yandex.ru> Date: Fri, 13 Jan 2023 18:56:17 +0700 Subject: [PATCH] Reference forge registries directly, when possible --- .../mc/otm/datagen/tags/TagsProvider.kt | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/tags/TagsProvider.kt b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/tags/TagsProvider.kt index 8e3ce80e7..3f6c10c52 100644 --- a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/tags/TagsProvider.kt +++ b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/tags/TagsProvider.kt @@ -16,6 +16,7 @@ import net.minecraft.world.item.Tiers import net.minecraft.world.level.block.Block import net.minecraftforge.data.event.GatherDataEvent import net.minecraftforge.registries.ForgeRegistries +import net.minecraftforge.registries.IForgeRegistry import ru.dbotthepony.mc.otm.capability.MatteryPlayerCapability import ru.dbotthepony.mc.otm.datagen.DataGen import java.util.stream.Stream @@ -60,10 +61,18 @@ interface ForgeTagAppender<T> : MTagAppender<T> { } } +private fun <T> vanillaLookup(key: ResourceKey<Registry<T>>): (T) -> ResourceLocation? { + val registry by lazy { (BuiltInRegistries.REGISTRY.get(key.location()) ?: throw NoSuchElementException("No such registry $key")) as Registry<T> } + return { registry.getKey(it) } +} + class TagsProvider( private val event: GatherDataEvent ) { - inner class Delegate<T>(registry: ResourceKey<Registry<T>>) : MinecraftTagsProvider<T>(event.generator.packOutput, registry, event.lookupProvider, DataGen.MOD_ID, event.existingFileHelper) { + inner class Delegate<T> private constructor(key: ResourceKey<Registry<T>>, val lookup: (T) -> ResourceLocation?) : MinecraftTagsProvider<T>(event.generator.packOutput, key, event.lookupProvider, DataGen.MOD_ID, event.existingFileHelper) { + constructor(registry: IForgeRegistry<T>) : this(registry.registryKey, registry::getKey) + constructor(key: ResourceKey<Registry<T>>) : this(key, vanillaLookup(key)) + init { event.generator.addProvider(true, this) } @@ -75,12 +84,10 @@ class TagsProvider( return } - val registry = (BuiltInRegistries.REGISTRY.get(registryKey.location()) ?: throw NoSuchElementException("No such registry $registryKey")) as Registry<T> - for ((tag, values) in tags) { tag(tag).also { for (value in values) { - it.add(ResourceKey.create(registryKey, registry.getKey(value) ?: throw NoSuchElementException("$value is missing from $registry"))) + it.add(ResourceKey.create(registryKey, lookup(value) ?: throw NoSuchElementException("$value is missing from $registryKey"))) } } } @@ -176,9 +183,9 @@ class TagsProvider( } } - val blocks = Delegate(Registries.BLOCK) - val items = Delegate(Registries.ITEM) - val mobEffects = Delegate(Registries.MOB_EFFECT) + val blocks = Delegate(ForgeRegistries.BLOCKS) + val items = Delegate(ForgeRegistries.ITEMS) + val mobEffects = Delegate(ForgeRegistries.MOB_EFFECTS) val androidImmuneEffects = mobEffects.appender(MatteryPlayerCapability.ANDROID_IMMUNE_EFFECTS)