diff options
author | hannibal2 <24389977+hannibal002@users.noreply.github.com> | 2023-08-10 17:44:07 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-10 17:44:07 +0200 |
commit | 9e474fb8fe76b02960b3743e31015018e8666b7f (patch) | |
tree | 8ee8f90e00ccdca3471847e0810ae4c91d57ce5d /src/main/java/at/hannibal2 | |
parent | 4d70a91399a6c1724097ef562d5210d2d1ae097a (diff) | |
download | skyhanni-9e474fb8fe76b02960b3743e31015018e8666b7f.tar.gz skyhanni-9e474fb8fe76b02960b3743e31015018e8666b7f.tar.bz2 skyhanni-9e474fb8fe76b02960b3743e31015018e8666b7f.zip |
Better Clipboard (#378)
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Diffstat (limited to 'src/main/java/at/hannibal2')
5 files changed, 103 insertions, 24 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/PasteIntoSigns.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/PasteIntoSigns.kt index e6f118a15..ba797ff74 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/PasteIntoSigns.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/PasteIntoSigns.kt @@ -4,6 +4,7 @@ import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.events.LorenzTickEvent import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.OSUtils +import kotlinx.coroutines.launch import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import org.lwjgl.input.Keyboard @@ -15,8 +16,11 @@ class PasteIntoSigns { if (!SkyHanniMod.feature.misc.pasteIntoSigns) return if (LorenzUtils.isControlKeyDown() && Keyboard.isKeyDown(Keyboard.KEY_V)) { - val clipboard = OSUtils.readFromClipboard() ?: return - LorenzUtils.setTextIntoSign(clipboard.take(15)) + SkyHanniMod.coroutineScope.launch { + OSUtils.readFromClipboard()?.let { + LorenzUtils.setTextIntoSign(it.take(15)) + } + } } } }
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/test/SkyHanniConfigSearchResetCommand.kt b/src/main/java/at/hannibal2/skyhanni/test/SkyHanniConfigSearchResetCommand.kt index 5d36cdb90..fbdb21257 100644 --- a/src/main/java/at/hannibal2/skyhanni/test/SkyHanniConfigSearchResetCommand.kt +++ b/src/main/java/at/hannibal2/skyhanni/test/SkyHanniConfigSearchResetCommand.kt @@ -12,6 +12,7 @@ import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators import at.hannibal2.skyhanni.utils.OSUtils import com.google.gson.JsonElement import io.github.moulberry.notenoughupdates.util.Shimmy +import kotlinx.coroutines.launch import java.lang.reflect.Field import java.lang.reflect.Modifier @@ -20,11 +21,13 @@ object SkyHanniConfigSearchResetCommand { private var lastCommand = emptyArray<String>() fun command(args: Array<String>) { - LorenzUtils.chat(runCommand(args)) + SkyHanniMod.coroutineScope.launch { + LorenzUtils.chat(runCommand(args)) + } lastCommand = args } - private fun runCommand(args: Array<String>): String { + private suspend fun runCommand(args: Array<String>): String { if (args.isEmpty()) { return "§c[SkyHanni] This is a powerful config-edit command, only use it if you know what you are doing!" } @@ -71,7 +74,7 @@ object SkyHanniConfigSearchResetCommand { } } - private fun setCommand(args: Array<String>): String { + private suspend fun setCommand(args: Array<String>): String { if (args.size < 3) return "§c/shconfig set <config name> <json element>" val term = args[1] var rawJson = args.drop(2).joinToString(" ") diff --git a/src/main/java/at/hannibal2/skyhanni/test/SkyHanniTestCommand.kt b/src/main/java/at/hannibal2/skyhanni/test/SkyHanniTestCommand.kt index a3ddcafd0..2cfb5ae94 100644 --- a/src/main/java/at/hannibal2/skyhanni/test/SkyHanniTestCommand.kt +++ b/src/main/java/at/hannibal2/skyhanni/test/SkyHanniTestCommand.kt @@ -45,6 +45,12 @@ class SkyHanniTestCommand { fun testCommand(args: Array<String>) { SoundUtils.playBeepSound() + val a = Thread { OSUtils.copyToClipboard("123") } + val b = Thread { OSUtils.copyToClipboard("456") } + a.start() + b.start() + + // for ((i, s) in ScoreboardData.siedebarLinesFormatted().withIndex()) { // println("$i: '$s'") // } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ClipboardUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ClipboardUtils.kt new file mode 100644 index 000000000..a7585702f --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/utils/ClipboardUtils.kt @@ -0,0 +1,83 @@ +package at.hannibal2.skyhanni.utils + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.test.command.CopyErrorCommand +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import java.awt.Toolkit +import java.awt.datatransfer.Clipboard +import java.awt.datatransfer.DataFlavor +import java.awt.datatransfer.StringSelection +import java.awt.datatransfer.UnsupportedFlavorException +import java.util.* +import kotlin.time.Duration +import kotlin.time.Duration.Companion.milliseconds + +object ClipboardUtils { + private var lastClipboardAccessTime = SimpleTimeMark.farPast() + + private fun canAccessClibpard(): Boolean { + val result = lastClipboardAccessTime.passedSince() > 10.milliseconds + if (result) { + lastClipboardAccessTime = SimpleTimeMark.now() + } + return result + } + + private fun runDelayed(duration: Duration, runnable: () -> Unit) { + Timer().schedule(object : TimerTask() { + override fun run() { + runnable() + } + }, duration.inWholeMilliseconds) + } + + private suspend fun getClipboard(): Clipboard? { + val deferred = CompletableDeferred<Clipboard?>() + if (canAccessClibpard()) { + deferred.complete(Toolkit.getDefaultToolkit().systemClipboard) + } else { + runDelayed(5.milliseconds) { + SkyHanniMod.coroutineScope.launch { + deferred.complete(getClipboard()) + } + } + } + return deferred.await() + } + + fun copyToClipboard(text: String, step: Int = 0) { + SkyHanniMod.coroutineScope.launch { + try { + getClipboard()?.setContents(StringSelection(text), null) + } catch (e: Exception) { + if (step == 3) { + CopyErrorCommand.logError(e, "Error while trying to access the clipboard.") + } else { + copyToClipboard(text, step + 1) + } + } + } + } + + suspend fun readFromClipboard(step: Int = 0): String? { + try { + return try { + withContext(Dispatchers.IO) { + getClipboard()?.getData(DataFlavor.stringFlavor)?.toString() + } + } catch (e: UnsupportedFlavorException) { + null + } + } catch (e: Exception) { + return if (step == 3) { + CopyErrorCommand.logError(e, "Error while trying to access the clipboard.") + null + } else { + readFromClipboard(step + 1) + } + } + } +}
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/utils/OSUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/OSUtils.kt index 9c56d142f..254d8ad09 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/OSUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/OSUtils.kt @@ -1,13 +1,8 @@ package at.hannibal2.skyhanni.utils -import at.hannibal2.skyhanni.test.command.CopyErrorCommand import net.minecraft.client.settings.KeyBinding import org.lwjgl.input.Keyboard import java.awt.Desktop -import java.awt.Toolkit -import java.awt.datatransfer.DataFlavor -import java.awt.datatransfer.StringSelection -import java.awt.datatransfer.UnsupportedFlavorException import java.io.IOException import java.net.URI @@ -29,22 +24,10 @@ object OSUtils { } fun copyToClipboard(text: String) { - try { - Toolkit.getDefaultToolkit().systemClipboard.setContents(StringSelection(text), null) - } catch (e: Exception) { - CopyErrorCommand.logError(e, "Error while trying to set clipboard content.") - } + ClipboardUtils.copyToClipboard(text) } - fun readFromClipboard(): String? { - val systemClipboard = Toolkit.getDefaultToolkit().systemClipboard ?: return null - try { - val data = systemClipboard.getData(DataFlavor.stringFlavor) ?: return null - return data.toString() - } catch (e: UnsupportedFlavorException) { - return null - } - } + suspend fun readFromClipboard() = ClipboardUtils.readFromClipboard() fun KeyBinding.isActive(): Boolean { if (!Keyboard.isCreated()) return false |