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
import com.mojang.serialization.JsonOps
import net.minecraft.core.Holder
import net.minecraft.core.HolderOwner
import net.minecraft.core.RegistrySetBuilder
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.ResourceLocation
import net.minecraft.server.packs.PackType
import net.minecraft.tags.BlockTags
import net.minecraft.world.level.levelgen.VerticalAnchor
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.PlacedFeature
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 ru.dbotthepony.mc.otm.registry.MBlocks
private object Owner : HolderOwner<Nothing> {
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 val oreKey by lazy { ResourceKey.create(Registries.CONFIGURED_FEATURE, modLocation("tritanium_ore")) }
private data class Both<T>(val key: Holder<T>, val value: Holder<T>)
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) {
private fun configuredFeatures(context: BootstapContext<ConfiguredFeature<*, *>>) {
val stone = TagMatchTest(BlockTags.STONE_ORE_REPLACEABLES)
val deepslate = TagMatchTest(BlockTags.DEEPSLATE_ORE_REPLACEABLES)
val target by lazy {
listOf(
OreConfiguration.target(stone, MBlocks.TRITANIUM_ORE.defaultBlockState()),
OreConfiguration.target(deepslate, MBlocks.DEEPSLATE_TRITANIUM_ORE.defaultBlockState()),
)
}
val target = listOf(
OreConfiguration.target(stone, MBlocks.TRITANIUM_ORE.defaultBlockState()),
OreConfiguration.target(deepslate, MBlocks.DEEPSLATE_TRITANIUM_ORE.defaultBlockState()),
)
val ops = RegistryOps.create(JsonOps.INSTANCE, event.lookupProvider.join())
val tritanium = createHolders(ResourceKey.create(Registries.CONFIGURED_FEATURE, modLocation("ore_tritanium")), ConfiguredFeature(Feature.ORE, OreConfiguration(target, 9)))
val configuredFeatures = remap(tritanium)
context.register(oreKey, ConfiguredFeature(Feature.ORE, OreConfiguration(target, 9)))
}
event.generator.addProvider(event.includeServer(), JsonCodecProvider(
event.generator.packOutput, event.existingFileHelper, DataGen.MOD_ID, ops,
PackType.SERVER_DATA, "worldgen/configured_feature", ConfiguredFeature.CODEC, configuredFeatures
))
private fun placedFeatures(context: BootstapContext<PlacedFeature>) {
fun location(name: String) = ResourceKey.create(Registries.PLACED_FEATURE, modLocation(name))
val placedTritanium = createHolders(ResourceKey.create(Registries.PLACED_FEATURE, modLocation("normal_tritanium")), PlacedFeature(
tritanium.key,
val configured = context.lookup(Registries.CONFIGURED_FEATURE)
val ore = configured.getOrThrow(oreKey)
context.register(location("normal_tritanium"), PlacedFeature(
ore,
listOf(
CountPlacement.of(8),
InSquarePlacement.spread(),
@ -75,19 +47,20 @@ fun registerOreGen(event: GatherDataEvent) {
)
))
val placedTritaniumDeep = createHolders(ResourceKey.create(Registries.PLACED_FEATURE, modLocation("deep_tritanium")), PlacedFeature(
tritanium.key,
context.register(location("deep_tritanium"), PlacedFeature(
ore,
listOf(
CountPlacement.of(10),
InSquarePlacement.spread(),
HeightRangePlacement.uniform(VerticalAnchor.aboveBottom(8), VerticalAnchor.absolute(0))
)
))
val placedFeatures = remap(placedTritanium, placedTritaniumDeep)
event.generator.addProvider(event.includeServer(), JsonCodecProvider(
event.generator.packOutput, event.existingFileHelper, DataGen.MOD_ID, ops,
PackType.SERVER_DATA, "worldgen/placed_feature", PlacedFeature.CODEC, placedFeatures
))
}
fun registerOreGen(event: GatherDataEvent) {
val set = RegistrySetBuilder()
.add(Registries.CONFIGURED_FEATURE, ::configuredFeatures)
.add(Registries.PLACED_FEATURE, ::placedFeatures)
event.generator.addProvider(event.includeServer(), DatapackBuiltinEntriesProvider(event.generator.packOutput, event.lookupProvider, set, setOf(DataGen.MOD_ID)))
}