Skip to content

Commit

Permalink
Add Copycat and clean up OneConfig file
Browse files Browse the repository at this point in the history
  • Loading branch information
Deftu committed Nov 14, 2024
1 parent 9e2d711 commit 48f44ad
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 38 deletions.
13 changes: 13 additions & 0 deletions buildSrc/src/main/kotlin/org/polyfrost/gradle/addDependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ fun Project.provideIncludedDependencies(version: Triple<Int, Int, Int>?, 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())
Expand Down
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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" }
Expand Down
2 changes: 2 additions & 0 deletions modules/utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 48f44ad

Please sign in to comment.