Skip to content

Commit

Permalink
Added ConfigArgumentType
Browse files Browse the repository at this point in the history
  • Loading branch information
ya-ilya committed Sep 14, 2023
1 parent f5ec41e commit d7195f7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 18 deletions.
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ package org.progreso.api.config
*
* @param name Config name
*/
abstract class AbstractConfig(val name: String)
abstract class AbstractConfig(val name: String) {
override fun toString(): String {
return name
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.progreso.client.commands
import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import org.progreso.api.command.AbstractCommand
import org.progreso.api.command.arguments.ConfigArgumentType
import org.progreso.api.managers.ConfigManager

@AbstractCommand.Register("config")
Expand All @@ -12,19 +13,10 @@ object ConfigCommand : AbstractCommand() {
val literal = literal(category.name)
.then(
literal("load").then(
argument("config", StringArgumentType.string()).executesSuccess { context ->
val config = StringArgumentType.getString(context, "config")

try {
category.load(config)
} catch (ex: Exception) {
errorLocalized(
"command.config.load_error",
config
)
return@executesSuccess
}
argument("config", ConfigArgumentType(category)).executesSuccess { context ->
val config = ConfigArgumentType[context]

category.load(config.name)
infoLocalized(
"command.config.load",
config
Expand Down Expand Up @@ -54,10 +46,10 @@ object ConfigCommand : AbstractCommand() {
)
.then(
literal("refresh").then(
argument("config", StringArgumentType.string()).executesSuccess { context ->
val config = StringArgumentType.getString(context, "config")
argument("config", ConfigArgumentType(category)).executesSuccess { context ->
val config = ConfigArgumentType[context]

category.refresh(config)
category.refresh(config.name)
infoLocalized(
"command.config.refresh",
config
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"argument.config.error": "Config '%s' not found in %s category",
"argument.font.error": "Font '%s' not found",
"argument.friend.error": "Friend '%s' not found",
"argument.key.error": "Key '%s' not exists",
Expand All @@ -16,7 +17,6 @@
"command.bind.current": "Current %s bind: %s",

"command.config.load": "Loaded %s config",
"command.config.load_error": "Config %s not found",
"command.config.save": "Saved %s config",
"command.config.save_many": "Saved %s configs",
"command.config.refresh": "Refreshed %s config",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"argument.config.error": "Конфиг '%s' не найден в категории %s",
"argument.font.error": "Шрифт '%s' не найден",
"argument.friend.error": "У вас нету друга с именем %s",
"argument.key.error": "Клавиша '%s' не существует",
Expand All @@ -16,7 +17,6 @@
"command.bind.current": "Текущий бинд %s: %s",

"command.config.load": "Конфиг '%s' загружен",
"command.config.load_error": "Конфиг '%s' не найден",
"command.config.save": "Конфиг '%s' сохранен",
"command.config.save_many": "Конфиги в хелпере '%s' сохранены",
"command.config.refresh": "Конфиг '%s' обновлен",
Expand Down

0 comments on commit d7195f7

Please sign in to comment.