Skip to content

Commit 08a1b73

Browse files
committed
Only store one hashmap for subcommands
1 parent 0f62baa commit 08a1b73

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

src/main/kotlin/me/devoxin/flight/api/CommandClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class CommandClient(
8282
?: commands.values.firstOrNull { it.properties.aliases.contains(command) }
8383
?: return dispatchSafely { it.onUnknownCommand(event, command, args) }
8484

85-
val subcommand = args.firstOrNull()?.lowercase().let { cmd.subcommands[it] ?: cmd.subcommandAliases[it] }
85+
val subcommand = args.firstOrNull()?.lowercase().let { cmd.subcommands[it] }
8686
val invoked = subcommand ?: cmd
8787

8888
if (subcommand != null) {

src/main/kotlin/me/devoxin/flight/api/CommandFunction.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ class CommandFunction(
2929
) : Executable(name, method, cog, arguments, contextParameter) {
3030
val contextType: ContextType
3131
val subcommands = hashMapOf<String, SubCommandFunction>()
32-
val subcommandAliases = hashMapOf<String, SubCommandFunction>()
32+
33+
@Deprecated("Use #subcommands with a mapping/filter function.")
34+
val subcommandAliases: Map<String, SubCommandFunction>
35+
get() = subcommands.values.flatMap { it.properties.aliases.map { a -> a to it } }
36+
.associateBy({ it.first }) { it.second }
3337

3438
init {
3539
val jvmCtx = contextParameter.type
@@ -44,13 +48,13 @@ class CommandFunction(
4448
subcommands[sc.name] = sc
4549

4650
for (trigger in sc.properties.aliases) {
47-
val existing = subcommandAliases[trigger]
51+
val existing = subcommands[trigger]
4852

4953
if (existing != null) {
5054
throw IllegalStateException("The trigger '$trigger' for sub-command '${sc.name}' within command '$name' is already assigned to '${existing.name}'!")
5155
}
5256

53-
subcommandAliases[trigger] = sc
57+
subcommands[trigger] = sc
5458
}
5559
}
5660
}

src/main/kotlin/me/devoxin/flight/api/entities/CommandRegistry.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CommandRegistry : HashMap<String, CommandFunction>() {
2323
.setNSFW(command.properties.nsfw)
2424

2525
if (command.subcommands.isNotEmpty()) {
26-
for (sc in command.subcommands.values) {
26+
for (sc in command.subcommands.values.toSet()) {
2727
val scData = SubcommandData(sc.name, sc.properties.description)
2828

2929
if (sc.arguments.isNotEmpty()) {

0 commit comments

Comments
 (0)