From 6cfe7aa0f56878c4729ca6e2479170e05260fae6 Mon Sep 17 00:00:00 2001 From: Obsidian <108832807+Obsidianninja11@users.noreply.github.com> Date: Mon, 11 Dec 2023 16:05:48 -0900 Subject: Better sign editing (#751) Changes ctrl+v in signs to better sign editing. #751 --- .../skyhanni/features/misc/BetterSignEditing.kt | 75 ++++++++++++++++++++++ .../skyhanni/features/misc/PasteIntoSigns.kt | 29 --------- 2 files changed, 75 insertions(+), 29 deletions(-) create mode 100644 src/main/java/at/hannibal2/skyhanni/features/misc/BetterSignEditing.kt delete mode 100644 src/main/java/at/hannibal2/skyhanni/features/misc/PasteIntoSigns.kt (limited to 'src/main/java/at/hannibal2/skyhanni/features/misc') diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/BetterSignEditing.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/BetterSignEditing.kt new file mode 100644 index 000000000..1fffb43eb --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/BetterSignEditing.kt @@ -0,0 +1,75 @@ +package at.hannibal2.skyhanni.features.misc + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator +import at.hannibal2.skyhanni.events.LorenzTickEvent +import at.hannibal2.skyhanni.mixins.transformers.AccessorGuiEditSign +import at.hannibal2.skyhanni.utils.ClipboardUtils +import at.hannibal2.skyhanni.utils.KeyboardManager +import at.hannibal2.skyhanni.utils.KeyboardManager.isKeyHeld +import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.OSUtils +import kotlinx.coroutines.launch +import net.minecraft.client.Minecraft +import net.minecraft.client.gui.GuiScreen +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import org.lwjgl.input.Keyboard + +class BetterSignEditing { + private var pasteLastClicked = false + private var copyLastClicked = false + private var deleteWordLastClicked = false + + @SubscribeEvent + fun onTick(event: LorenzTickEvent) { + if (!LorenzUtils.onHypixel) return + if (!SkyHanniMod.feature.misc.betterSignEditing) return + + val gui = Minecraft.getMinecraft().currentScreen + checkPaste() + checkCopying(gui) + checkDeleting(gui) + } + + private fun checkDeleting(gui: GuiScreen?) { + val deleteWordClicked = Keyboard.KEY_BACK.isKeyHeld() && KeyboardManager.isModifierKeyDown() + if (!deleteWordLastClicked && deleteWordClicked && gui is AccessorGuiEditSign) { + SkyHanniMod.coroutineScope.launch { + val newLine = if (KeyboardManager.isShiftKeyDown()) "" else { + val currentLine = gui.tileSign.signText[gui.editLine].unformattedText + val lastSpaceIndex = currentLine.lastIndexOf(' ') + if (lastSpaceIndex >= 0) currentLine.substring(0, lastSpaceIndex + 1) else "" + } + LorenzUtils.setTextIntoSign(newLine, gui.editLine) + } + } + deleteWordLastClicked = deleteWordClicked + } + + private fun checkCopying(gui: GuiScreen?) { + val copyClicked = KeyboardManager.isCopyingKeysDown() + if (!copyLastClicked && copyClicked && gui is AccessorGuiEditSign) { + SkyHanniMod.coroutineScope.launch { + ClipboardUtils.copyToClipboard(gui.tileSign.signText[gui.editLine].unformattedText) + } + } + copyLastClicked = copyClicked + } + + private fun checkPaste() { + val pasteClicked = KeyboardManager.isPastingKeysDown() + if (!pasteLastClicked && pasteClicked) { + SkyHanniMod.coroutineScope.launch { + OSUtils.readFromClipboard()?.let { + LorenzUtils.addTextIntoSign(it) + } + } + } + pasteLastClicked = pasteClicked + } + + @SubscribeEvent + fun onConfigFix(event: ConfigUpdaterMigrator.ConfigFixEvent) { + event.move(16, "misc.pasteIntoSigns", "misc.betterSignEditing") + } +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/PasteIntoSigns.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/PasteIntoSigns.kt deleted file mode 100644 index 5bedd57cc..000000000 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/PasteIntoSigns.kt +++ /dev/null @@ -1,29 +0,0 @@ -package at.hannibal2.skyhanni.features.misc - -import at.hannibal2.skyhanni.SkyHanniMod -import at.hannibal2.skyhanni.events.LorenzTickEvent -import at.hannibal2.skyhanni.utils.KeyboardManager -import at.hannibal2.skyhanni.utils.LorenzUtils -import at.hannibal2.skyhanni.utils.OSUtils -import kotlinx.coroutines.launch -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent - -class PasteIntoSigns { - private var lastClicked = false - - @SubscribeEvent - fun onTick(event: LorenzTickEvent) { - if (!LorenzUtils.onHypixel) return - if (!SkyHanniMod.feature.misc.pasteIntoSigns) return - - val currentlyClicked = KeyboardManager.isPastingKeysDown() - if (!lastClicked && currentlyClicked) { - SkyHanniMod.coroutineScope.launch { - OSUtils.readFromClipboard()?.let { - LorenzUtils.addTextIntoSign(it.take(15)) - } - } - } - lastClicked = currentlyClicked - } -} \ No newline at end of file -- cgit