package ru.dbotthepony.mc.otm.registry

import net.minecraft.core.Holder
import net.minecraft.network.chat.Component
import net.minecraft.world.damagesource.DamageSource
import net.minecraft.world.damagesource.DamageType
import net.minecraft.world.entity.Entity
import net.minecraft.world.entity.LivingEntity
import net.minecraft.world.item.ItemStack
import net.minecraft.world.phys.Vec3
import ru.dbotthepony.mc.otm.core.TranslatableComponent

class MatteryDamageSource(
	type: Holder<DamageType>,
	entity: Entity? = null,
	inflictor: ItemStack? = null,
	entityInflictor: Entity? = null,
	pos: Vec3? = null
) : DamageSource(type, entity, entityInflictor, pos) {
	val inflictor = inflictor?.copy()

	override fun getLocalizedDeathMessage(victim: LivingEntity): Component {
		val itemStack = inflictor ?: (entity as? LivingEntity)?.mainHandItem ?: ItemStack.EMPTY

		if (!itemStack.isEmpty && itemStack.hasCustomHoverName()) {
			return TranslatableComponent("death.attack.$msgId.player.item", victim.displayName, entity!!.displayName, itemStack.displayName)
		}

		if (entity != null) {
			return TranslatableComponent("death.attack.$msgId.player", victim.displayName, entity!!.displayName)
		}

		return TranslatableComponent("death.attack.$msgId", victim.displayName)
	}
}
