Skip to content

Commit

Permalink
Chocfactorykeybinds (#1644)
Browse files Browse the repository at this point in the history
Co-authored-by: Cal <cwolfson58@gmail.com>
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
  • Loading branch information
3 people authored May 2, 2024
1 parent 382f226 commit 510c025
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactor
import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactoryBarnManager
import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactoryDataLoader
import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactoryInventory
import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactoryKeybinds
import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactoryShortcut
import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactoryStats
import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactoryTimeTowerManager
Expand Down Expand Up @@ -643,6 +644,7 @@ class SkyHanniMod {
loadModule(ChocolateFactoryTooltipCompact)
loadModule(ChocolateFactoryTimeTowerManager)
loadModule(ChocolateFactoryTooltip)
loadModule(ChocolateFactoryKeybinds)
loadModule(ChocolateShopPrice)
loadModule(ChocolateFactoryUpgradeWarning)
loadModule(HoppityNpc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,9 @@ public class ChocolateFactoryConfig {
@Accordion
public ChocolateShopPriceConfig chocolateShopPrice = new ChocolateShopPriceConfig();

@Expose
@ConfigOption(name = "Chocolate Factory Keybinds", desc = "")
@Accordion
public ChocolateFactoryKeybindsConfig keybinds = new ChocolateFactoryKeybindsConfig();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package at.hannibal2.skyhanni.config.features.inventory.chocolatefactory;

import at.hannibal2.skyhanni.config.FeatureToggle;
import com.google.gson.annotations.Expose;
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorBoolean;
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorKeybind;
import io.github.notenoughupdates.moulconfig.annotations.ConfigOption;
import org.lwjgl.input.Keyboard;

public class ChocolateFactoryKeybindsConfig {
@Expose
@ConfigOption(name = "Enabled", desc = "In the Chocolate Factory, press buttons with your number row on the keyboard to upgrade the rabbits.")
@ConfigEditorBoolean
@FeatureToggle
public boolean enabled = false;

@Expose
@ConfigOption(name = "Key 1", desc = "Key for Rabbit Bro.")
@ConfigEditorKeybind(defaultKey = Keyboard.KEY_1)
public int key1 = Keyboard.KEY_1;

@Expose
@ConfigOption(name = "Key 2", desc = "Key for Rabbit Cousin.")
@ConfigEditorKeybind(defaultKey = Keyboard.KEY_2)
public int key2 = Keyboard.KEY_2;

@Expose
@ConfigOption(name = "Key 3", desc = "Key for Rabbit Sis.")
@ConfigEditorKeybind(defaultKey = Keyboard.KEY_3)
public int key3 = Keyboard.KEY_3;

@Expose
@ConfigOption(name = "Key 4", desc = "Key for Rabbit Daddy.")
@ConfigEditorKeybind(defaultKey = Keyboard.KEY_4)
public int key4 = Keyboard.KEY_4;

@Expose
@ConfigOption(name = "Key 5", desc = "Key for Rabbit Granny.")
@ConfigEditorKeybind(defaultKey = Keyboard.KEY_5)
public int key5 = Keyboard.KEY_5;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package at.hannibal2.skyhanni.events

import net.minecraft.client.gui.inventory.GuiContainer
import net.minecraftforge.fml.common.eventhandler.Cancelable

@Cancelable
class GuiKeyPressEvent(val guiContainer: GuiContainer) : LorenzEvent()
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package at.hannibal2.skyhanni.features.inventory
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.events.GuiContainerEvent
import at.hannibal2.skyhanni.events.GuiKeyPressEvent
import at.hannibal2.skyhanni.events.InventoryCloseEvent
import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent
import at.hannibal2.skyhanni.events.RenderItemTipEvent
Expand All @@ -20,7 +21,6 @@ import net.minecraft.client.Minecraft
import net.minecraft.client.gui.ScaledResolution
import net.minecraft.client.gui.inventory.GuiChest
import net.minecraft.item.Item
import net.minecraftforge.client.event.GuiScreenEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.network.FMLNetworkEvent
import kotlin.time.Duration.Companion.milliseconds
Expand All @@ -34,17 +34,6 @@ object HarpFeatures {

private const val closeButtonSlot = 40

private object KeyIterable : Iterable<Int> {

override fun iterator() = object : Iterator<Int> {
private var currentIndex = 0

override fun hasNext() = currentIndex < 7

override fun next() = getKey(currentIndex++) ?: throw NoSuchElementException("currentIndex: $currentIndex")
}
}

private val buttonColors = listOf('d', 'e', 'a', '2', '5', '9', 'b')

private val patternGroup = RepoPattern.group("harp")
Expand All @@ -65,16 +54,19 @@ object HarpFeatures {
private fun isMenuGui(chestName: String) = menuTitlePattern.matches(chestName)

@SubscribeEvent
fun onGui(event: GuiScreenEvent) {
fun onGui(event: GuiKeyPressEvent) {
if (!LorenzUtils.inSkyBlock) return
if (!config.keybinds) return
if (!isHarpGui(InventoryUtils.openInventoryName())) return
val chest = event.gui as? GuiChest ?: return
val chest = event.guiContainer as? GuiChest ?: return

for ((index, key) in KeyIterable.withIndex()) {
for (index in 0..6) {
val key = getKey(index) ?: error("no key for index $index")
if (!key.isKeyHeld()) continue
if (lastClick.passedSince() < 200.milliseconds) break

event.cancel()

Minecraft.getMinecraft().playerController.windowClick(
chest.inventorySlots.windowId,
37 + index,
Expand All @@ -87,7 +79,7 @@ object HarpFeatures {
}
}

fun getKey(index: Int) = when (index) {
private fun getKey(index: Int) = when (index) {
0 -> config.harpKeybinds.key1
1 -> config.harpKeybinds.key2
2 -> config.harpKeybinds.key3
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package at.hannibal2.skyhanni.features.inventory.chocolatefactory

import at.hannibal2.skyhanni.events.GuiKeyPressEvent
import at.hannibal2.skyhanni.utils.KeyboardManager.isKeyClicked
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.inventory.GuiChest
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration.Companion.milliseconds

object ChocolateFactoryKeybinds {
private val config get() = ChocolateFactoryAPI.config.keybinds
private var lastClick = SimpleTimeMark.farPast()

@SubscribeEvent
fun onKeyPress(event: GuiKeyPressEvent) {
if (!LorenzUtils.inSkyBlock) return
if (!config.enabled) return
if (!ChocolateFactoryAPI.inChocolateFactory) return

val chest = event.guiContainer as? GuiChest ?: return

for (index in 0..4) {
val key = getKey(index) ?: error("no key for index $index")
if (!key.isKeyClicked()) continue
if (lastClick.passedSince() < 200.milliseconds) break
lastClick = SimpleTimeMark.now()

event.cancel()

Minecraft.getMinecraft().playerController.windowClick(
chest.inventorySlots.windowId,
29 + index,
2,
3,
Minecraft.getMinecraft().thePlayer
)
break
}
}

private fun getKey(index: Int) = when (index) {
0 -> config.key1
1 -> config.key2
2 -> config.key3
3 -> config.key4
4 -> config.key5
else -> null
}
}
6 changes: 4 additions & 2 deletions src/main/java/at/hannibal2/skyhanni/utils/KeyboardManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ object KeyboardManager {
fun getModifierKeyName(): String = if (SystemUtils.IS_OS_MAC) "Command" else "Control"

@SubscribeEvent
fun onGuiScreenKeybind(event: GuiScreenEvent.KeyboardInputEvent.Post) {
fun onGuiScreenKeybind(event: GuiScreenEvent.KeyboardInputEvent.Pre) {
val guiScreen = event.gui as? GuiContainer ?: return
GuiKeyPressEvent(guiScreen).postAndCatch()
if (GuiKeyPressEvent(guiScreen).postAndCatch()) {
event.isCanceled = true
}
}

@SubscribeEvent
Expand Down

0 comments on commit 510c025

Please sign in to comment.