Skip to content

Commit 48f44ad

Browse files
committed
Add Copycat and clean up OneConfig file
1 parent 9e2d711 commit 48f44ad

File tree

7 files changed

+73
-38
lines changed

7 files changed

+73
-38
lines changed

buildSrc/src/main/kotlin/org/polyfrost/gradle/addDependencies.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ fun Project.provideIncludedDependencies(version: Triple<Int, Int, Int>?, loader:
1616
deps.add(libs.findLibrary("snakeyaml").get().get())
1717
deps.add(libs.findLibrary("isolated-lwjgl3-loader").get().get())
1818
deps.add(libs.findLibrary("polyio").get().get())
19+
val copycat = libs.findLibrary("copycat").get().get()
20+
deps.add(copycat)
21+
setOf(
22+
"windows" to setOf("x64", "x86"),
23+
"linux" to setOf("x64", "x86", "arm", "arm64"),
24+
"osx" to setOf("x64", "arm64")
25+
).forEach { (os, arches) ->
26+
arches.forEach { arch ->
27+
deps.add("${copycat.group}:${copycat.name}-natives-$os-$arch:${copycat.version}")
28+
}
29+
}
30+
31+
deps.add(libs.findLibrary("copycat-image-awt").get().get())
1932
deps.add(libs.findLibrary("polyui").get().get())
2033
deps.add(libs.findLibrary("hypixel-modapi").get().get())
2134
deps.add(libs.findLibrary("hypixel-data").get().get())

gradle/libs.versions.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ snakeyaml = "1.31"
1616
nightconfig = "3.6.6"
1717
isolated-lwjgl3-loader = "0.1.1"
1818
polyio = "0.1.0"
19+
copycat = "0.1.1"
20+
copycat-image-awt = "0.1.1"
1921

2022
# Per version
2123
universalcraft = "299"
@@ -45,6 +47,8 @@ mixin = { module = "org.polyfrost:polymixin", version.ref = "mixin" }
4547

4648
isolated-lwjgl3-loader = { module = "org.polyfrost:isolated-lwjgl3-loader", version.ref = "isolated-lwjgl3-loader" }
4749
polyio = { module = "org.polyfrost:polyio", version.ref = "polyio" }
50+
copycat = { module = "dev.deftu:copycat", version.ref = "copycat" }
51+
copycat-image-awt = { module = "dev.deftu:copycat-image-awt", version.ref = "copycat-image-awt" }
4852

4953
hypixel-modapi = { module = "net.hypixel:mod-api", version.ref = "hypixel-modapi" }
5054
hypixel-data = { module = "net.hypixel:hypixel-data", version.ref = "hypixel-data" }

modules/utils/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ dependencies {
2828
compileOnly("org.polyfrost:universalcraft-1.8.9-forge:${libs.versions.universalcraft.get()}")
2929
@Suppress("VulnerableLibrariesLocal") // reason: we need to use version this because it is what 1.8.9 uses
3030
compileOnly("com.google.code.gson:gson:2.2.4")
31+
compileOnly(rootProject.libs.copycat)
32+
compileOnly(rootProject.libs.copycat.image.awt)
3133
api(libs.hypixel.modapi)
3234
}

modules/utils/src/main/java/org/polyfrost/oneconfig/utils/v1/IOUtils.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,6 @@ public final class IOUtils {
4949
private IOUtils() {
5050
}
5151

52-
/**
53-
* Copy the specified String to the System Clipboard.
54-
*
55-
* @param s the string to copy
56-
*/
57-
public static void copyStringToClipboard(String s) {
58-
StringSelection stringSelection = new StringSelection(s);
59-
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
60-
}
61-
62-
/**
63-
* Return the String on the system clipboard.
64-
*
65-
* @return the string on the system clipboard, or null if there is no string on the clipboard or another error occurred.
66-
*/
67-
public static String getStringFromClipboard() {
68-
try {
69-
return Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor).toString();
70-
} catch (Exception e) {
71-
return null;
72-
}
73-
}
74-
7552
/**
7653
* Copy the given image to the System Clipboard.
7754
*

modules/utils/src/main/java/org/polyfrost/oneconfig/utils/v1/OneImage.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
package org.polyfrost.oneconfig.utils.v1;
2828

29+
import dev.deftu.clipboard.BufferedClipboardImage;
30+
import dev.deftu.clipboard.Clipboard;
2931
import org.apache.logging.log4j.LogManager;
3032
import org.apache.logging.log4j.Logger;
3133
import org.jetbrains.annotations.ApiStatus;
@@ -191,7 +193,7 @@ public String uploadToImgur() {
191193
* Copy the image to the system clipboard and delete the graphics object.
192194
*/
193195
public void copyToClipboard() {
194-
IOUtils.copyImageToClipboard(image);
196+
Clipboard.getInstance().setImage(BufferedClipboardImage.toClipboardImage(this.image));
195197
dispose();
196198
}
197199

versions/src/main/java/org/polyfrost/oneconfig/api/ui/v1/internal/wrappers/MCWindow.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626

2727
package org.polyfrost.oneconfig.api.ui.v1.internal.wrappers;
2828

29+
import dev.deftu.clipboard.Clipboard;
2930
import net.minecraft.client.Minecraft;
3031
import org.jetbrains.annotations.ApiStatus;
3132
import org.jetbrains.annotations.NotNull;
3233
import org.jetbrains.annotations.Nullable;
3334
import org.polyfrost.oneconfig.api.platform.v1.Platform;
34-
import org.polyfrost.oneconfig.utils.v1.IOUtils;
3535
import org.polyfrost.polyui.PolyUI;
3636
import org.polyfrost.polyui.renderer.Window;
3737
import org.polyfrost.polyui.data.Cursor;
@@ -73,20 +73,12 @@ public boolean supportsRenderPausing() {
7373
@Nullable
7474
@Override
7575
public String getClipboard() {
76-
//#if MC>=11300
77-
//$$ return glfwGetClipboardString(handle);
78-
//#else
79-
return IOUtils.getStringFromClipboard();
80-
//#endif
76+
return Clipboard.getInstance().getString();
8177
}
8278

8379
@Override
8480
public void setClipboard(@Nullable String s) {
85-
//#if MC>=11300
86-
//$$ glfwSetClipboardString(handle, s);
87-
//#else
88-
IOUtils.copyStringToClipboard(s);
89-
//#endif
81+
Clipboard.getInstance().setString(s);
9082
}
9183

9284
@Override

versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfig.java

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
package org.polyfrost.oneconfig.internal;
2828

29+
import dev.deftu.clipboard.Clipboard;
2930
import kotlin.Unit;
3031
import org.apache.logging.log4j.LogManager;
3132
import org.apache.logging.log4j.Logger;
@@ -88,37 +89,81 @@ private void onInit(net.minecraftforge.fml.common.event.FMLPostInitializationEve
8889

8990
private void init() {
9091
BlurHandler.init();
91-
preload();
92+
93+
preloadCopycat();
94+
preloadPolyUI();
95+
96+
registerCommands();
97+
registerKeybinds();
98+
registerEventHandlers();
99+
100+
LOGGER.info("OneConfig initialized!");
101+
}
102+
103+
private static void registerCommands() {
92104
CommandBuilder b = CommandBuilder.command("oneconfig", "ocfg", "ocfgv1").description("OneConfig main command");
93105
b.then(runs().does((Runnable) OneConfigUI.INSTANCE::open).description("Opens the OneConfig GUI"));
94106
b.then(runs("updateCheck").does(() -> Multithreading.submit(() -> UChat.chat(MavenUpdateChecker.oneconfig().hasUpdate() ? "Update available!" : "No updates available"))).description("Check for updates"));
95107
b.then(runs("locraw").does(() -> UChat.chat(HypixelUtils.getLocation()))).description("Get your current location on Hypixel");
96108
b.then(runs("hud").does(() -> Platform.screen().display(HudManager.INSTANCE.getWithEditor())).description("Opens the OneConfig HUD editor"));
97109
CommandManager.registerCommand(b.build());
110+
}
111+
112+
private static void registerKeybinds() {
98113
OCKeybindHelper builder = OCKeybindHelper.builder();
99114
builder.mods(KeyModifiers.RSHIFT).does((s) -> {
100115
if (s) OneConfigUI.INSTANCE.open();
101116
return Unit.INSTANCE;
102117
});
118+
103119
builder.register();
120+
}
121+
122+
private static void registerEventHandlers() {
104123
EventManager.register(InitializationEvent.class, e -> HudManager.INSTANCE.initialize());
105-
LOGGER.info("OneConfig initialized!");
106124
}
107125

108126
/**
109127
* Ensure that key PolyUI classes are loaded to prevent lag-spikes when loading PolyUI for the first time.
110128
*/
111-
private static void preload() {
129+
private static void preloadPolyUI() {
112130
long t1 = System.nanoTime();
113131
try {
132+
// PolyUI
114133
Class.forName(PolyUI.class.getName());
115134
Class.forName(Drawable.class.getName());
116135
Class.forName(Translator.class.getName());
136+
137+
// OneConfig PolyUI renderer
117138
// todo: fix for fabric loaders as fails due to running too early
118139
// UIManager.INSTANCE.getRenderer();
119140
} catch (Exception e) {
120141
throw new IllegalStateException("Failed to preload necessary PolyUI classes", e);
121142
}
143+
122144
LOGGER.info("PolyUI preload took {}ms", (System.nanoTime() - t1) / 1_000_000.0);
123145
}
146+
147+
private static void preloadCopycat() {
148+
long t1 = System.nanoTime();
149+
try {
150+
// Copycat
151+
/// Ensure native is loaded
152+
Clipboard.getInstance();
153+
154+
/// Ensure copying works
155+
Clipboard clipboard = Clipboard.getInstance();
156+
String currentContent = clipboard.getString();
157+
clipboard.setString("OneConfig Test");
158+
if (!"OneConfig Test".equals(clipboard.getString())) {
159+
throw new IllegalStateException("Failed to set clipboard content");
160+
}
161+
162+
clipboard.setString(currentContent); // Reset clipboard
163+
} catch (Exception e) {
164+
throw new IllegalStateException("Failed to preload necessary Copycat classes", e);
165+
}
166+
167+
LOGGER.info("Copycat preload took {}ms", (System.nanoTime() - t1) / 1_000_000.0);
168+
}
124169
}

0 commit comments

Comments
 (0)