-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add util to open files / urls on every OS (#83)
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/main/java/com/gtnewhorizon/gtnhlib/util/FilesUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |