Skip to content

Commit 7294267

Browse files
committed
Fix debugger media extraction when not holding debugger in original hand
1 parent cc7f648 commit 7294267

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
88

99
### Fixed
1010

11-
- Fixed a bug where the Evaluator was unable to cast any spells requiring media.
11+
- The Evaluator was unable to cast any spells requiring media.
12+
- When debugging, spells requiring media would fail if a Debugger was not in the hand that the debug session was started with.
1213

1314
## 0.2.0+1.20.1
1415

Common/src/main/kotlin/gay/object/hexdebug/casting/eval/DebuggerCastEnv.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import at.petrak.hexcasting.api.casting.castables.Action
44
import at.petrak.hexcasting.api.casting.eval.env.PackagedItemCastEnv
55
import at.petrak.hexcasting.api.casting.eval.sideeffects.OperatorSideEffect
66
import gay.`object`.hexdebug.debugger.DebugStepType
7+
import gay.`object`.hexdebug.utils.findMediaHolderInHand
8+
import gay.`object`.hexdebug.utils.otherHand
79
import net.minecraft.network.chat.Component
810
import net.minecraft.server.level.ServerPlayer
911
import net.minecraft.world.InteractionHand
@@ -12,6 +14,8 @@ class DebuggerCastEnv(
1214
caster: ServerPlayer,
1315
castingHand: InteractionHand,
1416
) : PackagedItemCastEnv(caster, castingHand), IDebugCastEnv {
17+
private val item = caster.getItemInHand(castingHand).item
18+
1519
override var lastEvaluatedAction: Action? = null
1620
override var lastDebugStepType: DebugStepType? = null
1721

@@ -24,4 +28,29 @@ class DebuggerCastEnv(
2428
super.sendMishapMsgToPlayer(mishap)
2529
printDebugMishap(this, caster, mishap)
2630
}
31+
32+
override fun extractMediaEnvironment(cost: Long): Long {
33+
if (caster.isCreative) return 0
34+
35+
var costLeft = cost
36+
37+
// allow extracting from a debugger item in either hand, preferring the one we started casting with
38+
// NOTE: if holding two debuggers and the first is empty, this will take the empty one, not the other one
39+
val casterMediaHolder = caster.findMediaHolderInHand(castingHand, item)
40+
?: caster.findMediaHolderInHand(castingHand.otherHand, item)
41+
42+
// The contracts on the AD and on this function are different.
43+
// ADs return the amount extracted, this wants the amount left
44+
if (casterMediaHolder != null) {
45+
val extracted = casterMediaHolder.withdrawMedia(costLeft.toInt().toLong(), false)
46+
costLeft -= extracted
47+
}
48+
49+
// debugger can always extract from inventory
50+
if (costLeft > 0) {
51+
costLeft = extractMediaFromInventory(costLeft, canOvercast())
52+
}
53+
54+
return costLeft
55+
}
2756
}

Common/src/main/kotlin/gay/object/hexdebug/utils/Extensions.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package gay.`object`.hexdebug.utils
22

3+
import at.petrak.hexcasting.xplat.IXplatAbstractions
34
import net.minecraft.world.InteractionHand
5+
import net.minecraft.world.entity.LivingEntity
6+
import net.minecraft.world.item.Item
47
import java.util.concurrent.CompletableFuture
58
import kotlin.enums.enumEntries
69
import kotlin.math.ceil
@@ -35,6 +38,12 @@ val InteractionHand.otherHand get() = when (this) {
3538
InteractionHand.OFF_HAND -> InteractionHand.MAIN_HAND
3639
}
3740

41+
fun LivingEntity.getItemInHand(hand: InteractionHand, item: Item) =
42+
getItemInHand(hand).takeIf { it.`is`(item) }
43+
44+
fun LivingEntity.findMediaHolderInHand(hand: InteractionHand, item: Item) =
45+
getItemInHand(hand, item)?.let(IXplatAbstractions.INSTANCE::findMediaHolder)
46+
3847
// ceil the denominator to a power of 2 so we don't have issues with eg. 1/3
3948
@OptIn(ExperimentalStdlibApi::class)
4049
inline val <reified T : Enum<T>> T.itemPredicate get() =

0 commit comments

Comments
 (0)