Skip to content

Commit

Permalink
Added BindCommand, added getSettingByName function to SettingContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
ya-ilya committed Sep 10, 2023
1 parent 1745cf4 commit c5f513d
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@ package org.progreso.api.setting.container
import org.progreso.api.setting.AbstractSetting
import org.progreso.api.setting.settings.*
import java.awt.Color
import kotlin.reflect.KClass

/**
* Interface for setting container
*/
interface SettingContainer {
val settings: MutableSet<AbstractSetting<*>>

fun <T : AbstractSetting<*>> getSettingByName(name: String, clazz: KClass<T>): T {
return getSettingByNameOrNull(name, clazz)!!
}

@Suppress("UNCHECKED_CAST")
fun <T : AbstractSetting<*>> getSettingByNameOrNull(name: String, clazz: KClass<T>): T? {
return settings.first { it::class == clazz && it.name == name } as T?
}

fun setting(
name: String,
initialValue: Int,
Expand Down
3 changes: 3 additions & 0 deletions progreso-client/src/main/kotlin/org/progreso/client/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.progreso.client.accessors.*
import org.progreso.client.gui.clickgui.ClickGUI
import org.progreso.client.gui.clickgui.HudEditor
import org.progreso.client.managers.CombatManager
import org.progreso.client.util.client.KeyboardUtil
import org.reflections.Reflections
import org.slf4j.LoggerFactory

Expand Down Expand Up @@ -66,6 +67,8 @@ class Client : ModInitializer {
}
}

println(KeyboardUtil.keyMap)

LOGGER.info("Initializing client commands...")
for (clazz in Reflections("org.progreso.client.commands")
.getSubTypesOf(AbstractCommand::class.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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.ModuleArgumentType
import org.progreso.api.setting.settings.BindSetting
import org.progreso.client.util.client.KeyboardUtil

@AbstractCommand.Register("bind")
object BindCommand : AbstractCommand() {
override fun build(builder: LiteralArgumentBuilder<Any>) {
builder.then(
argument("module", ModuleArgumentType())
.then(
argument("key", StringArgumentType.string()).executesSuccess { context ->
val module = ModuleArgumentType[context]
val key = StringArgumentType.getString(context, "key")

module.bind = try {
KeyboardUtil.getKeyCode(key)
} catch (ex: Exception) {
errorLocalized("command.bind.key_error", key)
return@executesSuccess
}

infoLocalized("command.bind.key", module.name, key)
}
)
.then(
literal("reset").executesSuccess { context ->
val module = ModuleArgumentType[context]
module.bind = module.getSettingByName("Bind", BindSetting::class).initialValue

infoLocalized("command.bind.reset", module.name)
}
)
.executesSuccess { context ->
val module = ModuleArgumentType[context]

infoLocalized("command.bind.current", module.name, KeyboardUtil.getKeyName(module.bind))
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ class BindElement(
"Listening.."
} else {
when (setting.value) {
InputUtil.GLFW_KEY_LEFT_SHIFT -> "LSHIFT"
InputUtil.GLFW_KEY_RIGHT_SHIFT -> "RSHIFT"
InputUtil.GLFW_KEY_LEFT_CONTROL -> "LCTRL"
InputUtil.GLFW_KEY_RIGHT_CONTROL -> "RCTRL"
InputUtil.GLFW_KEY_LEFT_ALT -> "LALT"
InputUtil.GLFW_KEY_RIGHT_ALT -> "RALT"
-1, 0, 256 -> "NONE"
else -> KeyboardUtil.getKeyName(setting.value, -1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,33 @@ package org.progreso.client.util.client
import net.minecraft.client.util.InputUtil

object KeyboardUtil {
var keyMap = InputUtil::class.java
.fields.filter { it.type == Int::class.java }
.filter { it.name.startsWith("GLFW_KEY_") }
.associate {
val key = it.get(null) as Int
key to getKeyName(key)
}

fun getKeyName(keyCode: Int, scanCode: Int = -1): String {
return InputUtil.fromKeyCode(keyCode, scanCode)
.toString()
.split(".")
.last()
.uppercase()
return when (keyCode) {
InputUtil.GLFW_KEY_LEFT_SHIFT -> "LSHIFT"
InputUtil.GLFW_KEY_RIGHT_SHIFT -> "RSHIFT"
InputUtil.GLFW_KEY_LEFT_CONTROL -> "LCTRL"
InputUtil.GLFW_KEY_RIGHT_CONTROL -> "RCTRL"
InputUtil.GLFW_KEY_LEFT_ALT -> "LALT"
InputUtil.GLFW_KEY_RIGHT_ALT -> "RALT"
else -> InputUtil.fromKeyCode(keyCode, scanCode)
.toString()
.split(".")
.last()
.uppercase()
}
}

fun getKeyCode(keyName: String): Int {
return keyMap.entries
.first { it.value.uppercase() == keyName.uppercase() }
.key
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{
"argument.config_category.error": "Config category '%s' not found",
"argument.friend.error": "Friend '%s' not found",
"argument.module.error": "Module '%s' not found",
"argument.locale.error": "Locale '%s' not found",
"argument.plugin.error": "Plugin '%s' not found",

"command.command_not_found": "Command not found",
"command.invalid_syntax": "Invalid syntax: %s",
"command.failed_to_execute": "Failed to execute command",

"command.bind.key": "%s bound to %s",
"command.bind.key_error": "Key %s not found",
"command.bind.reset": "Reset %s bind",
"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",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{
"argument.config_category.error": "Категория конфигов '%s' не найдена",
"argument.friend.error": "У вас нету друга с именем %s",
"argument.module.error": "Модуль '%s' не найден",
"argument.locale.error": "Перевод '%s' не найден",
"argument.plugin.error": "Плагин '%s' не найден",

"command.command_not_found": "Команда не найдена",
"command.invalid_syntax": "Неправильный синтаксис. Сообщение об ошибке: %s",
"command.failed_to_execute": "Ошибка при выполнении команды",

"command.bind.key": "%s забинжен на клавишу '%s'",
"command.bind.key_error": "Клавина '%s' не найдена",
"command.bind.reset": "Клавиша %s сброшена",
"command.bind.current": "Текущий бинд %s: %s",

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

0 comments on commit c5f513d

Please sign in to comment.