Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Commit

Permalink
enh: Added ability to have multiple keys for keybinds (#2149)
Browse files Browse the repository at this point in the history
Co-authored-by: Xiaro <[email protected]>
  • Loading branch information
5HT2 and Luna5ama authored Mar 28, 2021
1 parent 80398e2 commit f60cc0e
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 115 deletions.
21 changes: 10 additions & 11 deletions src/main/kotlin/org/kamiblue/client/command/commands/BindCommand.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package org.kamiblue.client.command.commands

import net.minecraft.util.text.TextFormatting
import org.kamiblue.client.command.ClientCommand
import org.kamiblue.client.module.ModuleManager
import org.kamiblue.client.module.modules.client.CommandConfig
import org.kamiblue.client.util.KeyboardUtils
import org.kamiblue.client.util.text.MessageSendHelper
import org.kamiblue.client.util.text.format
import org.kamiblue.client.util.text.formatValue

object BindCommand : ClientCommand(
Expand All @@ -16,12 +13,14 @@ object BindCommand : ClientCommand(
init {
literal("list") {
execute("List used module binds") {
val modules = ModuleManager.modules.filter { it.bind.value.key > 0 }.sortedBy { it.bind.toString() }
val binds = ModuleManager.modules.asSequence()
.filter { it.bind.value.key in 1..255 }
.map { "${formatValue(it.bind)} ${it.name}" }
.sorted()
.toList()

MessageSendHelper.sendChatMessage("Used binds: ${formatValue(modules.size)}")
modules.forEach {
MessageSendHelper.sendRawChatMessage("${formatValue(it.bind)} ${it.name}")
}
MessageSendHelper.sendChatMessage("Used binds: ${formatValue(binds.size)}")
binds.forEach(MessageSendHelper::sendRawChatMessage)
}
}

Expand All @@ -41,15 +40,15 @@ object BindCommand : ClientCommand(
val module = moduleArg.value
val bind = bindArg.value

if (bind.equals("none", true)) {
if (bind.equals("None", true)) {
module.bind.resetValue()
MessageSendHelper.sendChatMessage("Reset bind for ${module.name}!")
return@execute
}


val key = KeyboardUtils.getKey(bind)
if (key <= 0) {

if (key !in 1..255) {
KeyboardUtils.sendUnknownKeyError(bind)
} else {
module.bind.setValue(bind)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ object MacroCommand : ClientCommand(
literal("list") {
string("key") { keyArg ->
execute("List macros for a key") {
val key = KeyboardUtils.getKey(keyArg.value)
val input = keyArg.value
val key = KeyboardUtils.getKey(input)

if (key < 1) {
KeyboardUtils.sendUnknownKeyError(keyArg.value)
if (key !in 1..255) {
KeyboardUtils.sendUnknownKeyError(input)
return@execute
}

val macros = MacroManager.macros.filter { it.key == key }
val formattedName = formatValue(KeyboardUtils.getKeyName(key))
val macros = MacroManager.macros[key]
val formattedName = formatValue(KeyboardUtils.getDisplayName(key) ?: "Unknown")

if (macros.isEmpty()) {
if (macros.isNullOrEmpty()) {
MessageSendHelper.sendChatMessage("&cYou have no macros for the key $formattedName")
} else {
MessageSendHelper.sendChatMessage("You have has the following macros for $formattedName: ")
for ((_, value) in macros) {
MessageSendHelper.sendRawChatMessage("$formattedName $value")
for (macro in macros) {
MessageSendHelper.sendRawChatMessage("$formattedName $macro")
}
}
}
Expand All @@ -41,47 +42,52 @@ object MacroCommand : ClientCommand(
MessageSendHelper.sendChatMessage("&cYou have no macros")
} else {
MessageSendHelper.sendChatMessage("You have the following macros: ")
for ((key, value) in MacroManager.macros.entries.sortedBy { it.key }) {
MessageSendHelper.sendRawChatMessage("${formatValue(KeyboardUtils.getKeyName(key))} $value")
for ((key, value) in MacroManager.macros) {
val formattedName = formatValue(KeyboardUtils.getDisplayName(key) ?: "Unknown")
MessageSendHelper.sendRawChatMessage("$formattedName $value")
}
}

}
}

literal("clear") {
string("key") { keyArg ->
execute("Clear macros for a key") {
val key = KeyboardUtils.getKey(keyArg.value)
val input = keyArg.value
val key = KeyboardUtils.getKey(input)

if (key < 1) {
KeyboardUtils.sendUnknownKeyError(keyArg.value)
if (key !in 1..255) {
KeyboardUtils.sendUnknownKeyError(input)
return@execute
}

val formattedName = formatValue(KeyboardUtils.getDisplayName(key) ?: "Unknown")

MacroManager.removeMacro(key)
MacroManager.saveMacros()
MacroManager.loadMacros()
MessageSendHelper.sendChatMessage("Cleared macros for ${formatValue(KeyboardUtils.getKeyName(key))}")
MessageSendHelper.sendChatMessage("Cleared macros for $formattedName")
}
}
}

string("key") { keyArg ->
greedy("command / message") { greedyArg ->
greedy("command / message") { macroArg ->
execute("Set a command / message for a key") {
val key = KeyboardUtils.getKey(keyArg.value)
val input = keyArg.value
val key = KeyboardUtils.getKey(input)

if (key < 1) {
KeyboardUtils.sendUnknownKeyError(keyArg.value)
if (key !in 1..255) {
KeyboardUtils.sendUnknownKeyError(input)
return@execute
}

MacroManager.addMacroToKey(key, greedyArg.value)
val macro = macroArg.value
val formattedName = formatValue(KeyboardUtils.getDisplayName(key) ?: "Unknown")

MacroManager.addMacroToKey(key, macro)
MacroManager.saveMacros()
MessageSendHelper.sendChatMessage("Added macro ${formatValue(greedyArg.value)} for key " +
formatValue(KeyboardUtils.getKeyName(key))
)
MessageSendHelper.sendChatMessage("Added macro ${formatValue(macro)} for key $formattedName")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object KamiClickGui : AbstractKamiGui<ModuleSettingWindow, AbstractModule>() {
}

override fun keyTyped(typedChar: Char, keyCode: Int) {
if (keyCode == Keyboard.KEY_ESCAPE || keyCode == ClickGUI.bind.value.key && !searching) {
if (keyCode == Keyboard.KEY_ESCAPE || keyCode == ClickGUI.bind.value.key && !searching && settingWindow?.listeningChild == null) {
ClickGUI.disable()
} else {
super.keyTyped(typedChar, keyCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ object KamiHudGui : AbstractKamiGui<HudSettingWindow, AbstractHudElement>() {
}

override fun keyTyped(typedChar: Char, keyCode: Int) {
if (keyCode == Keyboard.KEY_ESCAPE || HudEditor.bind.value.isDown(keyCode) && !searching) {
if (keyCode == Keyboard.KEY_ESCAPE || HudEditor.bind.value.isDown(keyCode) && !searching && settingWindow?.listeningChild == null) {
HudEditor.disable()
} else {
super.keyTyped(typedChar, keyCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ import org.lwjgl.input.Keyboard
import java.io.File
import java.io.FileReader
import java.io.FileWriter
import java.util.*
import kotlin.collections.ArrayList

object MacroManager : Manager {
private var macroMap = LinkedHashMap<Int, ArrayList<String>>()
private var macroMap = TreeMap<Int, ArrayList<String>>()
val isEmpty get() = macroMap.isEmpty()
val macros: Map<Int, List<String>> get() = macroMap

private val gson = GsonBuilder().setPrettyPrinting().create()
private val type = object : TypeToken<LinkedHashMap<Int, List<String>>>() {}.type
private val type = object : TypeToken<TreeMap<Int, List<String>>>() {}.type
private val file get() = File(KamiMod.DIRECTORY + "macros.json")

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ internal object CommandConfig : Module(
var prefix by setting("Prefix", ";", { false })
val toggleMessages by setting("Toggle Messages", false)
private val customTitle = setting("Window Title", true)
val modifierEnabled by setting("Modifier Enabled", true, { false })

private val timer = TickTimer()
private val prevTitle = Display.getTitle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import com.google.gson.JsonElement
import com.google.gson.JsonPrimitive
import org.kamiblue.client.setting.settings.ImmutableSetting
import org.kamiblue.client.util.Bind
import org.lwjgl.input.Keyboard
import org.kamiblue.client.util.KeyboardUtils
import java.util.*

class BindSetting(
name: String,
Expand All @@ -13,38 +14,37 @@ class BindSetting(
description: String = ""
) : ImmutableSetting<Bind>(name, value, visibility, { _, input -> input }, description) {

override val defaultValue: Bind = Bind(value.ctrl, value.alt, value.shift, value.key)
override val defaultValue: Bind = Bind(TreeSet(value.modifierKeys), value.key)

override fun resetValue() {
value.setBind(defaultValue.ctrl, defaultValue.alt, defaultValue.shift, defaultValue.key)
value.setBind(defaultValue.modifierKeys, defaultValue.key)
}

override fun setValue(valueIn: String) {
var string = valueIn

if (string.equals("None", ignoreCase = true)) {
value.setBind(ctrlIn = false, altIn = false, shiftIn = false, keyIn = 0)
if (valueIn.equals("None", ignoreCase = true)) {
value.clear()
return
}

val ctrl = string.startsWith("Ctrl+")
if (ctrl) {
string = string.substring(5)
}
val splitNames = valueIn.split('+')
val lastKey = KeyboardUtils.getKey(splitNames.last())

val alt = string.startsWith("Alt+")
if (alt) {
string = string.substring(4)
// Don't clear if the string is fucked
if (lastKey !in 1..255) {
println("Invalid last key")
return
}

val shift = string.startsWith("Shift+")
if (shift) {
string = string.substring(6)
}
val modifierKeys = TreeSet(Bind.keyComparator)
for (index in 0 until splitNames.size - 1) {
val name = splitNames[index]
val key = KeyboardUtils.getKey(name)

val key = Keyboard.getKeyIndex(string.toUpperCase())
if (key !in 1..255) continue
modifierKeys.add(key)
}

value.setBind(ctrl, alt, shift, key)
value.setBind(modifierKeys, lastKey)
}

override fun write() = JsonPrimitive(value.toString())
Expand All @@ -53,4 +53,4 @@ class BindSetting(
setValue(jsonElement?.asString ?: "None")
}

}
}
Loading

0 comments on commit f60cc0e

Please sign in to comment.