Skip to content

Commit ca435be

Browse files
authored
[1.20.4] Ref: Inline setting event functions (#104)
This pull request changes the `AbstractSetting` event subscribers such as `onValueChanged`, `onValueSet` and others to facilitate subscription without having to wrap it in an `apply block` Before: ```kt private val mesh by setting("Mesh", true, "Connect similar adjacent blocks").apply { onValueSet(::rebuildMesh) } ``` After ```kt private val mesh by setting("Mesh", true, "Connect similar adjacent blocks").onValueSet(::rebuildMesh) ``` Instead, we call `apply` in the event subscription event ```kt fun onValueSet(block: (from: T, to: T) -> Unit) = apply { listeners.add(ValueListener(false, block)) } ```
1 parent 9d6b130 commit ca435be

File tree

9 files changed

+31
-47
lines changed

9 files changed

+31
-47
lines changed

common/src/main/kotlin/com/lambda/config/AbstractSetting.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import net.minecraft.command.CommandRegistryAccess
3939
import java.lang.reflect.Type
4040
import kotlin.properties.Delegates
4141
import kotlin.reflect.KProperty
42+
import kotlin.to
4243

4344
/**
4445
* Represents a setting with a [defaultValue], [visibility] condition, and [description].
@@ -125,19 +126,19 @@ abstract class AbstractSetting<T : Any>(
125126
* Will only register changes of the variable, not the content of the variable!
126127
* E.g., if the variable is a list, it will only register if the list reference changes, not if the content of the list changes.
127128
*/
128-
fun onValueChange(block: SafeContext.(from: T, to: T) -> Unit) {
129+
fun onValueChange(block: SafeContext.(from: T, to: T) -> Unit) = apply {
129130
listeners.add(ValueListener(true) { from, to ->
130131
runSafe {
131132
block(from, to)
132133
}
133134
})
134135
}
135136

136-
fun onValueChangeUnsafe(block: (from: T, to: T) -> Unit) {
137+
fun onValueChangeUnsafe(block: (from: T, to: T) -> Unit) = apply {
137138
listeners.add(ValueListener(true, block))
138139
}
139140

140-
fun onValueSet(block: (from: T, to: T) -> Unit) {
141+
fun onValueSet(block: (from: T, to: T) -> Unit) = apply {
141142
listeners.add(ValueListener(false, block))
142143
}
143144

common/src/main/kotlin/com/lambda/module/HudModule.kt

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,19 @@ abstract class HudModule(
5252
override val width get() = this@HudModule.width
5353
override val height get() = this@HudModule.height
5454

55-
override val autoDocking by setting("Auto Docking", true).apply {
56-
onValueChange { _, _ ->
57-
autoDocking()
58-
}
59-
}
55+
override val autoDocking by setting("Auto Docking", true).onValueChange { _, _ -> autoDocking() }
6056

61-
override var dockingH by setting("Docking H", HAlign.LEFT) { !autoDocking }.apply {
62-
onValueChange { from, to ->
57+
override var dockingH by setting("Docking H", HAlign.LEFT) { !autoDocking }
58+
.onValueChange { from, to ->
6359
val delta = to.multiplier - from.multiplier
6460
relativePosX += delta * (size.x - screenSize.x)
6561
}
66-
}
6762

68-
override var dockingV by setting("Docking V", VAlign.TOP) { !autoDocking }.apply {
69-
onValueChange { from, to ->
63+
override var dockingV by setting("Docking V", VAlign.TOP) { !autoDocking }
64+
.onValueChange { from, to ->
7065
val delta = to.multiplier - from.multiplier
7166
relativePosY += delta * (size.y - screenSize.y)
7267
}
73-
}
7468
}
7569

7670
var position by rectHandler::position

common/src/main/kotlin/com/lambda/module/modules/client/DiscordRPC.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ object DiscordRPC : Module(
7070

7171
/* Party settings */
7272
private val enableParty by setting("Enable Party", true, description = "Allows you to create parties.") { page == Page.Party }
73-
private val maxPlayers by setting("Max Players", 10, 2..20) { page == Page.Party }
74-
.apply { onValueChange { _, _ -> if (player.isPartyOwner) edit() } }
73+
private val maxPlayers by setting("Max Players", 10, 2..20) { page == Page.Party }.onValueChange { _, _ -> if (player.isPartyOwner) edit() }
7574

7675
private val rpc = KDiscordIPC(Lambda.APP_ID, scope = EventFlow.lambdaScope)
7776
private var startup = System.currentTimeMillis()

common/src/main/kotlin/com/lambda/module/modules/client/GuiSettings.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,8 @@ object GuiSettings : Module(
3535
private val page by setting("Page", Page.General)
3636

3737
// General
38-
private val scaleSetting by setting("Scale", 100, 50..300, 1, unit = "%", visibility = { page == Page.General }).apply {
39-
onValueSet { _, _ ->
40-
lastChange = System.currentTimeMillis()
41-
}
42-
}
38+
private val scaleSetting by setting("Scale", 100, 50..300, 1, unit = "%", visibility = { page == Page.General })
39+
.onValueSet { _, _ -> lastChange = System.currentTimeMillis() }
4340

4441
// Colors
4542
val primaryColor by setting("Primary Color", Color(130, 200, 255), visibility = { page == Page.Colors })

common/src/main/kotlin/com/lambda/module/modules/movement/Speed.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ object Speed : Module(
4949
defaultTags = setOf(ModuleTag.MOVEMENT)
5050
) {
5151
@JvmStatic
52-
val mode by setting("Mode", Mode.GRIM_STRAFE).apply {
53-
onValueChange { _, _ ->
54-
Speed.reset()
55-
}
56-
}
52+
val mode by setting("Mode", Mode.GRIM_STRAFE).onValueChange { _, _ -> reset() }
5753

5854
// Grim
5955
private val diagonal by setting("Diagonal", true) { mode == Mode.GRIM_STRAFE }

common/src/main/kotlin/com/lambda/module/modules/network/PacketLimiter.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,11 @@ object PacketLimiter : Module(
3434
defaultTags = setOf(ModuleTag.NETWORK)
3535
) {
3636
private var packetQueue = LimitedDecayQueue<PacketEvent.Send.Pre>(99, 1000)
37-
private val limit by setting("Limit", 99, 1..100, 1, "The maximum amount of packets to send per given time interval", unit = " packets").apply {
38-
onValueChange { _, to ->
39-
packetQueue.setMaxSize(to)
40-
}
41-
}
42-
private val interval by setting("Duration", 1000L, 1L..1000L, 50L, "The interval / duration in milliseconds to limit packets for", unit = " ms").apply {
43-
onValueChange { _, to ->
44-
packetQueue.setDecayTime(to)
45-
}
46-
}
37+
private val limit by setting("Limit", 99, 1..100, 1, "The maximum amount of packets to send per given time interval", unit = " packets")
38+
.onValueChange { _, to -> packetQueue.setMaxSize(to) }
39+
40+
private val interval by setting("Duration", 1000L, 1L..1000L, 50L, "The interval / duration in milliseconds to limit packets for", unit = " ms")
41+
.onValueChange { _, to -> packetQueue.setDecayTime(to) }
4742

4843
private val defaultIgnorePackets = setOf(
4944
CommonPongC2SPacket::class,

common/src/main/kotlin/com/lambda/module/modules/player/PacketMine.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ object PacketMine : Module(
8989
private val emptyReBreakDelay by setting("Empty Re-Break Delay", 0, 0..10, 1, "The delay between attempting to re-break the block if the block is currently empty", " ticks", visibility = { page == Page.ReBreak && reBreak.isFastAutomatic()})
9090
private val renderIfEmpty by setting("Render If Empty", false, "Draws the renders even if the re-break position is empty in the world", visibility = { page == Page.ReBreak && reBreak.isEnabled() })
9191

92-
private val queueBlocks by setting("Queue Blocks", false, "Queues any blocks you click for breaking", visibility = { page == Page.Queue }).apply { this.onValueSet { _, to -> if (!to) blockQueue.clear() } }
92+
private val queueBlocks by setting("Queue Blocks", false, "Queues any blocks you click for breaking", visibility = { page == Page.Queue })
93+
.onValueSet { _, to -> if (!to) blockQueue.clear() }
94+
9395
private val reverseQueueOrder by setting("Reverse Queue Order", false, "Breaks the latest addition to the queue first", visibility = { page == Page.Queue && queueBlocks})
9496
private val queueBreakDelay by setting("Break Delay", 0, 0..5, 1, "The delay after breaking a block to break the next queue block", " ticks", visibility = { page == Page.Queue && queueBlocks })
9597

common/src/main/kotlin/com/lambda/module/modules/render/BlockESP.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ object BlockESP : Module(
4343
description = "Render block ESP",
4444
defaultTags = setOf(ModuleTag.RENDER)
4545
) {
46-
private var drawFaces: Boolean by setting("Draw Faces", true, "Draw faces of blocks").apply { onValueSet(::rebuildMesh); onValueSet { _, to -> if (!to) drawOutlines = true } }
47-
private var drawOutlines: Boolean by setting("Draw Outlines", true, "Draw outlines of blocks").apply { onValueSet(::rebuildMesh); onValueSet { _, to -> if (!to) drawFaces = true } }
48-
private val mesh by setting("Mesh", true, "Connect similar adjacent blocks").apply { onValueSet(::rebuildMesh) }
46+
private var drawFaces: Boolean by setting("Draw Faces", true, "Draw faces of blocks").onValueSet(::rebuildMesh).onValueSet { _, to -> if (!to) drawOutlines = true }
47+
private var drawOutlines: Boolean by setting("Draw Outlines", true, "Draw outlines of blocks").onValueSet(::rebuildMesh).onValueSet { _, to -> if (!to) drawFaces = true }
48+
private val mesh by setting("Mesh", true, "Connect similar adjacent blocks").onValueSet(::rebuildMesh)
4949

50-
private val useBlockColor by setting("Use Block Color", false, "Use the color of the block instead").apply { onValueSet(::rebuildMesh) }
51-
private val faceColor by setting("Face Color", Color(100, 150, 255, 51), "Color of the surfaces") { drawFaces && !useBlockColor }.apply { onValueSet(::rebuildMesh) }
52-
private val outlineColor by setting("Outline Color", Color(100, 150, 255, 128), "Color of the outlines") { drawOutlines && !useBlockColor }.apply { onValueSet(::rebuildMesh) }
50+
private val useBlockColor by setting("Use Block Color", false, "Use the color of the block instead").onValueSet(::rebuildMesh)
51+
private val faceColor by setting("Face Color", Color(100, 150, 255, 51), "Color of the surfaces") { drawFaces && !useBlockColor }.onValueSet(::rebuildMesh)
52+
private val outlineColor by setting("Outline Color", Color(100, 150, 255, 128), "Color of the outlines") { drawOutlines && !useBlockColor }.onValueSet(::rebuildMesh)
5353

54-
private val outlineMode by setting("Outline Mode", DirectionMask.OutlineMode.AND, "Outline mode").apply { onValueSet(::rebuildMesh) }
54+
private val outlineMode by setting("Outline Mode", DirectionMask.OutlineMode.AND, "Outline mode").onValueSet(::rebuildMesh)
5555

56-
private val blocks by setting("Blocks", setOf(Blocks.BEDROCK), "Render blocks").apply { onValueSet(::rebuildMesh) }
56+
private val blocks by setting("Blocks", setOf(Blocks.BEDROCK), "Render blocks").onValueSet(::rebuildMesh)
5757

5858
@JvmStatic
5959
val barrier by setting("Solid Barrier Block", true, "Render barrier blocks")

common/src/main/kotlin/com/lambda/module/modules/render/StorageESP.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ object StorageESP : Module(
6464
private val distance by setting("Distance", 64.0, 10.0..256.0, 1.0, "Maximum distance for rendering") { page == Page.General }
6565

6666
/* Render settings */
67-
private var drawFaces: Boolean by setting("Draw Faces", true, "Draw faces of blocks") { page == Page.Render }.apply { onValueSet { _, to -> if (!to) drawOutlines = true } }
68-
private var drawOutlines: Boolean by setting("Draw Outlines", true, "Draw outlines of blocks") { page == Page.Render }.apply { onValueSet { _, to -> if (!to) drawFaces = true } }
67+
private var drawFaces: Boolean by setting("Draw Faces", true, "Draw faces of blocks") { page == Page.Render }.onValueSet { _, to -> if (!to) drawOutlines = true }
68+
private var drawOutlines: Boolean by setting("Draw Outlines", true, "Draw outlines of blocks") { page == Page.Render }.onValueSet { _, to -> if (!to) drawFaces = true }
6969
private val outlineMode by setting("Outline Mode", DirectionMask.OutlineMode.AND, "Outline mode") { page == Page.Render }
7070
private val mesh by setting("Mesh", true, "Connect similar adjacent blocks") { page == Page.Render }
7171

0 commit comments

Comments
 (0)