From 48f44ad4eb3d04ec8d786bc3401da440717114ce Mon Sep 17 00:00:00 2001 From: Deftu Date: Thu, 14 Nov 2024 14:52:11 +0200 Subject: [PATCH 1/3] Add Copycat and clean up OneConfig file --- .../org/polyfrost/gradle/addDependencies.kt | 13 +++++ gradle/libs.versions.toml | 4 ++ modules/utils/build.gradle.kts | 2 + .../polyfrost/oneconfig/utils/v1/IOUtils.java | 23 --------- .../oneconfig/utils/v1/OneImage.java | 4 +- .../api/ui/v1/internal/wrappers/MCWindow.java | 14 ++--- .../oneconfig/internal/OneConfig.java | 51 +++++++++++++++++-- 7 files changed, 73 insertions(+), 38 deletions(-) diff --git a/buildSrc/src/main/kotlin/org/polyfrost/gradle/addDependencies.kt b/buildSrc/src/main/kotlin/org/polyfrost/gradle/addDependencies.kt index e71b39be9..9a6d8ad52 100644 --- a/buildSrc/src/main/kotlin/org/polyfrost/gradle/addDependencies.kt +++ b/buildSrc/src/main/kotlin/org/polyfrost/gradle/addDependencies.kt @@ -16,6 +16,19 @@ fun Project.provideIncludedDependencies(version: Triple?, loader: deps.add(libs.findLibrary("snakeyaml").get().get()) deps.add(libs.findLibrary("isolated-lwjgl3-loader").get().get()) deps.add(libs.findLibrary("polyio").get().get()) + val copycat = libs.findLibrary("copycat").get().get() + deps.add(copycat) + setOf( + "windows" to setOf("x64", "x86"), + "linux" to setOf("x64", "x86", "arm", "arm64"), + "osx" to setOf("x64", "arm64") + ).forEach { (os, arches) -> + arches.forEach { arch -> + deps.add("${copycat.group}:${copycat.name}-natives-$os-$arch:${copycat.version}") + } + } + + deps.add(libs.findLibrary("copycat-image-awt").get().get()) deps.add(libs.findLibrary("polyui").get().get()) deps.add(libs.findLibrary("hypixel-modapi").get().get()) deps.add(libs.findLibrary("hypixel-data").get().get()) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 832e33db6..bcf5deb59 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -16,6 +16,8 @@ snakeyaml = "1.31" nightconfig = "3.6.6" isolated-lwjgl3-loader = "0.1.1" polyio = "0.1.0" +copycat = "0.1.1" +copycat-image-awt = "0.1.1" # Per version universalcraft = "299" @@ -45,6 +47,8 @@ mixin = { module = "org.polyfrost:polymixin", version.ref = "mixin" } isolated-lwjgl3-loader = { module = "org.polyfrost:isolated-lwjgl3-loader", version.ref = "isolated-lwjgl3-loader" } polyio = { module = "org.polyfrost:polyio", version.ref = "polyio" } +copycat = { module = "dev.deftu:copycat", version.ref = "copycat" } +copycat-image-awt = { module = "dev.deftu:copycat-image-awt", version.ref = "copycat-image-awt" } hypixel-modapi = { module = "net.hypixel:mod-api", version.ref = "hypixel-modapi" } hypixel-data = { module = "net.hypixel:hypixel-data", version.ref = "hypixel-data" } diff --git a/modules/utils/build.gradle.kts b/modules/utils/build.gradle.kts index 47b2cbe9e..6cfdbe2da 100644 --- a/modules/utils/build.gradle.kts +++ b/modules/utils/build.gradle.kts @@ -28,5 +28,7 @@ dependencies { compileOnly("org.polyfrost:universalcraft-1.8.9-forge:${libs.versions.universalcraft.get()}") @Suppress("VulnerableLibrariesLocal") // reason: we need to use version this because it is what 1.8.9 uses compileOnly("com.google.code.gson:gson:2.2.4") + compileOnly(rootProject.libs.copycat) + compileOnly(rootProject.libs.copycat.image.awt) api(libs.hypixel.modapi) } \ No newline at end of file diff --git a/modules/utils/src/main/java/org/polyfrost/oneconfig/utils/v1/IOUtils.java b/modules/utils/src/main/java/org/polyfrost/oneconfig/utils/v1/IOUtils.java index 64ddb8980..2f428f503 100644 --- a/modules/utils/src/main/java/org/polyfrost/oneconfig/utils/v1/IOUtils.java +++ b/modules/utils/src/main/java/org/polyfrost/oneconfig/utils/v1/IOUtils.java @@ -49,29 +49,6 @@ public final class IOUtils { private IOUtils() { } - /** - * Copy the specified String to the System Clipboard. - * - * @param s the string to copy - */ - public static void copyStringToClipboard(String s) { - StringSelection stringSelection = new StringSelection(s); - Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null); - } - - /** - * Return the String on the system clipboard. - * - * @return the string on the system clipboard, or null if there is no string on the clipboard or another error occurred. - */ - public static String getStringFromClipboard() { - try { - return Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor).toString(); - } catch (Exception e) { - return null; - } - } - /** * Copy the given image to the System Clipboard. * diff --git a/modules/utils/src/main/java/org/polyfrost/oneconfig/utils/v1/OneImage.java b/modules/utils/src/main/java/org/polyfrost/oneconfig/utils/v1/OneImage.java index ba570e890..132c5d8c2 100644 --- a/modules/utils/src/main/java/org/polyfrost/oneconfig/utils/v1/OneImage.java +++ b/modules/utils/src/main/java/org/polyfrost/oneconfig/utils/v1/OneImage.java @@ -26,6 +26,8 @@ package org.polyfrost.oneconfig.utils.v1; +import dev.deftu.clipboard.BufferedClipboardImage; +import dev.deftu.clipboard.Clipboard; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.ApiStatus; @@ -191,7 +193,7 @@ public String uploadToImgur() { * Copy the image to the system clipboard and delete the graphics object. */ public void copyToClipboard() { - IOUtils.copyImageToClipboard(image); + Clipboard.getInstance().setImage(BufferedClipboardImage.toClipboardImage(this.image)); dispose(); } diff --git a/versions/src/main/java/org/polyfrost/oneconfig/api/ui/v1/internal/wrappers/MCWindow.java b/versions/src/main/java/org/polyfrost/oneconfig/api/ui/v1/internal/wrappers/MCWindow.java index 06eeb1989..d748a3a94 100644 --- a/versions/src/main/java/org/polyfrost/oneconfig/api/ui/v1/internal/wrappers/MCWindow.java +++ b/versions/src/main/java/org/polyfrost/oneconfig/api/ui/v1/internal/wrappers/MCWindow.java @@ -26,12 +26,12 @@ package org.polyfrost.oneconfig.api.ui.v1.internal.wrappers; +import dev.deftu.clipboard.Clipboard; import net.minecraft.client.Minecraft; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.polyfrost.oneconfig.api.platform.v1.Platform; -import org.polyfrost.oneconfig.utils.v1.IOUtils; import org.polyfrost.polyui.PolyUI; import org.polyfrost.polyui.renderer.Window; import org.polyfrost.polyui.data.Cursor; @@ -73,20 +73,12 @@ public boolean supportsRenderPausing() { @Nullable @Override public String getClipboard() { - //#if MC>=11300 - //$$ return glfwGetClipboardString(handle); - //#else - return IOUtils.getStringFromClipboard(); - //#endif + return Clipboard.getInstance().getString(); } @Override public void setClipboard(@Nullable String s) { - //#if MC>=11300 - //$$ glfwSetClipboardString(handle, s); - //#else - IOUtils.copyStringToClipboard(s); - //#endif + Clipboard.getInstance().setString(s); } @Override diff --git a/versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfig.java b/versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfig.java index 3d6d7876e..8526681c7 100644 --- a/versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfig.java +++ b/versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfig.java @@ -26,6 +26,7 @@ package org.polyfrost.oneconfig.internal; +import dev.deftu.clipboard.Clipboard; import kotlin.Unit; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -88,37 +89,81 @@ private void onInit(net.minecraftforge.fml.common.event.FMLPostInitializationEve private void init() { BlurHandler.init(); - preload(); + + preloadCopycat(); + preloadPolyUI(); + + registerCommands(); + registerKeybinds(); + registerEventHandlers(); + + LOGGER.info("OneConfig initialized!"); + } + + private static void registerCommands() { CommandBuilder b = CommandBuilder.command("oneconfig", "ocfg", "ocfgv1").description("OneConfig main command"); b.then(runs().does((Runnable) OneConfigUI.INSTANCE::open).description("Opens the OneConfig GUI")); b.then(runs("updateCheck").does(() -> Multithreading.submit(() -> UChat.chat(MavenUpdateChecker.oneconfig().hasUpdate() ? "Update available!" : "No updates available"))).description("Check for updates")); b.then(runs("locraw").does(() -> UChat.chat(HypixelUtils.getLocation()))).description("Get your current location on Hypixel"); b.then(runs("hud").does(() -> Platform.screen().display(HudManager.INSTANCE.getWithEditor())).description("Opens the OneConfig HUD editor")); CommandManager.registerCommand(b.build()); + } + + private static void registerKeybinds() { OCKeybindHelper builder = OCKeybindHelper.builder(); builder.mods(KeyModifiers.RSHIFT).does((s) -> { if (s) OneConfigUI.INSTANCE.open(); return Unit.INSTANCE; }); + builder.register(); + } + + private static void registerEventHandlers() { EventManager.register(InitializationEvent.class, e -> HudManager.INSTANCE.initialize()); - LOGGER.info("OneConfig initialized!"); } /** * Ensure that key PolyUI classes are loaded to prevent lag-spikes when loading PolyUI for the first time. */ - private static void preload() { + private static void preloadPolyUI() { long t1 = System.nanoTime(); try { + // PolyUI Class.forName(PolyUI.class.getName()); Class.forName(Drawable.class.getName()); Class.forName(Translator.class.getName()); + + // OneConfig PolyUI renderer // todo: fix for fabric loaders as fails due to running too early // UIManager.INSTANCE.getRenderer(); } catch (Exception e) { throw new IllegalStateException("Failed to preload necessary PolyUI classes", e); } + LOGGER.info("PolyUI preload took {}ms", (System.nanoTime() - t1) / 1_000_000.0); } + + private static void preloadCopycat() { + long t1 = System.nanoTime(); + try { + // Copycat + /// Ensure native is loaded + Clipboard.getInstance(); + + /// Ensure copying works + Clipboard clipboard = Clipboard.getInstance(); + String currentContent = clipboard.getString(); + clipboard.setString("OneConfig Test"); + if (!"OneConfig Test".equals(clipboard.getString())) { + throw new IllegalStateException("Failed to set clipboard content"); + } + + clipboard.setString(currentContent); // Reset clipboard + } catch (Exception e) { + throw new IllegalStateException("Failed to preload necessary Copycat classes", e); + } + + LOGGER.info("Copycat preload took {}ms", (System.nanoTime() - t1) / 1_000_000.0); + } } From 649467ea5968210031eda2c1b9c41dc9e61c8615 Mon Sep 17 00:00:00 2001 From: Deftu Date: Thu, 14 Nov 2024 14:58:20 +0200 Subject: [PATCH 2/3] Remove the rest of the old clipboard-relating methods --- .../polyfrost/oneconfig/utils/v1/IOUtils.java | 23 ------------------- 1 file changed, 23 deletions(-) diff --git a/modules/utils/src/main/java/org/polyfrost/oneconfig/utils/v1/IOUtils.java b/modules/utils/src/main/java/org/polyfrost/oneconfig/utils/v1/IOUtils.java index 2f428f503..712837037 100644 --- a/modules/utils/src/main/java/org/polyfrost/oneconfig/utils/v1/IOUtils.java +++ b/modules/utils/src/main/java/org/polyfrost/oneconfig/utils/v1/IOUtils.java @@ -49,29 +49,6 @@ public final class IOUtils { private IOUtils() { } - /** - * Copy the given image to the System Clipboard. - * - * @param image the image to copy - */ - public static void copyImageToClipboard(Image image) { - ImageSelection imageSelection = new ImageSelection(image); - Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imageSelection, null); - } - - /** - * Return the image on the system clipboard. - * - * @return the image on the system clipboard, or null if there is no image on the clipboard or another error occurred. - */ - public static Image getImageFromClipboard() { - try { - return (Image) Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.imageFlavor); - } catch (Exception e) { - return null; - } - } - /** * Gets the SHA-256 hash of a file. * From 79fab0ecd4c63316865bd43325ee8b559c275f50 Mon Sep 17 00:00:00 2001 From: Deftu Date: Thu, 14 Nov 2024 15:11:45 +0200 Subject: [PATCH 3/3] Remove test code for Copycat --- .../org/polyfrost/oneconfig/internal/OneConfig.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfig.java b/versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfig.java index 8526681c7..1598c8551 100644 --- a/versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfig.java +++ b/versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfig.java @@ -148,18 +148,7 @@ private static void preloadCopycat() { long t1 = System.nanoTime(); try { // Copycat - /// Ensure native is loaded Clipboard.getInstance(); - - /// Ensure copying works - Clipboard clipboard = Clipboard.getInstance(); - String currentContent = clipboard.getString(); - clipboard.setString("OneConfig Test"); - if (!"OneConfig Test".equals(clipboard.getString())) { - throw new IllegalStateException("Failed to set clipboard content"); - } - - clipboard.setString(currentContent); // Reset clipboard } catch (Exception e) { throw new IllegalStateException("Failed to preload necessary Copycat classes", e); }