Skip to content

Commit

Permalink
Improved SettingArgumentType (getting setting by path)
Browse files Browse the repository at this point in the history
  • Loading branch information
ya-ilya committed Mar 24, 2024
1 parent 4a33a17 commit c354b91
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import com.mojang.brigadier.arguments.ArgumentType
import com.mojang.brigadier.arguments.StringArgumentType
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.module.AbstractModule
import org.progreso.api.setting.AbstractSetting
import org.progreso.api.setting.container.SettingContainer
import java.util.concurrent.CompletableFuture

class SettingArgumentType : ArgumentType<String> {
companion object {
Expand All @@ -18,18 +21,58 @@ class SettingArgumentType : ArgumentType<String> {
Api.TEXT.i18nMessage("argument.setting.error", settingName, moduleName)
}

operator fun get(context: CommandContext<*>): Pair<AbstractModule, AbstractSetting<*>> {
val settingName = StringArgumentType.getString(context, "setting")
private fun getSettingPaths(container: SettingContainer): List<String> {
val paths = mutableListOf<String>()

for (setting in container.settings) {
if (setting is SettingContainer) {
for (subPath in getSettingPaths(setting)) {
paths.add("${setting.name}.${subPath}")
}
} else {
paths.add(setting.name)
}
}

return paths
}

private fun getSettingByPath(path: String, container: SettingContainer): AbstractSetting<*>? {
var currentContainer = container
var currentSetting: AbstractSetting<*>? = null

for (pathPart in path.split(".")) {
val setting = currentContainer.getSettingByNameOrNull(pathPart, AbstractSetting::class)

if (setting is SettingContainer) {
currentContainer = setting
} else {
currentSetting = setting
}
}

return currentSetting
}

operator fun get(context: CommandContext<*>): Pair<AbstractSetting<*>, String> {
val path = StringArgumentType.getString(context, "setting")
val module = ModuleArgumentType[context]

return module to (
module.getSettingByNameOrNull(settingName, AbstractSetting::class)
?: throw NO_SUCH_SETTING.create(module.name to settingName)
)
return (getSettingByPath(path, module) ?: throw NO_SUCH_SETTING.create(module.name to path)) to path
}
}

override fun parse(reader: StringReader): String {
return reader.readString()
}

override fun <S : Any?> listSuggestions(
context: CommandContext<S>,
builder: SuggestionsBuilder
): CompletableFuture<Suggestions> {
return Api.COMMAND.suggestMatching(
try { getSettingPaths(ModuleArgumentType[context]) } catch (ex: Exception) { emptyList() },
builder
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ object ResetCommand : AbstractCommand() {
builder.then(
argument("module", ModuleArgumentType()).then(
argument("setting", SettingArgumentType()).execute { context ->
val (module, setting) = SettingArgumentType[context]
val module = ModuleArgumentType[context]
val (setting, path) = SettingArgumentType[context]

setting.reset()
infoLocalized(
"command.reset.setting",
setting.name,
path,
module.name
)
}
Expand Down

0 comments on commit c354b91

Please sign in to comment.