Skip to content

Commit ff97fbb

Browse files
committed
Some coarse cleanup
1 parent 35b8ef9 commit ff97fbb

File tree

10 files changed

+85
-82
lines changed

10 files changed

+85
-82
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ data class BreakContext(
4747
private val sideColor = Color(222, 0, 0, 100)
4848

4949
override val blockPos: BlockPos = result.blockPos
50-
override val expectedState: BlockState = cachedState.emptyState
50+
override val expectedState = cachedState.emptyState
5151

5252
val random = Random.nextDouble()
5353

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
package com.lambda.interaction.construction.context
1919

20-
import com.lambda.Lambda.mc
2120
import com.lambda.interaction.construction.result.Drawable
2221
import com.lambda.interaction.request.rotating.RotationRequest
22+
import com.lambda.threading.runSafe
2323
import net.minecraft.block.BlockState
2424
import net.minecraft.util.hit.BlockHitResult
2525
import net.minecraft.util.math.BlockPos
@@ -33,6 +33,6 @@ abstract class BuildContext : Comparable<BuildContext>, Drawable {
3333
abstract val blockPos: BlockPos
3434

3535
val distance by lazy {
36-
mc.player?.eyePos?.distanceTo(result.pos) ?: Double.MAX_VALUE
36+
runSafe { player.eyePos.distanceTo(result.pos) } ?: Double.MAX_VALUE
3737
}
3838
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,11 @@ data class PlaceContext(
5252
is PlaceContext -> compareBy<PlaceContext> {
5353
BlockUtils.fluids.indexOf(it.cachedState.fluidState.fluid)
5454
}.thenByDescending {
55-
if (it.cachedState.fluidState.level != 0) it.blockPos.y
56-
else 0
55+
if (it.cachedState.fluidState.level != 0) it.blockPos.y else 0
5756
}.thenByDescending {
5857
it.cachedState.fluidState.level
5958
}.thenBy {
60-
it.sneak == mc.player?.isSneaking
59+
it.sneak == (mc.player?.isSneaking ?: false)
6160
}.thenBy {
6261
it.rotation.target.angleDistance
6362
}.thenBy {

src/main/kotlin/com/lambda/interaction/construction/processing/PreProcessingInfo.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Lambda
2+
* Copyright 2025 Lambda
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by

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

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ object BreakManager : RequestHandler<BreakRequest>(
145145

146146
var lastPosStarted: BlockPos? = null
147147
set(value) {
148-
if (value != field) ReBreakManager.clearReBreak()
148+
if (value != field) RebreakManager.clearRebreak()
149149
field = value
150150
}
151151

@@ -171,7 +171,7 @@ object BreakManager : RequestHandler<BreakRequest>(
171171
}
172172

173173
listen<WorldEvent.BlockUpdate.Server>(priority = Int.MIN_VALUE) { event ->
174-
if (event.pos == ReBreakManager.reBreak?.context?.blockPos) return@listen
174+
if (event.pos == RebreakManager.rebreak?.context?.blockPos) return@listen
175175

176176
breakInfos
177177
.filterNotNull()
@@ -192,7 +192,7 @@ object BreakManager : RequestHandler<BreakRequest>(
192192
info.request.onStop?.invoke(info.context.blockPos)
193193
info.internalOnBreak()
194194
if (info.callbacksCompleted)
195-
ReBreakManager.offerReBreak(info)
195+
RebreakManager.offerRebreak(info)
196196
else info.startPending()
197197
info.nullify()
198198
}
@@ -203,7 +203,7 @@ object BreakManager : RequestHandler<BreakRequest>(
203203
if (it.entity !is ItemEntity) return@listen
204204

205205
// ToDo: Proper item drop prediction system
206-
ReBreakManager.reBreak?.let { reBreak ->
206+
RebreakManager.rebreak?.let { reBreak ->
207207
if (matchesBlockItem(reBreak, it.entity)) return@listen
208208
}
209209

@@ -523,7 +523,7 @@ object BreakManager : RequestHandler<BreakRequest>(
523523
}
524524
}
525525

526-
private fun SafeContext.checkForCancels() {
526+
private fun checkForCancels() {
527527
breakInfos
528528
.filterNotNull()
529529
.asSequence()
@@ -569,7 +569,7 @@ object BreakManager : RequestHandler<BreakRequest>(
569569
if (!info.callbacksCompleted) {
570570
info.startPending()
571571
} else {
572-
ReBreakManager.offerReBreak(info)
572+
RebreakManager.offerRebreak(info)
573573
}
574574
}
575575
BreakConfirmationMode.BreakThenAwait -> {
@@ -585,14 +585,14 @@ object BreakManager : RequestHandler<BreakRequest>(
585585
}
586586

587587
private fun BreakInfo.updatePreProcessing(player: ClientPlayerEntity, world: BlockView) {
588-
shouldProgress = !progressedThisTick &&
589-
tickStage in breakConfig.breakStageMask &&
590-
(rotated || type != Primary)
588+
shouldProgress = !progressedThisTick
589+
&& tickStage in breakConfig.breakStageMask
590+
&& (rotated || type != Primary)
591591

592592
if (updatedPreProcessingThisTick) return
593593
updatedPreProcessingThisTick = true
594594

595-
couldReBreak = ReBreakManager.couldReBreak(this, player, world)
595+
couldReBreak = RebreakManager.couldRebreak(this, player, world)
596596
shouldSwap = shouldSwap(player, world)
597597

598598
val cachedState = context.cachedState
@@ -602,8 +602,7 @@ object BreakManager : RequestHandler<BreakRequest>(
602602
val breakAmountNoEfficiency = cachedState.calcBreakDelta(player, world, context.blockPos, breakConfig, swapStack, ignoreEfficiency = true) * (breakingTicks + 1)
603603

604604
minSwapTicks = if (breakAmount >= getBreakThreshold() || couldReBreak) {
605-
val min = if (breakAmountNoEfficiency >= getBreakThreshold()) 0
606-
else 1
605+
val min = if (breakAmountNoEfficiency >= getBreakThreshold()) 0 else 1
607606
serverBreakTicks++
608607
min
609608
} else 0
@@ -633,19 +632,23 @@ object BreakManager : RequestHandler<BreakRequest>(
633632
private fun BreakInfo.cancelBreak() =
634633
runSafe {
635634
if (type == RedundantSecondary || abandoned) return@runSafe
636-
if (type == Primary) {
637-
nullify()
638-
setBreakingTextureStage(player, world, -1)
639-
abortBreakPacket(world, interaction)
640-
request.onCancel?.invoke(context.blockPos)
641-
} else if (type == Secondary) {
642-
if (breakConfig.unsafeCancels) {
643-
type = RedundantSecondary
635+
when (type) {
636+
Primary -> {
637+
nullify()
644638
setBreakingTextureStage(player, world, -1)
639+
abortBreakPacket(world, interaction)
645640
request.onCancel?.invoke(context.blockPos)
646-
} else {
647-
abandoned = true
648641
}
642+
Secondary -> {
643+
if (breakConfig.unsafeCancels) {
644+
type = RedundantSecondary
645+
setBreakingTextureStage(player, world, -1)
646+
request.onCancel?.invoke(context.blockPos)
647+
} else {
648+
abandoned = true
649+
}
650+
}
651+
else -> {}
649652
}
650653
}
651654

@@ -659,8 +662,7 @@ object BreakManager : RequestHandler<BreakRequest>(
659662
*/
660663
private fun BreakType.nullify() =
661664
when (this) {
662-
Primary,
663-
Rebreak -> primaryBreak = null
665+
Primary, Rebreak -> primaryBreak = null
664666
else -> secondaryBreak = null
665667
}
666668

@@ -772,26 +774,28 @@ object BreakManager : RequestHandler<BreakRequest>(
772774
private fun SafeContext.startBreaking(info: BreakInfo): Boolean {
773775
val ctx = info.context
774776

775-
if (info.couldReBreak) when (val reBreakResult = ReBreakManager.handleUpdate(info.context, info.request)) {
776-
is ReBreakResult.StillBreaking -> {
777-
primaryBreak = reBreakResult.breakInfo.apply {
778-
type = Primary
779-
ReBreakManager.clearReBreak()
780-
request.onStart?.invoke(ctx.blockPos)
781-
}
777+
if (info.couldReBreak) {
778+
when (val rebreakResult = RebreakManager.handleUpdate(info.context, info.request)) {
779+
is RebreakResult.StillBreaking -> {
780+
primaryBreak = rebreakResult.breakInfo.apply {
781+
type = Primary
782+
RebreakManager.clearRebreak()
783+
request.onStart?.invoke(ctx.blockPos)
784+
}
782785

783-
primaryBreak?.let { primary ->
784-
updateBreakProgress(primary)
786+
primaryBreak?.let { primary ->
787+
updateBreakProgress(primary)
788+
}
789+
return true
785790
}
786-
return true
787-
}
788-
is ReBreakResult.ReBroke -> {
789-
info.type = Rebreak
790-
info.nullify()
791-
info.request.onReBreak?.invoke(ctx.blockPos)
792-
return true
791+
is RebreakResult.Rebroke -> {
792+
info.type = Rebreak
793+
info.nullify()
794+
info.request.onReBreak?.invoke(ctx.blockPos)
795+
return true
796+
}
797+
else -> {}
793798
}
794-
else -> {}
795799
}
796800

797801
if (player.isBlockBreakingRestricted(world, ctx.blockPos, gamemode)) return false

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package com.lambda.interaction.request.breaking
1919

20-
import com.lambda.Lambda.mc
2120
import com.lambda.context.SafeContext
2221
import com.lambda.event.events.EntityEvent
2322
import com.lambda.event.events.WorldEvent
@@ -27,8 +26,9 @@ import com.lambda.interaction.request.PostActionHandler
2726
import com.lambda.interaction.request.breaking.BreakConfig.BreakConfirmationMode
2827
import com.lambda.interaction.request.breaking.BreakManager.lastPosStarted
2928
import com.lambda.interaction.request.breaking.BreakManager.matchesBlockItem
30-
import com.lambda.interaction.request.breaking.ReBreakManager.reBreak
29+
import com.lambda.interaction.request.breaking.RebreakManager.rebreak
3130
import com.lambda.module.modules.client.TaskFlowModule
31+
import com.lambda.threading.runSafe
3232
import com.lambda.util.BlockUtils.emptyState
3333
import com.lambda.util.BlockUtils.fluidState
3434
import com.lambda.util.BlockUtils.isEmpty
@@ -51,13 +51,16 @@ object BrokenBlockHandler : PostActionHandler<BreakInfo>() {
5151
override val pendingActions = LimitedDecayQueue<BreakInfo>(
5252
TaskFlowModule.build.maxPendingInteractions, TaskFlowModule.build.interactionTimeout * 50L
5353
) { info ->
54-
mc.world?.let { world ->
54+
runSafe {
5555
val pos = info.context.blockPos
5656
val loaded = world.isChunkLoaded(ChunkSectionPos.getSectionCoord(pos.x), ChunkSectionPos.getSectionCoord(pos.z))
57-
if (!loaded) return@let
57+
if (!loaded) return@runSafe
5858

59-
if (!info.broken) warn("${info.type} ${info::class.simpleName} at ${info.context.blockPos.toShortString()} timed out with cached state ${info.context.cachedState}")
60-
else if (!TaskFlowModule.ignoreItemDropWarnings) warn("${info.type} ${info::class.simpleName}'s item drop at ${info.context.blockPos.toShortString()} timed out")
59+
if (!info.broken) {
60+
warn("${info.type} ${info::class.simpleName} at ${info.context.blockPos.toShortString()} timed out with cached state ${info.context.cachedState}")
61+
} else if (!TaskFlowModule.ignoreItemDropWarnings) {
62+
warn("${info.type} ${info::class.simpleName}'s item drop at ${info.context.blockPos.toShortString()} timed out")
63+
}
6164

6265
if (!info.broken && info.breakConfig.breakConfirmation != BreakConfirmationMode.AwaitThenBreak) {
6366
world.setBlockState(info.context.blockPos, info.context.cachedState)
@@ -71,7 +74,7 @@ object BrokenBlockHandler : PostActionHandler<BreakInfo>() {
7174
listen<WorldEvent.BlockUpdate.Server>(priority = Int.MIN_VALUE) { event ->
7275
run {
7376
pendingActions.firstOrNull { it.context.blockPos == event.pos }
74-
?: if (reBreak?.context?.blockPos == event.pos) reBreak
77+
?: if (rebreak?.context?.blockPos == event.pos) rebreak
7578
else null
7679
}?.let { pending ->
7780
val currentState = pending.context.cachedState
@@ -101,7 +104,7 @@ object BrokenBlockHandler : PostActionHandler<BreakInfo>() {
101104
if (pending.callbacksCompleted) {
102105
pending.stopPending()
103106
if (lastPosStarted == pending.context.blockPos) {
104-
ReBreakManager.offerReBreak(pending)
107+
RebreakManager.offerRebreak(pending)
105108
}
106109
}
107110
return@listen
@@ -112,7 +115,7 @@ object BrokenBlockHandler : PostActionHandler<BreakInfo>() {
112115
if (it.entity !is ItemEntity) return@listen
113116
run {
114117
pendingActions.firstOrNull { info -> matchesBlockItem(info, it.entity) }
115-
?: reBreak?.let { info ->
118+
?: rebreak?.let { info ->
116119
return@run if (matchesBlockItem(info, it.entity)) info
117120
else null
118121
}
@@ -121,7 +124,7 @@ object BrokenBlockHandler : PostActionHandler<BreakInfo>() {
121124
if (pending.callbacksCompleted) {
122125
pending.stopPending()
123126
if (lastPosStarted == pending.context.blockPos) {
124-
ReBreakManager.offerReBreak(pending)
127+
RebreakManager.offerRebreak(pending)
125128
}
126129
}
127130
return@listen

src/main/kotlin/com/lambda/interaction/request/breaking/ReBreakManager.kt renamed to src/main/kotlin/com/lambda/interaction/request/breaking/RebreakManager.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ import net.minecraft.client.network.ClientPlayerEntity
3131
import net.minecraft.util.Hand
3232
import net.minecraft.world.BlockView
3333

34-
object ReBreakManager {
35-
var reBreak: BreakInfo? = null
34+
object RebreakManager {
35+
var rebreak: BreakInfo? = null
3636

3737
init {
3838
listen<TickEvent.Pre>(priority = Int.MIN_VALUE) {
39-
reBreak?.run {
39+
rebreak?.run {
4040
if (!progressedThisTick) {
4141
breakingTicks++
4242
progressedThisTick = true
@@ -45,27 +45,27 @@ object ReBreakManager {
4545
}
4646

4747
listenUnsafe<ConnectionEvent.Connect.Pre>(priority = Int.MIN_VALUE) {
48-
reBreak = null
48+
rebreak = null
4949
}
5050
}
5151

52-
fun offerReBreak(info: BreakInfo) {
52+
fun offerRebreak(info: BreakInfo) {
5353
if (!info.rebreakable) return
5454

55-
reBreak = info.apply {
55+
rebreak = info.apply {
5656
type = BreakInfo.BreakType.Rebreak
5757
breaking = true
5858
resetCallbacks()
5959
}
6060
info.request.onReBreakStart?.invoke(info.context.blockPos)
6161
}
6262

63-
fun clearReBreak() {
64-
reBreak = null
63+
fun clearRebreak() {
64+
rebreak = null
6565
}
6666

67-
fun couldReBreak(info: BreakInfo, player: ClientPlayerEntity, world: BlockView) =
68-
reBreak?.let { reBreak ->
67+
fun couldRebreak(info: BreakInfo, player: ClientPlayerEntity, world: BlockView) =
68+
rebreak?.let { reBreak ->
6969
val stack = if (info.breakConfig.swapMode.isEnabled())
7070
player.inventory.getStack(info.context.hotbarIndex)
7171
else player.mainHandStack
@@ -78,7 +78,7 @@ object ReBreakManager {
7878

7979
fun handleUpdate(ctx: BreakContext, breakRequest: BreakRequest) =
8080
runSafe {
81-
val reBreak = this@ReBreakManager.reBreak ?: return@runSafe ReBreakResult.Ignored
81+
val reBreak = this@RebreakManager.rebreak ?: return@runSafe RebreakResult.Ignored
8282

8383
reBreak.updateInfo(ctx, breakRequest)
8484

@@ -93,9 +93,9 @@ object ReBreakManager {
9393
swingHand(reBreak.breakConfig.swingType, Hand.MAIN_HAND)
9494
}
9595
BreakManager.breaksThisTick++
96-
ReBreakResult.ReBroke
96+
RebreakResult.Rebroke
9797
} else {
98-
ReBreakResult.StillBreaking(reBreak)
98+
RebreakResult.StillBreaking(reBreak)
9999
}
100100
}
101101
}

src/main/kotlin/com/lambda/interaction/request/breaking/ReBreakResult.kt renamed to src/main/kotlin/com/lambda/interaction/request/breaking/RebreakResult.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
package com.lambda.interaction.request.breaking
1919

20-
sealed class ReBreakResult {
21-
data object Ignored : ReBreakResult()
20+
sealed class RebreakResult {
21+
data object Ignored : RebreakResult()
2222

23-
data object ReBroke : ReBreakResult()
23+
data object Rebroke : RebreakResult()
2424

2525
class StillBreaking(
2626
val breakInfo: BreakInfo
27-
) : ReBreakResult()
27+
) : RebreakResult()
2828
}

0 commit comments

Comments
 (0)