Skip to content

Commit

Permalink
add util to open files / urls on every OS (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexdoru authored Oct 17, 2024
1 parent 28786c5 commit e12a874
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/main/java/com/gtnewhorizon/gtnhlib/util/FilesUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.gtnewhorizon.gtnhlib.util;

import java.io.IOException;
import java.net.URI;

import org.lwjgl.Sys;

import com.gtnewhorizon.gtnhlib.GTNHLib;

public class FilesUtil {

public static void openUri(URI uri) {
switch (net.minecraft.util.Util.getOSType()) {
case OSX -> {
try {
Runtime.getRuntime().exec(new String[] { "/usr/bin/open", uri.toString() });
return;
} catch (IOException e) {
e.printStackTrace();
}
}
case WINDOWS -> {
try {
Runtime.getRuntime()
.exec(new String[] { "rundll32", "url.dll,FileProtocolHandler", uri.toString() });
return;
} catch (IOException e) {
e.printStackTrace();
}
}
case LINUX -> {
try {
Runtime.getRuntime().exec(new String[] { "xdg-open", uri.toString() });
return;
} catch (IOException e) {
e.printStackTrace();
}
}
default -> {}
}
boolean openViaSystemClass = false;

try {
final Class<?> aClass = Class.forName("java.awt.Desktop");
final Object getDesktop = aClass.getMethod("getDesktop").invoke(null);
aClass.getMethod("browse", URI.class).invoke(getDesktop, uri);
} catch (Exception e) {
e.printStackTrace();
openViaSystemClass = true;
}

if (openViaSystemClass) {
GTNHLib.LOG.debug("Opening via system class!");
Sys.openURL("file://" + uri);
}
}

}

0 comments on commit e12a874

Please sign in to comment.