Proper ore json datagen

This commit is contained in:
DBotThePony 2023-01-13 22:33:57 +07:00
parent 4c419aaf35
commit fe91b184a6
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -1,13 +1,9 @@
package ru.dbotthepony.mc.otm.datagen package ru.dbotthepony.mc.otm.datagen
import com.mojang.serialization.JsonOps import net.minecraft.core.RegistrySetBuilder
import net.minecraft.core.Holder
import net.minecraft.core.HolderOwner
import net.minecraft.core.registries.Registries import net.minecraft.core.registries.Registries
import net.minecraft.resources.RegistryOps import net.minecraft.data.worldgen.BootstapContext
import net.minecraft.resources.ResourceKey import net.minecraft.resources.ResourceKey
import net.minecraft.resources.ResourceLocation
import net.minecraft.server.packs.PackType
import net.minecraft.tags.BlockTags import net.minecraft.tags.BlockTags
import net.minecraft.world.level.levelgen.VerticalAnchor import net.minecraft.world.level.levelgen.VerticalAnchor
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature import net.minecraft.world.level.levelgen.feature.ConfiguredFeature
@ -18,56 +14,32 @@ import net.minecraft.world.level.levelgen.placement.HeightRangePlacement
import net.minecraft.world.level.levelgen.placement.InSquarePlacement import net.minecraft.world.level.levelgen.placement.InSquarePlacement
import net.minecraft.world.level.levelgen.placement.PlacedFeature import net.minecraft.world.level.levelgen.placement.PlacedFeature
import net.minecraft.world.level.levelgen.structure.templatesystem.TagMatchTest import net.minecraft.world.level.levelgen.structure.templatesystem.TagMatchTest
import net.minecraftforge.common.data.JsonCodecProvider import net.minecraftforge.common.data.DatapackBuiltinEntriesProvider
import net.minecraftforge.data.event.GatherDataEvent import net.minecraftforge.data.event.GatherDataEvent
import ru.dbotthepony.mc.otm.registry.MBlocks import ru.dbotthepony.mc.otm.registry.MBlocks
private object Owner : HolderOwner<Nothing> { private val oreKey by lazy { ResourceKey.create(Registries.CONFIGURED_FEATURE, modLocation("tritanium_ore")) }
override fun canSerializeIn(p_255875_: HolderOwner<Nothing>): Boolean {
return true // because we can always reference things, even if they are not "registered" at this moment
}
}
private data class Both<T>(val key: Holder<T>, val value: Holder<T>) private fun configuredFeatures(context: BootstapContext<ConfiguredFeature<*, *>>) {
private fun <T> createHolders(key: ResourceKey<T>, value: T): Both<T> {
val keyOnly = Holder.Reference.createStandAlone(Owner as HolderOwner<T>, key)
val valueOnly = Holder.direct(value)
return Both(keyOnly, valueOnly)
}
private fun <T> remap(vararg input: Both<T>): Map<ResourceLocation, Holder<T>> {
val result = LinkedHashMap<ResourceLocation, Holder<T>>()
for (value in input) {
result[value.key.unwrapKey().get().location()] = value.value
}
return result
}
fun registerOreGen(event: GatherDataEvent) {
val stone = TagMatchTest(BlockTags.STONE_ORE_REPLACEABLES) val stone = TagMatchTest(BlockTags.STONE_ORE_REPLACEABLES)
val deepslate = TagMatchTest(BlockTags.DEEPSLATE_ORE_REPLACEABLES) val deepslate = TagMatchTest(BlockTags.DEEPSLATE_ORE_REPLACEABLES)
val target by lazy { val target = listOf(
listOf( OreConfiguration.target(stone, MBlocks.TRITANIUM_ORE.defaultBlockState()),
OreConfiguration.target(stone, MBlocks.TRITANIUM_ORE.defaultBlockState()), OreConfiguration.target(deepslate, MBlocks.DEEPSLATE_TRITANIUM_ORE.defaultBlockState()),
OreConfiguration.target(deepslate, MBlocks.DEEPSLATE_TRITANIUM_ORE.defaultBlockState()), )
)
}
val ops = RegistryOps.create(JsonOps.INSTANCE, event.lookupProvider.join()) context.register(oreKey, ConfiguredFeature(Feature.ORE, OreConfiguration(target, 9)))
val tritanium = createHolders(ResourceKey.create(Registries.CONFIGURED_FEATURE, modLocation("ore_tritanium")), ConfiguredFeature(Feature.ORE, OreConfiguration(target, 9))) }
val configuredFeatures = remap(tritanium)
event.generator.addProvider(event.includeServer(), JsonCodecProvider( private fun placedFeatures(context: BootstapContext<PlacedFeature>) {
event.generator.packOutput, event.existingFileHelper, DataGen.MOD_ID, ops, fun location(name: String) = ResourceKey.create(Registries.PLACED_FEATURE, modLocation(name))
PackType.SERVER_DATA, "worldgen/configured_feature", ConfiguredFeature.CODEC, configuredFeatures
))
val placedTritanium = createHolders(ResourceKey.create(Registries.PLACED_FEATURE, modLocation("normal_tritanium")), PlacedFeature( val configured = context.lookup(Registries.CONFIGURED_FEATURE)
tritanium.key, val ore = configured.getOrThrow(oreKey)
context.register(location("normal_tritanium"), PlacedFeature(
ore,
listOf( listOf(
CountPlacement.of(8), CountPlacement.of(8),
InSquarePlacement.spread(), InSquarePlacement.spread(),
@ -75,19 +47,20 @@ fun registerOreGen(event: GatherDataEvent) {
) )
)) ))
val placedTritaniumDeep = createHolders(ResourceKey.create(Registries.PLACED_FEATURE, modLocation("deep_tritanium")), PlacedFeature( context.register(location("deep_tritanium"), PlacedFeature(
tritanium.key, ore,
listOf( listOf(
CountPlacement.of(10), CountPlacement.of(10),
InSquarePlacement.spread(), InSquarePlacement.spread(),
HeightRangePlacement.uniform(VerticalAnchor.aboveBottom(8), VerticalAnchor.absolute(0)) HeightRangePlacement.uniform(VerticalAnchor.aboveBottom(8), VerticalAnchor.absolute(0))
) )
)) ))
}
val placedFeatures = remap(placedTritanium, placedTritaniumDeep)
fun registerOreGen(event: GatherDataEvent) {
event.generator.addProvider(event.includeServer(), JsonCodecProvider( val set = RegistrySetBuilder()
event.generator.packOutput, event.existingFileHelper, DataGen.MOD_ID, ops, .add(Registries.CONFIGURED_FEATURE, ::configuredFeatures)
PackType.SERVER_DATA, "worldgen/placed_feature", PlacedFeature.CODEC, placedFeatures .add(Registries.PLACED_FEATURE, ::placedFeatures)
))
event.generator.addProvider(event.includeServer(), DatapackBuiltinEntriesProvider(event.generator.packOutput, event.lookupProvider, set, setOf(DataGen.MOD_ID)))
} }