Skip to content

Commit 9218421

Browse files
committed
bridging
1 parent e6674ba commit 9218421

File tree

1 file changed

+26
-5
lines changed
  • src/main/kotlin/com/lambda/module/modules/player

1 file changed

+26
-5
lines changed

src/main/kotlin/com/lambda/module/modules/player/Scaffold.kt

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,27 @@ import com.lambda.config.groups.HotbarSettings
2222
import com.lambda.config.groups.InteractionSettings
2323
import com.lambda.config.groups.InventorySettings
2424
import com.lambda.config.groups.RotationSettings
25+
import com.lambda.context.SafeContext
2526
import com.lambda.event.events.TickEvent
2627
import com.lambda.event.listener.SafeListener.Companion.listen
27-
import com.lambda.interaction.construction.blueprint.Blueprint.Companion.toStructure
2828
import com.lambda.interaction.construction.blueprint.StaticBlueprint.Companion.toBlueprint
2929
import com.lambda.interaction.construction.context.BuildContext
30+
import com.lambda.interaction.construction.context.PlaceContext
3031
import com.lambda.interaction.construction.result.PlaceResult
3132
import com.lambda.interaction.construction.simulation.BuildSimulator.simulate
3233
import com.lambda.interaction.construction.verify.TargetState
3334
import com.lambda.interaction.request.Request.Companion.submit
3435
import com.lambda.interaction.request.placing.PlaceRequest
3536
import com.lambda.module.Module
3637
import com.lambda.module.tag.ModuleTag
38+
import com.lambda.util.BlockUtils.blockPos
39+
import com.lambda.util.BlockUtils.blockState
3740
import com.lambda.util.KeyCode
3841
import com.lambda.util.KeyboardUtils.isKeyPressed
3942
import com.lambda.util.NamedEnum
43+
import com.lambda.util.math.distSq
4044
import com.lambda.util.world.raycast.InteractionMask
45+
import net.minecraft.util.math.BlockPos
4146
import net.minecraft.util.math.Direction
4247
import java.util.concurrent.ConcurrentLinkedQueue
4348

@@ -55,6 +60,7 @@ object Scaffold : Module(
5560
Inventory("Inventory")
5661
}
5762

63+
private val bridgeRange by setting("Bridge Range", 5, 0..5, 1, "The range at which blocks can be placed to help build support for the player").group(Group.General)
5864
private val descend by setting("Descend", KeyCode.UNBOUND, "Lower the place position by one to allow the player to lower y level").group(Group.General)
5965
private val buildConfig = BuildSettings(this, Group.Build)
6066
private val rotationConfig = RotationSettings(this, Group.Rotation)
@@ -66,19 +72,34 @@ object Scaffold : Module(
6672

6773
init {
6874
listen<TickEvent.Pre> {
69-
player
70-
.blockPos
71-
.offset(Direction.DOWN, if (isKeyPressed(descend.code)) 2 else 1)
72-
.toStructure(TargetState.Solid)
75+
val beneath = player.blockPos.offset(Direction.DOWN, if (isKeyPressed(descend.code)) 2 else 1)
76+
val placements = getPlacements(beneath)
77+
if (placements == null) return@listen
78+
placements
79+
.associate { it to TargetState.Solid }
7380
.toBlueprint()
7481
.simulate(player.eyePos, interactionConfig, rotationConfig, inventoryConfig, buildConfig)
7582
.filterIsInstance<PlaceResult.Place>()
7683
.let { results ->
7784
val contexts = results
7885
.map { it.context }
7986
.distinctBy { it.blockPos }
87+
.sortedWith { o1, o2 -> getBridgeCompareBy(beneath).compare(o1, o2) }
8088
submit(PlaceRequest(contexts, buildConfig, rotationConfig, hotbarConfig, pendingActions))
8189
}
8290
}
8391
}
92+
93+
private fun SafeContext.getPlacements(beneath: BlockPos): List<BlockPos>? {
94+
if (blockState(beneath).isSolidBlock(world, beneath)) return null
95+
96+
return BlockPos
97+
.iterateOutwards(beneath, bridgeRange, bridgeRange, bridgeRange)
98+
.map { it.blockPos }
99+
}
100+
101+
private fun getBridgeCompareBy(blockPos: BlockPos) =
102+
compareBy<PlaceContext> {
103+
it.blockPos.toCenterPos() distSq blockPos.toCenterPos()
104+
}
84105
}

0 commit comments

Comments
 (0)