From 2d2571cff5aa30d2142b5d809f74cf85c267a0e9 Mon Sep 17 00:00:00 2001 From: aUniqueUser Date: Sat, 22 Feb 2020 18:31:41 -0500 Subject: [PATCH] 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(); + } + + } +}