Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Better DSL
  • Loading branch information
Avanatiker committed Aug 25, 2024
commit bab19fd974d399d252d4e773e47b881147493da5
10 changes: 5 additions & 5 deletions common/src/main/kotlin/com/lambda/config/groups/Targeting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ abstract class Targeting(
validate(player, entity)
}

return@runSafe entitySearch<LivingEntity> {
range(targetingRange)
filter(predicate)
comparator { priority.factor(this, it) }
}.minBy()
return@runSafe entitySearch<LivingEntity>(targetingRange) {
predicate(it)
}.minBy {
priority.factor(this, it)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,13 @@ object BlockTest : Module(

init {
listener<RenderEvent.StaticESP> {
blockSearch {
range(range)
step(step)

filter { _, block -> block.isOf(Blocks.DIAMOND_BLOCK) }
iterator { pos, state ->
state.getOutlineShape(world, pos).boundingBoxes.forEach { box ->
it.renderer.build(box.offset(pos), filledColor, outlineColor)
}
blockSearch(range, step) { _, state ->
state.isOf(Blocks.DIAMOND_BLOCK)
}.forEach { (pos, state) ->
state.getOutlineShape(world, pos).boundingBoxes.forEach { box ->
it.renderer.build(box.offset(pos), filledColor, outlineColor)
}
}.build()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ object RenderTest : Module(

init {
listener<RenderEvent.DynamicESP> {
entitySearch<LivingEntity> {
range(8)

iterator { entity ->
entitySearch<LivingEntity>(8.0)
.forEach { entity ->
it.renderer.build(entity.dynamicBox, filledColor, outlineColor)
}
}.build()
}

listener<RenderEvent.StaticESP> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.lambda.module.modules.movement

import com.lambda.context.SafeContext
import com.lambda.event.events.PacketEvent
import com.lambda.event.events.TickEvent
import com.lambda.event.listener.SafeListener.Companion.listener
Expand All @@ -18,18 +17,20 @@ object EntityControl : Module(
) {
private val forceMount by setting("Force Mount", true, description = "Attempts to force mount chested entities.").apply {
onValueChange { _, _ ->
resetHorseFlags()
resetMounts()
}
}
private val modified = mutableSetOf<AbstractHorseEntity>()

init {
listener<TickEvent.Pre> {
if (forceMount) {
entitySearch<AbstractHorseEntity> {
range(8)
iterator { it.setHorseFlag(4, true) }
if (!forceMount) return@listener

entitySearch<AbstractHorseEntity>(8.0)
.forEach {
it.setHorseFlag(4, true)
modified.add(it)
}
}
}

listener<PacketEvent.Send.Pre> { event ->
Expand All @@ -44,14 +45,11 @@ object EntityControl : Module(
}

onDisable {
resetHorseFlags()
resetMounts()
}
}

fun SafeContext.resetHorseFlags() {
entitySearch<AbstractHorseEntity> {
range(8)
iterator { it.updateSaddle() }
}
private fun resetMounts() {
modified.forEach { it.updateSaddle() }
}
}
16 changes: 6 additions & 10 deletions common/src/main/kotlin/com/lambda/module/modules/movement/Speed.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,14 @@ object Speed : Module(

var boostAmount = 0.0

entitySearch<LivingEntity> {
range(3.0)
filter { player.boundingBox.expand(1.0) in it.boundingBox && it !is ArmorStandEntity }
iterator { boostAmount += 0.08 * grimEntityBoost }
}
boostAmount += entitySearch<LivingEntity>(3.0) {
player.boundingBox.expand(1.0) in it.boundingBox && it !is ArmorStandEntity
}.sumOf { 0.08 * grimEntityBoost }

if (grimBoatBoost > 0.0) {
entitySearch<BoatEntity> {
range(4.0)
filter { player.boundingBox in it.boundingBox.expand(0.01) }
iterator { boostAmount += grimBoatBoost }
}
boostAmount += entitySearch<BoatEntity>(4.0) {
player.boundingBox in it.boundingBox.expand(0.01)
}.sumOf { grimBoatBoost }
}

addSpeed(boostAmount)
Expand Down
14 changes: 8 additions & 6 deletions common/src/main/kotlin/com/lambda/util/collections/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ inline fun <reified R, C : MutableCollection<in R>> Iterable<*>.filterPointer(
) {
var index = 0

for (element in this) {
val fulfilled = predicate(element as R ?: continue)
forEach { element ->
val fulfilled = predicate(element as R)

if (fulfilled && destination != null) {
destination.add(element)
Expand All @@ -50,16 +50,18 @@ inline fun <reified R, C : MutableCollection<in R>> Iterable<*>.filterPointer(
* @param predicate The predicate function that determines whether an element should be included based on its type and other criteria.
*/
inline fun <R : Any, C : MutableCollection<in R>> Iterable<*>.filterPointer(
kclass: KClass<out R>,
kClass: KClass<out R>,
destination: C?,
iterator: (R) -> Unit,
predicate: (R) -> Boolean,
) {
for (element in this) {
val fulfilled = kclass.isInstance(element) && predicate(element as R)
forEach { element ->
// Cannot be replaced with reified type due to type erasure
(element as? R) ?: return@forEach
val fulfilled = kClass.isInstance(element) && predicate(element)

if (fulfilled && destination != null) {
destination.add(element as R)
destination.add(element)
iterator(element)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.lambda.util.primitives.extension

import com.lambda.util.world.FastVector
import com.lambda.util.world.x
import com.lambda.util.world.y
import com.lambda.util.world.z
import net.minecraft.block.BlockState
import net.minecraft.block.Blocks
import net.minecraft.fluid.FluidState
Expand Down Expand Up @@ -27,3 +31,6 @@ fun World.getFluidState(x: Int, y: Int, z: Int): FluidState {
val section = chunk.getSection(sectionIndex)
return section.getFluidState(x and 0xF, y and 0xF, z and 0xF)
}

fun World.getBlockState(vec: FastVector): BlockState = getBlockState(vec.x, vec.y, vec.z)
fun World.getFluidState(vec: FastVector): FluidState = getFluidState(vec.x, vec.y, vec.z)
141 changes: 41 additions & 100 deletions common/src/main/kotlin/com/lambda/util/world/BlockDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,131 +16,72 @@ import net.minecraft.util.math.Vec3i
annotation class BlockDslMarker

/**
* The `BlockDsl` class provides a DSL for performing block search operations
* within a specified range in a Minecraft world. It allows for filtering and iterating blocks
* around a given position.
* The [BlockDsl] class provides a DSL for performing block search operations
* within a specified range in a Minecraft world.
* It allows for filtering blocks around a given position.
*
* @param safeContext The context in which the block search is performed, providing safe access to world data.
* @param pos The position from which to start the block search.
*
* ### Usage Example:
*
* ```kotlin
* val blocks = blockSearch {
* range(10.0) // Search for blocks within a 10 block radius
* step(2.0) // Check for blocks every 2 block
* filter { _, block -> block.isOf(Blocks.DIAMOND_BLOCK) } // Filter out blocks that are not diamond blocks
* iterator { pos, state -> println("Found diamond block at: $pos") } // Print the position of each diamond block
* }.build() // Finalize the block search
* val blocks = blockSearch(range = Vec3i(10, 10, 10)) {
* it.isOf(Blocks.DIAMOND_BLOCK) // Filter out blocks that are not diamond blocks
* }
*
* println("Found ${blocks.size} diamond blocks.")
* blocks.forEach { (pos, state) ->
* println("Found diamond block at: $pos")
* }
* ```
*/
@BlockDslMarker
class BlockDsl(
private val safeContext: SafeContext,
pos: BlockPos,
private val range: Vec3i,
private val step: Vec3i,
private val predicate: (BlockPos, BlockState) -> Boolean
) {
private val fastVector = pos.toFastVec()
private val receiver: MutableMap<FastVector, BlockState> = mutableMapOf()

private var range: FastVector = MAGICVECTOR times 8
private var step: FastVector = MAGICVECTOR
private var predicate: (FastVector, BlockState) -> Boolean = { _, _ -> true }
private var iterator: (FastVector, BlockState) -> Unit = { _, _ -> }

/**
* Sets the vector representing the maximum distances from the position to search for blocks.
*/
fun range(range: Int): BlockDsl {
this.range = fastVectorOf(range, range, range)
return this
}

/**
* Sets the vector representing the maximum distances from the position to search for blocks.
*/
fun range(range: Double): BlockDsl {
this.range = fastVectorOf(range.toInt(), range.toInt(), range.toInt())
return this
}

/**
* Sets the vector representing the maximum distances from the position to search for blocks.
*/
fun range(range: Vec3i): BlockDsl {
this.range = range.toFastVec()
return this
}

/**
* Sets the vector representing the maximum distances from the position to search for blocks.
*/
fun range(range: Vec3d): BlockDsl {
this.range = range.toFastVec()
return this
}

/**
* Sets the vector representing the intervals at which to check for blocks.
*/
fun step(step: Int): BlockDsl {
this.step = fastVectorOf(step, step, step)
return this
}

/**
* Sets the vector representing the intervals at which to check for blocks.
*/
fun step(step: Double): BlockDsl {
this.step = fastVectorOf(step.toInt(), step.toInt(), step.toInt())
return this
}

/**
* Sets the vector representing the intervals at which to check for blocks.
*/
fun step(step: Vec3i): BlockDsl {
this.step = step.toFastVec()
return this
}

/**
* Sets the vector representing the intervals at which to check for blocks.
*/
fun step(step: Vec3d): BlockDsl {
this.step = step.toFastVec()
return this
}

/**
* Sets a predicate to filter blocks.
*/
fun filter(predicate: (BlockPos, BlockState) -> Boolean): BlockDsl {
this.predicate = { pos, state -> predicate(pos.toBlockPos(), state) }
return this
}

/**
* Sets an iterator to perform operations on each block.
*/
fun iterator(iterator: (BlockPos, BlockState) -> Unit): BlockDsl {
this.iterator = { pos, state -> iterator(pos.toBlockPos(), state) }
return this
}

fun build(): Map<BlockPos, BlockState> {
safeContext.internalSearchBlocks(fastVector, range, step, receiver, predicate, iterator)
safeContext.internalSearchBlocks(fastVector, range.toFastVec(), step.toFastVec(), receiver, { pos, state -> predicate(pos.toBlockPos(), state) }, { _, _ -> })
return receiver.mapKeys { it.key.toBlockPos() }
}
}

/**
* Searches for blocks around the player's position and applies the specified block operations.
* Don't forget to call [BlockDsl.build] to finalize the search.
*
* @param pos The position around which to search for blocks.
* @param block The block operations to apply.
* @param pos The position around which to search for blocks. Defaults to the player's current position.
* @param range The `x`, `y`, `z` range around the position to search for blocks.
* @param step The `x`, `y`, `z` step intervals at which to check for blocks.
* @param predicate The predicate to filter blocks.
* @return A map of block positions and their states matching the predicate within the specified range.
*/
fun SafeContext.blockSearch(pos: BlockPos = player.blockPos, block: (@BlockDslMarker BlockDsl).() -> Unit) = BlockDsl(this, pos).apply(block)
fun SafeContext.blockSearch(
range: Vec3i,
step: Vec3i,
pos: BlockPos = player.blockPos,
predicate: (BlockPos, BlockState) -> Boolean
): Map<BlockPos, BlockState> =
BlockDsl(this, pos, range, step, predicate).build()

/**
* Searches for blocks around the player's position and applies the specified block operations.
*
* @param pos The position around which to search for blocks. Defaults to the player's current position.
* @param range The range around the position to search for blocks.
* @param step The step intervals at which to check for blocks.
* @param predicate The predicate to filter blocks.
* @return A map of block positions and their states matching the predicate within the specified range.
*/
fun SafeContext.blockSearch(
range: Int,
step: Int,
pos: BlockPos = player.blockPos,
predicate: (BlockPos, BlockState) -> Boolean
): Map<BlockPos, BlockState> =
blockSearch(Vec3i(range, range, range), Vec3i(step, step, step), pos, predicate)
Loading