Skip to content

Commit

Permalink
new: use onekeybind for keybinds
Browse files Browse the repository at this point in the history
  • Loading branch information
Wyvest committed Oct 26, 2022
1 parent 9c1cb41 commit b678ba6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod_name = SimpleToggleSprint
mod_id = simpletogglesprint
mod_version = 1.0.0
mod_version = 1.0.0-beta2

essential.defaults.loom=0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@
*/
package mynameisjeff.simpletogglesprint

import cc.polyfrost.oneconfig.libs.universal.UKeyboard
import cc.polyfrost.oneconfig.libs.universal.UMinecraft
import cc.polyfrost.oneconfig.utils.commands.CommandManager
import mynameisjeff.simpletogglesprint.commands.SimpleToggleSprintCommand
import mynameisjeff.simpletogglesprint.core.SimpleToggleSprintConfig
import mynameisjeff.simpletogglesprint.core.checkKeyCode
import mynameisjeff.simpletogglesprint.mixins.accessors.AccessorGameSettings
import mynameisjeff.simpletogglesprint.mixins.accessors.AccessorKeybinding
import net.minecraft.client.settings.KeyBinding
import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.fml.client.registry.ClientRegistry
import net.minecraftforge.fml.common.Mod
import net.minecraftforge.fml.common.event.FMLInitializationEvent
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent
Expand All @@ -51,23 +48,19 @@ object SimpleToggleSprint {
get() = UMinecraft.getPlayer()
val gameSettings
get() = UMinecraft.getSettings() as AccessorGameSettings
val keySprint = KeyBinding("Toggle Sprint", UKeyboard.KEY_NONE, "SimpleToggleSprint")
val keySneak = KeyBinding("Toggle Sneak", UKeyboard.KEY_NONE, "SimpleToggleSprint")

var sprintHeld = false
var sneakHeld = false

@Mod.EventHandler
fun onInit(event: FMLInitializationEvent) {
SimpleToggleSprintConfig.initialize()
SimpleToggleSprintConfig
MinecraftForge.EVENT_BUS.register(this)
}

@Mod.EventHandler
fun onPostInit(event: FMLPostInitializationEvent) {
CommandManager.INSTANCE.registerCommand(SimpleToggleSprintCommand())
ClientRegistry.registerKeyBinding(keySprint)
ClientRegistry.registerKeyBinding(keySneak)
}


Expand All @@ -76,9 +69,7 @@ object SimpleToggleSprint {
if (!SimpleToggleSprintConfig.enabled) return
val sprint = (gameSettings.keyBindSprint as AccessorKeybinding).keyCode
val sneak = (gameSettings.keyBindSneak as AccessorKeybinding).keyCode
val sprintToggle = (keySprint as AccessorKeybinding).keyCode
val sneakToggle = (keySneak as AccessorKeybinding).keyCode
if ((SimpleToggleSprintConfig.keybindToggleSprint && checkKeyCode(sprintToggle)) || (!SimpleToggleSprintConfig.keybindToggleSprint && checkKeyCode(sprint))) {
if (!SimpleToggleSprintConfig.keybindToggleSprint && checkKeyCode(sprint)) {
if (SimpleToggleSprintConfig.enabled && SimpleToggleSprintConfig.toggleSprint && !sprintHeld) {
SimpleToggleSprintConfig.toggleSprintState = !SimpleToggleSprintConfig.toggleSprintState
SimpleToggleSprintConfig.save()
Expand All @@ -87,7 +78,7 @@ object SimpleToggleSprint {
} else {
sprintHeld = false
}
if ((SimpleToggleSprintConfig.keybindToggleSneak && checkKeyCode(sneakToggle)) || (!SimpleToggleSprintConfig.keybindToggleSneak && checkKeyCode(sneak))) {
if (!SimpleToggleSprintConfig.keybindToggleSneak && checkKeyCode(sneak)) {
if (SimpleToggleSprintConfig.enabled && SimpleToggleSprintConfig.toggleSneak && !sneakHeld) {
SimpleToggleSprintConfig.toggleSneakState = !SimpleToggleSprintConfig.toggleSneakState
SimpleToggleSprintConfig.save()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package mynameisjeff.simpletogglesprint.core

import cc.polyfrost.oneconfig.config.Config
import cc.polyfrost.oneconfig.config.annotations.HUD
import cc.polyfrost.oneconfig.config.annotations.KeyBind
import cc.polyfrost.oneconfig.config.annotations.Switch
import cc.polyfrost.oneconfig.config.annotations.Text
import cc.polyfrost.oneconfig.config.core.OneKeyBind
import cc.polyfrost.oneconfig.config.data.Mod
import cc.polyfrost.oneconfig.config.data.ModType
import cc.polyfrost.oneconfig.config.migration.VigilanceMigrator
import cc.polyfrost.oneconfig.hud.TextHud
import cc.polyfrost.oneconfig.libs.universal.UKeyboard
import mynameisjeff.simpletogglesprint.SimpleToggleSprint
import mynameisjeff.simpletogglesprint.core.SimpleToggleSprintConfig.ToggleSprintHud.DisplayState.Companion.activeDisplay
import mynameisjeff.simpletogglesprint.mixins.accessors.AccessorEntityPlayer
Expand Down Expand Up @@ -40,23 +43,61 @@ object SimpleToggleSprintConfig : Config(Mod("SimpleToggleSprint", ModType.PVP,
@Switch(
name = "Seperate Keybind for Toggle Sprint",
subcategory = "Toggle Sprint",
description = "Use a seperate keybind for Toggle Sprint.\nConfigure it in the In-Game Controls menu."
description = "Use a seperate keybind for Toggle Sprint."
)
var keybindToggleSprint = false

@KeyBind(
name = "Toggle Sprint Keybind",
subcategory = "Toggle Sprint"
)
var keybindToggleSprintKey = OneKeyBind(UKeyboard.KEY_NONE)

@Switch(
name = "Seperate Keybind for Toggle Sneak",
subcategory = "Toggle Sneak",
description = "Use a seperate keybind for Toggle Sneak.\nConfigure it in the In-Game Controls menu."
description = "Use a seperate keybind for Toggle Sneak."
)
var keybindToggleSneak = false

@KeyBind(
name = "Toggle Sneak Keybind",
subcategory = "Toggle Sneak"
)
var keybindToggleSneakKey = OneKeyBind(UKeyboard.KEY_NONE)

@HUD(
name = "HUD",
subcategory = "HUD"
)
var hud = ToggleSprintHud()

init {
initialize()
addDependency("keybindToggleSprint", "toggleSprint")
addDependency("keybindToggleSneak", "toggleSneak")
addDependency("keybindToggleSprintKey", "keybindToggleSprint")
addDependency("keybindToggleSneakKey", "keybindToggleSneak")
registerKeyBind(keybindToggleSprintKey) {
if (keybindToggleSprint) {
if (enabled && toggleSprint && !SimpleToggleSprint.sprintHeld) {
toggleSprintState = !toggleSprintState
SimpleToggleSprintConfig.save()
}
SimpleToggleSprint.sprintHeld = !SimpleToggleSprint.sprintHeld
}
}
registerKeyBind(keybindToggleSneakKey) {
if (keybindToggleSneak) {
if (enabled && toggleSneak && !SimpleToggleSprint.sneakHeld) {
toggleSneakState = !toggleSneakState
SimpleToggleSprintConfig.save()
}
SimpleToggleSprint.sneakHeld = !SimpleToggleSprint.sneakHeld
}
}
}

class ToggleSprintHud : TextHud(true, 0, 1080 - 19) {
@Switch(name = "Brackets")
private var brackets = true
Expand Down

0 comments on commit b678ba6

Please sign in to comment.