diff options
| author | NopoTheGamer <40329022+NopoTheGamer@users.noreply.github.com> | 2024-07-08 23:40:46 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-08 15:40:46 +0200 |
| commit | d929b60c6817be79a85513635a733384567217bd (patch) | |
| tree | be5e5d3d7ddde7ddc4820e5f2eec4d8255ef75bc /src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java | |
| parent | 24b755c74439ec10860402c34b83e0ec9bb90c28 (diff) | |
| download | notenoughupdates-d929b60c6817be79a85513635a733384567217bd.tar.gz notenoughupdates-d929b60c6817be79a85513635a733384567217bd.tar.bz2 notenoughupdates-d929b60c6817be79a85513635a733384567217bd.zip | |
meta: Add clipboard caching (#1229)
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java')
| -rw-r--r-- | src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java b/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java index 041bf104..b94fac03 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java @@ -75,6 +75,11 @@ import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL14; import java.awt.*; +import java.awt.datatransfer.ClipboardOwner; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.StringSelection; +import java.awt.datatransfer.Transferable; +import java.awt.datatransfer.UnsupportedFlavorException; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; @@ -2433,4 +2438,39 @@ public class Utils { .resolveToItemStack(false); return itemStack != null; } + + public static void copyToClipboard(String str) { + Toolkit.getDefaultToolkit().getSystemClipboard() + .setContents(new StringSelection(str), null); + } + + public static void copyToClipboard(StringSelection stringSelection, ClipboardOwner owner) { + Toolkit.getDefaultToolkit().getSystemClipboard() + .setContents(stringSelection, owner); + } + + private static String clipboardCache = ""; + private static long lastClipboard = -1; + + public static String getClipboard() { + if (System.currentTimeMillis() - lastClipboard < 500) { + return clipboardCache; + } + lastClipboard = System.currentTimeMillis(); + try { + Transferable clipboard = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); + + if (clipboard != null && clipboard.isDataFlavorSupported(DataFlavor.stringFlavor)) { + String clipboardText = (String) clipboard.getTransferData(DataFlavor.stringFlavor); + clipboardCache = clipboardText; + return clipboardText; + } else { + clipboardCache = null; + return null; + } + } catch (UnsupportedFlavorException | IOException | HeadlessException | IllegalStateException ignored) { + clipboardCache = null; + return null; + } + } } |
