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.resources.ResourceLocation
import net.minecraft.server.level.ServerPlayer
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.data.minRange
import java.util.*

object AndroidTravelUnderwater : MCriterionTrigger<AndroidTravelUnderwater.Instance>(ResourceLocation(OverdriveThatMatters.MOD_ID, "android_walk_underwater")) {
	fun trigger(player: ServerPlayer, travelled: Double) {
		trigger(player) { it.distanceToTravel <= travelled }
	}

	override val codec: Codec<Instance> = RecordCodecBuilder.create {
		it.group(
			Codec.DOUBLE.minRange(0.0).fieldOf("distanceToTravel").forGetter(Instance::distanceToTravel),
			playerPredicateCodec.forGetter(Instance::playerPredicate)
		).apply(it, ::Instance)
	}

	class Instance(val distanceToTravel: Double, playerPredicate: Optional<ContextAwarePredicate> = Optional.empty()) : AbstractInstance(playerPredicate)
}
