-
Notifications
You must be signed in to change notification settings - Fork 0
/
FailsafeUtils.java
159 lines (146 loc) · 5.25 KB
/
FailsafeUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package net.taunahi_v3.ezskyblock.util;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;
import net.minecraft.client.Minecraft;
import org.apache.commons.lang3.SystemUtils;
import org.lwjgl.opengl.Display;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Objects;
public class FailsafeUtils {
private static FailsafeUtils instance;
private final TrayIcon trayIcon;
public FailsafeUtils() {
if (!SystemUtils.IS_OS_WINDOWS) {
trayIcon = null;
return;
}
BufferedImage image;
try {
image = ImageIO.read(Objects.requireNonNull(getClass().getResource("/taunahi/icon-mod/rat.png")));
} catch (IOException e) {
throw new RuntimeException(e);
}
trayIcon = new TrayIcon(image, "Taunahi Failsafe Notification");
trayIcon.setImageAutoSize(true);
trayIcon.setToolTip("Taunahi Failsafe Notification");
SystemTray tray = SystemTray.getSystemTray();
try {
tray.add(trayIcon);
} catch (AWTException e) {
e.printStackTrace();
}
}
public static FailsafeUtils getInstance() {
if (instance == null) {
instance = new FailsafeUtils();
}
return instance;
}
public static void bringWindowToFront() {
if (SystemUtils.IS_OS_WINDOWS) {
bringWindowToFrontUsingWinApi();
System.out.println("Bringing window to front using WinApi.");
} else {
bringWindowToFrontUsingRobot();
System.out.println("Bringing window to front using Robot.");
}
}
public static void bringWindowToFrontUsingWinApi() {
try {
User32 user32 = User32.INSTANCE;
WinDef.HWND hWnd = user32.FindWindow(null, Display.getTitle());
if (hWnd == null) {
System.out.println("Window not found.");
bringWindowToFrontUsingRobot();
return;
}
if (!user32.IsWindowVisible(hWnd)) {
user32.ShowWindow(hWnd, WinUser.SW_RESTORE);
System.out.println("Window is not visible, restoring.");
}
user32.ShowWindow(hWnd, WinUser.SW_SHOW);
user32.SetForegroundWindow(hWnd);
user32.SetFocus(hWnd);
} catch (Exception e) {
System.out.println("Failed to restore the game window.");
e.printStackTrace();
System.out.println("Trying to bring window to front using Robot instead.");
bringWindowToFrontUsingRobot();
}
}
public static void bringWindowToFrontUsingRobot() {
SwingUtilities.invokeLater(() -> {
int TAB_KEY = Minecraft.isRunningOnMac ? KeyEvent.VK_META : KeyEvent.VK_ALT;
try {
Robot robot = new Robot();
int i = 0;
while (!Display.isActive()) {
i++;
robot.keyPress(TAB_KEY);
for (int j = 0; j < i; j++) {
robot.keyPress(KeyEvent.VK_TAB);
robot.delay(100);
robot.keyRelease(KeyEvent.VK_TAB);
}
robot.keyRelease(TAB_KEY);
robot.delay(100);
if (i > 25) {
System.out.println("Failed to bring window to front.");
return;
}
}
} catch (AWTException e) {
System.out.println("Failed to use Robot, got exception: " + e.getMessage());
e.printStackTrace();
}
});
}
public void sendNotification(String text, TrayIcon.MessageType type) {
try {
if (SystemUtils.IS_OS_WINDOWS) {
windows(text, type);
} else if (SystemUtils.IS_OS_MAC_OSX) {
mac(text);
} else if (SystemUtils.IS_OS_LINUX) {
linux(text);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void windows(String text, TrayIcon.MessageType type) {
if (SystemTray.isSupported()) {
try {
trayIcon.displayMessage("Taunahi", text, type);
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("SystemTray is not supported");
}
}
private void mac(String text) {
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("osascript", "-e", "display notification \"" + text + "\" with title \"Taunahi\"");
try {
processBuilder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
private void linux(String text) {
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("notify-send", "-a", "Taunahi", text);
try {
processBuilder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}