package ru.dbotthepony.mc.otm.data.loot

import com.mojang.serialization.MapCodec
import com.mojang.serialization.codecs.RecordCodecBuilder
import net.minecraft.util.valueproviders.IntProvider
import net.minecraft.world.level.storage.loot.LootContext
import net.minecraft.world.level.storage.loot.providers.number.LootNumberProviderType
import net.minecraft.world.level.storage.loot.providers.number.NumberProvider
import ru.dbotthepony.mc.otm.registry.data.MLootNumberProviders

class Int2NumberProvider(val parent: IntProvider) : NumberProvider {
	override fun getFloat(context: LootContext): Float {
		return getInt(context).toFloat()
	}

	override fun getInt(context: LootContext): Int {
		return parent.sample(context.random)
	}

	override fun getType(): LootNumberProviderType {
		return MLootNumberProviders.INT_2_NUMBER_TYPE
	}

	companion object {
		val CODEC: MapCodec<Int2NumberProvider> by lazy {
			RecordCodecBuilder.mapCodec {
				it.group(IntProvider.CODEC.fieldOf("value").forGetter(Int2NumberProvider::parent)).apply(it, ::Int2NumberProvider)
			}
		}
	}
}
