-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
55 additions
and
18 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
progreso-api/src/main/kotlin/org/progreso/api/command/arguments/ConfigArgumentType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.progreso.api.command.arguments | ||
|
||
import com.mojang.brigadier.StringReader | ||
import com.mojang.brigadier.arguments.ArgumentType | ||
import com.mojang.brigadier.context.CommandContext | ||
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType | ||
import com.mojang.brigadier.suggestion.Suggestions | ||
import com.mojang.brigadier.suggestion.SuggestionsBuilder | ||
import org.progreso.api.Api | ||
import org.progreso.api.config.AbstractConfig | ||
import org.progreso.api.config.AbstractConfigCategory | ||
import java.util.concurrent.CompletableFuture | ||
|
||
class ConfigArgumentType(private val category: AbstractConfigCategory<*>) : ArgumentType<AbstractConfig> { | ||
companion object { | ||
@Suppress("UNCHECKED_CAST") | ||
private val NO_SUCH_CONFIG = DynamicCommandExceptionType { name: Any -> | ||
val (categoryName, configName) = name as Pair<String, String> | ||
|
||
Api.TEXT.i18nMessage("argument.config.error", configName, categoryName) | ||
} | ||
|
||
operator fun get(context: CommandContext<*>): AbstractConfig { | ||
return context.getArgument("config", AbstractConfig::class.java) | ||
} | ||
} | ||
|
||
override fun parse(reader: StringReader): AbstractConfig { | ||
val argument = reader.readString() | ||
|
||
return category.configs.firstOrNull { it.name == argument } | ||
?: throw NO_SUCH_CONFIG.create(category.name to argument) | ||
} | ||
|
||
override fun <S : Any?> listSuggestions( | ||
context: CommandContext<S>, | ||
builder: SuggestionsBuilder | ||
): CompletableFuture<Suggestions> { | ||
return Api.COMMAND.suggestMatching(category.configs.map { it.name }, builder) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters