HexCasting/Common/src/main/java/at/petrak/hexcasting/common/casting/actions/spells/OpMakeBattery.kt

92 lines
3.2 KiB
Kotlin

package at.petrak.hexcasting.common.casting.actions.spells
import at.petrak.hexcasting.api.casting.ParticleSpray
import at.petrak.hexcasting.api.casting.RenderedSpell
import at.petrak.hexcasting.api.casting.castables.SpellAction
import at.petrak.hexcasting.api.casting.eval.CastingEnvironment
import at.petrak.hexcasting.api.casting.getItemEntity
import at.petrak.hexcasting.api.casting.iota.Iota
import at.petrak.hexcasting.api.casting.mishaps.MishapBadItem
import at.petrak.hexcasting.api.casting.mishaps.MishapBadOffhandItem
import at.petrak.hexcasting.api.misc.MediaConstants
import at.petrak.hexcasting.api.mod.HexTags
import at.petrak.hexcasting.api.utils.extractMedia
import at.petrak.hexcasting.api.utils.isMediaItem
import at.petrak.hexcasting.common.items.magic.ItemMediaHolder
import at.petrak.hexcasting.common.lib.HexItems
import net.minecraft.world.InteractionHand
import net.minecraft.world.entity.item.ItemEntity
import net.minecraft.world.item.ItemStack
// TODO: how to handle in cirles
object OpMakeBattery : SpellAction {
override val argc = 1
override fun execute(
args: List<Iota>,
ctx: CastingEnvironment
): SpellAction.Result {
val entity = args.getItemEntity(0, argc)
val (handStack, hand) = ctx.getHeldItemToOperateOn { it.`is`(HexTags.Items.PHIAL_BASE) }
?: throw MishapBadOffhandItem.of(ItemStack.EMPTY.copy(), null, "bottle") // TODO: hack
if (hand == null)
throw MishapBadOffhandItem.of(handStack, null, "havent_handled_null_hand_yet") // TODO: hack!
if (!handStack.`is`(HexTags.Items.PHIAL_BASE)) {
throw MishapBadOffhandItem.of(
handStack,
hand,
"bottle"
)
}
if (handStack.count != 1) {
throw MishapBadOffhandItem.of(
handStack,
hand,
"only_one"
)
}
ctx.assertEntityInRange(entity)
if (!isMediaItem(entity.item) || extractMedia(
entity.item,
drainForBatteries = true,
simulate = true
) <= 0
) {
throw MishapBadItem.of(
entity,
"media_for_battery"
)
}
return SpellAction.Result(
Spell(entity, hand),
MediaConstants.CRYSTAL_UNIT,
listOf(ParticleSpray.burst(entity.position(), 0.5))
)
}
private data class Spell(val itemEntity: ItemEntity, val hand: InteractionHand) : RenderedSpell {
override fun cast(ctx: CastingEnvironment) {
if (itemEntity.isAlive) {
val entityStack = itemEntity.item.copy()
val mediamount = extractMedia(entityStack, drainForBatteries = true)
if (mediamount > 0) {
ctx.caster?.setItemInHand(
hand,
ItemMediaHolder.withMedia(ItemStack(HexItems.BATTERY), mediamount, mediamount)
) ?: return
}
itemEntity.item = entityStack
if (entityStack.isEmpty)
itemEntity.kill()
}
}
}
}