From 2d2571cff5aa30d2142b5d809f74cf85c267a0e9 Mon Sep 17 00:00:00 2001 From: aUniqueUser Date: Sat, 22 Feb 2020 18:31:41 -0500 Subject: [PATCH 1/5] Desktop Notification System --- src/api/java/baritone/api/Settings.java | 4 ++ .../pathing/calc/AbstractNodeCostSearch.java | 3 + .../baritone/utils/NotificationHelper.java | 72 +++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 src/main/java/baritone/utils/NotificationHelper.java diff --git a/src/api/java/baritone/api/Settings.java b/src/api/java/baritone/api/Settings.java index 69bcabcb..1bbde3b2 100644 --- a/src/api/java/baritone/api/Settings.java +++ b/src/api/java/baritone/api/Settings.java @@ -1046,6 +1046,10 @@ public final class Settings { */ public final Setting renderSelectionCorners = new Setting<>(true); + /** + * Desktop Notifications + */ + public final Setting desktopNotifications = new Setting<>(false); /** * A map of lowercase setting field names to their respective setting diff --git a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java index a67384ac..baaeed8b 100644 --- a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java +++ b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java @@ -26,6 +26,7 @@ import baritone.api.utils.Helper; import baritone.api.utils.PathCalculationResult; import baritone.pathing.movement.CalculationContext; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; +import baritone.utils.NotificationHelper; import java.util.Optional; @@ -216,6 +217,8 @@ public abstract class AbstractNodeCostSearch implements IPathFinder, Helper { if (logInfo) { logDebug("Even with a cost coefficient of " + COEFFICIENTS[COEFFICIENTS.length - 1] + ", I couldn't get more than " + Math.sqrt(bestDist) + " blocks"); logDebug("No path found =("); + if (Baritone.settings().desktopNotifications.value) + NotificationHelper.notify("No path found =(", true); } return Optional.empty(); } diff --git a/src/main/java/baritone/utils/NotificationHelper.java b/src/main/java/baritone/utils/NotificationHelper.java new file mode 100644 index 00000000..927befb0 --- /dev/null +++ b/src/main/java/baritone/utils/NotificationHelper.java @@ -0,0 +1,72 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.utils; + +import java.awt.*; +import java.io.IOException; + +public class NotificationHelper { + public static void notify(String text, boolean error) { + if (System.getProperty("os.name").contains("Linux")) + linux(text); + else + notification(text, error); + } + + public static void notification(String text, boolean error) { + if (SystemTray.isSupported()) { + try { + SystemTray tray = SystemTray.getSystemTray(); + Image image = Toolkit.getDefaultToolkit().createImage(""); + // Replace with some logo + + TrayIcon trayIcon = new TrayIcon(image, "Baritone"); + trayIcon.setImageAutoSize(true); + trayIcon.setToolTip("Baritone"); + tray.add(trayIcon); + + if(error) + trayIcon.displayMessage("Baritone", text, TrayIcon.MessageType.ERROR); + else + trayIcon.displayMessage("Baritone", text, TrayIcon.MessageType.INFO); + } + catch (Exception e) { + e.printStackTrace(); + } + } + else { + System.out.println("SystemTray is not supported"); + } + } + + /* + * The only way to display notifications on linux is to use the java-gnome library, or send notify-send to shell with a ProcessBuilder + * Unfortunately the java-gnome library is licenced under the GPL, see: (https://en.wikipedia.org/wiki/Java-gnome) + */ + + public static void linux(String text) { + ProcessBuilder processBuilder = new ProcessBuilder(); + processBuilder.command("notify-send", "-a", "Baritone", text); + try { + processBuilder.start(); + } catch (IOException e) { + e.printStackTrace(); + } + + } +} From 0dcb9d4b6968584f9cf3c15f1089bfcff055034e Mon Sep 17 00:00:00 2001 From: aUniqueUser Date: Sat, 22 Feb 2020 20:12:43 -0500 Subject: [PATCH 2/5] Formatting --- .../pathing/calc/AbstractNodeCostSearch.java | 3 +- .../baritone/utils/NotificationHelper.java | 30 +++++++++++-------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java index baaeed8b..eea23905 100644 --- a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java +++ b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java @@ -217,8 +217,9 @@ public abstract class AbstractNodeCostSearch implements IPathFinder, Helper { if (logInfo) { logDebug("Even with a cost coefficient of " + COEFFICIENTS[COEFFICIENTS.length - 1] + ", I couldn't get more than " + Math.sqrt(bestDist) + " blocks"); logDebug("No path found =("); - if (Baritone.settings().desktopNotifications.value) + if (Baritone.settings().desktopNotifications.value) { NotificationHelper.notify("No path found =(", true); + } } return Optional.empty(); } diff --git a/src/main/java/baritone/utils/NotificationHelper.java b/src/main/java/baritone/utils/NotificationHelper.java index 927befb0..9ab0b545 100644 --- a/src/main/java/baritone/utils/NotificationHelper.java +++ b/src/main/java/baritone/utils/NotificationHelper.java @@ -20,12 +20,20 @@ package baritone.utils; import java.awt.*; import java.io.IOException; +/** + * This class is not called from the main game thread. + * Do not refer to any Minecraft classes, it wouldn't be thread safe. + * + * @author aUniqueUser + */ public class NotificationHelper { + public static void notify(String text, boolean error) { - if (System.getProperty("os.name").contains("Linux")) + if (System.getProperty("os.name").contains("Linux")) { linux(text); - else + } else { notification(text, error); + } } public static void notification(String text, boolean error) { @@ -33,30 +41,29 @@ public class NotificationHelper { try { SystemTray tray = SystemTray.getSystemTray(); Image image = Toolkit.getDefaultToolkit().createImage(""); - // Replace with some logo TrayIcon trayIcon = new TrayIcon(image, "Baritone"); trayIcon.setImageAutoSize(true); trayIcon.setToolTip("Baritone"); tray.add(trayIcon); - if(error) + if (error) { trayIcon.displayMessage("Baritone", text, TrayIcon.MessageType.ERROR); - else + } else { trayIcon.displayMessage("Baritone", text, TrayIcon.MessageType.INFO); - } - catch (Exception e) { + } + } catch (Exception e) { e.printStackTrace(); } - } - else { + } else { System.out.println("SystemTray is not supported"); } } /* - * The only way to display notifications on linux is to use the java-gnome library, or send notify-send to shell with a ProcessBuilder - * Unfortunately the java-gnome library is licenced under the GPL, see: (https://en.wikipedia.org/wiki/Java-gnome) + * The only way to display notifications on linux is to use the java-gnome library, + * or send notify-send to shell with a ProcessBuilder. Unfortunately the java-gnome + * library is licenced under the GPL, see (https://en.wikipedia.org/wiki/Java-gnome) */ public static void linux(String text) { @@ -67,6 +74,5 @@ public class NotificationHelper { } catch (IOException e) { e.printStackTrace(); } - } } From cf691118b90c3b13de13f851cc28ef55cfa11bc3 Mon Sep 17 00:00:00 2001 From: aUniqueUser Date: Sat, 22 Feb 2020 20:19:25 -0500 Subject: [PATCH 3/5] comment fixed --- src/main/java/baritone/utils/NotificationHelper.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/baritone/utils/NotificationHelper.java b/src/main/java/baritone/utils/NotificationHelper.java index 9ab0b545..76cd5b1c 100644 --- a/src/main/java/baritone/utils/NotificationHelper.java +++ b/src/main/java/baritone/utils/NotificationHelper.java @@ -60,12 +60,9 @@ public class NotificationHelper { } } - /* - * The only way to display notifications on linux is to use the java-gnome library, - * or send notify-send to shell with a ProcessBuilder. Unfortunately the java-gnome - * library is licenced under the GPL, see (https://en.wikipedia.org/wiki/Java-gnome) - */ - + // The only way to display notifications on linux is to use the java-gnome library, + // or send notify-send to shell with a ProcessBuilder. Unfortunately the java-gnome + // library is licenced under the GPL, see (https://en.wikipedia.org/wiki/Java-gnome) public static void linux(String text) { ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.command("notify-send", "-a", "Baritone", text); From 25b85f17a30bf71133fdcefebc2b693576d528f1 Mon Sep 17 00:00:00 2001 From: aUniqueUser Date: Sat, 22 Feb 2020 20:41:06 -0500 Subject: [PATCH 4/5] mfw --- src/main/java/baritone/utils/NotificationHelper.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/baritone/utils/NotificationHelper.java b/src/main/java/baritone/utils/NotificationHelper.java index 76cd5b1c..3e7b22ae 100644 --- a/src/main/java/baritone/utils/NotificationHelper.java +++ b/src/main/java/baritone/utils/NotificationHelper.java @@ -60,9 +60,11 @@ public class NotificationHelper { } } - // The only way to display notifications on linux is to use the java-gnome library, - // or send notify-send to shell with a ProcessBuilder. Unfortunately the java-gnome - // library is licenced under the GPL, see (https://en.wikipedia.org/wiki/Java-gnome) + /* + * The only way to display notifications on linux is to use the java-gnome library, + * or send notify-send to shell with a ProcessBuilder. Unfortunately the java-gnome + * library is licenced under the GPL, see (https://en.wikipedia.org/wiki/Java-gnome) + */ public static void linux(String text) { ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.command("notify-send", "-a", "Baritone", text); From a8645afbdb8c8d376ff3ca510771c84c4d43cd8a Mon Sep 17 00:00:00 2001 From: aUniqueUser Date: Sat, 22 Feb 2020 21:09:36 -0500 Subject: [PATCH 5/5] Revert "mfw" This reverts commit 25b85f17a30bf71133fdcefebc2b693576d528f1. --- src/main/java/baritone/utils/NotificationHelper.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/baritone/utils/NotificationHelper.java b/src/main/java/baritone/utils/NotificationHelper.java index 3e7b22ae..76cd5b1c 100644 --- a/src/main/java/baritone/utils/NotificationHelper.java +++ b/src/main/java/baritone/utils/NotificationHelper.java @@ -60,11 +60,9 @@ public class NotificationHelper { } } - /* - * The only way to display notifications on linux is to use the java-gnome library, - * or send notify-send to shell with a ProcessBuilder. Unfortunately the java-gnome - * library is licenced under the GPL, see (https://en.wikipedia.org/wiki/Java-gnome) - */ + // The only way to display notifications on linux is to use the java-gnome library, + // or send notify-send to shell with a ProcessBuilder. Unfortunately the java-gnome + // library is licenced under the GPL, see (https://en.wikipedia.org/wiki/Java-gnome) public static void linux(String text) { ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.command("notify-send", "-a", "Baritone", text);