charged amethyst is useful again

This commit is contained in:
yrsegal@gmail.com 2022-04-11 12:27:38 -04:00
parent 582c7fa316
commit 0438832fb1
4 changed files with 29 additions and 40 deletions

View file

@ -4,6 +4,7 @@ import at.petrak.hexcasting.HexConfig;
import at.petrak.hexcasting.api.spell.ParticleSpray;
import at.petrak.hexcasting.common.casting.CastingContext;
import at.petrak.hexcasting.common.casting.CastingHarness;
import at.petrak.hexcasting.common.casting.ManaHelper;
import at.petrak.hexcasting.common.casting.SpellCircleContext;
import at.petrak.hexcasting.common.casting.colors.FrozenColorizer;
import at.petrak.hexcasting.common.items.HexItems;
@ -506,8 +507,8 @@ public abstract class BlockEntityAbstractImpetus extends PaucalBlockEntity imple
@NotNull
@Override
public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
var manamount = getManaAmount(stack);
if (manamount != null) {
var manamount = ManaHelper.extractMana(stack, -1, false, simulate);
if (manamount > 0) {
if (!simulate) {
BlockEntityAbstractImpetus.this.mana += manamount;
BlockEntityAbstractImpetus.this.setChanged();
@ -531,23 +532,7 @@ public abstract class BlockEntityAbstractImpetus extends PaucalBlockEntity imple
@Override
public boolean isItemValid(int slot, @NotNull ItemStack stack) {
return getManaAmount(stack) != null;
}
// a separate method from the ctx or harness or whatever cause it's different and special
private static @Nullable Integer getManaAmount(ItemStack stack) {
int baseAmt;
if (stack.is(HexItems.AMETHYST_DUST.get())) {
baseAmt = HexConfig.dustManaAmount.get();
} else if (stack.is(Items.AMETHYST_SHARD)) {
baseAmt = HexConfig.shardManaAmount.get();
} else if (stack.is(HexItems.CHARGED_AMETHYST.get())) {
baseAmt = HexConfig.chargedCrystalManaAmount.get();
} else {
return null;
}
return baseAmt * stack.getCount();
return ManaHelper.extractMana(stack, -1, false, true) > 0;
}
};
}

View file

@ -12,33 +12,38 @@ import kotlin.math.min
import kotlin.math.roundToInt
object ManaHelper {
@JvmStatic
fun isManaItem(stack: ItemStack): Boolean {
return stack.`is`(HexItems.AMETHYST_DUST.get())
|| stack.`is`(Items.AMETHYST_SHARD)
|| stack.`is`(HexItems.CHARGED_AMETHYST.get())
|| stack.item is ItemManaBattery
return extractMana(stack, simulate = true) > 0
}
/**
* 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.
*
* If [drainFromBatteries] is false, this will not consider phials a valid source of mana.
*
* Return the amount of mana extracted. This may be over [cost] if mana is wasted.
*/
fun extractMana(stack: ItemStack, cost: Int = -1, simulate: Boolean = false): Int {
@JvmStatic
@JvmOverloads
fun extractMana(stack: ItemStack, cost: Int = -1, drainFromBatteries: Boolean = true, simulate: Boolean = false): 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
val tag = stack.orCreateTag
val manaThere = battery.getManaAmt(tag)
val manaToExtract = if (cost < 0) manaThere else min(cost, manaThere)
if (simulate)
return manaToExtract
return battery.withdrawMana(tag, manaToExtract)
is ItemManaBattery -> {
if (drainFromBatteries) {
val battery = stack.item as ItemManaBattery
val tag = stack.orCreateTag
val manaThere = battery.getManaAmt(tag)
val manaToExtract = if (cost < 0) manaThere else min(cost, manaThere)
if (simulate)
return manaToExtract
return battery.withdrawMana(tag, manaToExtract)
} else
return 0
}
else -> return 0
}
@ -62,15 +67,14 @@ object ManaHelper {
HexItems.CHARGED_AMETHYST.get() -> 1
Items.AMETHYST_SHARD -> 2
HexItems.AMETHYST_DUST.get() -> 3
HexItems.BATTERY.get() -> 4
is ItemManaBattery -> 4
else -> 0
}
intcode(aitem) - intcode(bitem)
} else if (aitem == HexItems.BATTERY.get()) {
} else if (aitem is ItemManaBattery) {
val atag = astack.orCreateTag
val btag = bstack.orCreateTag
val battery = aitem as ItemManaBattery
battery.getManaAmt(atag) - battery.getManaAmt(btag)
aitem.getManaAmt(atag) - aitem.getManaAmt(btag)
} else {
astack.count - bstack.count
}

View file

@ -43,7 +43,7 @@ object OpMakeBattery : SpellOperator {
ctx.assertEntityInRange(entity)
if (!ManaHelper.isManaItem(entity.item) || ManaHelper.extractMana(entity.item, simulate = true) <= 0) {
if (!ManaHelper.isManaItem(entity.item) || ManaHelper.extractMana(entity.item, drainFromBatteries = false, 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.extractMana(entityStack)
val manaAmt = ManaHelper.extractMana(entityStack, drainFromBatteries = false)
if (manaAmt > 0) {
val replaceItem = ItemStack(HexItems.BATTERY.get())
val tag = replaceItem.orCreateTag

View file

@ -37,7 +37,7 @@ class OpMakePackagedSpell<T : ItemPackagedSpell>(val itemType: T, val cost: Int)
}
ctx.assertEntityInRange(entity)
if (!ManaHelper.isManaItem(entity.item) || ManaHelper.extractMana(entity.item, simulate = true) <= 0) {
if (!ManaHelper.isManaItem(entity.item) || ManaHelper.extractMana(entity.item, drainFromBatteries = false, simulate = true) <= 0) {
throw MishapBadItem.of(
entity.item,
"mana"
@ -58,7 +58,7 @@ class OpMakePackagedSpell<T : ItemPackagedSpell>(val itemType: T, val cost: Int)
&& itemEntity.isAlive
) {
val entityStack = itemEntity.item.copy()
val manaAmt = ManaHelper.extractMana(entityStack)
val manaAmt = ManaHelper.extractMana(entityStack, drainFromBatteries = false)
if (manaAmt > 0) {
tag.putInt(ItemPackagedSpell.TAG_MANA, manaAmt)
tag.putInt(ItemPackagedSpell.TAG_MAX_MANA, manaAmt)