Skip to content

Commit d17b14d

Browse files
committed
use suggestMatching for commands so it filters based on the current input
1 parent 444c92a commit d17b14d

8 files changed

Lines changed: 70 additions & 60 deletions

File tree

src/main/kotlin/com/lambda/command/commands/BuildCommand.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import com.lambda.threading.runSafe
3636
import com.lambda.util.Communication.info
3737
import com.lambda.util.extension.CommandBuilder
3838
import com.lambda.util.extension.move
39+
import net.minecraft.command.CommandSource.suggestMatching
3940
import java.nio.file.InvalidPathException
4041
import java.nio.file.NoSuchFileException
4142
import java.nio.file.Path
@@ -51,8 +52,7 @@ object BuildCommand : LambdaCommand(
5152
required(literal("place")) {
5253
required(greedyString("structure")) { structure ->
5354
suggests { _, builder ->
54-
StructureRegistry.forEach { key, _ -> builder.suggest(key) }
55-
builder.buildFuture()
55+
suggestMatching(StructureRegistry.keys, builder)
5656
}
5757
executeWithResult {
5858
val pathString = structure().value()
@@ -72,9 +72,9 @@ object BuildCommand : LambdaCommand(
7272

7373
return@executeWithResult success()
7474
}
75-
} catch (e: InvalidPathException) {
75+
} catch (_: InvalidPathException) {
7676
return@executeWithResult failure("Invalid path $pathString")
77-
} catch (e: NoSuchFileException) {
77+
} catch (_: NoSuchFileException) {
7878
return@executeWithResult failure("Structure $pathString not found")
7979
} catch (e: Exception) {
8080
return@executeWithResult failure(

src/main/kotlin/com/lambda/command/commands/CapeCommand.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ import com.lambda.brigadier.argument.value
2323
import com.lambda.brigadier.execute
2424
import com.lambda.brigadier.required
2525
import com.lambda.command.LambdaCommand
26-
import com.lambda.network.CapeManager
26+
import com.lambda.network.CapeManager.availableCapes
2727
import com.lambda.network.CapeManager.updateCape
2828
import com.lambda.util.Communication.info
2929
import com.lambda.util.Communication.logError
3030
import com.lambda.util.extension.CommandBuilder
31+
import net.minecraft.command.CommandSource.suggestMatching
3132

3233
object CapeCommand : LambdaCommand(
3334
name = "cape",
@@ -38,10 +39,7 @@ object CapeCommand : LambdaCommand(
3839
required(literal("set")) {
3940
required(string("id")) { id ->
4041
suggests { _, builder ->
41-
CapeManager.availableCapes
42-
.forEach { builder.suggest(it) }
43-
44-
builder.buildFuture()
42+
suggestMatching(availableCapes, builder)
4543
}
4644

4745
execute {

src/main/kotlin/com/lambda/command/commands/ConfigCommand.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.lambda.command.LambdaCommand
2828
import com.lambda.config.Configuration
2929
import com.lambda.util.Communication.info
3030
import com.lambda.util.extension.CommandBuilder
31+
import net.minecraft.command.CommandSource.suggestMatching
3132

3233
object ConfigCommand : LambdaCommand(
3334
name = "config",
@@ -58,20 +59,14 @@ object ConfigCommand : LambdaCommand(
5859
required(literal("reset")) {
5960
required(string("config")) { config ->
6061
suggests { _, builder ->
61-
Configuration.configurables.forEach {
62-
builder.suggest(it.commandName)
63-
}
64-
builder.buildFuture()
62+
suggestMatching(Configuration.configurables.map { it.commandName }, builder)
6563
}
6664
required(string("setting")) { setting ->
6765
suggests { ctx, builder ->
6866
val conf = config(ctx).value()
6967
Configuration.configurableByName(conf)?.let { configurable ->
70-
configurable.settings.forEach {
71-
builder.suggest(it.commandName)
72-
}
73-
}
74-
builder.buildFuture()
68+
suggestMatching(configurable.settings.map { it.commandName }, builder)
69+
} ?: builder.buildFuture()
7570
}
7671
executeWithResult {
7772
val confName = config().value()

src/main/kotlin/com/lambda/command/commands/FriendCommand.kt

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import com.lambda.util.text.buildText
3939
import com.lambda.util.text.literal
4040
import com.lambda.util.text.styled
4141
import kotlinx.coroutines.runBlocking
42+
import net.minecraft.command.CommandSource.suggestMatching
4243
import java.awt.Color
4344
import java.util.UUID
4445

@@ -87,13 +88,12 @@ object FriendCommand : LambdaCommand(
8788
required(literal("add")) {
8889
required(string("player name")) { player ->
8990
suggests { _, builder ->
90-
mc.networkHandler
91+
val playerNames = mc.networkHandler
9192
?.playerList
92-
?.filter { it.profile != mc.gameProfile }
9393
?.map { it.profile.name }
94-
?.forEach { builder.suggest(it) }
94+
?.toList() ?: emptyList()
9595

96-
builder.buildFuture()
96+
suggestMatching(playerNames, builder)
9797
}
9898

9999
executeWithResult {
@@ -103,9 +103,6 @@ object FriendCommand : LambdaCommand(
103103
val profile = FriendManager.latestGameProfile(name)
104104
?: return@runBlocking failure("Could not find the player")
105105

106-
if (mc.gameProfile.id == profile.id)
107-
return@runBlocking failure("You can't befriend yourself")
108-
109106
if (FriendManager.isFriend(profile.id))
110107
return@runBlocking failure("This player is already in your friend list")
111108

@@ -121,21 +118,17 @@ object FriendCommand : LambdaCommand(
121118
required(literal("add-uuid")) {
122119
required(uuid("player uuid")) { player ->
123120
suggests { _, builder ->
124-
mc.networkHandler
121+
val uuids = mc.networkHandler
125122
?.playerList
126-
?.filter { it.profile != mc.gameProfile }
127-
?.map { it.profile.id }
128-
?.forEach { builder.suggest(it.toString()) }
123+
?.map { it.profile.id.toString() }
124+
?.toList() ?: emptyList()
129125

130-
builder.buildFuture()
126+
suggestMatching(uuids, builder)
131127
}
132128

133129
executeWithResult {
134130
val uuid = player().value()
135131

136-
if (mc.gameProfile.id == uuid)
137-
return@executeWithResult failure("You can't befriend yourself")
138-
139132
if (FriendManager.isFriend(uuid))
140133
return@executeWithResult failure("This player is already in your friend list")
141134

@@ -159,11 +152,8 @@ object FriendCommand : LambdaCommand(
159152
required(literal("remove")) {
160153
required(string("player name")) { player ->
161154
suggests { _, builder ->
162-
FriendManager.friends
163-
.map { FriendManager.friendDisplayName(it) }
164-
.forEach { builder.suggest(it) }
165-
166-
builder.buildFuture()
155+
val playerNames = FriendManager.friends.map { FriendManager.friendDisplayName(it) }
156+
suggestMatching(playerNames, builder)
167157
}
168158

169159
executeWithResult {
@@ -189,6 +179,15 @@ object FriendCommand : LambdaCommand(
189179

190180
required(literal("remove-uuid")) {
191181
required(uuid("player uuid")) { player ->
182+
suggests { _, builder ->
183+
val uuids = mc.networkHandler
184+
?.playerList
185+
?.map { it.profile.id.toString() }
186+
?.toList() ?: emptyList()
187+
188+
suggestMatching(uuids, builder)
189+
}
190+
192191
executeWithResult {
193192
val uuid = player().value()
194193

src/main/kotlin/com/lambda/command/commands/ModuleCommand.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import com.lambda.util.text.buildText
3939
import com.lambda.util.text.clickEvent
4040
import com.lambda.util.text.literal
4141
import com.lambda.util.text.styled
42+
import net.minecraft.command.CommandSource.suggestMatching
4243
import java.awt.Color
4344

4445
object ModuleCommand : LambdaCommand(
@@ -75,10 +76,7 @@ object ModuleCommand : LambdaCommand(
7576

7677
required(string("module name")) { moduleName ->
7778
suggests { _, builder ->
78-
ModuleRegistry.moduleNameMap.keys.forEach {
79-
builder.suggest(it)
80-
}
81-
builder.buildFuture()
79+
suggestMatching(ModuleRegistry.moduleNameMap.keys, builder)
8280
}
8381
optional(boolean("enable")) { enable ->
8482
executeWithResult {

src/main/kotlin/com/lambda/command/commands/ReplayCommand.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import com.lambda.module.modules.player.Replay
3030
import com.lambda.util.FileUtils.listRecursive
3131
import com.lambda.util.FolderRegister
3232
import com.lambda.util.extension.CommandBuilder
33+
import net.minecraft.command.CommandSource.suggestMatching
3334
import kotlin.io.path.exists
3435

3536
object ReplayCommand : LambdaCommand(
@@ -50,10 +51,11 @@ object ReplayCommand : LambdaCommand(
5051
required(greedyString("replay filepath")) { replayName ->
5152
suggests { _, builder ->
5253
val dir = FolderRegister.replay.toFile()
53-
dir.listRecursive { it.isFile }.forEach {
54-
builder.suggest(it.relativeTo(dir).path)
55-
}
56-
builder.buildFuture()
54+
val paths = dir
55+
.listRecursive { it.isFile }
56+
.map { it.relativeTo(dir).path }
57+
.toList()
58+
suggestMatching(paths, builder)
5759
}
5860

5961
executeWithResult {

src/main/kotlin/com/lambda/command/commands/TransferCommand.kt

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import com.lambda.task.Task
3737
import com.lambda.threading.runSafeAutomated
3838
import com.lambda.util.Communication.info
3939
import com.lambda.util.extension.CommandBuilder
40+
import net.minecraft.command.CommandSource.suggestMatching
4041

4142
object TransferCommand : LambdaCommand(
4243
name = "transfer",
@@ -50,28 +51,45 @@ object TransferCommand : LambdaCommand(
5051
required(integer("amount", 1)) { amount ->
5152
required(string("from")) { from ->
5253
suggests { ctx, builder ->
53-
val count = amount(ctx).value()
54-
val selection = selectStack(count) {
54+
val selection = selectStack(amount(ctx).value()) {
5555
isItem(stack(ctx).value().item)
5656
}
5757
AutomationConfig.Companion.DEFAULT.runSafeAutomated {
58-
selection.findContainersWithMaterial().forEachIndexed { i, container ->
59-
builder.suggest("\"${i + 1}. ${container.name}\"", container.description(selection))
60-
}
61-
}
62-
builder.buildFuture()
58+
val containers = selection.findContainersWithMaterial()
59+
val indexedContainers = containers.withIndex()
60+
61+
suggestMatching(
62+
indexedContainers,
63+
builder,
64+
{ (index, container) ->
65+
"\"${index + 1}. ${container.name}\""
66+
},
67+
{ (_, container) ->
68+
container.description(selection)
69+
}
70+
)
71+
} ?: builder.buildFuture()
6372
}
6473
required(string("to")) { to ->
6574
suggests { ctx, builder ->
6675
val selection = selectStack(amount(ctx).value()) {
6776
isItem(stack(ctx).value().item)
6877
}
6978
AutomationConfig.Companion.DEFAULT.runSafeAutomated {
70-
selection.findContainersWithSpace().forEachIndexed { i, container ->
71-
builder.suggest("\"${i + 1}. ${container.name}\"", container.description(selection))
72-
}
73-
}
74-
builder.buildFuture()
79+
val containers = selection.findContainersWithSpace()
80+
val indexedContainers = containers.withIndex()
81+
82+
suggestMatching(
83+
indexedContainers,
84+
builder,
85+
{ (index, container) ->
86+
"\"${index + 1}. ${container.name}\""
87+
},
88+
{ (_, container) ->
89+
container.description(selection)
90+
}
91+
)
92+
} ?: builder.buildFuture()
7593
}
7694
executeWithResult {
7795
val selection = selectStack(amount().value()) {

src/main/kotlin/com/lambda/module/modules/render/Freecam.kt

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

1818
package com.lambda.module.modules.render
1919

20-
import com.lambda.Lambda
2120
import com.lambda.Lambda.mc
2221
import com.lambda.config.AutomationConfig.Companion.setDefaultAutomationConfig
2322
import com.lambda.config.applyEdits
@@ -161,7 +160,8 @@ object Freecam : Module(
161160
mc.options.perspective = Perspective.FIRST_PERSON
162161

163162
// Don't block baritone from working
164-
if (!event.input.handledByBaritone) { // Reset actual input
163+
if (!event.input.handledByBaritone) {
164+
// Reset actual input
165165
event.input.cancel()
166166
}
167167

0 commit comments

Comments
 (0)