Skip to content

Commit 7a6e049

Browse files
committed
Command overhaul
1 parent c10c0e8 commit 7a6e049

File tree

5 files changed

+174
-160
lines changed

5 files changed

+174
-160
lines changed

common/src/main/kotlin/com/lambda/command/CommandManager.kt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,9 @@ object CommandManager : Configurable(LambdaConfig), Loadable {
3131
val prefix by setting("prefix", ';')
3232

3333
val commands = mutableSetOf<LambdaCommand>()
34-
private val dispatcher by lazy { CommandDispatcher<CommandSource>() }
34+
val dispatcher by lazy { CommandDispatcher<CommandSource>() }
3535
private const val ERROR_PADDING = 10
3636

37-
fun register(
38-
command: String,
39-
vararg alias: String,
40-
action: LiteralArgumentBuilder<CommandSource>.() -> Unit,
41-
) {
42-
(listOf(command) + alias).forEach {
43-
dispatcher.register(it, action)
44-
}
45-
}
46-
4737
fun executeCommand(command: String) {
4838
runSafe {
4939
val isolatedCommand = command.drop(1)
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
package com.lambda.command
22

3+
import com.lambda.command.CommandManager.dispatcher
34
import com.lambda.util.Nameable
5+
import com.lambda.util.primitives.extension.CommandBuilder
6+
import com.mojang.brigadier.builder.LiteralArgumentBuilder
7+
import net.minecraft.command.CommandSource
48

5-
interface LambdaCommand : Nameable
9+
abstract class LambdaCommand(
10+
final override val name: String,
11+
val aliases: Set<String> = emptySet(),
12+
val usage: String = "",
13+
val description: String = "",
14+
) : Nameable {
15+
16+
init {
17+
(listOf(name) + aliases).forEach {
18+
val argument = LiteralArgumentBuilder.literal<CommandSource>(it)
19+
argument.create()
20+
dispatcher.register(argument)
21+
}
22+
}
23+
24+
abstract fun CommandBuilder.create()
25+
}

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

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,30 @@
11
package com.lambda.command.commands
22

3-
import com.lambda.brigadier.CommandResult.Companion.failure
4-
import com.lambda.brigadier.CommandResult.Companion.success
5-
import com.lambda.brigadier.argument.string
6-
import com.lambda.brigadier.argument.value
7-
import com.lambda.brigadier.argument.word
8-
import com.lambda.brigadier.executeWithResult
9-
import com.lambda.brigadier.optional
3+
import com.lambda.brigadier.argument.literal
4+
import com.lambda.brigadier.execute
105
import com.lambda.brigadier.required
11-
import com.lambda.command.CommandManager.register
126
import com.lambda.command.LambdaCommand
137
import com.lambda.config.Configuration
8+
import com.lambda.util.primitives.extension.CommandBuilder
149

15-
object ConfigCommand : LambdaCommand {
16-
override val name = "config"
17-
18-
init {
19-
register(name, "cfg") {
20-
required(word("action")) { action ->
21-
val actions = listOf("save", "load")
22-
23-
suggests { _, builder ->
24-
actions.forEach {
25-
builder.suggest(it)
26-
}
27-
builder.buildFuture()
10+
object ConfigCommand : LambdaCommand(
11+
name = "config",
12+
aliases = setOf("cfg"),
13+
usage = "config <save|load>",
14+
description = "Save or load the configuration files"
15+
) {
16+
override fun CommandBuilder.create() {
17+
required(literal("save")) {
18+
execute {
19+
Configuration.configurations.forEach {
20+
it.trySave()
2821
}
29-
30-
executeWithResult {
31-
val action = action().value()
32-
if (action !in actions) {
33-
return@executeWithResult failure("Invalid action $action. Did you mean ${actions.joinToString()}?")
34-
}
35-
36-
Configuration.configurations.forEach {
37-
when (action) {
38-
"save" -> it.trySave()
39-
else -> it.tryLoad()
40-
}
41-
}
42-
43-
success()
22+
}
23+
}
24+
required(literal("load")) {
25+
execute {
26+
Configuration.configurations.forEach {
27+
it.tryLoad()
4428
}
4529
}
4630
}

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

Lines changed: 90 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -10,124 +10,123 @@ import com.lambda.brigadier.get
1010
import com.lambda.brigadier.optional
1111
import com.lambda.brigadier.required
1212
import com.lambda.command.CommandManager.prefix
13-
import com.lambda.command.CommandManager.register
1413
import com.lambda.command.LambdaCommand
1514
import com.lambda.module.ModuleRegistry
1615
import com.lambda.threading.runSafe
1716
import com.lambda.util.Communication.info
17+
import com.lambda.util.Communication.joinToText
1818
import com.lambda.util.Communication.warn
1919
import com.lambda.util.StringUtils
20+
import com.lambda.util.primitives.extension.CommandBuilder
2021
import com.lambda.util.text.*
2122
import com.lambda.util.text.ClickEvents.suggestCommand
2223
import java.awt.Color
2324

24-
object ModuleCommand : LambdaCommand {
25-
override val name = "module"
25+
object ModuleCommand : LambdaCommand(
26+
name = "module",
27+
aliases = setOf("mod"),
28+
usage = "module <module name> [enable]",
29+
description = "Enable or disable a module"
30+
) {
31+
override fun CommandBuilder.create() {
32+
executeWithResult {
33+
val enabled = ModuleRegistry.modules.filter {
34+
it.isEnabled
35+
}
2636

27-
init {
28-
register(name, "mod") {
29-
executeWithResult {
30-
val enabled = ModuleRegistry.modules.filter {
31-
it.isEnabled
32-
}
37+
if (enabled.isEmpty()) {
38+
info("No modules are enabled")
39+
return@executeWithResult success()
40+
}
3341

34-
if (enabled.isEmpty()) {
35-
info("No modules are enabled")
36-
return@executeWithResult success()
42+
this@ModuleCommand.info(buildText {
43+
styled(Color.GRAY) {
44+
literal("Enabled Modules: ")
3745
}
38-
39-
this@ModuleCommand.info(buildText {
40-
styled(Color.GRAY) {
41-
literal("Enabled Modules: ")
42-
}
43-
enabled.forEachIndexed { index, module ->
44-
if (index != 0) {
45-
literal(", ")
46-
}
47-
clickEvent(suggestCommand("$prefix${input} ${module.name}")) {
48-
styled(if (module.isEnabled) Color.GREEN else Color.RED) {
49-
literal(module.name)
50-
}
46+
joinToText(enabled) {
47+
clickEvent(suggestCommand("$prefix${input} ${it.name}")) {
48+
styled(if (it.isEnabled) Color.GREEN else Color.RED) {
49+
literal(it.name)
5150
}
5251
}
53-
})
54-
return@executeWithResult success()
55-
}
52+
}
53+
})
54+
return@executeWithResult success()
55+
}
5656

57-
required(string("module name")) { moduleName ->
58-
suggests { _, builder ->
59-
ModuleRegistry.modules.map {
60-
it.name
61-
}.forEach {
62-
builder.suggest(it)
63-
}
64-
builder.buildFuture()
57+
required(string("module name")) { moduleName ->
58+
suggests { _, builder ->
59+
ModuleRegistry.modules.map {
60+
it.name
61+
}.forEach {
62+
builder.suggest(it)
6563
}
66-
optional(boolean("enable")) { enable ->
67-
executeWithResult {
68-
val name = this[moduleName].value()
69-
val module = ModuleRegistry.modules.find {
70-
it.name.equals(name, true)
71-
} ?: return@executeWithResult failure(buildText {
72-
styled(Color.RED) {
73-
literal("Module ")
64+
builder.buildFuture()
65+
}
66+
optional(boolean("enable")) { enable ->
67+
executeWithResult {
68+
val name = moduleName().value()
69+
val module = ModuleRegistry.modules.find {
70+
it.name.equals(name, true)
71+
} ?: return@executeWithResult failure(buildText {
72+
styled(Color.RED) {
73+
literal("Module ")
74+
styled(Color.GRAY) {
75+
literal("$name ")
76+
}
77+
literal("not found!")
78+
}
79+
val similarModules = StringUtils.findSimilarStrings(
80+
name,
81+
ModuleRegistry.moduleNames,
82+
3
83+
)
84+
if (similarModules.isEmpty()) return@buildText
85+
86+
literal(" Did you mean ")
87+
similarModules.forEachIndexed { index, s ->
88+
if (index != 0) {
89+
literal(", ")
90+
}
91+
clickEvent(suggestCommand("$prefix${input.replace(name, s)}")) {
7492
styled(Color.GRAY) {
75-
literal("$name ")
93+
literal(s)
7694
}
77-
literal("not found!")
7895
}
79-
val similarModules = StringUtils.findSimilarStrings(
80-
name,
81-
ModuleRegistry.moduleNames,
82-
3
83-
)
84-
if (similarModules.isEmpty()) return@buildText
96+
}
97+
literal("?")
98+
})
8599

86-
literal(" Did you mean ")
87-
similarModules.forEachIndexed { index, s ->
88-
if (index != 0) {
89-
literal(", ")
90-
}
91-
clickEvent(suggestCommand("$prefix${input.replace(name, s)}")) {
100+
runSafe {
101+
if (enable == null) {
102+
module.toggle()
103+
} else {
104+
if (enable().value() == module.isEnabled) {
105+
this@ModuleCommand.warn(buildText {
92106
styled(Color.GRAY) {
93-
literal(s)
107+
literal("$name already ")
108+
literal(if (module.isEnabled) "enabled" else "disabled")
94109
}
95-
}
110+
})
111+
return@runSafe success()
96112
}
97-
literal("?")
98-
})
99113

100-
runSafe {
101-
if (enable == null) {
102-
module.toggle()
114+
if (enable().value()) {
115+
module.enable()
103116
} else {
104-
if (enable().value() == module.isEnabled) {
105-
this@ModuleCommand.warn(buildText {
106-
styled(Color.GRAY) {
107-
literal("$name already ")
108-
literal(if (module.isEnabled) "enabled" else "disabled")
109-
}
110-
})
111-
return@runSafe success()
112-
}
113-
114-
if (enable().value()) {
115-
module.enable()
116-
} else {
117-
module.disable()
118-
}
117+
module.disable()
119118
}
120-
this@ModuleCommand.info(buildText {
121-
styled(Color.GRAY) {
122-
literal("$name ")
123-
}
124-
styled(if (module.isEnabled) Color.GREEN else Color.RED) {
125-
literal(if (module.isEnabled) "enabled" else "disabled")
126-
}
127-
})
128-
success()
129-
} ?: failure("Failed to ${if (module.isEnabled) "enable" else "disable"} module $name")
130-
}
119+
}
120+
this@ModuleCommand.info(buildText {
121+
styled(Color.GRAY) {
122+
literal("$name ")
123+
}
124+
styled(if (module.isEnabled) Color.GREEN else Color.RED) {
125+
literal(if (module.isEnabled) "enabled" else "disabled")
126+
}
127+
})
128+
success()
129+
} ?: failure("Failed to ${if (module.isEnabled) "enable" else "disable"} module $name")
131130
}
132131
}
133132
}

0 commit comments

Comments
 (0)