Skip to content

Commit 2fccb8c

Browse files
committed
add logs
1 parent 858553d commit 2fccb8c

File tree

11 files changed

+92
-28
lines changed

11 files changed

+92
-28
lines changed

src/main/kotlin/com/lambda/gui/dsl/ImGuiBuilder.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,15 @@ object ImGuiBuilder {
435435
* @param text The text to display
436436
*/
437437
@ImGuiDsl
438-
fun textColored(text: String, color: Color) = textColored(getColorU32(color.rgb), text)
438+
fun textColored(text: String, color: Color) = textColored(
439+
getColorU32(
440+
color.red / 255f,
441+
color.green / 255f,
442+
color.blue / 255f,
443+
color.alpha / 255f
444+
),
445+
text
446+
)
439447

440448
/**
441449
* Text with disabled coloring.

src/main/kotlin/com/lambda/interaction/construction/context/BreakContext.kt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ import com.lambda.graphics.renderer.esp.DirectionMask
2222
import com.lambda.graphics.renderer.esp.DirectionMask.exclude
2323
import com.lambda.interaction.material.StackSelection
2424
import com.lambda.interaction.request.breaking.BreakConfig
25-
import com.lambda.interaction.request.breaking.BreakRequest
2625
import com.lambda.interaction.request.hotbar.HotbarManager
27-
import com.lambda.interaction.request.hotbar.HotbarRequest
2826
import com.lambda.interaction.request.rotating.RotationRequest
2927
import com.lambda.util.BlockUtils.emptyState
3028
import net.minecraft.block.BlockState
@@ -76,11 +74,4 @@ data class BreakContext(
7674
withState(cachedState, blockPos, baseColor, DirectionMask.ALL.exclude(result.side))
7775
withState(cachedState, blockPos, sideColor, result.side)
7876
}
79-
80-
fun requestSwap(breakRequest: BreakRequest, minKeepTicks: Int = 0): Boolean =
81-
HotbarRequest(
82-
hotbarIndex,
83-
breakRequest.hotbar,
84-
breakRequest.hotbar.keepTicks.coerceAtLeast(minKeepTicks)
85-
).submit(false).done
8677
}

src/main/kotlin/com/lambda/interaction/request/DebugLogger.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,23 @@ class DebugLogger(name: String, description: String) : HudModule(
3838
private val showWarning = ImBoolean(true)
3939
private val showError = ImBoolean(true)
4040

41-
fun log(message: String, logColor: LogType) {
41+
private fun log(message: String, logColor: LogType) {
4242
logs.add(LogEntry(message, logColor))
4343
if (logs.size > maxLogEntries) {
4444
logs.removeFirst()
4545
}
4646
}
4747

48-
fun logDebug(message: String) = log(message, LogType.Debug)
49-
fun logSuccess(message: String) = log(message, LogType.Success)
50-
fun logWarning(message: String) = log(message, LogType.Warning)
51-
fun logError(message: String) = log(message, LogType.Error)
48+
fun debug(message: String) = log(message, LogType.Debug)
49+
fun success(message: String) = log(message, LogType.Success)
50+
fun warning(message: String) = log(message, LogType.Warning)
51+
fun error(message: String) = log(message, LogType.Error)
5252

5353
override fun ImGuiBuilder.buildLayout() {
5454
checkbox("Auto-scroll", autoScroll)
5555
sameLine()
56+
checkbox("Wrap Text", wrapText)
57+
sameLine()
5658
checkbox("Debug", showDebug)
5759
sameLine()
5860
checkbox("Info", showSuccess)
@@ -76,6 +78,7 @@ class DebugLogger(name: String, description: String) : HudModule(
7678
}
7779

7880
sameLine()
81+
textColored(logEntry.message, logEntry.type.color)
7982
}
8083
}
8184

src/main/kotlin/com/lambda/interaction/request/Request.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.lambda.interaction.request
1919

2020
abstract class Request {
21+
abstract val requestID: Int
2122
abstract val config: RequestConfig
2223
var fresh = true
2324

src/main/kotlin/com/lambda/interaction/request/breaking/BreakManager.kt

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import com.lambda.interaction.request.breaking.BrokenBlockHandler.pendingActions
6767
import com.lambda.interaction.request.breaking.BrokenBlockHandler.setPendingConfigs
6868
import com.lambda.interaction.request.breaking.BrokenBlockHandler.startPending
6969
import com.lambda.interaction.request.breaking.SwapInfo.Companion.getSwapInfo
70+
import com.lambda.interaction.request.hotbar.HotbarRequest
7071
import com.lambda.interaction.request.interacting.InteractionManager
7172
import com.lambda.interaction.request.placing.PlaceManager
7273
import com.lambda.interaction.request.rotating.RotationRequest
@@ -319,6 +320,7 @@ object BreakManager : RequestHandler<BreakRequest>(
319320
*/
320321
private fun SafeContext.processRequest(breakRequest: BreakRequest?) {
321322
breakRequest?.let { request ->
323+
logger.debug("Processing request (${request.requestID}) at tick stage ${tickStage?.run { this::class.qualifiedName }}")
322324
if (request.fresh) populateFrom(request)
323325
}
324326

@@ -343,6 +345,7 @@ object BreakManager : RequestHandler<BreakRequest>(
343345
}
344346

345347
if (instantBreaks.isEmpty() && breaks.isEmpty()) {
348+
if (activeRequest != null) logger.debug("Clearing active request")
346349
activeRequest = null
347350
}
348351
if (breaksThisTick > 0 || activeInfos.isNotEmpty()) {
@@ -359,6 +362,8 @@ object BreakManager : RequestHandler<BreakRequest>(
359362
* @see canAccept
360363
*/
361364
private fun SafeContext.populateFrom(request: BreakRequest) {
365+
logger.debug("Populating from request (${request.requestID})")
366+
362367
// Sanitize the new breaks
363368
val newBreaks = request.contexts
364369
.distinctBy { it.blockPos }
@@ -370,6 +375,7 @@ object BreakManager : RequestHandler<BreakRequest>(
370375
.forEach { info ->
371376
newBreaks.find { ctx -> ctx.blockPos == info.context.blockPos && canAccept(ctx) }?.let { ctx ->
372377
if ((!info.updatedThisTick || info.type == RedundantSecondary) || info.abandoned) {
378+
logger.debug("Updating info for ${info.type}")
373379
if (info.type == RedundantSecondary)
374380
info.request.onStart?.invoke(info.context.blockPos)
375381
else if (info.abandoned) {
@@ -389,10 +395,14 @@ object BreakManager : RequestHandler<BreakRequest>(
389395
.filter { it.instantBreak }
390396
.toMutableList()
391397

398+
logger.debug("${instantBreaks.size} unprocessed instant breaks")
399+
392400
breaks = newBreaks
393401
.filter { !it.instantBreak }
394402
.toMutableList()
395403

404+
logger.debug("${breaks.size} unprocessed instant breaks")
405+
396406
val breakConfig = request.config
397407
val pendingLimit = (breakConfig.maxPendingBreaks - pendingBreakCount).coerceAtLeast(0)
398408
maxBreaksThisTick = breakConfig.breaksPerTick.coerceAtMost(pendingLimit)
@@ -417,9 +427,11 @@ object BreakManager : RequestHandler<BreakRequest>(
417427
activeInfos
418428
.filter { it.updatedThisTick }
419429
.let { infos ->
430+
if (infos.isNotEmpty()) logger.debug("Handling pre-processing")
420431
rotationRequest = infos.firstOrNull { info -> info.breakConfig.rotateForBreak }
421432
?.let { info ->
422433
val rotation = info.context.rotation
434+
logger.debug("Requesting rotation (${rotation.requestID})")
423435
rotation.submit(false)
424436
}
425437

@@ -430,8 +442,18 @@ object BreakManager : RequestHandler<BreakRequest>(
430442
infos.firstOrNull()?.let { info ->
431443
infos.lastOrNull { it.swapInfo.swap && it.shouldProgress }?.let { last ->
432444
val minSwapTicks = max(info.swapInfo.minKeepTicks, last.swapInfo.minKeepTicks)
433-
if (!info.context.requestSwap(info.request, minSwapTicks))
445+
val hotbarRequest = with(info) {
446+
HotbarRequest(
447+
context.hotbarIndex,
448+
request.hotbar,
449+
request.hotbar.keepTicks.coerceAtLeast(minSwapTicks)
450+
).submit(false)
451+
}
452+
logger.debug("Submitting request for hotbar index ${info.context.hotbarIndex} with min swap ticks $minSwapTicks (${hotbarRequest.requestID})")
453+
if (!hotbarRequest.done) {
454+
logger.debug("hotbar request failed (${hotbarRequest.requestID})")
434455
return false
456+
}
435457
if (minSwapTicks > 0) {
436458
val alreadySwapped = swappedThisTick
437459
currentStack = info.swapStack
@@ -450,6 +472,7 @@ object BreakManager : RequestHandler<BreakRequest>(
450472
* @return false if a break could not be performed.
451473
*/
452474
private fun SafeContext.performInstantBreaks(request: BreakRequest): Boolean {
475+
if (instantBreaks.isNotEmpty()) logger.debug("Processing instant breaks")
453476
val iterator = instantBreaks.iterator()
454477
while (iterator.hasNext()) {
455478
if (breaksThisTick + 1 > maxBreaksThisTick) return false
@@ -478,6 +501,7 @@ object BreakManager : RequestHandler<BreakRequest>(
478501
* @see initNewBreak
479502
*/
480503
private fun SafeContext.processNewBreaks(request: BreakRequest): Boolean {
504+
if (breaks.isNotEmpty()) logger.debug("Processing new breaks")
481505
val iterator = breaks.iterator()
482506
while (iterator.hasNext()) {
483507
val ctx = iterator.next()
@@ -515,8 +539,9 @@ object BreakManager : RequestHandler<BreakRequest>(
515539
return secondaryBreak
516540
}
517541

542+
logger.debug("Transforming ${primaryInfo.type} to $Secondary")
518543
primaryInfo.stopBreakPacket(world, interaction)
519-
primaryInfo.makeSecondary()
544+
secondaryBreak = primaryInfo.apply { type = Secondary }
520545
return@let
521546
}
522547

@@ -605,6 +630,8 @@ object BreakManager : RequestHandler<BreakRequest>(
605630
}
606631

607632
private fun BreakInfo.updatePreProcessing(player: ClientPlayerEntity, world: BlockView) {
633+
logger.debug("Updating pre-processing for $type")
634+
608635
shouldProgress = !progressedThisTick
609636
&& tickStage in breakConfig.breakStageMask
610637
&& (rotated || type != Primary)
@@ -617,17 +644,6 @@ object BreakManager : RequestHandler<BreakRequest>(
617644
swapInfo = getSwapInfo(this, player, world)
618645
}
619646

620-
/**
621-
* Makes the [BreakInfo] a secondary if not already.
622-
*/
623-
private fun BreakInfo.makeSecondary() {
624-
if (secondaryBreak === this) return
625-
secondaryBreak = this.apply {
626-
type = Secondary
627-
}
628-
primaryBreak = null
629-
}
630-
631647
/**
632648
* Attempts to cancel the break.
633649
*
@@ -643,17 +659,20 @@ object BreakManager : RequestHandler<BreakRequest>(
643659
if (type == RedundantSecondary || abandoned) return@runSafe
644660
when (type) {
645661
Primary -> {
662+
logger.debug("Cancelling $type")
646663
nullify()
647664
setBreakingTextureStage(player, world, -1)
648665
abortBreakPacket(world, interaction)
649666
request.onCancel?.invoke(context.blockPos)
650667
}
651668
Secondary -> {
652669
if (breakConfig.unsafeCancels) {
670+
logger.warning("Making $type redundant")
653671
type = RedundantSecondary
654672
setBreakingTextureStage(player, world, -1)
655673
request.onCancel?.invoke(context.blockPos)
656674
} else {
675+
logger.debug("Abandoning $type")
657676
abandoned = true
658677
}
659678
}
@@ -713,6 +732,7 @@ object BreakManager : RequestHandler<BreakRequest>(
713732
if (blockState.isEmpty) {
714733
info.nullify()
715734
info.request.onCancel?.invoke(ctx.blockPos)
735+
logger.warning("Block state was unexpectedly empty")
716736
return false
717737
}
718738

@@ -723,6 +743,7 @@ object BreakManager : RequestHandler<BreakRequest>(
723743
ctx.blockPos,
724744
config
725745
) * (info.breakingTicks - config.fudgeFactor)
746+
logger.debug("${info.type} progress: $progress, breaking ticks: ${info.breakingTicks}")
726747

727748
if (config.sounds) {
728749
if (info.soundsCooldown % 4.0f == 0.0f) {
@@ -751,6 +772,7 @@ object BreakManager : RequestHandler<BreakRequest>(
751772

752773
val swing = config.swing
753774
if (progress >= info.getBreakThreshold() && info.swapInfo.canCompleteBreak) {
775+
logger.success("Breaking ${info.type}")
754776
if (info.type == Primary) {
755777
onBlockBreak(info)
756778
info.stopBreakPacket(world, interaction)
@@ -777,8 +799,10 @@ object BreakManager : RequestHandler<BreakRequest>(
777799
val ctx = info.context
778800

779801
if (info.rebreakPotential.isPossible()) {
802+
logger.debug("Handling potential rebreak")
780803
when (val rebreakResult = RebreakHandler.handleUpdate(info.context, info.request)) {
781804
is RebreakResult.StillBreaking -> {
805+
logger.debug("Rebreak not complete")
782806
primaryBreak = rebreakResult.breakInfo.apply {
783807
type = Primary
784808
RebreakHandler.clearRebreak()
@@ -792,6 +816,7 @@ object BreakManager : RequestHandler<BreakRequest>(
792816
return true
793817
}
794818
is RebreakResult.Rebroke -> {
819+
logger.debug("Rebroke")
795820
info.type = Rebreak
796821
info.nullify()
797822
info.request.onReBreak?.invoke(ctx.blockPos)
@@ -825,9 +850,11 @@ object BreakManager : RequestHandler<BreakRequest>(
825850
val progress = blockState.calcBreakDelta(player, world, ctx.blockPos, info.breakConfig)
826851
info.vanillaInstantBreakable = progress >= 1 && info.swapInfo.canCompleteBreak
827852
if (progress >= info.getBreakThreshold() && info.swapInfo.canCompleteBreak) {
853+
logger.success("Instantly breaking")
828854
onBlockBreak(info)
829855
if (!info.vanillaInstantBreakable) breakCooldown = info.breakConfig.breakDelay
830856
} else {
857+
logger.debug("Starting ${info.type}")
831858
info.apply {
832859
breaking = true
833860
breakingTicks = 1

src/main/kotlin/com/lambda/interaction/request/breaking/BreakRequest.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ data class BreakRequest(
4141
val inventory: InventoryConfig = TaskFlowModule.inventory,
4242
val interact: InteractionConfig = TaskFlowModule.interaction
4343
) : Request() {
44+
override val requestID = ++requestCount
45+
4446
override val config = build.breaking
4547
var onStart: ((BlockPos) -> Unit)? = null
4648
var onUpdate: ((BlockPos) -> Unit)? = null
@@ -111,6 +113,8 @@ data class BreakRequest(
111113
}
112114

113115
companion object {
116+
var requestCount = 0
117+
114118
@BreakRequestBuilder
115119
fun breakRequest(
116120
contexts: Collection<BreakContext>,

src/main/kotlin/com/lambda/interaction/request/hotbar/HotbarRequest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class HotbarRequest(
2525
override var keepTicks: Int = config.keepTicks,
2626
override var swapPause: Int = config.swapPause
2727
) : Request(), HotbarConfig by config {
28+
override val requestID = ++requestCount
29+
2830
var activeRequestAge = 0
2931
var swapPauseAge = 0
3032

@@ -37,4 +39,8 @@ class HotbarRequest(
3739

3840
override fun submit(queueIfClosed: Boolean) =
3941
HotbarManager.request(this, queueIfClosed)
42+
43+
companion object {
44+
var requestCount = 0
45+
}
4046
}

src/main/kotlin/com/lambda/interaction/request/interacting/InteractRequest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ data class InteractRequest(
3636
val hotbar: HotbarConfig,
3737
val rotation: RotationConfig
3838
) : Request(), InteractConfig by config {
39+
override val requestID = ++requestCount
40+
3941
override val done: Boolean
4042
get() = contexts.all { mc.world?.getBlockState(it.blockPos)?.matches(it.expectedState) == true }
4143

4244
override fun submit(queueIfClosed: Boolean) =
4345
InteractionManager.request(this, queueIfClosed)
46+
47+
companion object {
48+
var requestCount = 0
49+
}
4450
}

src/main/kotlin/com/lambda/interaction/request/inventory/InventoryRequest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@ import com.lambda.interaction.request.Request
2222
class InventoryRequest(
2323
override val config: InventoryConfig
2424
) : Request(), InventoryConfig by config {
25+
override val requestID = ++requestCount
26+
2527
override val done: Boolean
2628
get() = TODO("Not yet implemented")
2729

2830
override fun submit(queueIfClosed: Boolean) =
2931
InventoryManager.request(this, queueIfClosed)
32+
33+
companion object {
34+
var requestCount = 0
35+
}
3036
}

src/main/kotlin/com/lambda/interaction/request/placing/PlaceRequest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ data class PlaceRequest(
3636
val pendingInteractions: MutableCollection<BuildContext>,
3737
val onPlace: ((BlockPos) -> Unit)? = null
3838
) : Request(), PlaceConfig by build.placing {
39+
override val requestID = ++requestCount
40+
3941
override val config = build.placing
4042
override val done: Boolean
4143
get() = runSafe {
@@ -44,4 +46,8 @@ data class PlaceRequest(
4446

4547
override fun submit(queueIfClosed: Boolean) =
4648
PlaceManager.request(this, queueIfClosed)
49+
50+
companion object {
51+
var requestCount = 0
52+
}
4753
}

0 commit comments

Comments
 (0)