package ru.dbotthepony.mc.otm.triggers

import com.mojang.serialization.Codec
import com.mojang.serialization.codecs.RecordCodecBuilder
import net.minecraft.advancements.critereon.ContextAwarePredicate
import net.minecraft.advancements.critereon.MinMaxBounds.Doubles
import net.minecraft.server.level.ServerPlayer
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.core.ResourceLocation
import java.util.*

object NanobotsArmorTrigger : MCriterionTrigger<NanobotsArmorTrigger.Instance>(ResourceLocation(OverdriveThatMatters.MOD_ID, "nanobots_armor")) {
	override val codec: Codec<Instance> = RecordCodecBuilder.create {
		it.group(
			Doubles.CODEC.fieldOf("predicate").forGetter(Instance::predicate),
			ContextAwarePredicate.CODEC.optionalFieldOf("player").forGetter(Instance::playerPredicate),
		).apply(it, ::Instance)
	}

	fun trigger(player: ServerPlayer, damageAbsorbed: Double) {
		trigger(player) { it.predicate.matches(damageAbsorbed) }
	}

	class Instance(val predicate: Doubles, player: Optional<ContextAwarePredicate> = Optional.empty()) : AbstractInstance(player)
}
