@@ -5,13 +5,15 @@ import at.petrak.hexcasting.api.casting.iota.EntityIota
55import at.petrak.hexcasting.api.casting.iota.Iota
66import at.petrak.hexcasting.api.utils.hasByte
77import at.petrak.hexcasting.api.utils.hasFloat
8+ import gay.`object`.hexdebug.core.api.HexDebugCoreAPI
89import net.minecraft.nbt.CompoundTag
910import net.minecraft.nbt.ListTag
1011import net.minecraft.network.chat.Component
1112import net.minecraft.network.syncher.EntityDataAccessor
1213import net.minecraft.network.syncher.EntityDataSerializers
1314import net.minecraft.network.syncher.SynchedEntityData
1415import net.minecraft.server.level.ServerLevel
16+ import net.minecraft.server.level.ServerPlayer
1517import net.minecraft.world.entity.EntityType
1618import net.minecraft.world.entity.player.Player
1719import net.minecraft.world.level.Level
@@ -23,7 +25,9 @@ import ram.talia.hexal.api.nbt.SerialisedIotaList
2325import ram.talia.hexal.api.plus
2426import ram.talia.hexal.api.times
2527import ram.talia.hexal.common.lib.HexalEntities
28+ import ram.talia.hexal.interop.hexdebug.WispDebugEnv
2629import java.lang.Double.min
30+ import java.util.UUID
2731
2832class TickingWisp : BaseCastingWisp {
2933 override val shouldComplainNotEnoughMedia = false
@@ -73,6 +77,24 @@ class TickingWisp : BaseCastingWisp {
7377 val maximumMoveMultiplier: Float
7478 get() = entityData.get(MAXIMUM_MOVE_MULTIPLIER )
7579
80+ private var debugSessionId: UUID ? = null
81+
82+ val isDebugging: Boolean
83+ get() = debugSessionId != null
84+
85+ private val isDebuggingAndPaused: Boolean
86+ get() = isDebugging && getDebugEnv()?.isPaused != false
87+
88+ fun getDebugEnv (): WispDebugEnv ? {
89+ val debugSessionId = debugSessionId ? : return null
90+ val caster = caster as ? ServerPlayer ? : return null
91+ return HexDebugCoreAPI .INSTANCE .getDebugEnv(caster, debugSessionId) as ? WispDebugEnv
92+ }
93+
94+ fun setDebugEnv (debugEnv : WispDebugEnv ) {
95+ debugSessionId = debugEnv.sessionId
96+ }
97+
7698 constructor (entityType: EntityType <out BaseCastingWisp >, world: Level ) : super (entityType, world)
7799 constructor (
78100 entityType: EntityType <out TickingWisp >,
@@ -121,6 +143,12 @@ class TickingWisp : BaseCastingWisp {
121143 }
122144 }
123145
146+ // if the debug session has ended, destroy the wisp
147+ // TODO: this can result in a zombie session when the player dies (https://github.com/object-Object/HexDebug/issues/64)
148+ if (isDebugging && caster != null && getDebugEnv() == null ) {
149+ discard()
150+ }
151+
124152 super .tick()
125153 }
126154
@@ -143,7 +171,7 @@ class TickingWisp : BaseCastingWisp {
143171 }
144172
145173 override fun move () {
146- if (reachedTargetPos()) // also checks if within close enough distance of target.
174+ if (reachedTargetPos() || isDebuggingAndPaused ) // also checks if within close enough distance of target.
147175 return
148176
149177 val currentTarget = getTargetMovePosRaw()
@@ -163,10 +191,16 @@ class TickingWisp : BaseCastingWisp {
163191 // Seon wisps have the same max range as the caster.
164192 override fun maxSqrCastingDistance () = if (seon) { PlayerBasedCastEnv .AMBIT_RADIUS * PlayerBasedCastEnv .AMBIT_RADIUS } else { CASTING_RADIUS * CASTING_RADIUS }
165193
194+ override fun canScheduleCast (): Boolean {
195+ return super .canScheduleCast() && ! isDebuggingAndPaused
196+ }
197+
166198 override fun castCallback (result : WispCastingManager .WispCastResult ) {
167199// HexalAPI.LOGGER.info("ticking wisp $uuid had a cast successfully completed!")
168- setStack(result.endStack)
169- setRavenmind(result.endRavenmind)
200+ if (! result.cancelled) {
201+ setStack(result.endStack)
202+ setRavenmind(result.endRavenmind)
203+ }
170204
171205 super .castCallback(result)
172206 }
@@ -184,25 +218,40 @@ class TickingWisp : BaseCastingWisp {
184218 entityData.set(TARGET_MOVE_POS_Z , pos.z.toFloat())
185219 }
186220
221+ fun clearTargetMovePos () {
222+ entityData.set(HAS_TARGET_MOVE_POS , false )
223+ }
224+
187225 fun reachedTargetPos (): Boolean {
188226 return if (! entityData.get(HAS_TARGET_MOVE_POS )) {
189227 true
190228 } else if ((getTargetMovePosRaw() - position()).lengthSqr() < 0.01 ) {
191229 setPos(getTargetMovePosRaw())
192- entityData.set( HAS_TARGET_MOVE_POS , false )
230+ clearTargetMovePos( )
193231 true
194232 } else {
195233 false
196234 }
197235 }
198236
237+ override fun remove (reason : RemovalReason ) {
238+ if (reason.shouldDestroy()) {
239+ getDebugEnv()?.let { HexDebugCoreAPI .INSTANCE .removeDebugThread(it) }
240+ }
241+ super .remove(reason)
242+ }
243+
199244 override fun readAdditionalSaveData (compound : CompoundTag ) {
200245 super .readAdditionalSaveData(compound)
201246
202247 when (val stackTag = compound.get(TAG_STACK )) {
203248 null -> serStack.set(mutableListOf ())
204249 else -> serStack.set(stackTag as ListTag )
205250 }
251+ debugSessionId = when (compound.hasUUID(TAG_DEBUG_SESSION_ID )) {
252+ true -> compound.getUUID(TAG_DEBUG_SESSION_ID )
253+ false -> null
254+ }
206255 entityData.set(HAS_TARGET_MOVE_POS , when (compound.hasByte(TAG_HAS_TARGET_MOVE_POS )) {
207256 true -> compound.getBoolean(TAG_HAS_TARGET_MOVE_POS )
208257 false -> false
@@ -233,6 +282,7 @@ class TickingWisp : BaseCastingWisp {
233282 super .addAdditionalSaveData(compound)
234283
235284 compound.put(TAG_STACK , serStack.getTag())
285+ debugSessionId?.let { compound.putUUID(TAG_DEBUG_SESSION_ID , it) }
236286 compound.putBoolean(TAG_HAS_TARGET_MOVE_POS , entityData.get(HAS_TARGET_MOVE_POS ))
237287 compound.putFloat(TAG_TARGET_MOVE_POS_X , entityData.get(TARGET_MOVE_POS_X ))
238288 compound.putFloat(TAG_TARGET_MOVE_POS_Y , entityData.get(TARGET_MOVE_POS_Y ))
@@ -261,6 +311,7 @@ class TickingWisp : BaseCastingWisp {
261311 val MAXIMUM_MOVE_MULTIPLIER : EntityDataAccessor <Float > = SynchedEntityData .defineId(TickingWisp ::class .java, EntityDataSerializers .FLOAT )
262312
263313 const val TAG_STACK = " stack"
314+ const val TAG_DEBUG_SESSION_ID = " debug_session_id"
264315 const val TAG_HAS_TARGET_MOVE_POS = " has_target_move_pos"
265316 const val TAG_TARGET_MOVE_POS_X = " target_move_pos_x"
266317 const val TAG_TARGET_MOVE_POS_Y = " target_move_pos_y"
@@ -274,4 +325,4 @@ class TickingWisp : BaseCastingWisp {
274325 const val BASE_MAX_SPEED_PER_TICK = 6.0 / 20
275326 const val SCALE = 0.2
276327 }
277- }
328+ }
0 commit comments