Skip to content

Commit 2585445

Browse files
committed
cleaned up and removed essentially redundant context values
1 parent e325869 commit 2585445

File tree

12 files changed

+80
-101
lines changed

12 files changed

+80
-101
lines changed

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

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,45 +25,32 @@ import com.lambda.interaction.request.breaking.BreakRequest
2525
import com.lambda.interaction.request.hotbar.HotbarManager
2626
import com.lambda.interaction.request.hotbar.HotbarRequest
2727
import com.lambda.interaction.request.rotating.RotationRequest
28-
import com.lambda.util.world.raycast.RayCastUtils.distanceTo
2928
import net.minecraft.block.BlockState
3029
import net.minecraft.block.FallingBlock
3130
import net.minecraft.util.hit.BlockHitResult
3231
import net.minecraft.util.math.BlockPos
33-
import net.minecraft.util.math.Direction
34-
import net.minecraft.util.math.Vec3d
3532
import java.awt.Color
3633

3734
data class BreakContext(
38-
override val pov: Vec3d,
3935
override val result: BlockHitResult,
4036
override val rotation: RotationRequest,
41-
override var checkedState: BlockState,
42-
override val targetState: TargetState,
4337
override var hotbarIndex: Int,
44-
var instantBreak: Boolean,
45-
) : BuildContext {
38+
override var cachedState: BlockState,
39+
override val targetState: TargetState,
40+
var instantBreak: Boolean
41+
) : BuildContext() {
4642
private val baseColor = Color(222, 0, 0, 25)
4743
private val sideColor = Color(222, 0, 0, 100)
4844

4945
override val expectedPos: BlockPos
5046
get() = result.blockPos
5147

52-
override val distance: Double by lazy {
53-
result.distanceTo(pov)
54-
}
55-
56-
fun exposedSides(ctx: SafeContext) =
57-
Direction.entries.filter {
58-
ctx.world.isAir(expectedPos.offset(it))
59-
}
60-
61-
override val expectedState: BlockState = checkedState.fluidState.blockState
48+
override val expectedState: BlockState = cachedState.fluidState.blockState
6249

6350
override fun compareTo(other: BuildContext): Int {
6451
return when (other) {
6552
is BreakContext -> compareByDescending<BreakContext> {
66-
if (it.checkedState.block is FallingBlock) it.expectedPos.y else 0
53+
if (it.cachedState.block is FallingBlock) it.expectedPos.y else 0
6754
}.thenBy {
6855
it.instantBreak
6956
}.thenBy {
@@ -77,8 +64,8 @@ data class BreakContext(
7764
}
7865

7966
override fun SafeContext.buildRenderer() {
80-
withState(checkedState, expectedPos, baseColor, DirectionMask.ALL.exclude(result.side))
81-
withState(checkedState, expectedPos, sideColor, result.side)
67+
withState(cachedState, expectedPos, baseColor, DirectionMask.ALL.exclude(result.side))
68+
withState(cachedState, expectedPos, sideColor, result.side)
8269
}
8370

8471
fun requestDependencies(breakRequest: BreakRequest, minKeepTicks: Int = 0): Boolean {

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,24 @@
1717

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

20+
import com.lambda.Lambda.mc
2021
import com.lambda.interaction.construction.result.Drawable
2122
import com.lambda.interaction.construction.verify.TargetState
2223
import com.lambda.interaction.request.rotating.RotationRequest
2324
import net.minecraft.block.BlockState
2425
import net.minecraft.util.hit.BlockHitResult
2526
import net.minecraft.util.math.BlockPos
26-
import net.minecraft.util.math.Vec3d
2727

28-
interface BuildContext : Comparable<BuildContext>, Drawable {
29-
val pov: Vec3d
30-
val result: BlockHitResult
31-
val rotation: RotationRequest
32-
val distance: Double
33-
val expectedState: BlockState
34-
val targetState: TargetState
35-
val expectedPos: BlockPos
36-
val checkedState: BlockState
37-
val hotbarIndex: Int
28+
abstract class BuildContext : Comparable<BuildContext>, Drawable {
29+
abstract val result: BlockHitResult
30+
abstract val rotation: RotationRequest
31+
abstract val hotbarIndex: Int
32+
abstract val expectedPos: BlockPos
33+
abstract val cachedState: BlockState
34+
abstract val expectedState: BlockState
35+
abstract val targetState: TargetState
36+
37+
val distance by lazy {
38+
mc.player?.eyePos?.distanceTo(result.pos) ?: Double.MAX_VALUE
39+
}
3840
}

common/src/main/kotlin/com/lambda/interaction/construction/context/InteractionContext.kt

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

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

20-
import com.lambda.config.groups.InteractionConfig
2120
import com.lambda.context.SafeContext
2221
import com.lambda.graphics.renderer.esp.DirectionMask
2322
import com.lambda.graphics.renderer.esp.DirectionMask.exclude
@@ -31,30 +30,26 @@ import com.lambda.util.BlockUtils.blockState
3130
import net.minecraft.block.BlockState
3231
import net.minecraft.util.hit.BlockHitResult
3332
import net.minecraft.util.math.BlockPos
34-
import net.minecraft.util.math.Vec3d
3533
import java.awt.Color
3634

3735
class InteractionContext(
38-
override val pov: Vec3d,
3936
override val result: BlockHitResult,
4037
override val rotation: RotationRequest,
41-
override val distance: Double,
38+
override var hotbarIndex: Int,
39+
override val expectedPos: BlockPos,
40+
override val cachedState: BlockState,
4241
override val expectedState: BlockState,
4342
override val targetState: TargetState,
44-
override val expectedPos: BlockPos,
45-
override val checkedState: BlockState,
46-
override var hotbarIndex: Int,
47-
val interaction: InteractionConfig,
48-
) : BuildContext {
43+
) : BuildContext() {
4944
private val baseColor = Color(35, 254, 79, 25)
5045
private val sideColor = Color(35, 254, 79, 100)
5146

5247
override fun compareTo(other: BuildContext) =
5348
when {
5449
other is InteractionContext -> compareBy<BuildContext> {
55-
BlockUtils.fluids.indexOf(it.checkedState.fluidState.fluid)
50+
BlockUtils.fluids.indexOf(it.cachedState.fluidState.fluid)
5651
}.thenByDescending {
57-
it.checkedState.fluidState.level
52+
it.cachedState.fluidState.level
5853
}.thenBy {
5954
it.rotation.target.angleDistance
6055
}.thenBy {

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,29 @@ import com.lambda.util.BlockUtils.blockState
3131
import net.minecraft.block.BlockState
3232
import net.minecraft.util.hit.BlockHitResult
3333
import net.minecraft.util.math.BlockPos
34-
import net.minecraft.util.math.Vec3d
3534
import java.awt.Color
3635

3736
data class PlaceContext(
38-
override val pov: Vec3d,
3937
override val result: BlockHitResult,
4038
override val rotation: RotationRequest,
41-
override val distance: Double,
42-
override val expectedState: BlockState,
43-
override val checkedState: BlockState,
4439
override val hotbarIndex: Int,
4540
override val expectedPos: BlockPos,
41+
override val cachedState: BlockState,
42+
override val expectedState: BlockState,
4643
override val targetState: TargetState,
4744
val sneak: Boolean,
4845
val insideBlock: Boolean,
49-
val currentDirIsInvalid: Boolean = false,
50-
) : BuildContext {
46+
val currentDirIsInvalid: Boolean = false
47+
) : BuildContext() {
5148
private val baseColor = Color(35, 188, 254, 25)
5249
private val sideColor = Color(35, 188, 254, 100)
5350

5451
override fun compareTo(other: BuildContext) =
5552
when (other) {
5653
is PlaceContext -> compareBy<PlaceContext> {
57-
BlockUtils.fluids.indexOf(it.checkedState.fluidState.fluid)
54+
BlockUtils.fluids.indexOf(it.cachedState.fluidState.fluid)
5855
}.thenByDescending {
59-
it.checkedState.fluidState.level
56+
it.cachedState.fluidState.level
6057
}.thenBy {
6158
it.sneak == mc.player?.isSneaking
6259
}.thenBy {

common/src/main/kotlin/com/lambda/interaction/construction/simulation/BuildSimulator.kt

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,11 @@ object BuildSimulator {
414414
}
415415

416416
return if (placing) checkPlaceOn(pos, validHits, eye, preProcessing, targetState, place, rotation, interact, inventory)
417-
else checkInteractOn(pos, eye, item, validHits, expectedState, targetState, state, rotation, interact, inventory)
417+
else checkInteractOn(pos, item, validHits, expectedState, targetState, state, rotation, interact, inventory)
418418
}
419419

420420
private fun SafeContext.checkInteractOn(
421421
pos: BlockPos,
422-
eye: Vec3d,
423422
item: Item,
424423
validHits: MutableList<CheckedHit>,
425424
expectedState: BlockState,
@@ -433,16 +432,13 @@ object BuildSimulator {
433432
val checkedResult = checkedHit.hit
434433
val rotationTarget = lookAt(checkedHit.targetRotation, 0.001)
435434
val context = InteractionContext(
436-
eye,
437435
checkedResult.blockResult ?: return null,
438436
RotationRequest(rotationTarget, rotation),
439-
eye.distanceTo(checkedResult.pos),
440-
expectedState,
441-
targetState,
437+
player.inventory.selectedSlot,
442438
pos,
443439
currentState,
444-
player.inventory.selectedSlot,
445-
interact
440+
expectedState,
441+
targetState
446442
)
447443

448444
val stackSelection = item.select()
@@ -596,18 +592,16 @@ object BuildSimulator {
596592
} else lookAt(rot, 0.001)
597593

598594
val placeContext = PlaceContext(
599-
eye,
600595
blockHit,
601596
RotationRequest(rotationRequest, rotation),
602-
eye.distanceTo(blockHit.pos),
603-
resultState,
604-
blockState(blockHit.blockPos.offset(blockHit.side)),
605597
player.inventory.selectedSlot,
606598
context.blockPos,
599+
resultState,
600+
blockState(blockHit.blockPos.offset(blockHit.side)),
607601
targetState,
608602
shouldSneak,
609603
false,
610-
currentDirIsInvalid,
604+
currentDirIsInvalid
611605
)
612606

613607
val currentHandStack = player.getStackInHand(Hand.MAIN_HAND)
@@ -693,11 +687,6 @@ object BuildSimulator {
693687
val verify: CheckedHit.() -> Boolean = {
694688
hit.blockResult?.blockPos == pos
695689
}
696-
val targetState = if (!state.fluidState.isEmpty) {
697-
TargetState.State(state.fluidState.blockState)
698-
} else {
699-
TargetState.Air
700-
}
701690

702691
/* the player is buried inside the block */
703692
if (boxes.any { it.contains(eye) }) {
@@ -706,12 +695,11 @@ object BuildSimulator {
706695
lookAtBlock(pos, config = interact), rotation
707696
)
708697
val breakContext = BreakContext(
709-
eye,
710698
blockHit,
711699
rotationRequest,
712-
state,
713-
targetState,
714700
player.inventory.selectedSlot,
701+
state,
702+
TargetState.Empty,
715703
instantBreakable(state, pos, breaking.breakThreshold)
716704
)
717705
acc.add(BreakResult.Break(pos, breakContext))
@@ -759,9 +747,7 @@ object BuildSimulator {
759747
val request = RotationRequest(target, rotation)
760748
val instant = instantBreakable(state, pos, breaking.breakThreshold)
761749

762-
val breakContext = BreakContext(
763-
eye, blockHit, request, state, targetState, player.inventory.selectedSlot, instant
764-
)
750+
val breakContext = BreakContext(blockHit, request, player.inventory.selectedSlot, state, TargetState.Empty, instant)
765751

766752
if (gamemode.isCreative) {
767753
acc.add(BreakResult.Break(pos, breakContext))

common/src/main/kotlin/com/lambda/interaction/construction/verify/TargetState.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,19 @@ import net.minecraft.util.math.Direction
3434
sealed class TargetState(val type: Type) : StateMatcher {
3535

3636
enum class Type {
37-
AIR, SOLID, SUPPORT, STATE, BLOCK, STACK
37+
EMPTY, AIR, SOLID, SUPPORT, STATE, BLOCK, STACK
38+
}
39+
40+
data object Empty : TargetState(Type.EMPTY) {
41+
override fun toString() = "Empty"
42+
43+
override fun matches(state: BlockState, pos: BlockPos, world: ClientWorld, ignoredProperties: Collection<Property<*>>) =
44+
state.isEmpty
45+
46+
override fun getStack(world: ClientWorld, pos: BlockPos): ItemStack =
47+
ItemStack.EMPTY
48+
49+
override fun isEmpty() = true
3850
}
3951

4052
data object Air : TargetState(Type.AIR) {

common/src/main/kotlin/com/lambda/interaction/request/breaking/BreakInfo.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ data class BreakInfo(
107107
}
108108

109109
private fun getBreakTextureProgress(player: PlayerEntity, world: ClientWorld): Int {
110-
val breakDelta = context.checkedState.calcItemBlockBreakingDelta(player, world, context.expectedPos, player.mainHandStack)
110+
val breakDelta = context.cachedState.calcItemBlockBreakingDelta(player, world, context.expectedPos, player.mainHandStack)
111111
val progress = (breakDelta * breakingTicks) / getBreakThreshold()
112112
return if (progress > 0.0f) (progress * 10.0f).toInt() else -1
113113
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ object BreakManager : RequestHandler<BreakRequest>(
151151
.firstOrNull { it.context.expectedPos == event.pos }
152152
?.let { info ->
153153
// if not broken
154-
if (isNotBroken(info.context.checkedState, event.newState)) {
155-
this@BreakManager.warn("Break at ${event.pos.toShortString()} was rejected with ${event.newState} instead of ${info.context.checkedState.emptyState}")
154+
if (isNotBroken(info.context.cachedState, event.newState)) {
155+
this@BreakManager.warn("Break at ${event.pos.toShortString()} was rejected with ${event.newState} instead of ${info.context.cachedState.emptyState}")
156156
// update the checked state
157-
info.context.checkedState = event.newState
157+
info.context.cachedState = event.newState
158158
return@listen
159159
}
160160
destroyBlock(info)
@@ -189,7 +189,7 @@ object BreakManager : RequestHandler<BreakRequest>(
189189
.forEach { info ->
190190
val config = info.breakConfig
191191
if (!config.renders) return@listen
192-
val breakDelta = info.context.checkedState.calcBreakDelta(
192+
val breakDelta = info.context.cachedState.calcBreakDelta(
193193
player,
194194
world,
195195
info.context.expectedPos,
@@ -200,7 +200,7 @@ object BreakManager : RequestHandler<BreakRequest>(
200200
if (info.isPrimary) it * (2 - info.breakConfig.breakThreshold)
201201
else it
202202
}.toDouble()
203-
val state = info.context.checkedState
203+
val state = info.context.cachedState
204204
val boxes = state.getOutlineShape(world, info.context.expectedPos).boundingBoxes.map {
205205
it.offset(info.context.expectedPos)
206206
}
@@ -278,7 +278,7 @@ object BreakManager : RequestHandler<BreakRequest>(
278278
.forEach { info ->
279279
if (info.updatedProgressThisTick) return@forEach
280280
val minKeepTicks = if (info.isSecondary) {
281-
val breakDelta = info.context.checkedState.calcBreakDelta(
281+
val breakDelta = info.context.cachedState.calcBreakDelta(
282282
player,
283283
world,
284284
info.context.expectedPos,
@@ -363,7 +363,7 @@ object BreakManager : RequestHandler<BreakRequest>(
363363
}
364364

365365
val blockState = blockState(ctx.expectedPos)
366-
val hardness = ctx.checkedState.getHardness(world, ctx.expectedPos)
366+
val hardness = ctx.cachedState.getHardness(world, ctx.expectedPos)
367367

368368
return blockState.isNotEmpty && hardness != 600f && hardness != -1f
369369
}
@@ -764,7 +764,7 @@ object BreakManager : RequestHandler<BreakRequest>(
764764
*/
765765
fun matchesBlockItem(info: BreakInfo, entity: ItemEntity): Boolean {
766766
val inRange = info.context.expectedPos.toCenterPos().isInRange(entity.pos, 0.5)
767-
val correctMaterial = info.context.checkedState.block == entity.stack.item.block
767+
val correctMaterial = info.context.cachedState.block == entity.stack.item.block
768768
return inRange && correctMaterial
769769
}
770770

0 commit comments

Comments
 (0)