diff --git a/src/main/java/at/petrak/hexcasting/common/casting/ManaHelper.kt b/src/main/java/at/petrak/hexcasting/common/casting/ManaHelper.kt index 6dfaea29..e3827556 100644 --- a/src/main/java/at/petrak/hexcasting/common/casting/ManaHelper.kt +++ b/src/main/java/at/petrak/hexcasting/common/casting/ManaHelper.kt @@ -20,35 +20,12 @@ object ManaHelper { } /** - * Try to extract the given amount of mana from this item. - * This may mutate the itemstack. + * Extract [cost] mana from [stack]. If [cost] is less than zero, extract all mana instead. + * This may mutate [stack] (and may consume it) unless [simulate] is set. * - * Return the actual amount of mana extracted, or null if this cannot have mana extracted. + * Return the amount of mana extracted. This may be over [cost] if mana is wasted. */ - fun extractMana(stack: ItemStack, cost: Int): Int? { - val base = when (stack.item) { - HexItems.AMETHYST_DUST.get() -> HexConfig.dustManaAmount.get() - Items.AMETHYST_SHARD -> HexConfig.shardManaAmount.get() - HexItems.CHARGED_AMETHYST.get() -> HexConfig.chargedCrystalManaAmount.get() - HexItems.BATTERY.get() -> { - val battery = stack.item as ItemManaBattery - return battery.withdrawMana(stack.orCreateTag, cost) - } - else -> return null - } - val itemsReqd = ceil(cost.toFloat() / base.toFloat()).toInt() - val actualItemsConsumed = min(stack.count, itemsReqd) - stack.shrink(actualItemsConsumed) - return base * actualItemsConsumed - } - - /** - * Extract the entirety of the mana out of this. - * This may mutate the itemstack (and will probably consume it). - * - * Return the amount of mana extracted. - */ - fun extractAllMana(stack: ItemStack): Int { + fun extractMana(stack: ItemStack, cost: Int = -1, simulate: Boolean = false): Int { val base = when (stack.item) { HexItems.AMETHYST_DUST.get() -> HexConfig.dustManaAmount.get() Items.AMETHYST_SHARD -> HexConfig.shardManaAmount.get() @@ -58,13 +35,18 @@ object ManaHelper { val battery = stack.item as ItemManaBattery val tag = stack.orCreateTag val manaThere = battery.getManaAmt(tag) - return battery.withdrawMana(tag, manaThere) + val manaToExtract = if (cost < 0) manaThere else min(cost, manaThere) + if (simulate) + return manaToExtract + return battery.withdrawMana(tag, manaToExtract) } else -> return 0 } val count = stack.count - stack.shrink(count) - return base * count + val countToExtract = if (cost < 0) count else min(count, ceil(cost.toDouble() / base).toInt()) + if (!simulate) + stack.shrink(countToExtract) + return base * countToExtract } /** diff --git a/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/OpMakeBattery.kt b/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/OpMakeBattery.kt index 057893cc..741cd2b6 100644 --- a/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/OpMakeBattery.kt +++ b/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/OpMakeBattery.kt @@ -43,7 +43,7 @@ object OpMakeBattery : SpellOperator { ctx.assertEntityInRange(entity) - if (!ManaHelper.isManaItem(entity.item)) { + if (!ManaHelper.isManaItem(entity.item) || ManaHelper.extractMana(entity.item, simulate = true) <= 0) { throw MishapBadItem.of( entity.item, "mana" @@ -58,7 +58,7 @@ object OpMakeBattery : SpellOperator { val (handStack, hand) = ctx.getHeldItemToOperateOn { it.item == Items.GLASS_BOTTLE } if (handStack.item == Items.GLASS_BOTTLE && itemEntity.isAlive) { val entityStack = itemEntity.item.copy() - val manaAmt = ManaHelper.extractAllMana(entityStack) + val manaAmt = ManaHelper.extractMana(entityStack) if (manaAmt > 0) { val replaceItem = ItemStack(HexItems.BATTERY.get()) val tag = replaceItem.orCreateTag diff --git a/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/OpMakePackagedSpell.kt b/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/OpMakePackagedSpell.kt index dc2ae3a7..75f766ae 100644 --- a/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/OpMakePackagedSpell.kt +++ b/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/OpMakePackagedSpell.kt @@ -36,7 +36,7 @@ class OpMakePackagedSpell(val itemType: T, val cost: Int) } ctx.assertEntityInRange(entity) - if (!ManaHelper.isManaItem(entity.item)) { + if (!ManaHelper.isManaItem(entity.item) || ManaHelper.extractMana(entity.item, simulate = true) <= 0) { throw MishapBadItem.of( entity.item, "mana" @@ -57,7 +57,7 @@ class OpMakePackagedSpell(val itemType: T, val cost: Int) && itemEntity.isAlive ) { val entityStack = itemEntity.item.copy() - val manaAmt = ManaHelper.extractAllMana(entityStack) + val manaAmt = ManaHelper.extractMana(entityStack) if (manaAmt > 0) { tag.putInt(ItemPackagedSpell.TAG_MANA, manaAmt) tag.putInt(ItemPackagedSpell.TAG_MAX_MANA, manaAmt) diff --git a/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/OpRecharge.kt b/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/OpRecharge.kt index 5a1e5de2..74eedf8d 100644 --- a/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/OpRecharge.kt +++ b/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/OpRecharge.kt @@ -47,19 +47,20 @@ object OpRecharge : SpellOperator { if (handStack.item is ItemManaHolder && itemEntity.isAlive) { val entityStack = itemEntity.item.copy() - val manaAmt = ManaHelper.extractAllMana(entityStack) - if (manaAmt > 0) { - val tag = handStack.orCreateTag - val maxMana = if (tag.contains(ItemManaHolder.TAG_MAX_MANA)) - tag.getInt(ItemManaHolder.TAG_MAX_MANA) - else - Int.MAX_VALUE - val existingMana = if (tag.contains(ItemManaHolder.TAG_MANA)) - tag.getInt(ItemManaHolder.TAG_MANA) - else - 0 - tag.putInt(ItemManaHolder.TAG_MANA, Mth.clamp(existingMana + manaAmt, 0, maxMana)) - } + + val tag = handStack.orCreateTag + val maxMana = if (tag.contains(ItemManaHolder.TAG_MAX_MANA)) + tag.getInt(ItemManaHolder.TAG_MAX_MANA) + else + Int.MAX_VALUE + val existingMana = if (tag.contains(ItemManaHolder.TAG_MANA)) + tag.getInt(ItemManaHolder.TAG_MANA) + else + 0 + + val manaAmt = ManaHelper.extractMana(entityStack, maxMana - existingMana) + + tag.putInt(ItemManaHolder.TAG_MANA, Mth.clamp(existingMana + manaAmt, 0, maxMana)) itemEntity.item = entityStack if (entityStack.isEmpty)